CS Yaxin.Huang.HWM10- Solution

Size: px
Start display at page:

Download "CS Yaxin.Huang.HWM10- Solution"

Transcription

1 Q1(Exercise ) (1) The algorithm for the adjacency-list: Assuming that the input of the algorithm is an array A[1..n], which contains V elements. Each index represents a node in the graph, and each element is a pointer to the list of nodes which is the end of edges from the node of this index. (So this adjacency-list is exactly the same as the one described in section 22.1.) The algorithm designed below will output the square of the directed graph in the form of adjacency-list. SQUARE-GRAPH-ADJ-LIST(A) let B[1..n] be a new array n = A.length for i = 1 to n adj-node-ptr = A[i] //A pointer to travel the list of //nodes adjacent to node i. while adj-node-ptr!= NIL if (*adj-node-ptr) not in the adj-list of B[i] //linear search add (*adj-node-ptr) to the head of list B[i] //(*adj-node-ptr) denotes the node index adj-adj-ptr = A[(*adj-node-ptr)] //the next while-loop explores the nodes adjacent //to node (*adj-node-ptr) while adj-adj-ptr!= NIL if (*adj-adj-ptr) not in the adj-list of B[i] add (*adj-adj-ptr) to the head of list B[i] adj-adj-ptr = adj-adj-ptr.next adj-node-ptr = adj-node-ptr.next return B Running time analysis: The graph has V nodes and E edges. The for loop needs to examine V nodes. The first while loop examines the nodes adjacent to node i and the second while loop examines the nodes adjacent to the nodes that are adjacent to node i. Therefore, each directed edge is at most examined twice. Because the insertion always happens at the head of the list, we only need O(1) time to finish the insertion. The two bold if need to do searching in the adjacency list. In the worst case, when the graph is fully connected, all together you need to search O( V * E ) times.

2 The running time of this algorithm is O( V * E ). (2) The algorithm for the adjacency-matrix: Assuming that the input of the algorithm is a matrix A[1..n, 1..n]. If there is an edge from node u to node v, then A[u,v] is 1, otherwise it is 0. SQUARE-GRAPH-ADJ-MATRIX(A,n) // 1... n is the indices of nodes // A[u,v] denotes there is a directed edge (u,v) in G let B[1..n, 1..n] be a new table for i = 1 to n for j = 1 to n if A[i,j] == 1 B[i,j] = 1 for k = 1 to n if A[j,k] == 1 B[i,k] = 1 return B Running time analysis: There are V nodes in graph G and E edges. The first for loop examines every node in graph G. So this for loop has to execute V times. The second for loop also needs to execute V times. If there is an edge from the adjacent node to another adjacent-adjacent nodes, then the third for loop needs to be executed. Thus, the third for loop in total needs to be executed at most E times. The running time is O(2 V + E ). Q2(Exercise )

3 Given the graph above, it can be represented by an adjacency-list as: A B C (or C B) B D E (or D E) C D E (or D E) C NIL E NIL For example we choose A as the source, then we may have these trees because the different orders of vertices in the adjacency-list: And here is one tree consisting of shortest paths and it has these edges: (A B) (B D) (A C) (C E), you can see that non of the trees produced by BFS starting from A is the tree with shortest paths to each vertices. Q3(Exercise ) There are two types of professional wrestlers: babyfaces ( good guys ) and heels ( bad guys ). Between any pair of professional wrestlers, there may or may not be a rivalry. Suppose we have n professional wrestlers and we have a list of r pairs of wrestlers for which there are rivalries. Give an O.n C r/-time algorithm that determines whether it is possible to designate some of the wrestlers as babyfaces and the remainder as heels such that each rivalry is between a babyface and a heel. If it is possible to perform such a designation, your algorithm should produce

4 it. The rivalries between the wrestlers can be represented by an undirected graph. If there is a rivalry between wrestler u and wrestler v, then there exists an edge (u,v) in the graph G. And so this problem can be solve by doing some modifications on BFS. The input A is an array, and each index denotes a vertex. It is the same representation as the adjacency-list introduced in section Each element in A is an object, which has the fields : color, d, π and adj. (adj is the pointer to the adjacency-list). BFS-DESIGNATION(A) n = A.length let B[1..n] be a new array //B is to store the result of the designation. //true is babyface, false is heel. for i from 1 to n A[i].color = WHITE A[i].d = A[i].π = NIL B[i] = NIL // NIL represents no designation s = the index of the node with the most out-edges //here we need θ( V ) time to search s. A[s].color = GRAY A[s].d = 0 A[s].π = NIL B[s] = true //assign it as the babyface let Q be a new queue(empty). ENQUEUE(Q,s) while Q is nonempty u = DEQUEUE(Q) for each vertex index v in the list that A[u].adj points to: if A[v].color == WHILE A[v].color = GRAY A[v].d = A[u].d + 1 A[v].π = u if B[v] == NIL B[v] = not B[u] if B[v] == false if B[u] == false return false //no way to designate if B[u] == true return false

