CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Introduction to Algorithms 6.046J/18.401J/SMA5503
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
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.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
CPSC 311, Fall CPSC 311 Analysis of Algorithms More Graph Algorithms Prof. Jennifer Welch Fall 2009.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Topological Sorting and Least-cost Path Algorithms.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
David Luebke 1 9/13/2015 CS 332: Algorithms S-S Shortest Path: Dijkstra’s Algorithm Disjoint-Set Union Amortized Analysis.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
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
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.
CSCE 411 Design and Analysis of Algorithms Background Review Prof. Jennifer Welch CSCE 411, Background Review 1.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
Lecture 13 Algorithm Analysis
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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])
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
CSCE 411 Design and Analysis of Algorithms
Introduction to Algorithms
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Tree Shortest Paths
Lecture 12 Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
CSCE 411 Design and Analysis of Algorithms
Lecture 12 Algorithm Analysis
Lecture 14 Minimum Spanning Tree (cont’d)
Presentation transcript:

CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1

2 Generic MST Algorithm input: weighted undirected graph G = (V,E,w) T := empty set while T is not yet a spanning tree of G find an edge e in E s.t. T U {e} is a subgraph of some MST of G add e to T return T (as MST of G)

CSCE 411, Spring 2012: Set 93 Kruskal's Algorithm as a Special Case of Generic Algorithm Consider edges in increasing order of weight Add the next edge iff it doesn't cause a cycle At any point, T is a forest (set of trees); eventually T is a single tree

CSCE 411, Spring 2012: Set 94 Idea of Prim's Algorithm Instead of growing the MST as possibly multiple trees that eventually all merge, grow the MST from a single vertex, so that there is only one tree at any point. Also a special case of the generic algorithm: at each step, add the minimum weight edge that goes out from the tree constructed so far.

CSCE 411, Spring 2012: Set 95 Prim's Algorithm input: weighted undirected graph G = (V,E,w) T := empty set S := {any vertex in V} while |T| < |V| - 1 do let (u,v) be a min wt. outgoing edge (u in S, v not in S) add (u,v) to T add v to S return (S,T) (as MST of G)

CSCE 411, Spring 2012: Set 96 Prim's Algorithm Example a b h i c g d f e

CSCE 411, Spring 2012: Set 97 Correctness of Prim's Algorithm Let T i be the tree represented by (S,T) at the end of iteration i. Show by induction on i that T i is a subtree of some MST of G. Basis: i = 0 (before first iteration). T 0 contains just a single vertex, and thus is a subtree of every MST of G.

CSCE 411, Spring 2012: Set 98 Correctness of Prim's Algorithm Induction: Assume T i is a subtree of some MST M. We must show T i+1 is a subtree of some MST. Let (u,v) be the edge added in iteration i+1. u v TiTi T i+1 Case 1: (u,v) is in M. Then T i+1 is also a subtree of M.

CSCE 411, Spring 2012: Set 99 Correctness of Prim's Algorithm Case 2: (u,v) is not in M. There is a path P in M from u to v, since M spans G. Let (x,y) be the first edge in P with one endpoint in T i and the other not in T i. u v TiTi x y P

CSCE 411, Spring 2012: Set 910 Correctness of Prim's Algorithm Let M' = M - {(x,y)} U {(u,v)} M' is also a spanning tree of G. w(M') = w(M) - w(x,y) + w(u,v) ≤ w(M) since (u,v) is min wt outgoing edge So M' is also an MST and T i+1 is a subtree of M' u v TiTi x y T i+1

CSCE 411, Spring 2012: Set 911 Implementing Prim's Algorithm How do we find minimum weight outgoing edge? First cut: scan all adjacency lists at each iteration. Results in O(VE) time. Try to do better.

CSCE 411, Spring 2012: Set 912 Implementing Prim's Algorithm Idea: have each vertex not yet in the tree keep track of its best (cheapest) edge to the tree constructed so far. To find min wt. outgoing edge, find minimum among these values use a priority queue to store the best edge info (insert and extract-min operations)

CSCE 411, Spring 2012: Set 913 Implementing Prim's Algorithm When a vertex v is added to T, some other vertices might have their best edges affected, but only neighbors of v add decrease-key operation to the priority queue u v TiTi T i+1 w w's best edge to T i check if this edge is cheaper for w x x's best edge to T i v's best edge to T i

CSCE 411, Spring 2012: Set 914 Details on Prim's Algorithm Associate with each vertex v two fields: best-wt[v] : if v is not yet in the tree, then it holds the min. wt. of all edges from v to a vertex in the tree. Initially infinity. best-node[v] : if v is not yet in the tree, then it holds the name of the vertex (node) u in the tree s.t. w(v,u) is v's best-wt. Initially nil.

CSCE 411, Spring 2012: Set 915 Details on Prim's Algorithm input: G = (V,E,w) // initialization initialize priority queue Q to contain all vertices, using best-wt values as keys let v 0 be any vertex in V decrease-key(Q,v 0,0) // last line means change best-wt[v 0 ] to 0 and adjust Q accordingly

