Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.

Slides:



Advertisements
Similar presentations
Advanced Algorithm Design and Analysis Jiaheng Lu Renmin University of China
Advertisements

Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
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.
Chapter 23 Minimum Spanning Trees
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
All-Pairs Shortest Paths
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 What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Dijkstra's algorithm.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Graphs 2 Kevin Kauffman CS 309s. Problem We have sprinklers in our yard which need to be connected with pipe. Assuming pipes may only intersect at sprinkler.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Graphs David Kauchak cs302 Spring DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch.
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.
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
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)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Minimum- Spanning Trees
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Shortest Paths and Minimum Spanning Trees
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Tree.
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
CSE 373: Data Structures and Algorithms
Presentation transcript:

Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009

Administrative Grading final grade extra credit TA issues/concerns HW6 shorter will be due Wed. 8/12 before class Errors in class slides and notes Anonymized scores posted

Shortest paths What is the shortest path from a to d? A B CE D

Shortest paths BFS A B CE D

Shortest paths What is the shortest path from a to d? A B CE D

Shortest paths We can still use BFS A B CE D

Shortest paths We can still use BFS A B CE D A B CE D

Shortest paths We can still use BFS A B CE D

Shortest paths What is the problem? A B CE D

Shortest paths Running time is dependent on the weights A B C A B C

Shortest paths A B C A B C

Shortest paths A B C

A B C

A B C Nothing will change as we expand the frontier until we’ve gone out 100 levels

Dijkstra’s algorithm

prev keeps track of the shortest path

Dijkstra’s algorithm

Single source shortest paths All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms Why?

A B CE D

A B CE D    

A B CE D    0 Heap A 0 B  C  D  E 

A B CE D    0 Heap B  C  D  E 

A B CE D    0 Heap B  C  D  E 

A B CE D   1 0 Heap C 1 B  D  E 

A B CE D   1 0 Heap C 1 B  D  E 

A B CE D   1 0 Heap C 1 B 3 D  E 

A B CE D   1 0 Heap C 1 B 3 D  E 

3 A B CE D   1 0 Heap B 3 D  E 

3 A B CE D   1 0 Heap B 3 D  E 

3 A B CE D   1 0 Heap B 3 D  E 

3 A B CE D   1 0 Heap B 2 D  E 

3 A B CE D   1 0 Heap B 2 D  E 

3 A B CE D  Heap B 2 E 5 D 

3 A B CE D  Heap B 2 E 5 D  Frontier?

3 A B CE D  Heap B 2 E 5 D  All nodes reachable from starting node within a given distance

3 A B CE D Heap E 3 D 5

3 A B CE D Heap D 5

3 A B CE D Heap

A B CE D

Is Dijkstra’s algorithm correct? Invariant:

Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v

Is Dijkstra’s algorithm correct? Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v The only time a vertex gets visited is when the distance from s to that vertex is smaller than the distance to any remaining vertex Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path

Running time?

1 call to MakeHeap

Running time? |V| iterations

Running time? |V| calls

Running time? O(|E|) calls

Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|)

Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) O(|E| log |V|) Is this an improvement?If |E| < |V| 2 / log |V|

Running time? Depends on the heap implementation 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) Fib heapO(|V|)O(|V| log |V|) O(|E|) O(|V| log |V| + |E|) O(|E| log |V|)

What about Dijkstra’s on…? A B C E D

What about Dijkstra’s on…? A B C E D

What about Dijkstra’s on…? A B C E D Dijkstra’s algorithm only works for positive edge weights

Bounding the distance Another invariant: For each vertex v, dist[v] is an upper bound on the actual shortest distance start of at  only update the value if we find a shorter distance An update procedure

Can we ever go wrong applying this update rule? We can apply this rule as many times as we want and will never underestimate dist[v] When will dist[v] be right? If u is along the shortest path to v and dist[u] is correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct Consider the shortest path from s to v sp1p1 vp2p2 p3p3 pkpk

dist[v] will be right if u is along the shortest path to v and dist[u] is correct What happens if we update all of the vertices with the above update? sp1p1 vp2p2 p3p3 pkpk

dist[v] will be right if u is along the shortest path to v and dist[u] is correct What happens if we update all of the vertices with the above update? sp1p1 vp2p2 p3p3 pkpk correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct What happens if we update all of the vertices with the above update? sp1p1 vp2p2 p3p3 pkpk correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct Does the order that we update the vertices matter? sp1p1 vp2p2 p3p3 pkpk correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk

dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct

dist[v] will be right if u is along the shortest path to v and dist[u] is correct How many times do we have to do this for vertex p i to have the correct shortest path from s? i times sp1p1 vp2p2 p3p3 pkpk correct …

dist[v] will be right if u is along the shortest path to v and dist[u] is correct What is the longest (vetex-wise) the path from s to any node v can be? |V| - 1 edges/vertices sp1p1 vp2p2 p3p3 pkpk correct …

Bellman-Ford algorithm

Initialize all the distances

Bellman-Ford algorithm iterate over all edges/vertices and apply update rule

Bellman-Ford algorithm

check for negative cycles

Negative cycles A B C E D What is the shortest path from a to e?

Bellman-Ford algorithm

G S F E A D B C How many edges is the shortest path from s to: A:

Bellman-Ford algorithm G S F E A D B C How many edges is the shortest path from s to: A:3

