Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First."— Presentation transcript:

1 UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First Search Topological Sort Tuesday, 5/8/01 [Source: Cormen et al. textbook except where noted]

2 Depth-First Search (DFS) & Breadth-First Search (BFS) Running Time Analysis Vertex Color Changes Edge Classification Using the Results of DFS & BFS Examples

3 Running Time Analysis ä Key ideas in the analysis are similar for DFS and BFS. In both cases we assume an Adjacency List representation. Let’s examine DFS. ä Let t be number of DFS trees generated by DFS search ä Outer loop in DFS(G) executes t times ä each execution contains call: DFS_Visit(G,u) ä each such call constructs a DFS tree by visiting (recursively) every node reachable from vertex u ä Time: ä Now, let r i be the number of vertices in DFS tree i ä Time to construct DFS tree i: continued on next slide...

4 Running Time Analysis ä Total DFS time: ä Now, consider this expression for the extreme values of t: ä if t=1, all edges are in one DFS tree and the expression simplifies to O(E) ä if t=|V|, each vertex is its own (degenerate) DFS tree with no edges so the expression simplifies to O(V) ä O(V+E) is therefore an upper bound on the time for the extreme cases ä For values of t in between 1 and |V| we have these contributions to running time: ä 1 for each vertex that is its own (degenerate) DFS tree with no edges ä upper bound on this total is O(V) ä |AdjList[u]| for each vertex u that is the root of a non-degenerate DFS tree ä upper bound on this total is O(E) ä Total time for values of t in between 1 and |V| is therefore also O(V+E) Total time= Note that for an Adjacency Matrix representation, we would need to scan an entire matrix row (containing |V| entries) each time we examined the vertices adjacent to a vertex. This would make the running time O(V 2 ) instead of O(V+E).

5 Vertex Color Changes ä Vertex is WHITE if it has not yet been encountered during the search. ä Vertex is GRAY if it has been encountered but has not yet been fully explored. ä Vertex is if it has been fully explored. ä Vertex is BLACK if it has been fully explored.

6 Edge Classification ä Each edge of the original graph G is classified during the search ä produces information needed to: ä build DFS or BFS spanning forest of trees ä detect cycles (DFS) or find shortest paths (BFS) ä When vertex u is being explored, edge e = (u,v) is classified based on the color of v when the edge is first explored: ä e is a tree edge if v is WHITE [for DFS and BFS] ä e is a back edge if v is GRAY [for DFS only] ä for DFS this means v is an ancestor of u in the DFS tree ä e is a forward edge if v is and [for DFS only] v is a descendent of u in the DFS tree ä e is a forward edge if v is BLACK and [for DFS only] v is a descendent of u in the DFS tree ä e is a cross edge if v is and [for DFS only] there is no ancestor or descendent relationship between u and v in the DFS tree ä e is a cross edge if v is BLACK and [for DFS only] there is no ancestor or descendent relationship between u and v in the DFS tree ä Note that: ä For BFS we’ll only consider tree edges. ä For DFS we consider all 4 edge types. ä In DFS of an undirected graph, every edge is either a tree edge or a back edge.

7 Using the Results of 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. see p. 486 of text for proof Note: DFS can also be used to detect cycles in undirected graphs if notion of cycle is refined appropriately. see p. 472-475 of text for proof

8 Example: DFS of Directed Graph Source: Graph is from Computer Algorithms: Introduction to Design and Analysis by Baase and Gelder. A B C D E F GG=(V,E) Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Edge Classification Legend: T: tree edge B: back edge F: forward edge C: cross edge

9 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E

10 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T

11 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T

12 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T

13 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T

14 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T

15 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T

16 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T

17 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B

18 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C

19 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C

20 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C

21 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F

22 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F T

23 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F T

24 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB

25 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

26 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

27 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

28 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C

29 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C

30 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T

31 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T

32 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C

33 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C B

34 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C B

35 Example: (continued) DFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E T T T B C F TB C C T C B

36 Example: (continued) DFS of Directed Graph A D G B T T B C T F B F C C C C E T B A B C D E F G T T T B C F TB C C T C B T DFS Tree 1 DFS Tree 2

