UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2003

# UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2003

## UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2003

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. UMass Lowell Computer Science 91.404Analysis of AlgorithmsProf. Karen DanielsFall, 2003 Chapter 22: Graph Algorithms Chapter 23: Minimum Spanning Trees Chapter 24: Shortest Paths [Source: Cormen et al. textbook except where noted]

2. Overview: Graph Algorithms • Chapter 22: Elementary Graph Algorithms • Introductory Concepts • Graph Traversals: • Depth-First Search • Breadth-First Search • Topological Sort • Chapter 23: Minimum Spanning Trees • Kruskal • Prim • Chapter 24: Shortest Paths • Dijkstra

3. Chapter 22 Graph Algorithms Introductory Concepts Depth-First Search Breadth-First Search Topological Sort [Source: Cormen et al. textbook except where noted]

4. A A B B D F F E E D C C Introductory Graph Concepts • G= (V,E) • Vertex Degree • Self-Loops • Undirected Graph • No Self-Loops • Adjacency is symmetric • Directed Graph (digraph) • Degree: in/out • Self-Loops allowed This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.

5. A A B B A B C D E F A B C D E F D A B C D E F A BC B CEF C D D E BD F E A BC B ACEF C AB D E E BDF F BE A B C D E F F F E E D C C Introductory Graph Concepts:Representations • Undirected Graph • Directed Graph (digraph) Adjacency Matrix Adjacency List Adjacency List Adjacency Matrix This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.

6. A A B B F A B E E F E C D D D C C F Introductory Graph Concepts:Paths, Cycles path <A,B,F> • Path: • length: number of edges • simple: all vertices distinct • Cycle: • Directed Graph: • <v0,v1,...,vk > forms cycle if v0=vk and k>=1 • simple cycle: v1,v2..,vk also distinct • self-loop is cycle of length 1 • Undirected Graph: • <v0,v1,...,vk > forms (simple) cycle if v0=vk and k>=3 • simple cycle: v1,v2..,vk also distinct simple cycle <E,B,F,E> simple cycle <A,B,C,A>= <B,C,A,B> This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.

7. A A A A B B B D D F F F E E E D D C C C C B strongly connected component F E Introductory Graph Concepts:Connectivity connected • 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 stronglyconnected component • strongly connected components: • equivalence classes under “mutually reachable” relation 2 connected components not strongly connected This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.

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

9. Depth-First Search (DFS)

10. Example: DFS of Directed Graph G=(V,E) 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 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.

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

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

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 B G E C F

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

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

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

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 A D 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 A D T B G 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 A D T T B G 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 A D T T B G 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 G 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 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 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 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 F G C T E C F

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 T F G C T E C F

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 T F G C T E C F

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

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

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 E C F 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 E C F 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 B B T F G C T E C F 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 B B T F G C T 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 B B T F G C T T 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 B B T F G C T T E C F C C

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

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

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

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

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

41. Example: DFS of Undirected Graph 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

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 B G 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 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 G 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 G T E C F

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

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

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

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

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 T B T G T B E C F