5 return B //B is the final designation Q4(Exercise ) Rewrite the procedure DFS, using a stack to eliminate recursion. The input of the algorithm below is a graph G, who has the DFS-STACK-VERSION(G) let S be a new empty stack let Q be a new empty set //here we use a set to maintain the unvisited vertices //so we don t need coloring for each vertex u belongs to G.V u.π = NIL add u to Q time = 0 while Q is nonempty if EMPTY(S) == true u = pick one vertex from Q u.d = time u.π = NIL PUSH(S, u) v = TOP(S) //This will only return the value // of the top element if G.Adj[v] is nonempty for each vertex u belongs to G.Adj[v] kick out u from Q //need to do searching here u.d = time u.π = v PUSH(S, u) v.f = time POP(V) Q5(Exercise )Modify the pseudocode for depth-first search so that it prints out every edge in the directed graph G, together with its type. Show what modifications, if any, you need to make if G is undirected.

6 DFS-directed-print-edge(G) for each vertex u belongs to G.V u.color = WHITE u.π = NIL time = 0 for each vertex u belongs to G.V if u.color == WHITE DFS-VISIT-directed-print-edge(G,u) DFS-VISIT-directed-print-edge(G,u) u.d = time u.color = GRAY for each v belongs to G.Adj[u] if v.color == WHITE v.π = u print (u,v) tree edge DFS-VISIT-directed-print-edge(G,v) if v.color == GRAY print (u,v) back edge // v.color == BLACK if u.d < v.d print (u,v) forward edge print (u,v) cross edge u.color = BLACK u.f = time If given an undirected graph, the code in the blue box should be modified into this one: for each v belongs to G.Adj[u] if v.color == WHITE v.π = u print (u,v) tree edge DFS-VISIT-directed-print-edge(G,v) print (u,v) back edge Because according to the theorem 22.10, in an undirected graph, every edge is either a tree edge or a back edge.

7 Q6(Exercise ) Show that we can use a depth-first search of an undirected graph G to identify the connected components of G, and that the depth-first forest contains as many trees as G has connected components. More precisely, show how to modify depth-first search so that it assigns to each vertex v an integer label v.cc between 1 and k, where k is the number of connected components of G, such that u.cc = v.cc if and only if u and v are in the same connected component. DFS-CC(G) for each vertex u belongs to G.V u.color = WHITE u.π = NIL u.cc = 0 time = 0 cc-counter = 0 for each vertex u belongs to G.V if u.color == WHITE cc-counter = cc-counter + 1 DFS-VISIT-directed-print-edge(G,u) DFS-VISIT-CC(G,u) u.cc = cc-counter u.d = time u.color = GRAY for each v belongs to G.Adj[u] if v.color == WHITE v.π = u DFS-VISIT-directed-print-edge(G,v) u.color = BLACK u.f = time Q7(Exercise )Another way to perform topological sorting on a directed acyclic graph G = (V,E) is to repeatedly find a vertex of in-degree 0, output it, and remove it and all of its outgoing edges from the graph. Explain how to implement this idea so that it runs in time O(V+E). What happens to this algorithm if G has cycles? TOPOLOGICAL-SORT-BY-IN-DEGREE(G) for each vertex v belongs to G.V θ( V ) v.in-degree = 0 //initialization finished The second for loop: O( V + E ) because all for each vertex v belongs to G.V the edges are inspected once, and all the ptr = G.Adj[v] vertices have been searched its adj-list once.

8 //ptr now points to the head of v s adj-list while ptr!= NIL (*ptr).in-degree = (*ptr).in-degree + 1 //(*ptr) represents the vertex which //ptr points to ptr = ptr.next let Q be a new set equal to G.V let A[1.. V ] be a new array i = 1 this while loop: O( V + E ) while Q is nonempty if there is a vertex u with in-degree == 0 pick u from Q //Now u is not in Q return false //no way to make a // topological ordering A[i] = u ptr = G.Adj[u] while ptr!= NIL (*ptr).in-degree = (*ptr).in-degree - 1 return A If G has cycles, then the algorithm will return false to tell us that it cannot find a topological ordering for the graph, because if G has cycles, in the second while loop, you will come to the situation that no vertices in G has an in-degree equal to zero. The running time of the algorithm is O(V+E). Q8: Two special vertices s and t in the undirected graph G=(V,E) have the following property: any path from s to t has at least 1 + V /2 edges. Show that all paths from s to t must have a common vertex v (not equal to either s or t) and give an algorithm with running time O(V+E) to find such a node v. Because the statement of the problem, there cannot be only one path from s to t. (1) Proof: Any path from s to t has at least 1+ V /2 edges. There are at least V /2 vertices on the path from s to v (excluding s and v). Assuming that the vertices between s and t in two different paths are distinct, i.e., no two vertices are identical in this two paths except s and t. Then there will be at least V distinct vertices, which is a contradiction because we only have V - 2 vertices left. (2) Algorithm: Q9((Extra credit 25) Problem 22-3.)

