CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.

Slides:



Advertisements
Similar presentations
Greed is good. (Some of the time)
Advertisements

Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Minimum Spanning Tree Algorithms
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
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.
Minimum Spanning Trees
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Lecture 27 CSE 331 Nov 6, Homework related stuff Solutions to HW 7 and HW 8 at the END of the lecture Turn in HW 7.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
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.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
SPANNING TREES Lecture 21 CS2110 – Spring
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
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.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
1 Chapter 4 Minimum Spanning Trees Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 4.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 3.
CSE 421 Algorithms Richard Anderson Lecture 8 Optimal Caching Dijkstra’s algorithm.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 11.
Introduction to Algorithms
CSE373: Data Structures & Algorithms
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Graph Algorithm.
Lecture 24 CSE 331 Oct 28, 2016.
Lecture 24 CSE 331 Oct 27, 2017.
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
Autumn 2016 Lecture 9 Dijkstra’s algorithm
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Autumn 2015 Lecture 9 Dijkstra’s algorithm
Chapter 23 Minimum Spanning Tree
Richard Anderson Lecture 11 Minimum Spanning Trees
Richard Anderson Lecture 9 Dijkstra’s algorithm
Richard Anderson Lecture 9 Dijkstra’s algorithm
Lecture 26 CSE 331 Nov 2, 2012.
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
Lecture 25 CSE 331 Oct 27, 2014.
Richard Anderson Lecture 10 Minimum Spanning Trees
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Autumn 2016 Lecture 10 Minimum Spanning Trees
Winter 2019 Lecture 9 Dijkstra’s algorithm
CSE 417: Algorithms and Computational Complexity
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Lecture 25 CSE 331 Oct 28, 2011.
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 332: Minimum Spanning Trees
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Autumn 2019 Lecture 9 Dijkstra’s algorithm
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm

Single Source Shortest Path Problem Given a graph and a start vertex s –Determine distance of every vertex from s –Identify shortest paths to each vertex Express concisely as a “shortest paths tree” Each vertex has a pointer to a predecessor on shortest path s v x u s v x u 1 3 3

Construct Shortest Path Tree from s a b c s e g f d a b c s e g f d

Warmup If P is a shortest path from s to v, and if t is on the path P, the segment from s to t is a shortest path between s and t WHY? s t v

Dijkstra’s Algorithm S = {}; d[s] = 0; d[v] = infinity for v != s While S != V Choose v in V-S with minimum d[v] Add v to S For each w in the neighborhood of v d[w] = min(d[w], d[v] + c(v, w)) s u v z y x Assume all edges have non-negative cost

Simulate Dijkstra’s algorithm (strarting from s) on the graph Round Vertex Added sabcd bd c a s

Dijkstra’s Algorithm as a greedy algorithm Elements committed to the solution by order of minimum distance

Correctness Proof Elements in S have the correct label Key to proof: when v is added to S, it has the correct distance label. s y v x u

Proof Let v be a vertex in V-S with minimum d[v] Let P v be a path of length d[v], with an edge (u,v) Let P be some other path to v. Suppose P first leaves S on the edge (x, y) –P = P sx + c(x,y) + P yv –Len(P sx ) + c(x,y) >= d[y] –Len(P yv ) >= 0 –Len(P) >= d[y] + 0 >= d[v] s y v x u

Negative Cost Edges Draw a small example a negative cost edge and show that Dijkstra’s algorithm fails on this example

Bottleneck Shortest Path Define the bottleneck distance for a path to be the maximum cost edge along the path s v x u

Compute the bottleneck shortest paths a b c s e g f d a b c s e g f d

How do you adapt Dijkstra’s algorithm to handle bottleneck distances Does the correctness proof still apply?

Who was Dijkstra? What were his major contributions?

Edsger Wybe Dijkstra was one of the most influential members of computing science's founding generation. Among the domains in which his scientific contributions are fundamental are –algorithm design –programming languages –program design –operating systems –distributed processing –formal specification and verification –design of mathematical arguments

Shortest Paths Negative Cost Edges –Dijkstra’s algorithm assumes positive cost edges –For some applications, negative cost edges make sense –Shortest path not well defined if a graph has a negative cost cycle a b c s e g f

Negative Cost Edge Preview Topological Sort can be used for solving the shortest path problem in directed acyclic graphs Bellman-Ford algorithm finds shortest paths in a graph with negative cost edges (or reports the existence of a negative cost cycle).

Dijkstra’s Algorithm Implementation and Runtime S = {}; d[s] = 0; d[v] = infinity for v != s While S != V Choose v in V-S with minimum d[v] Add v to S For each w in the neighborhood of v d[w] = min(d[w], d[v] + c(v, w)) s u v z y x Edge costs are assumed to be non-negative a b HEAP OPERATIONS n Extract Mins m Heap Updates

Bottleneck Shortest Path Define the bottleneck distance for a path to be the maximum cost edge along the path s v x u

Compute the bottleneck shortest paths a b c s e g f d a b c s e g f d