CSCE 411, Spring 2012: Set 916 Details on Prim's Algorithm while Q is not empty do u := extract-min(Q) // vertex w/ smallest best-wt if u is not v 0 then add (u,best-node[u]) to T for each neighbor v of u do if v is in Q and w(u,v) < best-wt[v] then  best-node[v] := u  decrease-key(Q,v,w(u,v)) return (V,T) // as MST of G

CSCE 411, Spring 2012: Set 917 Running Time of Prim's Algorithm Depends on priority queue implementation. Let T ins be time for insert T dec be time for decrease-key T ex be time for extract-min Then we have |V| inserts and one decrease-key in the initialization: O(V  T ins +T dec ) |V| iterations of while one extract-min per iteration: O(V  T ex ) total

CSCE 411, Spring 2012: Set 918 Running Time of Prim's Algorithm Each iteration of while includes a for loop. Number of iterations of for loop varies, depending on how many neighbors the current vertex has Total number of iterations of for loop is O(E). Each iteration of for loop: one decrease key, so O(E  T dec ) total

CSCE 411, Spring 2012: Set 919 Running Time of Prim's Algorithm O(V(T ins + T ex ) + E  T dec ) If priority queue is implemented with a binary heap, then T ins = T ex = T dec = O(log V) total time is O(E log V) (Think about how to implement decrease-key in O(log V) time.)

CSCE 411, Spring 2012: Set 920 Shortest Paths in a Graph Let's review two important single-source shortest path algorithms: Dijkstra's algorithm Bellman-Ford algorithm

CSCE 411, Spring 2012: Set 921 Single Source Shortest Path Problem Given: directed or undirected graph G = (V,E,w) and source vertex s in V Find: For each t in V, a path in G from s to t with minimum weight Warning! Negative weights are a problem: s t 4 55

CSCE 411, Spring 2012: Set 922 Shortest Path Tree Result of a SSSP algorithm can be viewed as a tree rooted at the source Why not use breadth-first search? Works fine if all weights are the same: weight of each path is (a multiple of) the number of edges in the path Doesn't work when weights are different

CSCE 411, Spring 2012: Set 923 Dijkstra's SSSP Algorithm Assumes all edge weights are nonnegative Similar to Prim's MST algorithm Start with source vertex s and iteratively construct a tree rooted at s Each vertex keeps track of tree vertex that provides cheapest path from s (not just cheapest path from any tree vertex) At each iteration, include the vertex whose cheapest path from s is the overall cheapest

CSCE 411, Spring 2012: Set 924 Prim's vs. Dijkstra's s Prim's MST s Dijkstra's SSSP

CSCE 411, Spring 2012: Set 925 Implementing Dijkstra's Alg. How can each vertex u keep track of its best path from s? Keep an estimate, d[u], of shortest path distance from s to u Use d as a key in a priority queue When u is added to the tree, check each of u's neighbors v to see if u provides v with a cheaper path from s: compare d[v] to d[u] + w(u,v)

CSCE 411, Spring 2012: Set 926 Dijkstra's Algorithm input: G = (V,E,w) and source vertex s // initialization d[s] := 0 d[v] := infinity for all other vertices v initialize priority queue Q to contain all vertices using d values as keys

CSCE 411, Spring 2012: Set 927 Dijkstra's Algorithm while Q is not empty do u := extract-min(Q) for each neighbor v of u do if d[u] + w(u,v) < d[v] then  d[v] := d[u] + w(u,v)  decrease-key(Q,v,d[v])  parent(v) := u

CSCE 411, Spring 2012: Set 928 Dijkstra's Algorithm Example ab de c source is vertex a QabcdebcdecdedededØ d[a] d[b] ∞ d[c] ∞ 1210 d[d] ∞∞∞ 1613 d[e] ∞∞ 11 iteration

CSCE 411, Spring 2012: Set 929 Correctness of Dijkstra's Alg. Let T i be the tree constructed after i-th iteration of while loop: vertices not in Q edges indicated by parent variables Show by induction on i that the path in T i from s to u is a shortest path and has distance d[u], for all u in T i (i.e., show that T i is a correct shortest path tree). Basis: i = 1. s is the only vertex in T 1 and d[s] = 0.

CSCE 411, Spring 2012: Set 930 Correctness of Dijkstra's Alg. Induction: Assume T i is a correct shortest path tree. Show that T i+1 is a correct shortest path tree. Let u be the vertex added in iteration i. Let x = parent(u). s x TiTi u T i+1 Need to show path in T i+1 from s to u is a shortest path, and has distance d[u]

CSCE 411, Spring 2012: Set 931 Correctness of Dijkstra's Alg s x TiTi u T i+1 P, path in T i+1 from s to u (a,b) is first edge in P' that leaves T i a b P', another path from s to u