9 Q10((Extra credit 25) Problem 22-4.) Q11(Exercise )Show that if an edge (u,v) is contained in some minimum spanning tree, then it is a light edge crossing some cut of the graph. Proof: Let Tu and Tv be two trees produced by a cut of the minimum spanning tree. So the edge (u,v) now is crossing the cut, together with some other possible edges are also crossing the cut. Assuming that there is an edge (a,b) whose weight w(a,b) is smaller than w(u,v), then (a,b) can be the light edge which link Tu and Tv together to become a minimum spanning tree and the minimum spanning tree containing (u,v) is no longer a MST -- which is a contradiction. Therefore, if an edge (u,v) is contained in some minimum spanning tree, then it is a light edge crossing some cut of the graph. Q12(Exercise ) Suppose that we represent the graph G = (V,E) as an adjacency matrix. Give a simple implementation of Prim s algorithm for this case that runs in O(V 2 ) time. The input of the algorithm is an adjacency matrix, where the element of G[i,j] is if there is no edge between vertex i and vertex j. If the element is a positive integer, then there is an edge with the weight indicated by that integer existing between vertex i and vertex j. MST-PRIM-ADJ-MATRIX(G, V,r) //G is the input matrix, V is the number of vertices, //r is the source. let A[1.. V ] be a new empty min-heap //every element of A is an object containing these //fields: index, key, π for i = 1 to V This for loop contributes an // u is an object O( V ) running time. u.index = i if i == r u.key = 0 u.key = u.π = NIL MIN-HEAP-INSERT(A,u) // This insertion is done // according to u.key while A is nonempty i = HEAP-EXTRACT-MIN(A) // i is the object, // whose key value is minimum. for j = 1 to V if G.Adj-Matrix[i.index][j]!=

10 if the object with index j is in Q and G.Adj-Matrix[i.index][j] < A[j].key A[j].π = i.index A[j].key = G.Adj-Matrix[i.index][j] The bold while loop and the nested for loop together contribute an O( V 2 ) running time. Running time analysis: As the annotations beside the code, the algorithm together has an O( V 2 ) running time. Something needed to be added up here is, the bold while loop has to inspect V vertices and the nested bold for loop will inspect V vertices to see if they are adjacent to the current vertex. Thus, together the while loop contributes an O( V 2 ) running time. Q13(Exercise ) Suppose that all edge weights in a graph are integers in the range from 1 to V. How fast can you make Kruskal s algorithm run? What if the edge weights are integers in the range from 1 to W for some constant W? Here we use counting-sort to sort the edges of G.E into nondecreasing order by weight w. (1) Because counting-sort sorts n integers with range 1 to k in O(n+k) time, sorting E edges with the weight ranging from 1 to V will need O( E + V ) time. Then the Kruskal s algorithm will have an O(E+V+ElgV) = O(V+ElgV) running time. (2) Use counting-sort to sort E edges whose weight ranging from 1 to W will need an O( E +W) running time. Then the Kruskal s algorithm will have an O(E+W+ElgV) = O(W+ElgV) running time. Q14(Exercise ) Suppose that all edge weights in a graph are integers in the range from 1 to V. How fast can you make Prim s algorithm run? What if the edge weights are integers in the range from 1 to W for some constant W? No matter the range of the weights is 1 to V or 1 to W, there is no way to speed up MST-PRIM. So the running time of Prim s algorithm is O(E+VlgV) by using Fibonacci heap. Q15((Extra credit 40 pts) Problem 23-1.) Q16((Extra credit 30 pts) Exercise ) Q17(Extra credit 30 pts) Write the code for Kruskal algorithm in a language of your choice. You will first have to read on the disjoint sets datastructures and operations (Chapter 21 in the book) for an efficient implementation of Kruskal trees.

22.1 Representations of graphs

22.1 Representations of graphs 22.1 Representations of graphs There are two standard ways to represent a (directed or undirected) graph G = (V,E), where V is the set of vertices (or nodes) and E is the set of edges (or links). Adjacency

