1 / 61

CSCE 411 Design and Analysis of Algorithms

CSCE 411 Design and Analysis of Algorithms. Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2012. a. c. d. b. e. Adjacency List Representation. b. c. a. b. a. d. e. c. a. d. d. b. c. e. e. b. d. Space-efficient for sparse graphs. a. c. d. b. e.

mmillsap
Download Presentation

CSCE 411 Design and Analysis of Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE 411Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 8

  2. a c d b e Adjacency List Representation b c a b a d e c a d d b c e e b d Space-efficient for sparse graphs CSCE 411, Spring 2012: Set 8

  3. a c d b e Adjacency Matrix Representation a b c d e 0 a 1 1 0 0 b 1 0 0 1 1 c 1 0 0 1 0 d 0 1 1 0 1 e 0 1 0 1 0 Check for an edge in constant time CSCE 411, Spring 2012: Set 8

  4. Graph Traversals • Ways to traverse/search a graph • Visit every vertex exactly once • Breadth-First Search • Depth-First Search CSCE 411, Spring 2012: Set 8

  5. Breadth First Search (BFS) • Input: G = (V,E) and source s in V • for each vertex v in V do • mark v as unvisited • mark s as visited • enq(Q,s) // FIFO queue Q • while Q is not empty do • u := deq(Q) • for each unvisited neighbor v of u do • mark v as visited • enq(Q,v) CSCE 411, Spring 2012: Set 8

  6. a c d b s BFS Example • <board work> CSCE 411, Spring 2012: Set 8

  7. BFS Tree • We can make a spanning tree rooted at s by remembering the "parent" of each vertex CSCE 411, Spring 2012: Set 8

  8. Breadth First Search #2 • Input: G = (V,E) and source s in V • for each vertex v in V do • mark v as unvisited • parent[v] := nil • mark s as visited • parent[s] := s • enq(Q,s) // FIFO queue Q CSCE 411, Spring 2012: Set 8

  9. Breadth First Search #2 • while Q is not empty do • u := deq(Q) • for each unvisited neighbor v of u do • mark v as visited • parent[v] := u • enq(Q,v) CSCE 411, Spring 2012: Set 8

  10. a c d b s BFS Tree Example CSCE 411, Spring 2012: Set 8

  11. BFS Trees • BFS tree is not necessarily unique for a given graph • Depends on the order in which neighboring vertices are processed CSCE 411, Spring 2012: Set 8

  12. BFS Numbering • During the breadth-first search, assign an integer to each vertex • Indicate the distance of each vertex from the source s CSCE 411, Spring 2012: Set 8

  13. Breadth First Search #3 • Input: G = (V,E) and source s in V • for each vertex v in V do • mark v as unvisited • parent[v] := nil • d[v] := infinity • mark s as visited • parent[s] := s • d[s] := 0 • enq(Q,s) // FIFO queue Q CSCE 411, Spring 2012: Set 8

  14. Breadth First Search #3 • while Q is not empty do • u := deq(Q) • for each unvisited neighbor v of u do • mark v as visited • parent[v] := u • d[v] := d[u] + 1 • enq(Q,v) CSCE 411, Spring 2012: Set 8

  15. a c d b s BFS Numbering Example d = 1 d = 2 d = 0 d = 2 d = 1 CSCE 411, Spring 2012: Set 8

  16. Shortest Path Tree • Theorem: BFS algorithm • visits all and only vertices reachable from s • sets d[v] equal to the shortest path distance from s to v, for all vertices v, and • sets parent variables to form a shortest path tree CSCE 411, Spring 2012: Set 8

  17. Proof Ideas • Use induction on distance from s to show that the d-values are set properly. • Basis: distance 0. d[s] is set to 0. • Induction: Assume true for all vertices at distance x-1 and show for every vertex v at distance x. • Since v is at distance x, it has at least one neighbor at distance x-1. Let u be the first of these neighbors that is enqueued. CSCE 411, Spring 2012: Set 8

  18. u c d v s Proof Ideas dist=x+1 dist=x-1 dist=x dist=x-1 • Key property of shortest path distances: if v has distance x, • it must have a neighbor with distance x-1, • no neighbor has distance less than x-1, and • no neighbor has distance more than x+1 CSCE 411, Spring 2012: Set 8

  19. Proof Ideas • Fact: When u is dequeued, v is still unvisited. • because of how queue operates and since d never underestimates the distance • By induction, d[u] = x-1. • When v is enqueued, d[v] is set to d[u] + 1= x CSCE 411, Spring 2012: Set 8

  20. BFS Running Time • Initialization of each vertex takes O(V) time • Every vertex is enqueued once and dequeued once, taking O(V) time • When a vertex is dequeued, all its neighbors are checked to see if they are unvisited, taking time proportional to number of neighbors of the vertex, and summing to O(E) over all iterations • Total time is O(V+E) CSCE 411, Spring 2012: Set 8

  21. Depth-First Search • Input: G = (V,E) • for each vertex u in V do • mark u as unvisited • for each unvisited vertex u in V do • call recursiveDFS(u) • recursiveDFS(u): • mark u as visited • for each unvisited neighbor v of u do • call recursiveDFS(v) CSCE 411, Spring 2012: Set 8

  22. a b c d e f g h DFS Example • <board work> CSCE 411, Spring 2012: Set 8

  23. a d b c e Disconnected Graphs • What if graph is disconnected or is directed? • call DFS on several vertices to visit all vertices • purpose of second for-loop in non-recursive wrapper CSCE 411, Spring 2012: Set 8

  24. DFS Tree • Actually might be a DFS forest (collection of trees) • Keep track of parents CSCE 411, Spring 2012: Set 8

  25. Depth-First Search #2 • Input: G = (V,E) • for each vertex u in V do • mark u as unvisited • parent[u] := nil • for each unvisited vertex u in V do • parent[u] := u // a root • call recursive DFS(u) • recursiveDFS(u): • mark u as visited • for each unvisited neighbor v of u do • parent[v] := u • call recursiveDFS(v) CSCE 411, Spring 2012: Set 8

  26. More Properties of DFS • Need to keep track of more information for each vertex: • discovery time: when its recursive call starts • finish time: when its recursive call ends CSCE 411, Spring 2012: Set 8

  27. Depth-First Search #3 • Input: G = (V,E) • for each vertex u in V do • mark u as unvisited • parent[u] := nil • time := 0 • for each unvisited vertex u in V do • parent[u] := u // a root • call recursive DFS(u) • recursiveDFS(u): • mark u as visited • time++ • disc[u] := time • for each unvisited neighbor v of u do • parent[v] := u • call recursiveDFS(v) • time++ • fin[u] := time CSCE 411, Spring 2012: Set 8

  28. Running Time of DFS • initialization takes O(V) time • second for loop in non-recursive wrapper considers each vertex, so O(V) iterations • one recursive call is made for each vertex • in recursive call for vertex u, all its neighbors are checked; total time in all recursive calls is O(E) • Total time is O(V+E) CSCE 411, Spring 2012: Set 8

  29. Nested Intervals • Let interval for vertex v be [disc[v],fin[v]]. • Fact: For any two vertices, either one interval precedes the other or one is enclosed in the other. • because recursive calls are nested • Corollary: v is a descendant of u in the DFS forest if and only if v's interval is inside u's interval. CSCE 411, Spring 2012: Set 8

  30. Classifying Edges • Consider edge (u,v) in directed graph G = (V,E) w.r.t. DFS forest • tree edge: v is a child of u • back edge: v is an ancestor of u • forward edge: v is a descendant of u but not a child • cross edge: none of the above CSCE 411, Spring 2012: Set 8

  31. a e b c d f Example of Classifying Edges in DFS forest tree not in DFS forest forward tree tree cross back back tree CSCE 411, Spring 2012: Set 8

  32. DFS Application: Topological Sort • Given a directed acyclic graph (DAG), find a linear ordering of the vertices such that if (u,v) is an edge, then u precedes v. • DAG indicates precedence among events: • events are graph vertices, edge from u to v means event u has precedence over event v • Partial order because not all events have to be done in a certain order CSCE 411, Spring 2012: Set 8

  33. Precedence Example • Tasks that have to be done to eat breakfast: • get glass, pour juice, get bowl, pour cereal, pour milk, get spoon, eat. • Certain events must happen in a certain order (ex: get bowl before pouring milk) • For other events, it doesn't matter (ex: get bowl and get spoon) CSCE 411, Spring 2012: Set 8

  34. Precedence Example get glass get bowl pour juice pour cereal get spoon pour milk eat breakfast Order: glass, juice, bowl, cereal, milk, spoon, eat. CSCE 411, Spring 2012: Set 8

  35. Why Acyclic? • Why must directed graph by acyclic for the topological sort problem? • Otherwise, no way to order events linearly without violating a precedence constraint. CSCE 411, Spring 2012: Set 8

  36. Idea for Topological Sort Alg. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 eat milk spoon juice cereal glass bowl consider reverse order of finishing times: spoon, bowl, cereal, milk, glass, juice, eat CSCE 411, Spring 2012: Set 8

  37. Topological Sort Algorithm input: DAG G = (V,E) 1. call DFS on G to compute finish[v] for all vertices v 2. when each vertex's recursive call finishes, insert it on the front of a linked list 3. return the linked list Running Time: O(V+E) CSCE 411, Spring 2012: Set 8

  38. Correctness of T.S. Algorithm Show that if (u,v) is an edge, then v finishes before u finishes. Thus the algorithm correctly orders u before v. Case 1: u is discovered before v is discovered. By the way DFS works, u does not finish until v is discovered and v finishes. Then v finishes before u finishes. disc(v) fin(v) u v fin(u) disc(u) v u CSCE 411, Spring 2012: Set 8

  39. Correctness of T.S. Algorithm Show that if (u,v) is an edge, then v finishes before u finishes. Thus the algorithm correctly orders u before v. Case 2: v is discovered before u is discovered. Suppose u finishes before v finishes (i.e., u is nested inside v). Show this is impossible… disc(u) fin(u) u v fin(v) disc(v) v u CSCE 411, Spring 2012: Set 8

  40. Correctness of T.S. Algorithm • v is discovered but not yet finished when u is discovered. • Then u is a descendant of v. • But that would make (u,v) a back edge and a DAG cannot have a back edge (the back edge would form a cycle). • Thus v finishes before u finishes. CSCE 411, Spring 2012: Set 8

  41. DFS Application: Strongly Connected Components • Consider a directed graph. • A strongly connected component (SCC) of the graph is a maximal set of vertices with a (directed) path between every pair of vertices • Problem: Find all the SCCs of the graph. CSCE 411, Spring 2012: Set 8

  42. What Are SCCs Good For? Packaging software modules: • Construct directed graph of which modules call which other modules • A SCC is a set of mutually interacting modules • Pack together those in the same SCC from http://www.cs.princeton.edu/courses/archive/fall07/cos226/lectures.html CSCE 411, Spring 2012: Set 8

  43. h f a e g c b d SCC Example four SCCs CSCE 411, Spring 2012: Set 8

  44. How Can DFS Help? • Suppose we run DFS on the directed graph. • All vertices in the same SCC are in the same DFS tree. • But there might be several different SCCs in the same DFS tree. • Example: start DFS from vertex h in previous graph CSCE 411, Spring 2012: Set 8

  45. Main Idea of SCC Algorithm • DFS tells us which vertices are reachable from the roots of the individual trees • Also need information in the "other direction": is the root reachable from its descendants? • Run DFS again on the "transpose" graph (reverse the directions of the edges) CSCE 411, Spring 2012: Set 8

  46. SCC Algorithm input: directed graph G = (V,E) • call DFS(G) to compute finishing times • compute GT // transpose graph • call DFS(GT), considering vertices in decreasing order of finishing times • each tree from Step 3 is a separate SCC of G CSCE 411, Spring 2012: Set 8

  47. h f a e g c b d SCC Algorithm Example input graph - run DFS CSCE 411, Spring 2012: Set 8

  48. h c d e b g a f After Step 1 fin(c) fin(f) fin(d) fin(b) fin(e) fin(a) fin(h) fin(g) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Order of vertices for Step 3: f, g, h, a, e, b, d, c CSCE 411, Spring 2012: Set 8

  49. h f a e g c b d After Step 2 transposed input graph - run DFS with specified order of vertices CSCE 411, Spring 2012: Set 8

  50. g h c e f d a b After Step 3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 SCCs are {f,h,g} and {a,e} and {b,c} and {d}. CSCE 411, Spring 2012: Set 8

More Related