Dijkstra’s Algorithm for Bottleneck Shortest Paths S = {}; d[s] = negative infinity; d[v] = infinity for v != s While S != V Choose v in V-S with minimum d[v] Add v to S For each w in the neighborhood of v d[w] = min(d[w], max(d[v], c(v, w))) s u v z y x a b

Minimum Spanning Tree Introduce Problem Demonstrate three different greedy algorithms Provide proofs that the algorithms work

Minimum Spanning Tree a b c s e g f t u v

Greedy Algorithms for Minimum Spanning Tree Extend a tree by including the cheapest out going edge Add the cheapest edge that joins disjoint components Delete the most expensive edge that does not disconnect the graph a bc d e

Greedy Algorithm 1 Prim’s Algorithm Extend a tree by including the cheapest out going edge t a e c g f b s u v Construct the MST with Prim’s algorithm starting from vertex a Label the edges in order of insertion

Greedy Algorithm 2 Kruskal’s Algorithm Add the cheapest edge that joins disjoint components t a e c g f b s u v Construct the MST with Kruskal’s algorithm Label the edges in order of insertion

Greedy Algorithm 3 Reverse-Delete Algorithm Delete the most expensive edge that does not disconnect the graph t a e c g f b s u v Construct the MST with the reverse- delete algorithm Label the edges in order of removal

Why do the greedy algorithms work? For simplicity, assume all edge costs are distinct Let S be a subset of V, and suppose e = (u, v) is the minimum cost edge of E, with u in S and v in V-S e is in every minimum spanning tree

Proof Suppose T is a spanning tree that does not contain e Add e to T, this creates a cycle The cycle must have some edge e 1 = (u 1, v 1 ) with u 1 in S and v 1 in V-S T 1 = T – {e 1 } + {e} is a spanning tree with lower cost Hence, T is not a minimum spanning tree

Optimality Proofs Prim’s Algorithm computes a MST Kruskal’s Algorithm computes a MST

Reverse-Delete Algorithm Lemma: The most expensive edge on a cycle is never in a minimum spanning tree

Dealing with the assumption of no equal weight edges Force the edge weights to be distinct –Add small quantities to the weights –Give a tie breaking rule for equal weight edges

Dijkstra’s Algorithm for Minimum Spanning Trees S = {}; d[s] = 0; d[v] = infinity for v != s While S != V Choose v in V-S with minimum d[v] Add v to S For each w in the neighborhood of v d[w] = min(d[w], c(v, w)) s u v z y x a b

Minimum Spanning Tree a b c s e g f t u v Undirected Graph G=(V,E) with edge weights

Greedy Algorithms for Minimum Spanning Tree [Prim] Extend a tree by including the cheapest out going edge [Kruskal] Add the cheapest edge that joins disjoint components [ReverseDelete] Delete the most expensive edge that does not disconnect the graph a bc d e

Why do the greedy algorithms work? For simplicity, assume all edge costs are distinct

Edge inclusion lemma Let S be a subset of V, and suppose e = (u, v) is the minimum cost edge of E, with u in S and v in V-S e is in every minimum spanning tree of G –Or equivalently, if e is not in T, then T is not a minimum spanning tree SV - S e

Proof Suppose T is a spanning tree that does not contain e Add e to T, this creates a cycle The cycle must have some edge e 1 = (u 1, v 1 ) with u 1 in S and v 1 in V-S T 1 = T – {e 1 } + {e} is a spanning tree with lower cost Hence, T is not a minimum spanning tree SV - S e e is the minimum cost edge between S and V-S

Optimality Proofs Prim’s Algorithm computes a MST Kruskal’s Algorithm computes a MST Show that when an edge is added to the MST by Prim or Kruskal, the edge is the minimum cost edge between S and V-S for some set S.

Prim’s Algorithm S = { }; T = { }; while S != V choose the minimum cost edge e = (u,v), with u in S, and v in V-S add e to T add v to S

Prove Prim’s algorithm computes an MST Show an edge e is in the MST when it is added to T

Kruskal’s Algorithm Let C = {{v 1 }, {v 2 },..., {v n }}; T = { } while |C| > 1 Let e = (u, v) with u in C i and v in C j be the minimum cost edge joining distinct sets in C Replace C i and C j by C i U C j Add e to T

Prove Kruskal’s algorithm computes an MST Show an edge e is in the MST when it is added to T

Reverse-Delete Algorithm Lemma: The most expensive edge on a cycle is never in a minimum spanning tree

Dealing with the assumption of no equal weight edges Force the edge weights to be distinct –Add small quantities to the weights –Give a tie breaking rule for equal weight edges

Application: Clustering Given a collection of points in an r- dimensional space, and an integer K, divide the points into K sets that are closest together

Distance clustering Divide the data set into K subsets to maximize the distance between any pair of sets –dist (S 1, S 2 ) = min {dist(x, y) | x in S 1, y in S 2 }

Divide into 2 clusters

Divide into 3 clusters

Divide into 4 clusters

Distance Clustering Algorithm Let C = {{v 1 }, {v 2 },..., {v n }}; T = { } while |C| > K Let e = (u, v) with u in C i and v in C j be the minimum cost edge joining distinct sets in C Replace C i and C j by C i U C j

K-clustering