More information

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary Graph Algorithms Representations Breadth-First Search Depth-First Search Topological Sort Strongly Connected Components CS 5633 Analysis of Algorithms Chapter 22: Slide 1 Graph Representations

More information

22.3 Depth First Search

22.3 Depth First Search 22.3 Depth First Search Depth-First Search(DFS) always visits a neighbour of the most recently visited vertex with an unvisited neighbour. This means the search moves forward when possible, and only backtracks

More information

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort

Data Structures. Elementary Graph Algorithms BFS, DFS & Topological Sort Data Structures Elementary Graph Algorithms BFS, DFS & Topological Sort 1 Graphs A graph, G = (V, E), consists of two sets: V is a finite non-empty set of vertices. E is a set of pairs of vertices, called

More information

COT 6405 Introduction to Theory of Algorithms

COT 6405 Introduction to Theory of Algorithms COT 6405 Introduction to Theory of Algorithms Topic 14. Graph Algorithms 11/7/2016 1 Elementary Graph Algorithms How to represent a graph? Adjacency lists Adjacency matrix How to search a graph? Breadth-first

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP Complexity & Analysis of Data Structures & Algorithms Overview of Graphs Breadth irst Search, and Depth irst Search hanks to several people who contributed to these slides including Piyush Kumar and

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Next: Graph Algorithms Graphs Ch 22 Graph representations adjacency list adjacency matrix Minimum Spanning Trees Ch 23 Traversing graphs Breadth-First Search Depth-First Search 11/30/17 CSE 3101 1 Graphs

More information

Data Structure and Algorithm, Spring 2017 Final Examination

Data Structure and Algorithm, Spring 2017 Final Examination Data Structure and Algorithm, Spring 2017 Final Examination Date: Tuesday, June 20, 2017 Time: 13:20-16:20pm (180 minutes) 138 points Non-Programming problems Problem 1. In each of the following question,

More information

Minimum Spanning Trees My T. UF

Minimum Spanning Trees My T. UF Introduction to Algorithms Minimum Spanning Trees @ UF Problem Find a low cost network connecting a set of locations Any pair of locations are connected There is no cycle Some applications: Communication

More information

Figure 1: A directed graph.

Figure 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 information

Depth-first Search (DFS)

Depth-first Search (DFS) Depth-first Search (DFS) DFS Strategy: First follow one path all the way to its end, before we step back to follow the next path. (u.d and u.f are start/finish time for vertex processing) CH08-320201:

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 9 Minimum Spanning Trees Thanks to the text authors who contributed to these slides Why Minimum Spanning Trees (MST)? Example 1 A

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Topological sort Shortest paths Adam Smith The (Algorithm) Design Process 1. Work out the answer for some examples. Look for a general principle

More information

Elementary 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: 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 information

Data Structures and Algorithms. Werner Nutt

Data Structures and Algorithms. Werner Nutt Data Structures and Algorithms Werner Nutt nutt@inf.unibz.it http://www.inf.unibz/it/~nutt Part 9 Academic Year 2011-2012 1 Acknowledgements & Copyright Notice These slides are built on top of slides developed

More information

Unweighted directed graphs

Unweighted directed graphs Unweighted directed graphs Announcements Midterm & gradescope - will get an email today to register (username name is your email) - tests should appear next Tuesday (nothing there now) Graph A directed

More information

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo

More information

Data Structures and Algorithms. Chapter 7. Graphs

Data Structures and Algorithms. Chapter 7. Graphs 1 Data Structures and Algorithms Chapter 7 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8 Fundamental Algorithms CSCI-GA.70-00/Summer 206 Solution to Homework 8 Problem (CLRS 23.-6). ( point) Show that a graph has a unique minimum spanning tree if, for every cut of the graph, there is a unique

More information

Chapter 22: Elementary Graph Algorithms. Definitions:

Chapter 22: Elementary Graph Algorithms. Definitions: Chapter 22: Elementary Graph Algorithms. Definitions: 1. G = (V, E), where V is a set of points and E is a set of edges connecting point-pairs from V. 2. We use V synonymously with V and E with E when

More information

CSI 604 Elementary Graph Algorithms

CSI 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 information

Week 11: Minimum Spanning trees

