 # UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest.

## Presentation on theme: "UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest."— Presentation transcript:

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest Paths Chapters 24-25

91.404 Graph Review Elementary Graph Algorithms Minimum Spanning Trees Single-Source Shortest Paths

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 = most of our cycle work will be for directed graphs

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.

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 ä Every edge of undirected G is either a tree edge or a back edge ä EdgeColor of vertex when first tested determines edge type 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 See 91.404 DFS/BFS slide show

Elementary Graph Algorithms: DFS, BFS ä 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

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

Minimum Spanning Tree: Greedy Algorithms source: 91.503 textbook Cormen et al. for Undirected, Connected, Weighted Graph G=(V,E) A B C D E F G 2 2 1 1 3 4 4 5 6 6 8 7 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

Shortest Paths Chapters 24 & 25

BFS as a Basis for Some 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. but for weighted, directed graphs…

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.99.51.5.36.38.45.83.21.1.41.1.51.51.38.45.45.45.83.83.83.1.1.1.41.41.1.1.1 shortest path need not be unique

Shortest Path Trees source: Sedgewick, Graph Algorithms predecessor vertex in tree Shortest Path Tree is a spanning tree. 3 as root for reverse graph.41.51.50.36.38.45.21.32.29.29.32.41.29.21.32.36 st = Spanning Tree reverse edges have same weight as forward ones prelude to compact representation, except that uses next vertex, not predecessor

Shortest Path Principles: Opotimal Substructure ä Lemma: Any subpath of a shortest path is a shortest path. ä Proof: Cut-and-paste argument. source: 91.503 textbook Cormen et al. u x y v p ux p xy p yv u x y v p ux p’ xy p yv

Shortest Path Principles: Relaxation ä “Relax” a constraint to try to improve solution ä 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 source: 91.503 textbook Cormen et al.

Shortest Path Principles (cont.) 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 weighted, directed graph G=(V,E) with no negative-weight cycles shortest path weight shortest path weight estimate

Single-Source Shortest Paths Chapter 24

Single Source Shortest Paths Bellman-Ford source: 91.503 textbook Cormen et al. for (negative) weighted, directed graph G=(V,E) with no negative-weight cycles Time is in O(|V||E|) detect negative- weight cycle weights source why this upper bound? update d(v) if d(u)+w(u,v) < d(v)

Bellman-Ford source: 91.503 textbook Cormen et al. for (negative) weighted, directed graph G=(V,E) with no negative-weight cycles

Single Source Shortest Paths: Dijkstra’s Algorithm 1 2 3 4 6 5 10 1 5 4 3 31 2 6 1 1 8 Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. It also maintains, for each vertex v not in S, an upper bound d[v] on the weight of a shortest path from source s to v. Dijkstra’s algorithm solves problem efficiently for the case in which all weights are nonnegative (as in the example graph). The algorithm repeatedly selects the vertex u  V – S with minimum bound d[u], inserts u into S, and relaxes all edges leaving u (determines if passing through u makes it “faster” to get to a vertex adjacent to u).

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

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 sources: Sedgewick, Graph Algorithms & 91.503 textbook Cormen et al. for (nonnegative) weighted, directed graph G=(V,E) PFS = Priority-First Search = generalize graph search with priority queue to determine next step Fibonacci Heap (Ch. 20) O(VlgV + E) amortized analysis

All-Pairs Shortest Paths Chapter 25

Transitive Closure (Matrix): Unweighted, Directed Graph source: Sedgewick, Graph Algorithms Transitive Closure Graph contains edge (u,v) if there exists a directed path in G from u to v. G Transitive Closure concepts will be useful for All-Pairs Shortest Path calculation in directed, weighted graphs “self-loops” added for algorithmic purposes

Transitive Closure (Matrix) source: Sedgewick, Graph Algorithms Boolean Matrix Product: and, or replace *,+ G G2G2  22 “self-loops” added for algorithmic purposes

Transitive Closure (Matrix) source: Sedgewick, Graph Algorithms 22  33 44 Algorithm 1: Find ,  ,  ,...,   V-1  Algorithm 2: Find ,  ,  ,...,   V  Algorithm 3: [Warshall] for i 0 to |V|-1 for s 0 to |V|-1 for s 0 to |V|-1 for t 0 to |V|-1 for t 0 to |V|-1 if  [s][i] and  [i][t] then  [s][t] 1 Time: O(|V| 3 ) Time: O(|V| 4 ) Time: O(|V| 3 lg|V|) why this upper limit?

Transitive Closure (Matrix) source: Sedgewick, Graph Algorithms Warshall good for dense graphs

Transitive Closure (Matrix) Warshall Correctness by Induction on i: Inductive Hypothesis: ith iteration of loop sets  [s][t] to 1 iff there’s a directed path from s to t with (internal) indices at most i. Inductive Step for i+1 (sketch): 2 cases for path - -internal indices at most i - covered by inductive hypothesis in prior iteration so  [s][t] already set to 1 - -an internal index exceeds i (= i+1) -  [s][i+1],  [i+1][t] set in a prior iteration so  [s][t] set to 1 in current iteration source: Sedgewick, Graph Algorithms

All Shortest Paths source: Sedgewick, Graph Algorithms.41.51.50.36.38.45.29.21.32.32.29

All Shortest Paths (Compact) source: Sedgewick, Graph Algorithms Entry s,t gives next vertex on shortest path from s to t. Total distance of shortest path Shortest Paths.41.51.50.36.38.45.32.29.29.32.21

All Shortest Paths In a Network source: Sedgewick, Graph Algorithms Shortest Path Trees for reverse graph

All-Pairs Shortest Paths source: 91.503 textbook Cormen et al. for (negative) weighted, directed graph G=(V,E) with no negative-weight cycles similar to Transitive Closure Algorithm 1 Time: O(|V| 4 ) Note: D here is replaced by L in new edition

All-Pairs Shortest Paths source: 91.503 textbook Cormen et al. similar to Transitive Closure Algorithm 2 Time: O(|V| 3 lg|V|) Note: D here is replaced by L in new edition

All-Pairs Shortest Paths source: 91.503 textbook Cormen et al. How can output be used to detect a negative-weight cycle? Can have negative- weight edges similar to Transitive Closure Algorithm 3 [Warshall] Time: O(|V| 3 ) Note: D here is replaced by L in new edition

Food for thought… ä What does the following matrix (the nxn form of it) used in shortest-path algorithms correspond to in regular matrix multiplication? Note: D here is replaced by L in new edition

Shortest Path Algorithms source: Sedgewick, Graph Algorithms

Download ppt "UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest."

Similar presentations