Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.

Similar presentations


Presentation on theme: "Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency."— Presentation transcript:

1 Lecture 15: Depth First Search Shang-Hua Teng

2 Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency is symmetric

3 Graph Basics The size of a graph is the number of its vertices If two vertices are connected by an edge, they are neighbors The degree of a node is the number of edges it has For directed graphs, –The in-degree of a node is the number of in-edges it has –The out-degree of a node is the number of out-edges it has

4 Paths, Cycles Path: –simple: all vertices distinct Cycle: –Directed Graph: forms cycle if v 0 =v k and k>=1 simple cycle: v 1,v 2..,v k also distinct –Undirected Graph: forms (simple) cycle if v 0 =v k and k>=3 simple cycle: v 1,v 2..,v k also distinct B E C F D A path path B E C F D A simple cycle simple cycle B E C F D A simple cycle = simple cycle =

5 Connectivity Undirected Graph: connected –every pair of vertices is connected by a path –one connected component –connected components: equivalence classes under “is reachable from” relation Directed Graph: strongly connected –every pair of vertices is reachable from each other –one strongly connected component –strongly connected components: equivalence classes under “mutually reachable” relation B E C F D A connected B E C F D A 2 connected components strongly connected component B E C F D A not strongly connected B E C F D A

6 Subgraphs A subgraph S of a graph G is a graph such that –The edges of S are a subset of the edges of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph

7 Trees and Forests A (free) tree is an undirected graph T such that –T is connected –T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest

8 Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree

9 Complete Graphs A graph which has edge between all pair of vertex pair is complete. A complete digraph has edges between any two vertices.

10 Graph Search (traversal) How do we search a graph? –At a particular vertices, where shall we go next? Two common framework: the breadth-first search (BFS) and the depth-first search (DFS) –In BFS, one explore a graph level by level away (explore all neighbors first and then move on) –In DFS, go as far as possible along a single path until reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrack

11 Depth-First Search The basic idea behind this algorithm is that it traverses the graph using recursion –Go as far as possible until you reach a deadend –Backtrack to the previous path and try the next branch –The algorithm in the book is overly complicated, we will stick with the one on the right –The graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, j dfs(g, current) { mark current as visited x = set of nodes adjacent to current for(i gets next in x*) if(i has not been visited yet) dfs(g, i) } * - we will assume that adjacent nodes will be processed in numeric or alphabetical order a b hi c g e j d f

12 Color Scheme Vertices initially colored white Then colored gray when discovered Then black when finished

13 Time Stamps Discover time d[u]: when u is first discovered Finish time f[u]: when backtrack from u d[u] < f[u]

14 DFS Example source vertex

15 DFS Example 1 | | | | | | | | source vertex d f

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

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

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

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

20 DFS Example 1 | | | |5 | 63 | 4 2 | | source vertex d f

21 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

22 DFS Example 1 |8 | | |5 | 63 | 4 2 | 7 | source vertex d f

23 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f

24 DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f

25 DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

26 DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

27 DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f

28 DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f

29 DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f

30 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

31 Pseudocode DFS(G) 1. For each v in V, 2. color[v]=white;  [u]=NIL 3. time=0; 4. For each u in V 5. If (color[u]=white) 6. DFS-VISIT(u)

32 1.DFS-VISIT(u) 2. color[u]=gray; 3. time = time + 1; d[u] = time; 4. For each v in Adj(u) do 5. If (color[v] = white) 6.  [v] = u; 7. DFS-VISIT(v); 8. color[u] = black; 9. time = time + 1; f[u]= time;

33 Complexity Analysis There is only one DFS-VISIT(u) for each vertex u. Ignoring the recursion calls the complexity is O(deg(u)+1) The recursive call on v is charged to DFS-VISIT(v) Initialization complexity is O(V) Overall complexity is O(V + E)

34 DFS Forest  [v] = u then (u,v) is an edge 2.All descendant of u are reachable from u  defines a spanning forest

35 Parenthesis Lemma Relation between timestamps and ancestry u is an ancestor of v if and only if [d[u], f[u]]  [d[v], f[v]] u is a descendent of v if and only if [d[u], f[u]]  [d[v], f[v]] u and v are not related if and only if [d[u], f[u]] and [d[v], f[v]] are disjoint

36 DFS: Tree (Forest) 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

37 DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f Tree edges

38 DFS: Back 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 Tree edgesBack edges 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

40 DFS: Forward 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 Tree edgesBack edgesForward edges 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

42 DFS: Cross 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 Tree edgesBack edgesForward edgesCross edges 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

44 DFS: Types 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: Undirected Graph Theorem: If G is undirected, a DFS produces only tree and back edges Proof by contradiction: –Assume there’s a forward edge But F? edge must actually be a back edge (why?) source F?

46 DFS: Undirected Graph Theorem: If G is undirected, a DFS produces only tree and back edges Proof by contradiction: –Assume there’s a cross edge But C? edge cannot be cross: must be explored from one of the vertices it connects, becoming a tree vertex, before other vertex is explored So in fact the picture is wrong…both lower tree edges cannot in fact be tree edges source C?

47 DFS Undirected Graph Theorem: 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 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

66 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

67 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

68 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

69 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

70 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

71 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

72 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

73 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

74 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

75 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.

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

77 Directed Acyclic Graphs (DAGs) DAG - digraph with no cycles compare: tree, DAG, digraph with cycle DE CB A DE CB A DE CB A

78 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

79 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} { }

80 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

81 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


Download ppt "Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency."

Similar presentations


Ads by Google