Graph Algorithms: Part 1

Slides:



Advertisements
Similar presentations
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Advertisements

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Chapter 23 Minimum Spanning Trees
CS420 lecture twelve Shortest Paths wim bohm cs csu.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Chapter 23: Graph Algorithms Chapter 24: Minimum Spanning Trees.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Lecture 4 Tuesday, 2/19/02 Graph Algorithms: Part 2 Network.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lectures 3 Tuesday, 9/25/01 Graph Algorithms: Part 1 Shortest.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 4 Tuesday, 9/30/08 Graph Algorithms: Part 1 Shortest.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2006 Lecture 2 Monday, 2/6/06 Design Patterns for Optimization.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Lecture 2 Tuesday, 9/10/02 Design Patterns for Optimization.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2006 Lecture 2 Monday, 9/13/06 Design Patterns for Optimization Problems.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Wednesday, 9/26/01 Graph Basics.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2002 Monday, 12/2/02 Design Patterns for Optimization Problems Greedy.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 4 Tuesday, 10/2/01 Graph Algorithms: Part 2 Network.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2008 Lecture 2 Tuesday, 9/16/08 Design Patterns for Optimization.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2004 Lecture 5 Wednesday, 10/6/04 Graph Algorithms: Part 2.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
SPANNING TREES Lecture 21 CS2110 – Spring
Chapter 2 Graph Algorithms.
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
Lecture 13 Algorithm Analysis
Chapter 22: Elementary Graph Algorithms
Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 2 Tuesday, 2/2/10 Design Patterns for Optimization.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Chapter 22 Elementary Graph Algorithms
Lecture 10 Algorithm Analysis
Graph Algorithms: Part 1
Text Book: Introduction to algorithms By C L R S
Chapter 16 1 – Graphs Graph Categories Strong Components
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Graph Algorithms: Part 1 UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Spring, 2006 Lecture 4 Tuesday, 2/21/06 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 Undirected Graph No Self-Loops Adjacency is symmetric Directed Graph (digraph) Degree: in/out Self-Loops allowed 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.

Introductory Graph Concepts: Representations Undirected Graph Directed Graph (digraph) B E C F D A B E C F D A A B C D E F A B C D E F A B C D E F A B C D E F 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 List Adjacency Matrix This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.

Introductory Graph Concepts: Paths, Cycles 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 B E C F D A path <A,B,F> B E C F D A simple cycle <E,B,F,E> most of our cycle work will be for directed graphs simple cycle <A,B,C,A>= <B,C,A,B> B E C F D A This treatment follows 91.503 textbook Cormen et al. Some definitions differ slightly from other graph literature.

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

Elementary Graph Algorithms: SEARCHING: DFS, BFS for unweighted directed or undirected graph G=(V,E) Time: O(|V| + |E|) adj list O(|V|2) adj matrix predecessor subgraph = forest of spanning trees Breadth-First-Search (BFS): Shortest Path Distance From source to each reachable vertex Record during traversal Foundation of many “shortest path” algorithms 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 Vertex color shows status: not yet encountered encountered, but not yet finished finished See 91.404 DFS/BFS slide show See DFS, BFS Handout for PseudoCode

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. A C B E D F Tree Edge Cross Edge Back Edge B E C F D A

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

Minimum Spanning Tree: Greedy Algorithms Invariant: Minimum weight spanning forest Becomes single tree at end Time: O(|E|lg|E|) given fast FIND-SET, UNION A B C D E F G 2 1 3 4 5 6 8 7 Produces minimum weight tree of edges that includes every vertex. Time: O(|E|lg|V|) = O(|E|lg|E|) slightly faster with fast priority queue Invariant: Minimum weight tree Spans all vertices at end 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 B C D E F G 2 1 3 4 5 6 8 7

Shortest Paths Chapters 24 & 25

BFS as a Basis for Some Shortest Path Algorithms for unweighted, undirected graph G=(V,E) Time: O(|V| + |E|) adj list O(|V|2) adj matrix 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. 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. Time: O(|V|(|V| + |E|)) adj list O(|V|3) adj matrix but for weighted, directed graphs… source: based on Sedgewick, Graph Algorithms

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

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

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

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

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

Transitive Closure (Matrix) Warshall Correctness by Induction on i: Inductive Hypothesis: ith iteration of loop sets G[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 <s…t> internal indices at most i - covered by inductive hypothesis in prior iteration so G[s][t] already set to 1 an internal index exceeds i (= i+1) - G[s][i+1], G[i+1][t] set in a prior iteration so G[s][t] set to 1 in current iteration source: Sedgewick, Graph Algorithms

Shortest Path Trees Shortest Path Tree gives shortest path from root to each other vertex shortest path need not be unique .99 .51 .38 .45 .83 .1 .41 .83 .1 .45 .21 .1 .51 .38 .36 .41 .5 source: Sedgewick, Graph Algorithms

All Shortest Paths source: Sedgewick, Graph Algorithms .41 .29 .29 .45 .21 .51 .32 .38 .36 .32 .50 source: Sedgewick, Graph Algorithms

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

All Shortest Paths (Compact) Total distance of shortest path Shortest Paths .41 .29 .29 .45 .21 .32 .51 .38 .36 .32 .50 Entry s,t gives next vertex on shortest path from s to t. source: Sedgewick, Graph Algorithms

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

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 1 3 4 5 6 8 7

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

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

Single Source Shortest Paths: Dijkstra’s Algorithm Dijkstra’s algorithm solves problem efficiently for the case in which all weights are nonnegative (as in the example graph). Dijkstra’s algorithm maintains a set S of vertices whose final shortest path weights have already been determined. 1 2 3 4 6 5 10 8 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. The algorithm repeatedly selects the vertex u e 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 for (nonnegative) weighted, directed graph G=(V,E) implicit DECREASE-KEY source: 91.503 textbook Cormen et al.

Single Source Shortest Paths Dijkstra’s Algorithm for (nonnegative) weighted, directed graph G=(V,E) shortest path weight A B C D E F G 2 1 3 4 5 6 8 7 shortest path weight estimate 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 B C D E F G 2 1 3 4 5 6 8 7 Why can’t Dijkstra’s algorithm handle negative-weight edges?

Single Source Shortest Paths Dijkstra’s Algorithm for (nonnegative) weighted, directed graph G=(V,E) PFS = Priority-First Search = generalize graph search with priority queue to determine next step source: Sedgewick, Graph Algorithms

All-Pairs Shortest Paths 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 source: 91.503 textbook Cormen et al.

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

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

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