Presentation is loading. Please wait.

Presentation is loading. Please wait.

Many slides here are based on E. Demaine , D. Luebke slides

Similar presentations


Presentation on theme: "Many slides here are based on E. Demaine , D. Luebke slides"— Presentation transcript:

1 Many slides here are based on E. Demaine , D. Luebke slides
CS583 Lecture 09 Jana Kosecka Graph Algorithms Depth First Search Topological Sort Minimum Spanning Tree Many slides here are based on E. Demaine , D. Luebke slides

2 Depth-First Search Depth-first search is another strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

3 Depth-First Search Vertices initially colored white
Then colored gray when discovered Then black when finished

4 DFS Example source vertex

5 Green in figure -> gray in code
DFS Example source vertex 1 | | d f Green in figure -> gray in code

6 DFS Example source vertex d f 1 | | 2 |

7 DFS Example source vertex 1 | | 3 | 2 | d f

8 DFS Example 1 | | 3 | 4 2 | source vertex d f

9 DFS Example 1 | | 5 | 3 | 4 2 | source vertex d f

10 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

11 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; What does u->d represent?

12 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; What does u->f represent?

13 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; Will all vertices eventually be colored black?

14 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; What will be the running time?

15 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; Running time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times

16 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called?

17 Depth-First Search: The Code
DFS(G) { for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time; So, running time of DFS = O(V+E)

18 Depth-First Sort Analysis
This running time argument is an informal example of amortized analysis “Charge” the exploration of edge to the edge: Each loop in DFS_Visit can be attributed to an edge in the graph Runs once/edge if directed graph, twice if undirected Thus loop will run in O(E) time, algorithm O(V+E) Considered linear for graph, b/c adj list requires O(V+E) storage Important to be comfortable with this kind of reasoning and analysis

19 DFS Example source vertex

20 DFS Example source vertex d f 1 | |

21 DFS Example source vertex d f 1 | | 2 |

22 DFS Example 1 | | 3 | 2 | source vertex d f

23 DFS Example source vertex d f 1 | | 3 | 4 2 |

24 DFS Example source vertex d f 1 | | 5 | 3 | 4 2 |

25 DFS Example source vertex d f 1 | | 5 | 6 3 | 4 2 |

26 DFS Example source vertex d f 1 | 8 | | 5 | 6 3 | 4 2 | 7

27 DFS Example source vertex 1 | 8 | | 5 | 6 3 | 4 2 | 7 d f

28 What is the structure of the green vertices? What do they represent?
DFS Example source vertex 1 | 8 | | 5 | 6 3 | 4 2 | 7 9 | d f What is the structure of the green vertices? What do they represent?

29 DFS Example source vertex 1 | 8 | | 5 | 6 3 | 4 2 | 7 9 |10 d f

30 DFS Example 1 | 8 |11 | 5 | 6 3 | 4 2 | 7 9 |10 source vertex d f

31 DFS Example source vertex 1 |12 8 |11 | 5 | 6 3 | 4 2 | 7 9 |10 d f

32 DFS Example d f 1 |12 8 |11 13| | 5 | 6 3 | 4 2 | 7 9 |10
source vertex d f 1 |12 8 |11 13| | 5 | 6 3 | 4 2 | 7 9 |10

33 DFS Example 1 |12 8 |11 13| 14| 5 | 6 3 | 4 2 | 7 9 |10 d f
source vertex 1 |12 8 |11 13| 14| 5 | 6 3 | 4 2 | 7 9 |10 d f

34 DFS Example 1 |12 8 |11 13| 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f
source vertex d f

35 DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f
source vertex d f

36 DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex The tree edges form a spanning forest Can tree edges form cycles? Why or why not?

37 DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f
source vertex d f Tree edges

38 DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Encounter a grey vertex (grey to grey)

39 DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f
source vertex d f Tree edges Back edges

40 DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Forward edge: from ancestor to descendent Not a tree edge, though From grey node to black node

41 DFS Example 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 d f
source vertex d f Tree edges Back edges Forward edges

42 DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Forward edge: from ancestor to descendent Cross edge: between a tree or subtrees From a grey node to a black node

43 DFS Example d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10
source vertex d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 Tree edges Back edges Forward edges Cross edges

44 DFS: Kinds of edges DFS introduces an important distinction among edges in the original graph: Tree edge: encounter new (white) vertex Back edge: from descendent to ancestor Forward edge: from ancestor to descendent Cross edge: between a tree or subtrees Note: tree & back edges are important; most algorithms don’t distinguish forward & cross

45 DFS: Kinds Of Edges Thm 23.9 (22.10 – in 3rd edition): If G is undirected, a DFS produces only tree and back edges Suppose you have u.d < v.d Then search discovered u before v, so first time v is discovered it is white – hence the edge (u,v) is a tree edge Otherwise the search already explored this edge in direction from v to u edge must actually be a back edge since u is still gray

46 DFS: Kinds Of Edges Thm 23.9: If G is undirected, a DFS produces only tree and back edges – cannot be a forward edge source F? source C?

47 DFS And Graph Cycles Thm: An undirected graph is acyclic iff a DFS yields no back edges If acyclic, no back edges (because a back edge implies a cycle If no back edges, acyclic No back edges implies only tree edges (Why?) Only tree edges implies we have a tree or a forest Which by definition is acyclic Thus, can run DFS to find whether a graph has a cycle

48 DFS And Cycles How would you modify the code to detect cycles? DFS(G)
{ for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

49 DFS And Cycles What will be the running time ? DFS(G) {
for each vertex u  G->V u->color = WHITE; } time = 0; if (u->color == WHITE) DFS_Visit(u); DFS_Visit(u) { u->color = GREY; time = time+1; u->d = time; for each v  u->Adj[] if (v->color == WHITE) DFS_Visit(v); } u->color = BLACK; u->f = time;

50 DFS And Cycles What will be the running time? A: O(V+E)
We can actually determine if cycles exist in O(V) time: In an undirected acyclic forest, |E|  |V| - 1 So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way

51 Review: Kinds Of Edges Thm: If G is undirected, a DFS produces only tree and back edges Thm: An undirected graph is acyclic iff a DFS yields no back edges Thus, can run DFS to find cycles

52 Review: Kinds of Edges d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7
source vertex d f 1 |12 8 |11 13|16 14|15 5 | 6 3 | 4 2 | 7 9 |10 Tree edges Back edges Forward edges Cross edges

53 DFS And Cycles Running time: O(V+E)
We can actually determine if cycles exist in O(V) time: In an undirected acyclic forest, |E|  |V| - 1 So count the edges: if ever see |V| distinct edges, must have seen a back edge along the way Why not just test if |E| <|V| and answer the question in constant time? We can have some isolated component nodes not connected by any edges to the rest of the graph

54 Directed Acyclic Graphs
A directed acyclic graph or DAG is a directed graph with no directed cycles:

55 DFS and DAGs Argue that a directed graph G is acyclic iff a DFS of G yields no back edges: Forward: if G is acyclic, will be no back edges Trivial: a back edge implies a cycle Backward: if no back edges, G is acyclic Argue contrapositive: Suppose G has a cycle  we will show that DFS will produce a back edge Let v be the vertex on the cycle first discovered, and u be the predecessor of v on the cycle When v discovered, whole cycle is white Must visit everything reachable from v before returning from DFS-Visit() So path from uv is descendant of v hence (graygray), thus (u, v) is a back edge

56 Topological Sort Topological sort of a DAG:
Linear ordering of all vertices in graph G such that vertex u comes before vertex v if edge (u, v)  G Real-world example: getting dressed

57 Getting Dressed Underwear Socks Shoes Pants Belt Shirt Watch Tie
Jacket

58 Getting Dressed Underwear Socks Shoes Pants Belt Shirt Watch Tie
Jacket Socks Underwear Pants Shoes Watch Shirt Belt Tie Jacket

59 Topological Sort Algorithm
{ Run DFS When a vertex is finished, output it On the front of linked list Vertices are output in reverse topological order } Time: O(V+E) Correctness: Want to prove that (u,v)  G  uf > vf

60 Correctness of Topological Sort
Claim: (u,v)  G  uf > vf Show that if there is an edge from u to v, finishing time of u is greater then v When (u,v) is explored, u is gray v = gray  (u,v) is back edge. Contradiction (Why?) hence v cannot be gray – since there are no cycles v = white  v becomes descendent of u  vf < uf (since must finish v before backtracking and finishing u) v = black  v already finished  vf < uf

61 Strongly Connected Components
Application of DFS – find strongly connected components of the graph Strongly connected component C of the directed graph is a maximal set of vertices u and v such that for all u and v in C u is reachable from v and v is reachable from u

62 Strongly Connected Components
13|14 11|16 1|10 2|7 3 | 4 12|15 8|9 5|6

63 Strongly Connected Components
Call DFS to compute finishing times f[u] of each vertex Create transpose graph (directions of edges reversed) Call DFS on the transpose, but in the main loop of DFS, consider vertices in the decreasing order of f[u] Output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component

64 Strongly Connected components
SSC algorithm will induce component graph which is a DAG 13|14 11|16 1|10 8|9 12|15 3 | 4 2|7 5|6

65 Minimum Spanning Tree Problem: given a connected, undirected, weighted graph: 14 10 3 6 4 5 2 9 15 8

66 Minimum Spanning Tree Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 14 10 3 6 4 5 2 9 15 8

67 Minimum Spanning Tree Which edges form the minimum spanning tree (MST) of the below graph? H B C G E D F A 14 10 3 6 4 5 2 9 15 8

68 Minimum Spanning Tree Answer: H B C G E D F A 14 10 3 6 4 5 2 9 15 8

69 Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees Let T be an MST of G with an edge (u,v) in the middle Removing (u,v) partitions T into two trees T1 and T2 Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of G2 = (V2,E2) (Do V1 and V2 share vertices? Why?) Proof: w(T) = w(u,v) + w(T1) + w(T2) (There can’t be a better tree than T1 or T2, or T would be suboptimal)

70 Minimum Spanning Tree Thm:
Let T be MST of G, and let A  T be subtree of T Let (u,v) be min-weight edge connecting A to V-A Then (u,v)  T

71 Minimum Spanning Tree Thm:
Let T be MST of G, and let A  T be subtree of T Let (u,v) be min-weight edge connecting A to V-A Then (u,v)  T Proof: in book (see Thm 23.1)

72 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

73 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q
14 10 3 6 4 5 2 9 15 8 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph

74 Prim’s Algorithm  r MST-Prim(G, w, r) Q = V[G]; for each u  Q
14 10 3 6 4 5 2 9 15 8 r MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Pick a start vertex r

75 Prim’s Algorithm  r MST-Prim(G, w, r) Q = V[G]; for each u  Q
14 10 3 6 4 5 2 9 15 8 Pick a start vertex r r MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

76 Red vertices have been removed from Q
Prim’s Algorithm 14 10 3 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Red vertices have been removed from Q

77 Red arrows indicate parent pointers
Prim’s Algorithm 3 14 10 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Red arrows indicate parent pointers

78 Prim’s Algorithm  3 u MST-Prim(G, w, r) Q = V[G]; 14 for each u  Q
10 6 4 5 2 9 15 8 u MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

79 Prim’s Algorithm  3 u MST-Prim(G, w, r) Q = V[G]; for each u  Q 10
14 6 4 5 2 9 15 8 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

80 Prim’s Algorithm  8 3 MST-Prim(G, w, r) Q = V[G]; for each u  Q 10
8 14 6 4 5 2 9 15 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

81 Prim’s Algorithm  8 3 MST-Prim(G, w, r) Q = V[G]; for each u  Q 10
8 14 6 4 5 2 9 15 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 6

82 Prim’s Algorithm  8 3 u MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 8 14 6 4 5 2 9 15 u

83 Prim’s Algorithm 3 2  8 MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 2 8 14 6 4 5 9 15

84 Prim’s Algorithm 3 2  8 MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 2 8 15 14 6 4 5 9

85 Prim’s Algorithm 3 2 9 8  MST-Prim(G, w, r) Q = V[G]; 10
8 15 14 6 4 5 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

86 Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q
key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3 10 2 9 8 15 4 14 6 5

87 Prim’s Algorithm 2 9 8 4 3 MST-Prim(G, w, r) Q = V[G]; for each u  Q
5 2 9 8 15 4 14 10 3 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 3

88 Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q
5 2 9 8 15 4 14 10 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

89 Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q
5 2 9 8 15 4 14 10 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

90 Prim’s Algorithm 3 2 9 8 4 MST-Prim(G, w, r) Q = V[G]; for each u  Q
5 2 9 8 15 4 14 10 6 MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);

91 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v));

92 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?

93 Review: Prim’s Algorithm
MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? A: Depends on queue binary heap: O(E lg V) Fibonacci heap: O(V lg V + E)


Download ppt "Many slides here are based on E. Demaine , D. Luebke slides"

Similar presentations


Ads by Google