Unformatted text preview: UMass Lowell Computer Science 91.404 Analysis of Algorithms
Prof. Karen Daniels
Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees Chapter 25: Shortest Paths
[Source: Cormen et al. textbook except where noted] Overview: Graph Algorithms Chapter 23: Elementary Graph Algorithms
Introductory Concepts Graph Traversals: DepthFirst Search BreadthFirst Search Topological Sort Chapter 24: Minimum Spanning Trees
Kruskal Prim Chapter 25: Shortest Paths Dijkstra Chapter 23 Graph Algorithms
Introductory Concepts DepthFirst Search BreadthFirst Search Topological Sort
[Source: Cormen et al. textbook except where noted] Introductory Graph Concepts G= (V,E) Vertex Degree SelfLoops Undirected Graph No SelfLoops Adjacency is symmetric
A C D E F B Directed Graph (digraph) Degree: in/out SelfLoops allowed
A C D E F B This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature. Introductory Graph Concepts: Representations Undirected Graph
A C D E F B Directed Graph (digraph)
A C D E F B A B C D E F ABCDEF
0 1 1 0 0 0 1 0 1 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0 A B C D E F BC ACEF AB E BDF BE A B C D E F ABCDEF
0 1 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 1 0 A B C D E F BC CEF D BD E Adjacency Adjacency List This treatment follows 91.503 textbook Cormen et al. Matrix Adjacency List Adjacency Some definitions differ slightly from other graph literature. Matrix Introductory Graph Concepts: Paths, Cycles Path:
A C D E B Cycle: length: number of edges simple: all vertices distinct path <A,B,F> F A B simple cycle Directed Graph: <E,B,F,E> C <v ,v ,...,v > forms cycle if v =v 0 1 k 0 k and k>=1 D F simple cycle: v ,v ..,v also distinct E 1 2 k selfloop is cycle of length 1 simple cycle <A,B,C,A>= <B,C,A,B> Undirected Graph: A B <v ,v ,...,v > forms (simple) cycle 0 1 k C if v =v and k>=3 simple cycle: v1,v2..,vk also distinct 0 k D E F This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature. Introductory Graph Concepts: Connectivity A B
connected C Undirected Graph: connected D E B F every pair of vertices is connected by a path A one connected component connected components: C D A E C 2 connected components equivalence classes under "is reachable from" relation F B F E B strongly F Directed Graph: strongly connected every pair of vertices is reachable from each other one strongly connected component A strongly connected components: not strongly connected D equivalence classes under "mutually reachable" relation C D E connected component This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature. DepthFirst Search (DFS) & BreadthFirst Search (BFS)
Examples Vertex Color Changes Edge Classification Using the Results of DFS & BFS Running Time Analysis DepthFirst Search (DFS) Example: DFS of Directed Graph
G=(V,E) A B D G E Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E F C 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. 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 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. Note that: 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 B D G E 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 A T D B E G 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 A T D B E G 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 A T D B
T G E 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 A T D B
T G E 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 A T D B
T G E 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 A T D
T B
T G E 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 A T D
T B
T G E 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 A B T T D G E B
T 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 A B T T D
C B
T G E 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 A B T T D
C B
T G E 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 A B T T D
C B
T G E 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 A
F B T T D
C B
T G E 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 A
T F B T T D
C B
T G E 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 A
T F B T T D
C B
T G E 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 A
B T F B T T D
C B
T G E 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 A
B T F B T T D
C B
T G E 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 A
B T F B T T D
C B
T G E 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 A
B T F B T T D
C B
T G E 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 A
B T F B T T D
C B
T G E 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 A
B T F B T T D
C B
T G E F C 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 A
B T F B T T D
C T B
T G E F C 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 A
B T F B T T D
C T B
T G E F C 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 A
B T F B T T D
C C T B
T G E F C 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 A
B T F B T T D
C C T B
T G E
B F C 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 A
B T F B T T D
C C T B
T G E
B F C 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 A
B T F B T T D
C C T B
T G E
B F C C C Example: (continued) DFS of Directed Graph
A A
B T F B T T T D
C C T T B E
B T B F B
T B
T C T C F G
B G D
C C F C C C E C DFS Tree 1 DFS Tree 2 Example: DFS of Undirected Graph
G=(V,E) A 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 D G E F C 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 D G E F C 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 T D B G E F C 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 T D B G E F C 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 T D B
T G E F C 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 T D B
T G E F C 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 T D B
T G E F C 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 T D B
T T G E F C 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 T D B
T T G E F C Example: DFS of Undirected Graph
B T A
B D
T B
T G E F C 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 Example: DFS of Undirected Graph
B T B A
B D G E B
T T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T G E F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T G E F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B D T B
T T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B B D T B
T T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B B D T B
T T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B B D T B
T T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B B D T B
T T T B G
T F C 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 Example: DFS of Undirected Graph
B T B A
B B D T B
T T T B G
T F C 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 Example: (continued) DFS of Undirected Graph
A
B B T B T T B B B C A
B B D T B B
T T T B T T G
T D
B F C E T T F G E DFS Tree Elementary Graph Algorithms: DFS Review problem: TRUE or FALSE?
A Tree Edge Back Edge C Tree Edge The tree shown below on the right can be a DFS tree for some adjacency list representation of the graph shown below on the left. A B F E B Tree Edge Tree Edge F E Tree Edge Cross Edge D C D BreadthFirst Search (BFS) BFS PseudoCode See handout on web Example: BFS of Directed Graph
G=(V,E) A B D G E Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E F C Edge Classification Legend: T: tree edge only tree edges are used Source: Graph is from Computer Algorithms: Introduction to Design and Analysis by Baase and Gelder. Example: BFS of Directed Graph
A B D G E Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E F C Queue: A Example: BFS of Directed Graph
A D B G E Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E T F C Queue: AB Example: BFS of Directed Graph
A
T T D B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: ABC Example: BFS of Directed Graph
A
T T T D B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: ABCF Example: BFS of Directed Graph
A
T T T D B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: BCF Example: BFS of Directed Graph
A
T T T D
T B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: BCFD Example: BFS of Directed Graph
A
T T T D
T B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: CFD Example: BFS of Directed Graph
A
T T T D
T B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: FD Example: BFS of Directed Graph
A
T T T D
T B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: D Example: BFS of Directed Graph
A
T T T D
T B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue:  Example: BFS of Directed Graph
A
T T T D
T B G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: E Example: BFS of Directed Graph
A
T T T D
T B
T G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: EG Example: BFS of Directed Graph
A
T T T D
T B
T G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue: G Example: BFS of Directed Graph
A
T T T D
T B
T G E F C Adjacency List: A: B,C,F B: C,D C: D: A,C E: C,G F: A,C G: D,E Queue:  Example: (continued) BFS of Directed Graph
A A
T T T E
T T D
T T T B
T B G E D
T C F G
Shortest path distance from : E to G = 1 F C Shortest path distance from : A to B = 1 A to C = 1 A to F = 1 A to D = 2 BFS Tree 1 BFS Tree 2 Example: BFS of Undirected Graph
G=(V,E) A 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 D G E F C Edge Classification Legend: T: tree edge only tree edges are used 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 A B D G E F C Queue: A 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 A T D B G E F C Queue: AB 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 A
T T D B G E F C Queue: ABC Example: BFS of Undirected Graph
T T T A D B G E F C 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 Example: BFS of Undirected Graph
T T T A
T D B G E F C 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 Example: BFS of Undirected Graph
T T T A
T D B G E F C 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 Example: BFS of Undirected Graph
T T T A
T D B G E F C 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 Example: BFS of Undirected Graph
T T T A
T D B G
T F C 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 Queue: CDFE Example: BFS of Undirected Graph
T T T A
T D B G
T F C 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 Queue: DFE Example: BFS of Undirected Graph
T T T A
T D B T G
T F C 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 Queue: DFEG Example: BFS of Undirected Graph
T T T A
T D B T G
T F C 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 Queue: FEG Example: BFS of Undirected Graph
T T T A
T D B T G
T F C 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 Queue: EG Example: BFS of Undirected Graph
T T T A
T D B T G
T F C 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 Queue: G Example: BFS of Undirected Graph
T T T A
T D B T G
T F C 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 Queue:  Example: (continued) BFS of Undirected Graph
A A
T T T T T T T T D B T B C
T D
T F G
T E F C E G Shortest path distance from : A to B = 1 A to E = 2 A to C = 1 A to G = 2 A to D = 1 A to F = 1 BFS Tree DepthFirst Search (DFS) & BreadthFirst Search (BFS) Using the Results of DFS & BFS Running Time Analysis Using the Results of DFS & BFS Using DFS to Detect Cycles: A directed graph G is acyclic if and only if a DepthFirst 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 defined appropriately. Using BFS for Shortest Paths: A BreadthFirst Search of G yields shortest path information: For each BreadthFirst Search tree, the path from its root u to a vertex v yields the shortest path from u to v in G.
see p. 472475 of text for proof Elementary Graph Algorithms: SEARCHING: DFS, BFS SEARCHING
for unweighted directed or undirected graph G=(V,E)
Time: O(V + E) adj list O(V2) adj matrix predecessor subgraph = forest of spanning trees BreadthFirstSearch (BFS): DepthFirstSearch (DFS): Shortest Path Distance Encountering, finishing times From source to each reachable vertex Record during traversal "wellformed" nested (( )( ) ) structure Foundation of many "shortest path" algorithms Vertex color shows status:
not yet encountered encountered, but not yet finished finished DFS of undirected graph produces only back edges or tree edges Directed graph is acyclic if and only if DFS yields no back edges See DFS, BFS Handout for PseudoCode 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 t Time: time to construct DFS tree i i =1 Now, let ri be the number of vertices in DFS tree i Time to construct DFS tree i: AdjList[ jth vertex in DFS tree i]
j =1 ri continued on next slide... Running Time Analysis Total DFS time: i =1 j =1 Now, consider this expression for the extreme values of t:
t ri AdjList[ jth vertex in DFS tree i ] 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 nondegenerate 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= 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). Topological Sort Source: Previous 91.404 instructors Definition: DAG
A Directed Acyclic Graph often abbreviated DAG DAGs used in many applications to indicate precedences among events. If DFS of a directed graph yields no back edges, then the graph contains no cycles [Lemma 23.10 in text] A B D G E A B D G E F C F C This graph has more than one cycle. Can you find them all? This graph has no cycles, so it is a DAG. 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" . Elementary Graph Algorithms: Topological Sort
for Directed, Acyclic Graph (DAG) G=(V,E) TOPOLOGICALSORT(G) 1 DFS(G) computes "finishing times" for each vertex 2 as each vertex is finished, insert it onto front of list 3 return list
See also 91.404 DFS/BFS slide show Produces linear ordering of vertices. For edge (u,v), u is ordered before v. source: 91.503 textbook Cormen et al. Topological Sort The following algorithm topologically sorts a DAG: TOPOLOGICALSORT(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 Example
A D B G E
C For this DAG: F C
T DFS produces this result: this contains 2 DFS trees A
T D
C T Vertices are blackened in the following order: B
F T G
T C, B, F, A, D, E , G F C C C E 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 nontree edges point to the right
T C C T G T E
C D C A T F F T B C Topological Sort We can perform a topological sort in time (V + E), since depthfirst search takes (V + E) time and it takes 0(1) time to insert each of the V vertices onto the front of the linked list. Topological Sort Theorem 23.11: TOPOLOGICALSORT(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. Chapter 24 Minimum Spanning Trees
Kruskal Prim
[Source: Cormen et al. textbook except where noted] Minimum Spanning Tree: Greedy Algorithms
Time: O(E lgE) given fast FINDSET, UNION Invariant: Minimum weight spanning forest Produces minimum weight tree of edges that includes every vertex. A
Becomes single tree at end 3 1 Time: O(E lgV) = O( ElgE) slightly faster with fast priority queue G 5 6 2 4 6 E 8 2 7 F 4 B 1 Invariant: Minimum weight tree Spans all vertices at end D C for Undirected, Connected, Weighted Graph G=(V,E)
source: 91.503 textbook Cormen et al. Minimum Spanning Trees Review problem: For the undirected, weighted graph below, show 2 different Minimum Spanning Trees. Draw each using one of the 2 graph copies below. Thicken an edge to make it part of a spanning tree. What is the sum of the edge weights for each of your Minimum Spanning Trees? A 3 1 G 5 6 D 2 4 6 E 8 2 7 F 4 B 1 C Chapter 25 Shortest Paths
Dijkstra
[Source: Cormen et al. textbook except where noted] BFS as a Basis for Shortest Path Algorithms
for unweighted, undirected graph G=(V,E) Problem: Given 2 vertices u, v, find the shortest path in G from u to v. Problem: Given a vertex u, find the shortest path in G from u to each vertex. Problem: Find the shortest path in G from each vertex u to each vertex v. BFS Time: O(V + E) adj list O(V2) adj matrix Source/Sink Shortest Path SingleSource Shortest Paths AllPairs Shortest Paths Solution: BFS starting at u. Stop at v. Solution: BFS starting at u. Full BFS tree. Solution: For each u: BFS starting at u; full BFS tree.
Time: O(V(V + E)) adj list O(V3) adj matrix source: based on Sedgewick, Graph Algorithms Shortest Path Applications
for weighted, directed graph G=(V,E) Road maps Airline routes Telecommunications network routing VLSI design routing
source: based on Sedgewick, Graph Algorithms Weight ~ Cost ~ Distance Shortest Path Trees Shortest Path Tree gives shortest path from root to each other vertex source: Sedgewick, Graph Algorithms Shortest Path Principles: Relaxation "Relax" a constraint to try to improve solution "Rubber band" analogy [Sedgewick] Relaxation of an Edge (u,v): test if shortest path to v [found so far] can be improved by going through u
1 A 3 G 5 6 D 2 4 6 E 8 2 7 B 1 F 4 C Single Source Shortest Paths Dijkstra's Algorithm
for (nonnegative) weighted, directed graph G=(V,E) source: 91.503 textbook Cormen et al. Single Source Shortest Paths Dijkstra's Algorithm
for (nonnegative) weighted, directed graph G=(V,E) See separate ShortestPath 91.404 slide show A 3 1 G 5 6 D 2 4 6 E 8 2 7 B 1 F 4 C source: 91.503 textbook Cormen et al. Single Source Shortest Paths Dijkstra's Algorithm Review problem: For the directed, weighted graph below, find the shortest path that begins at vertex A and ends at vertex F. List the vertices in the order that they appear on that path. What is the sum of the edge weights of that path? A 3 1 G 5 6 D 2 4 6 E 8 2 7 1 F 4 B C Why can't Dijkstra's algorithm handle negativeweight edges? Single Source Shortest Paths Dijkstra's Algorithm
for (nonnegative) weighted, directed graph G=(V,E) source: Sedgewick, Graph Algorithms Shortest Path Algorithms source: Sedgewick, Graph Algorithms ...
View
Full Document
 Spring '11
 Staff
 Algorithms, Graph Theory, Adjacency list

Click to edit the document details