Download presentation
Presentation is loading. Please wait.
Published bySibyl Richard Modified over 9 years ago
1
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015
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 Mudasser Naseer 2 12/16/2015
3
Depth-First Search ● Vertices initially colored white ● Then colored gray when discovered ● Then black when finished Mudasser Naseer 3 12/16/2015
4
Depth-First Search: The Code Mudasser Naseer 4 12/16/2015 DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }
5
Depth-First Search: The Code What does u->d represent? Mudasser Naseer 5 12/16/2015 DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }
6
Depth-First Search: The Code What does u->f represent? Mudasser Naseer 6 12/16/2015 DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }
7
Depth-First Search: The Code Will all vertices eventually be colored black? Mudasser Naseer 7 12/16/2015 DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }
8
Depth-First Search: The Code What will be the running time? Mudasser Naseer 8 12/16/2015 DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }
9
Depth-First Search: The Code Running time: O(n 2 ) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times Mudasser Naseer 9 12/16/2015 DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; }
10
Depth-First Search: The Code DFS(G) { 1 for each vertex u G->V { 2 u->color = WHITE; } 3 time = 0; 4 for each vertex u G->V { 5 if (u->color == WHITE) 6 DFS_Visit(u); } DFS_Visit(u) { 1 u->color = GREY; 2 time = time+1; 3 u->d = time; 4 for each v u->Adj[] { 5 if (v->color == WHITE) 6 DFS_Visit(v); } 7 u->color = BLACK; 8 time = time+1; 9 u->f = time; } BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called? Mudasser Naseer 10 12/16/2015
11
Depth-First Search: The Code DFS(G) { for each vertex u G->V { u->color = WHITE; } time = 0; for each vertex u G->V { 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; time = time+1; u->f = time; } So, running time of DFS = O(V+E) Mudasser Naseer 11 12/16/2015
12
● What is the running time of DFS? The loops on lines 1–2 and lines 4–6 of DFS take time (V), exclusive of the time to execute the calls to DFS-VISIT. ● The procedure DFSVISIT is called exactly once for each vertex v ∈ V, since DFS-VISIT is invoked only on white vertices and the first thing it does is paint the vertex gray. During an execution of DFS-VISIT(v), the loop on lines 4–6 is executed |Adj[v]| times. Since the total cost of executing lines 4–6 of DFS-VISIT is (E). The running time of DFS is therefore (V + E). Mudasser Naseer 12 12/16/2015
13
DFS Example source vertex Mudasser Naseer 13 12/16/2015
14
DFS Example 1 | | | | | | | | source vertex d f Mudasser Naseer 14 12/16/2015
15
DFS Example 1 | | | | | | 2 | | source vertex d f Mudasser Naseer 15 12/16/2015
16
DFS Example 1 | | | | |3 | 2 | | source vertex d f Mudasser Naseer 16 12/16/2015
17
DFS Example 1 | | | | |3 | 4 2 | | source vertex d f Mudasser Naseer 17 12/16/2015
18
DFS Example 1 | | | |5 |3 | 4 2 | | source vertex d f Mudasser Naseer 18 12/16/2015
19
DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f Mudasser Naseer 19 12/16/2015
20
DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f Mudasser Naseer 20 12/16/2015
21
DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f Mudasser Naseer 21 12/16/2015
22
DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the grey vertices? What do they represent? Mudasser Naseer 22 12/16/2015
23
DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 23 12/16/2015
24
DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 24 12/16/2015
25
DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 25 12/16/2015
26
DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 26 12/16/2015
27
DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 27 12/16/2015
28
DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 28 12/16/2015
29
DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Mudasser Naseer 29 12/16/2015
30
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? Mudasser Naseer 30 12/16/2015
31
DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edges Mudasser Naseer 31 12/16/2015
32
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) Mudasser Naseer 32 12/16/2015
33
DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edges Mudasser Naseer 33 12/16/2015
34
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 Mudasser Naseer 34 12/16/2015
35
DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edges Mudasser Naseer 35 12/16/2015
36
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 Mudasser Naseer 36 12/16/2015
37
DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edgesBack edgesForward edgesCross edges Mudasser Naseer 37 12/16/2015
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 ■ 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 Mudasser Naseer 38 12/16/2015
39
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 Mudasser Naseer 39 12/16/2015
40
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 Mudasser Naseer 40 12/16/2015
41
Example: DFS of Undirected Graph A B C D E F G G=(V,E) Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E
42
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E
43
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T
44
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T
45
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T
46
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T
47
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B
48
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T
49
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T
50
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B
51
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B
52
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T
53
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T
54
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T
55
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T
56
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B
57
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B
58
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B
59
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B
60
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T
61
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T
62
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B
63
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B
64
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B
65
Example: DFS of Undirected Graph A B C D E F G Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E T T B T B B T T B T B
66
Example: DFS of Undirected Graph Adjacency List: A: B,C,D,F B: A,C,D C: A,B,D,E,F D: A,B,C,G E: C,G F: A,C G: D,E A B C D E F G T T B TBB T T B T B
67
Example: (continued) DFS of Undirected Graph DFS Tree A B C D E F G T T B TBB T T B T B A C D E F G B B T T T T T T B B B B
68
Using DFS & BFS A directed graph G is acyclic if and only if a Depth- First Search of G yields no back edges. Using DFS to Detect Cycles: Using BFS for Shortest Paths: A Breadth-First Search of G yields shortest path information: For each Breadth-First Search tree, the path from its root u to a vertex v yields the shortest path from u to v in G.
69
Directed Acyclic Graphs A directed acyclic graph or DAG is a directed graph with no directed cycles:
70
Directed Acyclic Graphs (DAGs) DAG - digraph with no cycles compare: tree, DAG, digraph with cycle DE CB A DE CB A DE CB A
71
Where DAGs are used Syntactic structure of arithmetic expressions with common sub-expressions e.g.((a+b)*c + ((a+b)+e)*(e+f)) * ((a+b)*c) * + * * ++ + ab c ef
72
Where DAGs are used? To represent partial orders A partial order R on a set S is a binary relation such that –for all a in Sa R a is false (irreflexive) –for all a, b, c in Sif a R b and b R c then a R c (transitive) examples: “less than” (<) and proper containment on sets S ={1, 2, 3} P(S) - power set of S (set of all subsets) {1, 2, 3} {1, 2}{1, 3}{2, 3} {1}{2}{3} { }
73
DAGs in use To model course prerequisites or dependent tasks Year 1Year 2Year 3Year 4 Compiler construction Prog. Languages DS&APUMA Data & Prog. Discrete Math Data Comm 1 Data Comm 2 Op Systems Real time systems Distributed Systems
74
DFS and DAGs Theorem: a directed graph G is acyclic iff a DFS of G yields no back edges: –=> if G is acyclic, will be no back edges Trivial: a back edge implies a cycle –<= if no back edges, G is acyclic Proof by contradiction: G has a cycle 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 grey grey, thus (u, v) is a back edge
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.