Week 11: Minimum Spanning trees Week 11: Minimum Spanning trees Agenda: Minimum Spanning Trees Prim s Algorithm Reading: Textbook : 61-7 1 Week 11: Minimum Spanning trees Minimum spanning tree (MST) problem: Input: edge-weighted (simple,

More information

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Chapter 23: Minimal Spanning Trees. Context: Weighted, connected, undirected graph, G = (V, E), with w : E R. Definition: A selection of edges from T E such that (V, T ) is a tree is called a spanning

More information

Graph Algorithms. Definition

Graph Algorithms. Definition Graph Algorithms Many problems in CS can be modeled as graph problems. Algorithms for solving graph problems are fundamental to the field of algorithm design. Definition A graph G = (V, E) consists of

More information

Announcements. HW3 is graded. Average is 81%

Announcements. HW3 is graded. Average is 81% CSC263 Week 9 Announcements HW3 is graded. Average is 81% Announcements Problem Set 4 is due this Tuesday! Due Tuesday (Nov 17) Recap The Graph ADT definition and data structures BFS gives us single-source

More information

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019 CS 341: Algorithms Douglas R. Stinson David R. Cheriton School of Computer Science University of Waterloo February 26, 2019 D.R. Stinson (SCS) CS 341 February 26, 2019 1 / 296 1 Course Information 2 Introduction

More information

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths Part VI Graph algorithms Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths 1 Chapter 22 Elementary Graph Algorithms Representations of graphs

More information

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, ) Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 1. if >. 2. error new key is greater than current key 3.. 4.. 5. if NIL and.

More information

Graph: representation and traversal

Graph: 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 information

Graph Representations and Traversal

Graph 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 information

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review 1 Review 1 Something I did not emphasize enough last time is that during the execution of depth-firstsearch, we construct depth-first-search trees. One graph may have multiple depth-firstsearch trees,

More information

23.2 Minimum Spanning Trees

23.2 Minimum Spanning Trees 23.2 Minimum Spanning Trees Kruskal s algorithm: Kruskal s algorithm solves the Minimum Spanning Tree problem in O( E log V ) time. It employs the disjoint-set data structure that is similarly used for

More information

Minimum Spanning Tree (undirected graph)

Minimum Spanning Tree (undirected graph) 1 Minimum Spanning Tree (undirected graph) 2 Path tree vs. spanning tree We have constructed trees in graphs for shortest path to anywhere else (from vertex is the root) Minimum spanning trees instead

More information

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees. Tree 1. Trees and their Properties. Spanning trees 3. Minimum Spanning Trees 4. Applications of Minimum Spanning Trees 5. Minimum Spanning Tree Algorithms 1.1 Properties of Trees: Definition: A graph G

More information

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms Cut vertices, Cut Edges and Biconnected components MTL776 Graph algorithms Articulation points, Bridges, Biconnected Components Let G = (V;E) be a connected, undirected graph. An articulation point of

More information

Graph Representation

Graph Representation Graph Representation Adjacency list representation of G = (V, E) An array of V lists, one for each vertex in V Each list Adj[u] contains all the vertices v such that there is an edge between u and v Adj[u]

More information

Chapter 23. Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees Chapter 23. Minimum Spanning Trees We are given a connected, weighted, undirected graph G = (V,E;w), where each edge (u,v) E has a non-negative weight (often called length) w(u,v). The Minimum Spanning

More information

Homework No. 4 Answers

Homework No. 4 Answers Homework No. 4 Answers CSCI 4470/6470 Algorithms, CS@UGA, Spring 2018 Due Thursday March 29, 2018 There are 100 points in total. 1. (20 points) Consider algorithm DFS (or algorithm To-Start-DFS in Lecture

More information

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem Minimum Spanning Trees (Forests) Given an undirected graph G=(V,E) with each edge e having a weight w(e) : Find a subgraph T of G of minimum total weight s.t. every pair of vertices connected in G are

More information

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai

Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai Slides credited from Hsueh-I Lu, Hsu-Chun Hsiao, & Michael Tsai Mini-HW 7 released Due on 11/30 (Thur) 17:20 Homework 3 released tonight Due on 12/14 (Thur) 17:20 (three weeks) Class change!! Start from

More information

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013 CSE373: Data Structures & Algorithms Lecture 7: Minimum Spanning Trees Dan Grossman Fall 03 Spanning Trees A simple problem: Given a connected undirected graph G=(V,E), find a minimal subset of edges such

More information

Homework Assignment #3 Graph

Homework Assignment #3 Graph CISC 4080 Computer Algorithms Spring, 2019 Homework Assignment #3 Graph Some of the problems are adapted from problems in the book Introduction to Algorithms by Cormen, Leiserson and Rivest, and some are

More information

Chapter 22. Elementary Graph Algorithms

Chapter 22. Elementary Graph Algorithms Graph Algorithms - Spring 2011 Set 7. Lecturer: Huilan Chang Reference: (1) Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 2nd Edition, The MIT Press. (2) Lecture notes from C. Y. Chen

More information

Graph representation

Graph representation Graph Algorithms 1 Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: 1. Adjacency lists. 2. Adjacency matrix. When expressing

More information

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V)