37 Example: DFS of Undirected Graph A B C D E F GG=(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

38 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

39 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

40 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

41 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

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 T T

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 T B

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 T B 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 B 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 B T B

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 T B 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 B 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 B 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 B T 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 T B T B B T 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 B T B B T T B

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 T B

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 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 B B T T B

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 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 B T T B T

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 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 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 B

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 B

62 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

63 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

64 Example: BFS of Directed Graph Source: Graph is from Computer Algorithms: Introduction to Design and Analysis by Baase and Gelder. A B C D E F GG=(V,E) Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Edge Classification Legend: T: tree edge only tree edges are used

65 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: A

66 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: AB T

67 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: ABC T T

68 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: ABCF T T T

69 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: BCF T T T

70 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: BCFD T T T T

71 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: CFD T T T T

72 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: FD T T T T

73 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: D T T T T

74 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: - T T T T

75 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: E T T T T

76 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: EG T T T T T

77 Example: BFS of Directed Graph A B C D E F G Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: G T T T T T

78 Example: BFS of Directed Graph Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Queue: - A B C D E F G T T T T T

79 Example: (continued) BFS of Directed Graph G E T A D B T T T F C T BFS Tree 1 BFS Tree 2 A B C D E F G T T T T T Shortest path distance from : A to B = 1 A to C = 1 A to F = 1 A to D = 2 Shortest path distance from : E to G = 1

80 Example: BFS of Undirected Graph A B C D E F GG=(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 Edge Classification Legend: T: tree edge only tree edges are used

81 Example: BFS 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 Queue: A

82 Example: BFS 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 Queue: AB T

83 Example: BFS 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 Queue: ABC T T

84 Example: BFS 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 Queue: ABCD T T T

85 Example: BFS 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 Queue: ABCDF T T T T

86 Example: BFS 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 Queue: BCDF T T T T

87 Example: BFS 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 Queue: CDF T T T T

88 Example: BFS 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 Queue: CDFE T T T T T

89 Example: BFS 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 Queue: DFE T T T T T

90 Example: BFS 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 Queue: DFEG T T T T T T

91 Example: BFS 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 Queue: FEG T T T T T T

92 Example: BFS 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 Queue: EG T T T T T T

93 Example: BFS 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 Queue: G T T T T T T

94 Example: BFS 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 Queue: - A B C D E F G T TTT T T

95 Example: (continued) BFS of Undirected Graph BFS Tree Shortest path distance from : A to B = 1 A to E = 2 A to C = 1A to G = 2 A to D = 1 A to F = 1 A B C D E F G T TTT T T A F B T T T D C T E G T T

96 Topological Sort Source: Previous 91.404 instructors

97 Definition: DAG ä A Directed Acyclic Graph is often abbreviated by DAG ä As noted on slide 7, if DFS of a directed graph yields no back edges, then the graph contains no cycles [this is Lemma 23.10 in text] A B C D E F G This graph has more than one cycle. Can you find them all? A B C D E F G This graph has no cycles, so it is a DAG.

98 Definition: Topological Sort ä A topological sort of a dag G = (V, E) is a linear ordering of all its vertices such that if G contains an edge (u, v), then u appears before v in the ordering. ä If the graph is not acyclic, then no linear ordering is possible.  A topological sort of a graph can be viewed as an ordering of its vertices along a horizontal line so that all directed edges go from left to right.  Topological sorting is thus different from the usual kind of "sorting".

99 Definition: Topological Sort ä Directed acyclic graphs are used in many applications to indicate precedences among events. ä Figure 23.7 gives an example that arises when Professor Bumstead gets dressed in the morning. The professor must don certain garments before others (e.g., socks before shoes). Other items may be put on in any order (e.g., socks and pants). A directed edge (u,v) in the dag of Figure 23.7(a) indicates that garment u must be donned before garment v. A topological sort of this dag therefore gives an order for getting dressed. Figure 23.7(b) shows the topologically sorted dag as an ordering of vertices along a horizontal line such that all directed edges go from left to right.

100 Definition: Topological Sort ä The following simple algorithm topologically sorts a dag. ä TOPOLOGICAL-SORT(G) ä call DFS(G) to compute finishing times f[v] for each vertex v (this is equal to the order in which vertices change color from gray to black) ä as each vertex is finished (turns black), insert it onto the front of a linked list ä return the linked list of vertices

101 Definition: Topological Sort ä Figure 23.7(b) shows how the topologically sorted vertices appear in reverse order of their finishing times. ä We can perform a topological sort in time Q(V + E), since depth-first search takes Q(V + E) time and it takes 0(1) time to insert each of the |V| vertices onto the front of the linked list.

102 Definition: Topological Sort ä Theorem 23.11: TOPOLOGICAL-SORT(G) produces a topological sort of a directed acyclic graph G. ä Proof: Suppose that DFS is run on a given dag G = (V, E) to determine finishing times for its vertices. It suffices to show that for any pair of distinct vertices u,v Î V, if there is an edge in G from u to v, then f[v] < f[u]. Consider any edge (u,v) explored by DFS(G). When this edge is explored, v cannot be gray, since then v would be an ancestor of u and (u,v) would be a back edge, contradicting Lemma 23.10. Therefore, v must be either white or black. If v is white, it becomes a descendant of u, and so f[v] < f[u]. If v is black, then f[v] < f[u] as well. Thus, for any edge (u,v) in the dag, we have f[v] < f[u], proving the theorem.

103 Example ä For the DAG of slide 97: ä DFS produces this result: ä this contains 2 DFS trees ä Vertices are blackened in the following order: ä C, B, F, A, D, E, G A B C D E F G A B C D E F G T T T T T F C C C C

104 Example ä Vertices are added to front of a linked list in the blackening order. ä Final result is shown below ä Note that all tree edges and non-tree edges point to the right A B C DE F G T F C T T T C T C C


Download ppt "UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First."

Similar presentations


Ads by Google