1.04k likes | 1.17k Views
UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001. Makeup Lecture Chapter 23: Graph Algorithms Depth-First Search Breadth-First Search Topological Sort Tuesday, 5/8/01 [Source: Cormen et al. textbook except where noted].
E N D
UMass Lowell Computer Science 91.404Analysis of AlgorithmsProf. Karen DanielsSpring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First Search Breadth-First Search Topological Sort Tuesday, 5/8/01 [Source: Cormen et al. textbook except where noted]
Depth-First Search (DFS) & Breadth-First Search (BFS) Running Time Analysis Vertex Color Changes Edge Classification Using the Results of DFS & BFS Examples
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 ri be the number of vertices in DFS tree i • Time to construct DFS tree i: continued on next slide...
Total time= 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) 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(V2) instead of O(V+E).
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 BLACK if it has been fully explored.
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 BLACK and [for DFS only] v is a descendent of u 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.
Using the Results of DFS & BFS • Using DFS to Detect Cycles: • Using BFS for Shortest Paths: A directed graph G is acyclic if and only if a Depth-First Search of G yields no back edges. 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. 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. 472-475 of text for proof
G=(V,E) A D B G E C F Example: DFS 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 Edge Classification Legend: T: tree edge B: back edge F: forward edge C: cross edge Source: Graph is from Computer Algorithms: Introduction to Design and Analysis by Baase and Gelder.
Example: (continued)DFS 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 A D B G E C F
Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Example: (continued)DFS of Directed Graph A D T B G E C F
Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Example: (continued)DFS of Directed Graph A D T B G E C F
Adjacency List: A: B,C,F B: C,D C: - D: A,C E: C,G F: A,C G: D,E Example: (continued)DFS of Directed Graph A D T B G T E C F
Example: (continued)DFS 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 A D T B G T E C F
Example: (continued)DFS 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 A D T B G T E C F
Example: (continued)DFS 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 A D T T B G T E C F
Example: (continued)DFS 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 A D T T B G T E C F
Example: (continued)DFS 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 B A D T T B G T E C F
Example: (continued)DFS 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 B A D T T B G C T E C F
Example: (continued)DFS 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 B A D T T B G C T E C F
Example: (continued)DFS 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 B A D T T B G C T E C F
Example: (continued)DFS 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 B A D T T B F G C T E C F
Example: (continued)DFS 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 B A D T T B T F G C T E C F
Example: (continued)DFS 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 B A D T T B T F G C T E C F
Example: (continued)DFS 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 B A D T T B B T F G C T E C F
Example: (continued)DFS 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 B A D T T B B T F G C T E C F C
Example: (continued)DFS 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 B A D T T B B T F G C T E C F C
Example: (continued)DFS 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 B A D T T B B T F G C T E C F C
Example: (continued)DFS 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 B A D T T B B T F G C T E C F C
Example: (continued)DFS 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 B A D T T B B T F G C T E C F C C
Example: (continued)DFS 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 B A D T T B B T F G C T T E C F C C
Example: (continued)DFS 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 B A D T T B B T F G C T T E C F C C
Example: (continued)DFS 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 B A D T T C B B T F G C T T E C F C C
Example: (continued)DFS 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 B A D T T C B B T F G C T T B E C F C C
Example: (continued)DFS 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 B A D T T C B B T F G C T T B E C F C C
Example: (continued)DFS 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 B A D T T C B B T F G C T T B E C F C C
Example: (continued)DFS of Directed Graph A T E T B A D B F T B T T C B F B B B T G F G C T T T C C T B D C E C F C C C C DFS Tree 2 DFS Tree 1
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 A D B G E C F Example: DFS of Undirected Graph
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 D B G E C F
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 D T B G E C F
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 D T B G E C F
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 D T B G T E C F
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 D T B G T E C F
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 D T B G T B E C F
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 D T B T G T B E C F
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 D T B T G T B E C F
Example: DFS of Undirected Graph B 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 D T B T G T B E C F
Example: DFS of Undirected Graph B 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 D B T B T G T B E C F
Example: DFS of Undirected Graph B 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 D B T T B T G T B E C F
Example: DFS of Undirected Graph B 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 D B T T B T G T B E C F
Example: DFS of Undirected Graph B 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 D B T T B T G T B T E C F