Graphs. Graph G = (V, E) Types of graphs E = O( V 2 ) V = set of vertices E = set of edges (V V) Graph Algorithms Graphs Graph G = (V, E) V = set of vertices E = set of edges (V V) Types of graphs Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.) Directed: (u, v) is edge from

More information

Algorithm Design and Analysis

Algorithm 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 information

Basic Graph Algorithms

Basic Graph Algorithms Basic Graph Algorithms 1 Representations of Graphs There are two standard ways to represent a graph G(V, E) where V is the set of vertices and E is the set of edges. adjacency list representation adjacency

More information

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1

Graph Algorithms. Chapter 22. CPTR 430 Algorithms Graph Algorithms 1 Graph Algorithms Chapter 22 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms

More information

Solutions to Midterm 2 - Monday, July 11th, 2009

Solutions to Midterm 2 - Monday, July 11th, 2009 Solutions to Midterm - Monday, July 11th, 009 CPSC30, Summer009. Instructor: Dr. Lior Malka. (liorma@cs.ubc.ca) 1. Dynamic programming. Let A be a set of n integers A 1,..., A n such that 1 A i n for each

More information

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms, Lecture 1 /1/200 Introduction to Algorithms.04J/1.401J LECTURE 11 Graphs, MST, Greedy, Prim Graph representation Minimum spanning trees Greedy algorithms hallmarks. Greedy choice

More information

Announcements Problem Set 5 is out (today)!

Announcements Problem Set 5 is out (today)! CSC263 Week 10 Announcements Problem Set is out (today)! Due Tuesday (Dec 1) Minimum Spanning Trees The Graph of interest today A connected undirected weighted graph G = (V, E) with weights w(e) for each

More information

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1 Undirected Graphs Terminology. Free Trees. Representations. Minimum Spanning Trees (algorithms: Prim, Kruskal). Graph Traversals (dfs, bfs). Articulation points & Biconnected Components. Graph Matching

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

Unit 2: Algorithmic Graph Theory

Unit 2: Algorithmic Graph Theory Unit 2: Algorithmic Graph Theory Course contents: Introduction to graph theory Basic graph algorithms Reading Chapter 3 Reference: Cormen, Leiserson, and Rivest, Introduction to Algorithms, 2 nd Ed., McGraw

More information

Minimum Spanning Trees

Minimum Spanning Trees CS124 Lecture 5 Spring 2011 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

Minimum Spanning Trees

Minimum Spanning Trees Minimum Spanning Trees Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and

More information

Introduction to Algorithms. Lecture 11

Introduction to Algorithms. Lecture 11 Introduction to Algorithms Lecture 11 Last Time Optimization Problems Greedy Algorithms Graph Representation & Algorithms Minimum Spanning Tree Prim s Algorithm Kruskal s Algorithm 2 Today s Topics Shortest

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees CIS 121 Data Structures and Algorithms Minimum Spanning Trees March 19, 2019 Introduction and Background Consider a very natural problem: we are given a set of locations V = {v 1, v 2,..., v n }. We want

More information

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems

Chapter 24. Shortest path problems. Chapter 24. Shortest path problems. 24. Various shortest path problems. Chapter 24. Shortest path problems Chapter 24. Shortest path problems We are given a directed graph G = (V,E) with each directed edge (u,v) E having a weight, also called a length, w(u,v) that may or may not be negative. A shortest path

More information

22 Elementary Graph Algorithms. There are two standard ways to represent a

22 Elementary Graph Algorithms. There are two standard ways to represent a VI Graph Algorithms Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths All-Pairs Shortest Paths 22 Elementary Graph Algorithms There are two standard ways to represent a graph

More information

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck Theory of Computing Lecture 10 MAS 714 Hartmut Klauck Seven Bridges of Königsberg Can one take a walk that crosses each bridge exactly once? Seven Bridges of Königsberg Model as a graph Is there a path

More information

Elementary Graph Algorithms

Elementary Graph Algorithms Elementary Graph Algorithms Graphs Graph G = (V, E)» V = set of vertices» E = set of edges (V V) Types of graphs» Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops.)» Directed: (u, v)

More information

Minimum Spanning Tree

Minimum Spanning Tree Minimum Spanning Tree 1 Minimum Spanning Tree G=(V,E) is an undirected graph, where V is a set of nodes and E is a set of possible interconnections between pairs of nodes. For each edge (u,v) in E, we

