1 / 104

UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

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

peta
Télécharger la présentation

UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Spring, 2001

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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]

  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 ri be the number of vertices in DFS tree i • Time to construct DFS tree i: continued on next slide...

  4. 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).

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related