Bellman-Ford algorithm G S F E A D B C How many edges is the shortest path from s to: A:3 B:

Bellman-Ford algorithm G S F E A D B C How many edges is the shortest path from s to: A:3 B:5

Bellman-Ford algorithm G S F E A D B C How many edges is the shortest path from s to: A:3 B:5 D:

Bellman-Ford algorithm G S F E A D B C How many edges is the shortest path from s to: A:3 B:5 D:7

Bellman-Ford algorithm G S F E A D B C        Iteration: 0

Bellman-Ford algorithm G S F E A D B C      8 Iteration: 1

Bellman-Ford algorithm G S F E A D B C    Iteration: 2

Bellman-Ford algorithm G S F E A D B C   Iteration: 3 A has the correct distance and path

Bellman-Ford algorithm G S F E A D B C  Iteration: 4

Bellman-Ford algorithm G S F E A D B C Iteration: 5 B has the correct distance and path

Bellman-Ford algorithm G S F E A D B C Iteration: 6

Bellman-Ford algorithm G S F E A D B C Iteration: 7 D (and all other nodes) have the correct distance and path

Correctness of Bellman-Ford Loop invariant:

Correctness of Bellman-Ford Loop invariant: After iteration i, all vertices with shortest paths from s of length i edges or less have correct distances

Runtime of Bellman-Ford O(|V| |E|)

Runtime of Bellman-Ford Can you modify the algorithm to run faster (in some circumstances)?

All pairs shortest paths Simple approach Call Bellman-Ford |V| times O(|V| 2 |E|) Floyd-Warshall – Θ(|V| 3 ) Johnson’s algorithm – O(|V| 2 log |V| + |V| |E|)

Minimum spanning trees What is the lowest weight set of edges that connects all vertices of an undirected graph with positive weights Input: An undirected, positive weight graph, G=(V,E) Output: A tree T=(V,E’) where E’  E that minimizes

MST example A BD C F E A BD C F E 5 4

MSTs Can an MST have a cycle? A BD C F E 5 4 4

MSTs Can an MST have a cycle? A BD C F E 5 4

Applications? Connectivity Networks (e.g. communications) Circuit desing/wiring hub/spoke models (e.g. flights, transportation) Traveling salesman problem?

Cuts A cut is a partitioning of the vertices into two sets S and V-S An edges “crosses” the cut if it connects a vertex u  V and v  V-S A BD C F E

Minimum cut property Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e. SV-S e’ e Consider an MST with edge e’ that is not the minimum edge

Minimum cut property Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e. SV-S e’ e Using e instead of e’, still connects the graph, but produces a tree with smaller weights

Algorithm ideas? Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e.

Kruskal’s algorithm A BD C F E G MST A BD C F E Add smallest edge that connects two sets not already connected

A BD C F E G MST A BD C 1 F E Add smallest edge that connects two sets not already connected Kruskal’s algorithm

A BD C F E G MST A BD C 1 2 F E Add smallest edge that connects two sets not already connected Kruskal’s algorithm

A BD C F E G MST A BD C F E Kruskal’s algorithm Add smallest edge that connects two sets not already connected

A BD C F E G MST A BD C F E 4 Kruskal’s algorithm Add smallest edge that connects two sets not already connected

A BD C F E G MST A BD C F E 5 4 Kruskal’s algorithm Add smallest edge that connects two sets not already connected

Correctness of Kruskal’s Never adds an edge that connects already connected vertices Always adds lowest cost edge to connect two sets. By min cut property, that edge must be part of the MST

Running time of Kruskal’s |V| calls to MakeSet

Running time of Kruskal’s O(|E| log |E|)

Running time of Kruskal’s 2 |E| calls to FindSet

Running time of Kruskal’s |V| calls to Union

Running time of Kruskal’s Disjoint set data structure O(|E| log |E|) + MakeSetFindSet |E| calls Union |V| calls Total Linked lists |V| O(|V| |E|) |V|O(|V||E| + |E| log |E|) O(|V| |E|) Linked lists + heuristics |V| O(|E| log |V|) |V|O(|E| log |V|+ |E| log |E|) O(|E| log |E| )

Prim’s algorithm

Start at some root node and build out the MST by adding the lowest weighted edge at the frontier

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E    0

Prim’s A BD C F E MST A BD C F E  4 5  60

Prim’s A BD C F E MST A BD C F E  4 5  60

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Prim’s A BD C F E MST A BD C F E

Correctness of Prim’s? Can we use the min-cut property? Given a partion S, let edge e be the minimum cost edge that crosses the partition. Every minimum spanning tree contains edge e. Let S be the set of vertices visited so far The only time we add a new edge is if it’s the lowest weight edge from S to V-S

Running time of Prim’s Θ(|V|)

Running time of Prim’s Θ(|V|)

Running time of Prim’s |V| calls to Extract-Min

Running time of Prim’s |E| calls to Decrease-Key

Running time of Prim’s Same as Dijksta’s algorithm 1 MakeHeap|V| ExtractMin|E| DecreaseKeyTotal ArrayO(|V|)O(|V| 2 ) O(|E|) O(|V| 2 ) Bin heapO(|V|)O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|) Fib heapO(|V|)O(|V| log |V|) O(|E|) O(|V| log |V| + |E|) O(|E| log |V|) Kruskal’s: O(|E| log |E| )