CSCE 411, Spring 2012: Set 932 Correctness of Dijkstra's Alg s x TiTi u T i+1 a b P' P Let P1 be part of P' before (a,b). Let P2 be part of P' after (a,b). w(P') = w(P1) + w(a,b) + w(P2) ≥ w(P1) + w(a,b) (nonneg wts) ≥ w(s->a path in T i ) + w(a,b) (inductive hypothesis) ≥ w(s->x path in T i ) + w(x,u) (alg chose u in iteration i and d-values are accurate, by inductive hypothesis) = w(P). So P is a shortest path, and d[u] is accurate after iteration i+1.

CSCE 411, Spring 2012: Set 933 Running Time of Dijkstra's Alg. initialization: insert each vertex once O(V T ins ) O(V) iterations of while loop one extract-min per iteration => O(V T ex ) for loop inside while loop has variable number of iterations… For loop has O(E) iterations total one decrease-key per iteration => O(E T dec ) Total is O(V (T ins + T ex ) + E T dec )

CSCE 411, Spring 2012: Set 934 Using Different Heap Implementations O(V(T ins + T ex ) + E  T dec ) If priority queue is implemented with a binary heap, then T ins = T ex = T dec = O(log V) total time is O(E log V) There are fancier implementations of the priority queue, such as Fibonacci heap: T ins = O(1), T ex = O(log V), T dec = O(1) (amortized) total time is O(V log V + E)

CSCE 411, Spring 2012: Set 935 Using Simpler Heap Implementations O(V(T ins + T ex ) + E  T dec ) If graph is dense, so that |E| =  (V 2 ), then it doesn't help to make T ins and T ex to be at most O(V). Instead, focus on making T dec be small, say constant. Implement priority queue with an unsorted array: T ins = O(1), T ex = O(V), T dec = O(1) total is O(V 2 )

CSCE 411, Spring 2012: Set 936 What About Negative Edge Weights? Dijkstra's SSSP algorithm requires all edge weights to be nonnegative even more restrictive than outlawing negative weight cycles Bellman-Ford SSSP algorithm can handle negative edge weights even "handles" negative weight cycles by reporting they exist

CSCE 411, Spring 2012: Set 937 Bellman-Ford Idea Consider each edge (u,v) and see if u offers v a cheaper path from s compare d[v] to d[u] + w(u,v) Repeat this process |V| - 1 times to ensure that accurate information propgates from s, no matter what order the edges are considered in

CSCE 411, Spring 2012: Set 938 Bellman-Ford SSSP Algorithm input: directed or undirected graph G = (V,E,w) //initialization initialize d[v] to infinity and parent[v] to nil for all v in V other than the source initialize d[s] to 0 and parent[s] to s // main body for i := 1 to |V| - 1 do for each (u,v) in E do // consider in arbitrary order if d[u] + w(u,v) < d[v] then d[v] := d[u] + w(u,v) parent[v] := u

CSCE 411, Spring 2012: Set 939 Bellman-Ford SSSP Algorithm // check for negative weight cycles for each (u,v) in E do if d[u] + w(u,v) < d[v] then output "negative weight cycle exists"

CSCE 411, Spring 2012: Set 940 Running Time of Bellman-Ford O(V) iterations of outer for loop O(E) iterations of inner for loop O(VE) time total

CSCE 411, Spring 2012: Set 941 Bellman-Ford Example s c a b 3 — process edges in order (c,b) (a,b) (c,a) (s,a) (s,c)

CSCE 411, Spring 2012: Set 942 Correctness of Bellman-Ford Assume no negative-weight cycles. Lemma: d[v] is never an underestimate of the actual shortest path distance from s to v. Lemma: If there is a shortest s-to-v path containing at most i edges, then after iteration i of the outer for loop, d[v] is at most the actual shortest path distance from s to v. Theorem: Bellman-Ford is correct. Proof: Follows from these 2 lemmas and fact that every shortest path has at most |V| - 1 edges.

CSCE 411, Spring 2012: Set 943 Correctness of Bellman-Ford Suppose there is a negative weight cycle. Then the distance will decrease even after iteration |V| - 1 shortest path distance is negative infinity This is what the last part of the code checks for.

CSCE 411, Spring 2012: Set 944 Speeding Up Bellman-Ford The previous example would have converged faster if we had considered the edges in a different order in the for loop move outward from s If the graph is a DAG (no cycles), we can fully exploit this idea to speed up the running time

CSCE 411, Spring 2012: Set 945 DAG Shortest Path Algorithm input: directed graph G = (V,E,w) and source vertex s in V topologically sort G d[v] := infinity for all v in V d[s] := 0 for each u in V in topological sort order do for each neighbor v of u do d[v] := min{d[v],d[u] + w(u,v)}

CSCE 411, Spring 2012: Set 946 DAG Shortest Path Algorithm Running Time is O(V + E). Example: