Implementing Algorithms
|
|
- Arnold Kelly
- 5 years ago
- Views:
Transcription
1 Implementing Algorithms 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first CS 401/MCS 401 Lecture 3 Computer Algorithms I Jan Verschelde, 22 June 2018 Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
2 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
3 implementing algorithms Niklaus Wirth: algorithms + data structures = programs. What do we mean by implementing an algorithm? Definition Implementing an algorithm is selecting the best data structures that will make the algorithm run efficiently. Example: we proved that in the worst case, the Gale-Shapley algorithm for n men and n women does require no more than n 2 iterations of the loop. This proof does not imply that the running time of the Gale-Shapley algorithm is O(n 2 ) because we have not proven yet that every step in each iteration of the loop has constant time. We denote constant time by O(1). Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
4 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
5 arrays An array A is a sequence of consecutive data elements of fixed size, size(a), and the cost of accessing the i-th element in A is O(1). Example: an array which stores 3,, 4: 3 4 Exercise 1: Given is an array of numbers A, sorted in increasing order, and some number x. Prove that finding the position of x in A can be done in sublinear time, in the size of A. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June 2018 / 43
6 two dimensional arrays A matrix is stored as an array of arrays: A A = Double indexing works as follows: A 2,1 = = A[2][1] = A[2, 1] Given indices i and j, the cost of accessing A i,j = A[i, j] is O(1). Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
7 linked lists Example: a linked list L to store 3,, 4: L Node Node Node NULL next: data: 3 next: data: next: data: 4 A node in a linked list consists of a data element and a pointer. A linked list consists of a head node, linked to the next node, which is linked to the next node, etc, linked to the last node in the list, which has NULL as the pointer to its next node. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
8 circular lists In a linked list, one often needs a separate pointer to the last element of the list, to append an element to the end of the list. When the order among the stored data does not matter, distinguishing between first and last does not matter either, and the list is a pointer to some element in the list. Example: a circular linked list L stores 3,, 4: L Node next: data: 3 Node next: data: Node next: data: 4 Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
9 doubly linked lists Example: A doubly linked list L to store 3,, 4: L Node next: prev: data: 3 Node Node next: prev: data: next: prev: data: 4 NULL NULL At the expense of one extra pointer per node, reverse transversal becomes efficient. Doubly linked list can also made to be circular. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
10 properties of linked lists The cost to locate an element in a list of n elements is O(n), because to reach the i-th element in a list, all previous i 1 nodes need to be visited. Linked lists are dynamic data structures: Insertion of a new node costs O(1) time. Given the pointer to a node in a doubly linked list, the deletion of the node costs O(1) time. Exercise 2: Given is a doubly linked list with n elements. Prove the above statements: inserting and deleting a node is O(1), independent of the size n of the list. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
11 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
12 input data of the Gale-Shapley algorithm Input data: every man and woman has a preference list with no ties. Example: Consider 4 men {1, 2, 3, 4} and 4 women {a, b, c, d}. Preferences for men Preferences for women 1 : a c b d a : : b a d c b : : a c b d c : : d a c b d : Read the first line as 1 prefers a to c, 1 prefers c to b, 1 prefers b to d, a prefers 2 to 3, a prefers 3 to 4, a prefers 4 to 1. The number of men and women is fixed at the start of the algorithm. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
13 storing the input data Order the n men and n women into a sequence 1, 2,..., n. Because of the fixed size of constants, we use a two dimensional array: Preferences for men 1 : : : : ManPref For a man m: ManPref[m, i] stores the i-th woman on the preference list of m. Similarly for the women, the two dimensional array WomanPref stores their preferences. For a woman w: WomanPref[w, i] stores the i-th man on the preference list of w. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
14 the first statement in the Gale-Shapley algorithm Consider the first statement in the Gale-Shapley algorithm: 1 Let m M: isfree(m) and there is a w W : hasproposed(m, w) is false. We need to be able to locate the next free man. The set of free men changes: A man is no longer free if his proposal is accepted. A man becomes free if an engagement is broken. A linked list FreeMen will store the set of free men: The next free men m is at the front of the list: O(1). If his proposal is accepted, then we delete the first element: O(1). If the woman who accepted the proposal of m breaks up with m, then m is inserted to the front of the list of free men: O(1). However, it takes O(n) time to initialize FreeMen. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
15 proposing to the highest ranked Consider the second statement in the Gale-Shapley algorithm: 2 Let w W : w is the highest ranked in the preference list of m for whom hasproposed(m, w) is false. Observe: 1 Proposals are made in the order of preference. 2 Preferences are stored in decreasing order: highest ranked first. ManPref m next[m] Consider the preferences of m. Assume m proposed to 2 and 1. Next time m is free, he proposes to 4. next is an array which stores for each m his next highest ranked: m proposes to ManPref[m, next[m]] Initially, next[m] = 1 for all m. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
16 storing the engagements Consider the beginning of the third statement in the loop: 3 hasproposed(m, w) := true If isfree(w) then S := S (m, w) The hasproposed(m, w) := true is implemented by incrementing next[m], as: next[m] := next[m] + 1. Need to know: isfree(w) and if not, who is w engaged to? Introduce an array current of size n: If isfree(w), then current[w] = 0. If not isfree(w), then current[w] = m, w is engaged to m. The current implements the set of engaged pairs S. Because accessing an array element is O(1), checking if w is free and to whom w is engaged is O(1). Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
17 ranking the men along preferences of the women Consider the breaking of an engagement: 3 if... else there is an m M: (m, w) S (w is engaged to m ) if w prefers m to m then S := S \ (m, w) (m and w are no longer engaged) If there is an m M: (m, w) S, then current[w] = m. How to determine if w prefers m to m? Let there be a two dimensional array Ranking such that Ranking[w, m] < Ranking[w, m ] if w prefers m to m. Ranking[w, m] is the position of m in the preference list of w, e.g.: if Ranking[w, m] = 2, then m is the second best choice of w. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
18 building the auxiliary data structure Ranking The cost of consulting Ranking[w, m] is O(1), but what about the cost of constructing Ranking? Preferences for women 1 : : : : WomanPref for all woman w = 1, 2,..., n: for all ranks r = 1, 2,..., n: Ranking[w, WomanPref[w, m]] := r The loop makes n 2 assignments, so the cost is O(n 2 ). Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
19 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
20 overview of the selected data structures 1 ManPref is a two dimensional array for the preferences of n men. 2 WoManPref is a two dimensional array for the preferences of n women. 3 FreeMen is a linked list of all free men. 4 next is an array of size n for the next woman to propose to. current is an array of size n to store the engaged pairs. 6 Ranking is a two dimensional array to rank men. Exercise 3: The construction of Ranking is O(n 2 ). It is the most time consuming operation. In which scenario is Ranking not needed? Explain. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
21 running the Gale-Shapley algorithm once again Consider 4 men {1, 2, 3, 4} and 4 women {1, 2, 3, 4}. Preferences for men Preferences for women 1 : : : : : : : : Read the first line as man 1 prefers woman 1 to 3, prefers 3 to 2, prefers 2 to 4; woman 1 prefers man 2 to 3, prefers 3 to 4, prefers 4 to 1. Exercise 4: Run the Gale-Shapley algorithm on the above input data, show the evolution of all data structures in each step. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
22 the Gale-Shapley algorithm is an efficient algorithm Theorem For n men and n women, the running time of the Gale-Shapley algorithm is O(n 2 ). Proof. Consider the costs of the data structures defined above. We have two n-by-n arrays: ManPref and WomanPref, given on input. The other n-by-n array Ranking is constructed only once which takes O(n 2 ) time. Accessing arrays is O(1). The linked list FreeMen takes O(n) time to initialize. Both deleting a free man and inserting a free man is O(1). The initialization of the arrays next and current takes O(n) time. Accessing arrays is O(1). After initialization, all steps in the algorithm take O(1) time. As proven in Lecture 1, the number of iterations is bounded by n 2, so the Gale-Shapley algorithm runs in O(n 2 ) time. Q.E.D. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
23 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
24 graphs, terminology and definition A node in a graph is called a vertex. A connection between two vertices is an edge. A graph G is defined by a tuple (V, E): V is the set of vertices, and E is the set of edges. An example: A B C D V = {A, B, C, D} E = {(A, B), (A, C), (A, D), (C, D), (D, D)} Two vertices are adjacent if there is an edge between them. A path is a sequence of successively adjacent vertices. For example, a path from B to D is B, A, D. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
25 directed and undirected graphs In an undirected graph (which is the default), the edge (A, B) means: there is an edge from A to B and there is an edge from B to A. A B V = {A, B, C, D} E = {(A, B), (A, C), (A, D), (C, D), (D, D)} C D A B C D V = {A, B, C, D} E = {(A, B), (B, A), (C, A), (A, D), (D, C), (D, D)} In a directed graph or digraph, (A, B) means there is an edge from A to B and (B, A) means there is an edge from B to A. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
26 weighted graphs In an weighted graph, a value (a weight) is associated to every edge. A B 8 3 C D 4 7 A B C D 4 7 Edges in a weighted graphs are triplets (u, v, w), with w the weight of the edge which connects u to v. A cycle is a path where the first vertex equals the last one. For example: A, D, C, A is a cycle. A graph without cycles can be viewed as a tree. The degree of a vertex is the number of edges that contain that vertex. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
27 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
28 adjacency matrices For a graph G = (V, E), the adjacency matrix M has as many rows and columns as the number of vertices in V ; for all v i, v j V : if (v i, v j ) E: M i,j = 0; for all v i, v j V : if (v i, v j ) E: M i,j = 1. A B C D A B C D A B C D A B C D A B C D A B C D Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
29 adjacency matrices for weighted graphs For a weighted graph G = (V, E), the adjacency matrix M has as many rows and columns as the number of vertices in V ; for all v i, v j V : if (v i, v j ) E: M i,j = 0; for all v i, v j V : if (v i, v j ) E: M i,j = w, w is the weight of (v i, v j ). A B 8 3 C D 4 7 A B C D A B C D A B C D 4 7 A B C D A B C D Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
30 Implementing Algorithms; Graphs 1 Data Structures implementing algorithms arrays and linked lists 2 Implementing the Gale-Shapley algorithm selecting data structures overview of the selected data structures 3 Graphs definition and terminology adjacency matrices graph traversals: breadth first and depth first Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
31 visiting each vertex in a graph Consider the graph: Problem: visit each vertex in a systematic order. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
32 breadth-first search We start at vertex at 0: visit 1, 3, at 1: visit and 6 Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
33 breadth-first search continued at 3: visit 2 and 8 Every vertex is visited only once. Although there is an edge from 4 to, the breadth-first search will not visit this edge because has already been visited. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
34 breadth-first search continued at 4: visit 7 Observe that the graph on the right is a tree. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
35 the breadth-first tree At the left is the result of the breadth-first search. At the right is the breadth-first tree The breadth-first tree of a graph contains all the vertices of the graph and the edges visited in the breadth-first search. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
36 the breadth-first search algorithm Input: G = (V, E). Select the start vertex v V. Initialize the queue Q with v: Q.push(v). while not Q.empty() do u = Q.pop(); visit u; for all v adjacent to u do if v is not visited then if v Q then Q.push(v). Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
37 running the breadth-first search algorithm V = {0, 1, 2, 3, 4,, 6, 7, 8} E = {(0, 1), (0, 3), (0, 4), (1, ), (1, 6), (2, 3), (2, ), (3, 8), (4, ), (4, 6), (4, 7), (, 7), (, 8), (7, 8)} Q = 0 pop 0, visit 0, Q = 1, 3, 4 pop 1, visit 1, Q = 3, 4,, 6 pop 3, visit 3, Q = 4,, 6, 2, 8 pop 4, visit 4, Q =, 6, 2, 8, 7 pop, visit, Q = 6, 2, 8, 7 pop 6, visit 6, Q = 2, 8, 7 pop 2, visit 2, Q = 8, 7 pop 8, visit 8, Q = 8 pop 7, visit 7, Q = Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
38 depth-first search We start at vertex Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
39 depth-first search continued Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
40 depth-first search continued Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
41 the depth-first search tree The depth-first search tree of a graph contains all the vertices of the graph and the edges visited in the depth-first search. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
42 the depth-first search algorithm algorithm VISIT(V, E, u, Visited) // Visits all vertices in the graph (V, E), starting at u. // Visited collects the vertices visited. visit u; Visited.push(u); for all v V, v is adjacent to u do if v Visited then VISIT(V, E, v, Visited). We call algorithm VISIT with vertex 0 for u and an empty list for Visited. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
43 running the depth-first search algorithm V = {0, 1, 2, 3, 4,, 6, 7, 8} E = {(0, 1), (0, 3), (0, 4), (1, ), (1, 6), (2, 3), (2, ), (3, 8), (4, ), (4, 6), (4, 7), (, 7), (, 8), (7, 8)} visit 0, VISIT(V, E, 1, {0}) visit 1, VISIT(V, E,, {0, 1}) visit, VISIT(V, E, 2, {0, 1, }) visit 2, VISIT(V, E, 3, {0, 1,, 2}) visit 3, VISIT(V, E, 8, {0, 1,, 2, 3}) visit 8, VISIT(V, E, 7, {0, 1,, 2, 3, 8}) visit 7, VISIT(V, E, 4, {0, 1,, 2, 3, 8, 7}) visit 4, Visited = {0, 1,, 2, 3, 8, 7, 4} visit 6, Visited = {0, 1,, 2, 3, 8, 7, 4, 6} The tree of recursive calls is the depth-first search tree. Computer Algorithms I (CS 401/MCS 401) Implementing Algorithms; Graphs L-3 22 June / 43
implementing the breadth-first search algorithm implementing the depth-first search algorithm
Graph Traversals 1 Graph Traversals representing graphs adjacency matrices and adjacency lists 2 Implementing the Breadth-First and Depth-First Search Algorithms implementing the breadth-first search algorithm
More informationReview for Midterm Exam
Review for Midterm Exam 1 Policies and Overview midterm exam policies overview of problems, algorithms, data structures overview of discrete mathematics 2 Sample Questions on the cost functions of algorithms
More informationReview implementation of Stable Matching Survey of common running times. Turn in completed problem sets. Jan 18, 2019 Sprenkle - CSCI211
Objectives Review implementation of Stable Matching Survey of common running times Turn in completed problem sets Jan 18, 2019 Sprenkle - CSCI211 1 Review: Asymptotic Analysis of Gale-Shapley Alg Not explicitly
More informationGraphs & Digraphs Tuesday, November 06, 2007
Graphs & Digraphs Tuesday, November 06, 2007 10:34 PM 16.1 Directed Graphs (digraphs) like a tree but w/ no root node & no guarantee of paths between nodes consists of: nodes/vertices - a set of elements
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 04 / 06 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Graphs Definition Terminology two ways to represent edges in implementation traversals
More informationGraphs. directed and undirected graphs weighted graphs adjacency matrices. abstract data type adjacency list adjacency matrix
Graphs 1 Graphs directed and undirected graphs weighted graphs adjacency matrices 2 Graph Representations abstract data type adjacency list adjacency matrix 3 Graph Implementations adjacency matrix adjacency
More informationmanaging an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm
Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing
More informationCopyright 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch.
Iterative Improvement Algorithm design technique for solving optimization problems Start with a feasible solution Repeat the following step until no improvement can be found: change the current feasible
More informationFundamental Algorithms
Fundamental Algorithms Chapter 8: Graphs Jan Křetínský Winter 2017/18 Chapter 8: Graphs, Winter 2017/18 1 Graphs Definition (Graph) A graph G = (V, E) consists of a set V of vertices (nodes) and a set
More informationGraph Algorithms Using Depth First Search
Graph Algorithms Using Depth First Search Analysis of Algorithms Week 8, Lecture 1 Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Graph Algorithms Using Depth
More informationLECTURE 17 GRAPH TRAVERSALS
DATA STRUCTURES AND ALGORITHMS LECTURE 17 GRAPH TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD STRATEGIES Traversals of graphs are also called searches We can use either breadth-first
More informationAssignment and Matching
Assignment and Matching By Geetika Rana IE 680 Dept of Industrial Engineering 1 Contents Introduction Bipartite Cardinality Matching Problem Bipartite Weighted Matching Problem Stable Marriage Problem
More informationGraph Theory II. Po-Shen Loh. June edges each. Solution: Spread the n vertices around a circle. Take parallel classes.
Graph Theory II Po-Shen Loh June 009 1 Warm-up 1. Let n be odd. Partition the edge set of K n into n matchings with n 1 edges each. Solution: Spread the n vertices around a circle. Take parallel classes..
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 3 Data Structures Graphs Traversals Strongly connected components Sofya Raskhodnikova L3.1 Measuring Running Time Focus on scalability: parameterize the running time
More informationCS Algorithms and Complexity
CS 350 - Algorithms and Complexity Graph Theory, Midterm Review Sean Anderson 2/6/18 Portland State University Table of contents 1. Graph Theory 2. Graph Problems 3. Uninformed Exhaustive Search 4. Informed
More informationElementary Graph Algorithms. Ref: Chapter 22 of the text by Cormen et al. Representing a graph:
Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. Representing a graph: Graph G(V, E): V set of nodes (vertices); E set of edges. Notation: n = V and m = E. (Vertices are numbered
More informationFORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE
TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)
More informationCPSC 320 Midterm #1. February 4, 2015
CPSC 320 Midterm #1 February 4, 2015 1 2 Reminders (but do not miss the problem also on this page!): ˆ f(n) O(g(n)) (big-o, that is) exactly when there is a positive real constant c and positive integer
More informationCSI 604 Elementary Graph Algorithms
CSI 604 Elementary Graph Algorithms Ref: Chapter 22 of the text by Cormen et al. (Second edition) 1 / 25 Graphs: Basic Definitions Undirected Graph G(V, E): V is set of nodes (or vertices) and E is the
More informationCSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT
CSE 373 NOVEMBER 20 TH TOPOLOGICAL SORT PROJECT 3 500 Internal Error problems Hopefully all resolved (or close to) P3P1 grades are up (but muted) Leave canvas comment Emails tomorrow End of quarter GRAPHS
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 4 Graphs Definitions Traversals Adam Smith 9/8/10 Exercise How can you simulate an array with two unbounded stacks and a small amount of memory? (Hint: think of a
More informationCPSC W1: Midterm #1
CPSC 320 2016W1: Midterm #1 2016-10-20 Thu 1 O'd to a Pair of Runtimes [10 marks] Consider the following pairs of functions (representing algorithm runtimes), expressed in terms of the positive variables
More informationCPSC W1: Midterm 1 Sample Solution
CPSC 320 2017W1: Midterm 1 Sample Solution January 26, 2018 Problem reminders: EMERGENCY DISTRIBUTION PROBLEM (EDP) EDP's input is an undirected, unweighted graph G = (V, E) plus a set of distribution
More informationmemoization or iteration over subproblems the direct iterative algorithm a basic outline of dynamic programming
Dynamic Programming 1 Introduction to Dynamic Programming weighted interval scheduling the design of a recursive solution memoizing the recursion 2 Principles of Dynamic Programming memoization or iteration
More information21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #
1. Prove that n log n n is Ω(n). York University EECS 11Z Winter 1 Problem Set 3 Instructor: James Elder Solutions log n n. Thus n log n n n n n log n n Ω(n).. Show that n is Ω (n log n). We seek a c >,
More informationData Structure. IBPS SO (IT- Officer) Exam 2017
Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data
More information1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1
Asymptotics, Recurrence and Basic Algorithms 1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1 1. O(logn) 2. O(n) 3. O(nlogn) 4. O(n 2 ) 5. O(2 n ) 2. [1 pt] What is the solution
More informationR10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May
www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the
More informationCS4800: Algorithms & Data Jonathan Ullman
CS4800: Algorithms & Data Jonathan Ullman Lecture 11: Graphs Graph Traversals: BFS Feb 16, 2018 What s Next What s Next Graph Algorithms: Graphs: Key Definitions, Properties, Representations Exploring
More informationA6-R3: DATA STRUCTURE THROUGH C LANGUAGE
A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF
More informationAlgorithm Design and Analysis
Algorithm Design and Analysis LECTURE 5 Exploring graphs Adam Smith 9/5/2008 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Puzzles Suppose an undirected graph G is connected.
More informationBreadth First Search. cse2011 section 13.3 of textbook
Breadth irst Search cse section. of textbook Graph raversal (.) Application example Given a graph representation and a vertex s in the graph, find all paths from s to the other vertices. wo common graph
More informationMatchings, Ramsey Theory, And Other Graph Fun
Matchings, Ramsey Theory, And Other Graph Fun Evelyne Smith-Roberge University of Waterloo April 5th, 2017 Recap... In the last two weeks, we ve covered: What is a graph? Eulerian circuits Hamiltonian
More informationCPSC W1: Midterm #1 (Group Version) Thu
CPSC 320 2016W1: Midterm #1 (Group Version) 2016-10-20 Thu 1 2 1 O'd to a Pair of Runtimes [10 marks] Consider the following pairs of functions (representing algorithm runtimes), expressed in terms of
More informationCMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013
CMPSCI 250: Introduction to Computation Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013 Induction and Recursion Three Rules for Recursive Algorithms Proving
More informationMA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review DFS, BFS Topological Sort MA/CSSE 473 Day 12 Questions? Interpolation Search Insertion sort analysis Depth first Search Breadth first
More informationDATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305
Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n
More informationDiscrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1
CS 70 Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1 PRINT Your Name:, (last) READ AND SIGN The Honor Code: As a member of the UC Berkeley community, I act with honesty,
More informationTheorem 3.1 (Berge) A matching M in G is maximum if and only if there is no M- augmenting path.
3 Matchings Hall s Theorem Matching: A matching in G is a subset M E(G) so that no edge in M is a loop, and no two edges in M are incident with a common vertex. A matching M is maximal if there is no matching
More informationREU 2006 Discrete Math Lecture 5
REU 2006 Discrete Math Lecture 5 Instructor: László Babai Scribe: Megan Guichard Editors: Duru Türkoğlu and Megan Guichard June 30, 2006. Last updated July 3, 2006 at 11:30pm. 1 Review Recall the definitions
More informationGreedy Homework Problems
CS 1510 Greedy Homework Problems 1. (2 points) Consider the following problem: INPUT: A set S = {(x i, y i ) 1 i n} of intervals over the real line. OUTPUT: A maximum cardinality subset S of S such that
More informationCS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting
CS2 Algorithms and Data Structures Note 10 Depth-First Search and Topological Sorting In this lecture, we will analyse the running time of DFS and discuss a few applications. 10.1 A recursive implementation
More informationTheory of Computing. Lecture 4/5 MAS 714 Hartmut Klauck
Theory of Computing Lecture 4/5 MAS 714 Hartmut Klauck How fast can we sort? There are deterministic algorithms that sort in worst case time O(n log n) Do better algorithms exist? Example [Andersson et
More informationDiscrete Mathematics and Probability Theory Summer 2016 Dinh, Psomas, and Ye HW 2
CS 70 Discrete Mathematics and Probability Theory Summer 2016 Dinh, Psomas, and Ye HW 2 Due Tuesday July 5 at 1:59PM 1. (8 points: 3/5) Hit or miss For each of the claims and proofs below, state whether
More informationIntroduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis
Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis p. 5 Statement Constructs p. 5 Pseudocode Example p.
More informationFigure 1: A directed graph.
1 Graphs A graph is a data structure that expresses relationships between objects. The objects are called nodes and the relationships are called edges. For example, social networks can be represented as
More informationEnd-Term Examination Second Semester [MCA] MAY-JUNE 2006
(Please write your Roll No. immediately) Roll No. Paper Code: MCA-102 End-Term Examination Second Semester [MCA] MAY-JUNE 2006 Subject: Data Structure Time: 3 Hours Maximum Marks: 60 Note: Question 1.
More informationAshish Gupta, Data JUET, Guna
Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because
More informationMatching and Covering
Matching and Covering Matchings A matching of size k in a graph G is a set of k pairwise disjoint edges The vertices belonging to the edges of a matching are saturated by the matching; the others are unsaturated
More informationUnderstand graph terminology Implement graphs using
raphs Understand graph terminology Implement graphs using djacency lists and djacency matrices Perform graph searches Depth first search Breadth first search Perform shortest-path algorithms Disjkstra
More informationMAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.
MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson, A207 paul.gibson@it-sudparis.eu http://www-public.it-sudparis.eu/~gibson/teaching/mat7003/ Graphs and Trees http://www-public.it-sudparis.eu/~gibson/teaching/mat7003/l2-graphsandtrees.pdf
More informationCSE 100: GRAPH ALGORITHMS
CSE 100: GRAPH ALGORITHMS 2 Graphs: Example A directed graph V5 V = { V = E = { E Path: 3 Graphs: Definitions A directed graph V5 V6 A graph G = (V,E) consists of a set of vertices V and a set of edges
More informationCSC 172 Data Structures and Algorithms. Lecture 24 Fall 2017
CSC 172 Data Structures and Algorithms Lecture 24 Fall 2017 ANALYSIS OF DIJKSTRA S ALGORITHM CSC 172, Fall 2017 Implementation and analysis The initialization requires Q( V ) memory and run time We iterate
More informationObjective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)
Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113) 1. The number of interchanges required to sort 5, 1, 6, 2 4 in ascending order using Bubble Sort (A)
More informationFORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours
TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART
More informationChapter 16: Graphs and Digraphs
Chapter 16: Graphs and Digraphs Exercises 16.1 1. adj: 2. adj: 3. adj: 4. adj: 5. E 1 1 0 1 1 1 1 0 0 1 1 1 1 0 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 1
More informationIntroduction to Graphs. CS2110, Spring 2011 Cornell University
Introduction to Graphs CS2110, Spring 2011 Cornell University A graph is a data structure for representing relationships. Each graph is a set of nodes connected by edges. Synonym Graph Hostile Slick Icy
More informationProblem set 2. Problem 1. Problem 2. Problem 3. CS261, Winter Instructor: Ashish Goel.
CS261, Winter 2017. Instructor: Ashish Goel. Problem set 2 Electronic submission to Gradescope due 11:59pm Thursday 2/16. Form a group of 2-3 students that is, submit one homework with all of your names.
More informationThis course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.
Lecture 9 Graphs This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science. You need to be familiar with the design and use of basic data structures such as Lists, Stacks,
More information12 Abstract Data Types
12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define
More informationW4231: Analysis of Algorithms
W4231: Analysis of Algorithms 10/21/1999 Definitions for graphs Breadth First Search and Depth First Search Topological Sort. Graphs AgraphG is given by a set of vertices V and a set of edges E. Normally
More informationGraphs: basic concepts and algorithms
: basic concepts and algorithms Topics covered by this lecture: - Reminder Trees Trees (in-order,post-order,pre-order) s (BFS, DFS) Denitions: Reminder Directed graph (digraph): G = (V, E), V - vertex
More information1. Graph and Representation
Chapter Graphs Tree Root Direction: parent-child relationships No cycles Graph Vertices + Edges No tree restrictions Examples World Wide Web Maps. Graph and Representation A graph G: a pair (V, E) vertices
More informationChapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1
Chapter 14 Graphs 2011 Pearson Addison-Wesley. All rights reserved 14 A-1 Terminology G = {V, E} A graph G consists of two sets A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset
More informationCOSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)
COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back
More informationGraph: representation and traversal
Graph: representation and traversal CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang! Acknowledgement The set of slides have use materials from the following resources Slides for textbook
More informationCS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics
CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics 1 Sorting 1.1 Problem Statement You are given a sequence of n numbers < a 1, a 2,..., a n >. You need to
More informationCS61BL. Lecture 5: Graphs Sorting
CS61BL Lecture 5: Graphs Sorting Graphs Graphs Edge Vertex Graphs (Undirected) Graphs (Directed) Graphs (Multigraph) Graphs (Acyclic) Graphs (Cyclic) Graphs (Connected) Graphs (Disconnected) Graphs (Unweighted)
More information1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \
BSc IT C Programming (2013-2017) Unit I Q1. What do you understand by type conversion? (2013) Q2. Why we need different data types? (2013) Q3 What is the output of the following (2013) main() Printf( %d,
More informationComputer Science E-22 Practice Final Exam
name Computer Science E-22 This exam consists of three parts. Part I has 10 multiple-choice questions that you must complete. Part II consists of 4 multi-part problems, of which you must complete 3, and
More informationGraph Theory: Matchings and Factors
Graph Theory: Matchings and Factors Pallab Dasgupta, Professor, Dept. of Computer Sc. and Engineering, IIT Kharagpur pallab@cse.iitkgp.ernet.in Matchings A matching of size k in a graph G is a set of k
More informationCSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Dijkstra s Algorithm (Graphs) Instructor: Lilian de Greef Quarter: Summer 207 Today Announcements Graph Traversals Continued Remarks on DFS & BFS Shortest
More informationFundamentals of Data Structure
Fundamentals of Data Structure Set-1 1. Which if the following is/are the levels of implementation of data structure A) Abstract level B) Application level C) Implementation level D) All of the above 2.
More informationINSTITUTE OF AERONAUTICAL ENGINEERING
INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech
More informationBreadth First Search. Graph Traversal. CSE 2011 Winter Application examples. Two common graph traversal algorithms
Breadth irst Search CSE Winter Graph raversal Application examples Given a graph representation and a vertex s in the graph ind all paths from s to the other vertices wo common graph traversal algorithms
More informationDefinition Example Solution
Section 11.4 Spanning Trees Definition: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G. Example: Find the spanning tree of this simple graph:
More information1 The Arthur-Merlin Story
Comp 260: Advanced Algorithms Tufts University, Spring 2011 Prof. Lenore Cowen Scribe: Andrew Winslow Lecture 1: Perfect and Stable Marriages 1 The Arthur-Merlin Story In the land ruled by the legendary
More informationLecture 7: Bipartite Matching
Lecture 7: Bipartite Matching Bipartite matching Non-bipartite matching What is a Bipartite Matching? Let G=(N,A) be an unrestricted bipartite graph. A subset X of A is said to be a matching if no two
More informationCS Data Structures and Algorithm Analysis
CS 483 - Data Structures and Algorithm Analysis Lecture VI: Chapter 5, part 2; Chapter 6, part 1 R. Paul Wiegand George Mason University, Department of Computer Science March 8, 2006 Outline 1 Topological
More informationR13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)
SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)
More informationBalls Into Bins Model Occupancy Problems
Balls Into Bins Model Occupancy Problems Assume that m balls are thrown randomly is n bins, i.e., the location of each ball is independently and uniformly chosen at random from the n possibilities. How
More informationComputer Science & Engineering 423/823 Design and Analysis of Algorithms
s of s Computer Science & Engineering 423/823 Design and Analysis of Lecture 03 (Chapter 22) Stephen Scott (Adapted from Vinodchandran N. Variyam) 1 / 29 s of s s are abstract data types that are applicable
More informationGraphs. The ultimate data structure. graphs 1
Graphs The ultimate data structure graphs 1 Definition of graph Non-linear data structure consisting of nodes & links between them (like trees in this sense) Unlike trees, graph nodes may be completely
More informationComputer Science 385 Design and Analysis of Algorithms Siena College Spring Topic Notes: Brute-Force Algorithms
Computer Science 385 Design and Analysis of Algorithms Siena College Spring 2019 Topic Notes: Brute-Force Algorithms Our first category of algorithms are called brute-force algorithms. Levitin defines
More informationand 6.855J February 6, Data Structures
15.08 and 6.855J February 6, 003 Data Structures 1 Overview of this Lecture A very fast overview of some data structures that we will be using this semester lists, sets, stacks, queues, networks, trees
More informationCS2 Algorithms and Data Structures Note 9
CS2 Algorithms and Data Structures Note 9 Graphs The remaining three lectures of the Algorithms and Data Structures thread will be devoted to graph algorithms. 9.1 Directed and Undirected Graphs A graph
More informationTable of Contents. Chapter 1: Introduction to Data Structures... 1
Table of Contents Chapter 1: Introduction to Data Structures... 1 1.1 Data Types in C++... 2 Integer Types... 2 Character Types... 3 Floating-point Types... 3 Variables Names... 4 1.2 Arrays... 4 Extraction
More informationCS473-Algorithms I. Lecture 13-A. Graphs. Cevdet Aykanat - Bilkent University Computer Engineering Department
CS473-Algorithms I Lecture 3-A Graphs Graphs A directed graph (or digraph) G is a pair (V, E), where V is a finite set, and E is a binary relation on V The set V: Vertex set of G The set E: Edge set of
More informationRepresentations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs
Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects
More informationGraph Representations and Traversal
COMPSCI 330: Design and Analysis of Algorithms 02/08/2017-02/20/2017 Graph Representations and Traversal Lecturer: Debmalya Panigrahi Scribe: Tianqi Song, Fred Zhang, Tianyu Wang 1 Overview This lecture
More informationSources for this lecture. 3. Matching in bipartite and general graphs. Symmetric difference
S-72.2420 / T-79.5203 Matching in bipartite and general graphs 1 3. Matching in bipartite and general graphs Let G be a graph. A matching M in G is a set of nonloop edges with no shared endpoints. Let
More informationMultidimensional Arrays & Graphs. CMSC 420: Lecture 3
Multidimensional Arrays & Graphs CMSC 420: Lecture 3 Mini-Review Abstract Data Types: List Stack Queue Deque Dictionary Set Implementations: Linked Lists Circularly linked lists Doubly linked lists XOR
More informationDynamic Programming: 1D Optimization. Dynamic Programming: 2D Optimization. Fibonacci Sequence. Crazy 8 s. Edit Distance
Dynamic Programming: 1D Optimization Fibonacci Sequence To efficiently calculate F [x], the xth element of the Fibonacci sequence, we can construct the array F from left to right (or bottom up ). We start
More information1 Introduction and Examples
1 Introduction and Examples Sequencing Problems Definition A sequencing problem is one that involves finding a sequence of steps that transforms an initial system state to a pre-defined goal state for
More informationResource Sharing & Management
Resource Sharing & Management P.C.P Bhatt P.C.P Bhatt OS/M6/V1/2004 1 Introduction Some of the resources connected to a computer system (image processing resource) may be expensive. These resources may
More informationLecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.
Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct
More informationSection Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees
Chapter 11 Copyright McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education. Chapter Summary Introduction to Trees Applications
More informationUML CS Algorithms Qualifying Exam Fall, 2003 ALGORITHMS QUALIFYING EXAM
NAME: This exam is open: - books - notes and closed: - neighbors - calculators ALGORITHMS QUALIFYING EXAM The upper bound on exam time is 3 hours. Please put all your work on the exam paper. (Partial credit
More informationLet s Talk About Logic
Let s Talk About Logic Jan van Eijck CWI & ILLC, Amsterdam Masterclass Logica, 2 Maart 2017 Abstract This lecture shows how to talk about logic in computer science. To keep things simple, we will focus
More informationUNIT 4 Branch and Bound
UNIT 4 Branch and Bound General method: Branch and Bound is another method to systematically search a solution space. Just like backtracking, we will use bounding functions to avoid generating subtrees
More information