More information

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms Elementary Graph Algorithms: Summary CmSc250 Intro to Algorithms Definition: A graph is a collection (nonempty set) of vertices and edges A path from vertex x to vertex y : a list of vertices in which

More information

6.1 Minimum Spanning Trees

6.1 Minimum Spanning Trees CS124 Lecture 6 Fall 2018 6.1 Minimum Spanning Trees A tree is an undirected graph which is connected and acyclic. It is easy to show that if graph G(V,E) that satisfies any two of the following properties

More information

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Greedy Algorithms. At each step in the algorithm, one of several choices can be made. Greedy Algorithms At each step in the algorithm, one of several choices can be made. Greedy Strategy: make the choice that is the best at the moment. After making a choice, we are left with one subproblem

More information

Outline. Graphs. Divide and Conquer.

Outline. Graphs. Divide and Conquer. GRAPHS COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges books. Outline Graphs.

More information

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES Announcements Algorithms ROBERT SEDGEWICK KEVIN WAYNE Exam Regrades Due by Wednesday s lecture. Teaching Experiment: Dynamic Deadlines (WordNet) Right now, WordNet is due at PM on April 8th. Starting Tuesday

More information

Complexity of Prim s Algorithm

Complexity of Prim s Algorithm The main loop is: Complexity of Prim s Algorithm while ( not ISEMPTY(Q) ): u = EXTRACT-MIN(Q) if p[u]!= NIL: A = A U {(p[u],u)} for v in adjacency-list[u]: if v in Q and w(u,v) < priority[v] : DECREASE-PRIORITY(v,

More information

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other.

Strongly connected: A directed graph is strongly connected if every pair of vertices are reachable from each other. Directed Graph In a directed graph, each edge (u, v) has a direction u v. Thus (u, v) (v, u). Directed graph is useful to model many practical problems (such as one-way road in traffic network, and asymmetric

More information

Practical Session No. 12 Graphs, BFS, DFS, Topological sort

Practical Session No. 12 Graphs, BFS, DFS, Topological sort Practical Session No. 12 Graphs, BFS, DFS, Topological sort Graphs and BFS Graph G = (V, E) Graph Representations (V G ) v1 v n V(G) = V - Set of all vertices in G E(G) = E - Set of all edges (u,v) in

More information

All Shortest Paths. Questions from exercises and exams

All Shortest Paths. Questions from exercises and exams All Shortest Paths Questions from exercises and exams The Problem: G = (V, E, w) is a weighted directed graph. We want to find the shortest path between any pair of vertices in G. Example: find the distance

More information

Minimum spanning trees

Minimum spanning trees Minimum spanning trees [We re following the book very closely.] One of the most famous greedy algorithms (actually rather family of greedy algorithms). Given undirected graph G = (V, E), connected Weight

More information

CS521 \ Notes for the Final Exam

CS521 \ Notes for the Final Exam CS521 \ Notes for final exam 1 Ariel Stolerman Asymptotic Notations: CS521 \ Notes for the Final Exam Notation Definition Limit Big-O ( ) Small-o ( ) Big- ( ) Small- ( ) Big- ( ) Notes: ( ) ( ) ( ) ( )

More information

Depth-First Search Depth-first search (DFS) is another way to traverse the graph.

Depth-First Search Depth-first search (DFS) is another way to traverse the graph. Depth-First Search Depth-first search (DFS) is another way to traverse the graph. Motivating example: In a video game, you are searching for a path from a point in a maze to the exit. The maze can be modeled

More information

CSE 100 Minimum Spanning Trees Prim s and Kruskal

CSE 100 Minimum Spanning Trees Prim s and Kruskal CSE 100 Minimum Spanning Trees Prim s and Kruskal Your Turn The array of vertices, which include dist, prev, and done fields (initialize dist to INFINITY and done to false ): V0: dist= prev= done= adj:

More information

Basic Graph Algorithms (CLRS B.4-B.5, )

Basic Graph Algorithms (CLRS B.4-B.5, ) Basic Graph Algorithms (CLRS B.-B.,.-.) Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices V and a finite set of edges E. Directed graphs: E is a set of ordered pairs of vertices

More information

Review: Graph Theory and Representation

Review: Graph Theory and Representation Review: Graph Theory and Representation Graph Algorithms Graphs and Theorems about Graphs Graph implementation Graph Algorithms Shortest paths Minimum spanning tree What can graphs model? Cost of wiring

More information

Lecture Notes for Chapter 23: Minimum Spanning Trees

Lecture Notes for Chapter 23: Minimum Spanning Trees Lecture Notes for Chapter 23: Minimum Spanning Trees Chapter 23 overview Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has

More information

Lecture 25 Spanning Trees

Lecture 25 Spanning Trees Lecture 25 Spanning Trees 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, Iliano Cervesato The following is a simple example of a connected, undirected graph with 5 vertices (A,

More information

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016 CS125 Lecture 2 Fall 2016 2.1 Greedy Algorithms We will start talking about methods high-level plans for constructing algorithms. One of the simplest is just to have your algorithm be greedy. Being greedy,

More information

COMP 251 Winter 2017 Online quizzes with answers

COMP 251 Winter 2017 Online quizzes with answers COMP 251 Winter 2017 Online quizzes with answers Open Addressing (2) Which of the following assertions are true about open address tables? A. You cannot store more records than the total number of slots

More information

The minimum spanning tree problem

The minimum spanning tree problem The minimum spanning tree problem MST is a minimum cost connection problem on graphs The graph can model the connection in a (hydraulic, electric, telecommunication) network: the nodes are the points that

More information

Solutions to Exam Data structures (X and NV)

Solutions to Exam Data structures (X and NV) Solutions to Exam Data structures X and NV 2005102. 1. a Insert the keys 9, 6, 2,, 97, 1 into a binary search tree BST. Draw the final tree. See Figure 1. b Add NIL nodes to the tree of 1a and color it

More information

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170 UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, 2003 Notes 8 for CS 170 1 Minimum Spanning Trees A tree is an undirected graph that is connected

More information

Graph: representation and traversal

Graph: representation and traversal Graph: representation and traversal CISC5835, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang Acknowledgement The set of slides have use materials from the following resources Slides for textbook

More information

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Trees Ch 23 Traversing graphs Net: Graph Algorithms Graphs Ch 22 Graph representations adjacenc list adjacenc matri Minimum Spanning Trees Ch 23 Traersing graphs Breadth-First Search Depth-First Search 7/16/2013 CSE 3101 1 Graphs Definition

More information

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u]. Tutorial Question 1 A depth-first forest classifies the edges of a graph into tree, back, forward, and cross edges. A breadth-first tree can also be used to classify the edges reachable from the source

More information

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity. 1. T F: Consider a directed graph G = (V, E) and a vertex s V. Suppose that for all v V, there exists a directed path in G from s to v. Suppose that a DFS is run on G, starting from s. Then, true or false:

More information

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Representations 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 information

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Computer 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 information

Algorithm Design, Anal. & Imp., Homework 4 Solution

Algorithm Design, Anal. & Imp., Homework 4 Solution Algorithm Design, Anal. & Imp., Homework 4 Solution Note: The solution is for your personal use for this course. You are not allowed to post the solution in public place. There could be mistakes in the

More information

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018 Q1: Prove or disprove: You are given a connected undirected graph G = (V, E) with a weight function w defined over its

More information

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees CSE1 Introduction to Algorithms Lecture 1 Minimum Spanning Trees Antoine Vigneron antoine@unist.ac.kr Ulsan National Institute of Science and Technology July 11, 201 Antoine Vigneron (UNIST) CSE1 Lecture

More information

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018 CS 4 Section # Shortest Paths and MSTs //08 Shortest Paths There are types of shortest paths problems: Single source single destination Single source to all destinations All pairs shortest path In today

More information

DFS & STRONGLY CONNECTED COMPONENTS

DFS & STRONGLY CONNECTED COMPONENTS DFS & STRONGLY CONNECTED COMPONENTS CS 4407 Search Tree Breadth-First Search (BFS) Depth-First Search (DFS) Depth-First Search (DFS) u d[u]: when u is discovered f[u]: when searching adj of u is finished

More information

(Re)Introduction to Graphs and Some Algorithms

(Re)Introduction to Graphs and Some Algorithms (Re)Introduction to Graphs and Some Algorithms Graph Terminology (I) A graph is defined by a set of vertices V and a set of edges E. The edge set must work over the defined vertices in the vertex set.

More information

CS 561, Lecture 9. Jared Saia University of New Mexico

CS 561, Lecture 9. Jared Saia University of New Mexico CS 561, Lecture 9 Jared Saia University of New Mexico Today s Outline Minimum Spanning Trees Safe Edge Theorem Kruskal and Prim s algorithms Graph Representation 1 Graph Definition A graph is a pair of

More information

Solutions to relevant spring 2000 exam problems

Solutions to relevant spring 2000 exam problems Problem 2, exam Here s Prim s algorithm, modified slightly to use C syntax. MSTPrim (G, w, r): Q = V[G]; for (each u Q) { key[u] = ; key[r] = 0; π[r] = 0; while (Q not empty) { u = ExtractMin (Q); for

More information