# UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees.

## Presentation on theme: "UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees."— Presentation transcript:

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: ä Depth-First Search ä Breadth-First Search ä Topological Sort ä Chapter 24: Minimum Spanning Trees ä Kruskal ä Prim ä Chapter 25: Shortest Paths ä Dijkstra

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

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

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

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

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

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

Depth-First Search (DFS)

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

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.

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Elementary Graph Algorithms: DFS ä Review problem: TRUE or FALSE? ä 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. B E C F D A A C B E D F Tree Edge Cross Edge Back Edge

BFS PseudoCode ä See handout on web

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Depth-First Search (DFS) & Breadth-First Search (BFS) Using the Results of DFS & BFS Running Time Analysis

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 defined appropriately. see p. 472-475 of text for proof

SEARCHING Elementary Graph Algorithms: SEARCHING: DFS, BFS ä Breadth-First-Search (BFS): ä Shortest Path Distance ä From source to each reachable vertex ä Record during traversal ä Foundation of many “shortest path” algorithms See DFS, BFS Handout for PseudoCode ä Depth-First-Search (DFS): ä Encountering, finishing times ä “well-formed” nested (( )( ) ) structure ä DFS of undirected graph produces only back edges or tree edges ä Directed graph is acyclic if and only if DFS yields no back edges for unweighted directed or undirected graph G=(V,E) Time: O(|V| + |E|) adj listO(|V| 2 ) adj matrix predecessor subgraph = forest of spanning trees Vertex color shows status: not yet encountered encountered, but not yet finished finished

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

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

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

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 source: 91.503 textbook Cormen et al. TOPOLOGICAL-SORT(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 for Directed, Acyclic Graph (DAG) G=(V,E) Produces linear ordering of vertices. For edge (u,v), u is ordered before v. See also 91.404 DFS/BFS slide show

Topological Sort ä The following 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

Example ä For this DAG: ä 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

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

Topological Sort  We can perform a topological sort in time  (V + E), since depth-first 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: 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.

Chapter 24 Minimum Spanning Trees KruskalPrim [Source: Cormen et al. textbook except where noted]

Minimum Spanning Tree: Greedy Algorithms A B C D E F G 2 2 1 1 3 4 4 5 6 6 8 7 source: 91.503 textbook Cormen et al. for Undirected, Connected, Weighted Graph G=(V,E) Produces minimum weight tree of edges that includes every vertex. Invariant: Minimum weight spanning forest Becomes single tree at end Invariant: Minimum weight tree Spans all vertices at end Time: O(|E|lg|E|) given fast FIND-SET, UNION Time: O(|E|lg|V|) = O(|E|lg|E|) slightly faster with fast priority queue

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 B C D E F G 2 2 1 1 3 4 4 5 6 6 8 7

Chapter 25 Shortest Paths Dijkstra [Source: Cormen et al. textbook except where noted]

BFS as a Basis for Shortest Path Algorithms Source/Sink Shortest Path Problem: Given 2 vertices u, v, find the shortest path in G from u to v. Solution: BFS starting at u. Stop at v. Single-Source Shortest Paths Problem: Given a vertex u, find the shortest path in G from u to each vertex. Solution: BFS starting at u. Full BFS tree. source: based on Sedgewick, Graph Algorithms BFS for unweighted, undirected graph G=(V,E) Time: O(|V| + |E|) adj list O(|V| 2 ) adj matrix O(|V| 2 ) adj matrix Time: O(|V|(|V| + |E|)) adj list O(|V| 3 ) adj matrix O(|V| 3 ) adj matrix All-Pairs Shortest Paths Problem: Find the shortest path in G from each vertex u to each vertex v. Solution: For each u: BFS starting at u; full BFS tree.

Shortest Path Applications ä Road maps ä Airline routes ä Telecommunications network routing ä VLSI design routing Weight ~ Cost ~ Distance source: based on Sedgewick, Graph Algorithms for weighted, directed graph G=(V,E)

Shortest Path Trees source: Sedgewick, Graph Algorithms Shortest Path Tree gives shortest path from root to each other vertex

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 A B C D E F G 2 2 1 1 3 4 4 5 6 6 8 7

Single Source Shortest Paths Dijkstra’s Algorithm source: 91.503 textbook Cormen et al. for (nonnegative) weighted, directed graph G=(V,E)

Single Source Shortest Paths Dijkstra’s Algorithm ä See separate ShortestPath 91.404 slide show A B C D E F G 2 2 1 1 3 4 4 5 6 6 8 7 source: 91.503 textbook Cormen et al. for (nonnegative) weighted, directed graph G=(V,E)

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 B C D E F G 2 2 1 1 3 4 4 5 6 6 8 7 Why can’t Dijkstra’s algorithm handle negative-weight edges?

Single Source Shortest Paths Dijkstra’s Algorithm source: Sedgewick, Graph Algorithms for (nonnegative) weighted, directed graph G=(V,E)

Shortest Path Algorithms source: Sedgewick, Graph Algorithms

Download ppt "UMass Lowell Computer Science 91.404 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees."

Similar presentations