Weighted Graph Algorithms Presented by Jason Yuan

Similar documents
Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Week 11: Minimum Spanning trees

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Unweighted Graphs & Algorithms

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

Week 12: Minimum Spanning trees and Shortest Paths

CS350: Data Structures Dijkstra s Shortest Path Alg.

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Minimum Spanning Trees

Minimum Spanning Tree

CS 310 Advanced Data Structures and Algorithms

Lecture 6 Basic Graph Algorithms

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Algorithm Design (8) Graph Algorithms 1/2

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Shortest path problems

Graph Algorithms (part 3 of CSC 282),

23.2 Minimum Spanning Trees

Solving problems on graph algorithms

Lecture 11: Analysis of Algorithms (CS ) 1

Algorithms for Minimum Spanning Trees

CHAPTER 13 GRAPH ALGORITHMS

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum Spanning Trees

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Minimum Spanning Trees My T. UF

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

All Shortest Paths. Questions from exercises and exams

Chapter 23. Minimum Spanning Trees

What is a minimal spanning tree (MST) and how to find one

Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

Chapter 25: All-Pairs Shortest-Paths

Shortest Path Problem

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

CSE 100: GRAPH ALGORITHMS

Introduction: (Edge-)Weighted Graph

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

Minimum Spanning Trees

Graph Algorithms shortest paths, minimum spanning trees, etc.

CSci 231 Final Review

Agenda. Graph Representation DFS BFS Dijkstra A* Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp

4/8/11. Single-Source Shortest Path. Shortest Paths. Shortest Paths. Chapter 24

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Dijkstra s algorithm for shortest paths when no edges have negative weight.

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI. Department of Computer Science and Engineering CS6301 PROGRAMMING DATA STRUCTURES II

Analysis of Algorithms, I

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CS420/520 Algorithm Analysis Spring 2009 Lecture 14

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Review of course COMP-251B winter 2010

from notes written mostly by Dr. Carla Savage: All Rights Reserved

Design and Analysis of Algorithms - - Assessment

Exam 3 Practice Problems

CS521 \ Notes for the Final Exam

Chapter 6. Dynamic Programming

END-TERM EXAMINATION

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

Algorithms and Data Structures (INF1) Lecture 15/15 Hua Lu

( ) n 3. n 2 ( ) D. Ο

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

22 Elementary Graph Algorithms. There are two standard ways to represent a

Graph Algorithms shortest paths, minimum spanning trees, etc.

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

Minimum Spanning Trees

6.1 Minimum Spanning Trees

Graph Algorithms (part 3 of CSC 282),

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

CS 6783 (Applied Algorithms) Lecture 5

Minimum Spanning Trees

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Shortest Path Algorithms

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

Single Source Shortest Path (SSSP) Problem

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

tree follows. Game Trees

Minimum Spanning Trees

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

More Graph Algorithms: Topological Sort and Shortest Distance

CS 561, Lecture 10. Jared Saia University of New Mexico

Minimum-Cost Spanning Tree. Example

Lecture 6: Shortest distance, Eulerian cycles, Maxflow

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

CS 125 Section #5 Graph Traversal and Linear Programs October 6, 2016

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Lecture Summary CSC 263H. August 5, 2016

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

looking ahead to see the optimum

Partha Sarathi Manal

COMP 251 Winter 2017 Online quizzes with answers

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

Transcription:

Weighted Graph Algorithms Presented by Jason Yuan Slides: Zachary Friggstad Programming Club Meeting

Weighted Graphs struct Edge { int u, v ; int w e i g h t ; // can be a double } ; Edge ( int uu = 0, int vv = 0, int ww = 0) : u ( uu ), v ( vv ), w(ww) {} bool operator <(const Edge& r h s ) const { return w e i g h t < r h s. w e i g h t ; } typedef v e c t o r <v e c t o r <Edge>> w e i g h t e d g r a p h ; w e i g h t e d g r a p h g ( n ) ; // create a graph with n nodes // read and add an edge Edge e ; c i n >> e. u >> e. v >> e. w e i g h t ; g [ e. u ]. push back ( e ) ;

Dijkstra s Algorithm Find the minimum-weight path from s to every other vertex. Assumption: w(u, v) 0 for all edges uv.

Dijkstra s Algorithm Find the minimum-weight path from s to every other vertex. Assumption: w(u, v) 0 for all edges uv. Light a fire at s. The fire takes w(s, v) seconds to reach every neighbour v of s. start 2 1 3 When a fire reaches a vertex v, if the vertex has not yet been burned then the fire spreads down each edge exiting v. 4 4 6 2

Use the Edge struct to model active fires spreading across edges. Edge.weight is the time when the fire reaches Edge.v.

Use the Edge struct to model active fires spreading across edges. Edge.weight is the time when the fire reaches Edge.v. w e i g h t e d g r a p h g ; // assume already populated p r i o r i t y q u e u e pq<edge >; // active fires v e c t o r <Edge> path ( g. s i z e ( ), Edge ( 1, 1, 1)); pq. push ( Edge ( s, s, 0 ) ) ; //a fire starts on s a time 0 while (! pq. empty ( ) ) { Edge c u r r = pq. top ( ) ; pq. pop ( ) ; if ( path [ c u r r. v ]. u!= 1) continue ; // already burned path [ c u r r. v ] = c u r r ; for ( auto& s u c c : g [ c u r r. v ] ) pq. push ( Edge ( s u c c. u, s u c c. v, s u c c. w e i g h t + c u r r. w e i g h t ) ) ; }

Use the Edge struct to model active fires spreading across edges. Edge.weight is the time when the fire reaches Edge.v. w e i g h t e d g r a p h g ; // assume already populated p r i o r i t y q u e u e pq<edge >; // active fires v e c t o r <Edge> path ( g. s i z e ( ), Edge ( 1, 1, 1)); pq. push ( Edge ( s, s, 0 ) ) ; //a fire starts on s a time 0 while (! pq. empty ( ) ) { Edge c u r r = pq. top ( ) ; pq. pop ( ) ; if ( path [ c u r r. v ]. u!= 1) continue ; // already burned path [ c u r r. v ] = c u r r ; for ( auto& s u c c : g [ c u r r. v ] ) pq. push ( Edge ( s u c c. u, s u c c. v, s u c c. w e i g h t + c u r r. w e i g h t ) ) ; } Now path[v].u is the vertex prior to v on a min-weight s v path and path[v].weight is the weight of this path.

Problem A c++ priority queue is a max-heap, meaning it will return the largest item in the heap. I just use change operator< to check weight > rhs.weight.

Problem A c++ priority queue is a max-heap, meaning it will return the largest item in the heap. I just use change operator< to check weight > rhs.weight. Running Time O(m log m) where m = # edges. An edge is burned at most once, and it takes O(log m) time to push and pop.

Handling Negative Weights Things are tricker with minimum-weight cycles. 3 t s 100 2 7-9 1 A walk from s to t could run around a negative weight cycle as long as it wants, so there is no minimum-weight walk. If we insist on not repeating a vertex, it is NP-hard to find the minimum-weight path. Shortest paths can still be found if no negative weight cycles exist.

Adjacency Matrix Representation 2 3 0 2 4-3 1-1 A = 1 3 1 0 3 2 0 3 4 0 1 1 1 0 ( no edge) It might make more sense to use on the diagonal. It depends on the application.

Floyd-Warshall Dijkstra s algorithm fails to find shortest paths if some edges have negative weight, even if there are no negative-weight cycles.

Floyd-Warshall Dijkstra s algorithm fails to find shortest paths if some edges have negative weight, even if there are no negative-weight cycles. An incredibly simple dynamic programming approach will compute the shortest path between any two vertices in just 4 lines of code.

Floyd-Warshall Dijkstra s algorithm fails to find shortest paths if some edges have negative weight, even if there are no negative-weight cycles. An incredibly simple dynamic programming approach will compute the shortest path between any two vertices in just 4 lines of code. Can handle negative weight edges and detect negative weight cycles.

Floyd-Warshall Dijkstra s algorithm fails to find shortest paths if some edges have negative weight, even if there are no negative-weight cycles. An incredibly simple dynamic programming approach will compute the shortest path between any two vertices in just 4 lines of code. Can handle negative weight edges and detect negative weight cycles. Let G be represented with an adjacency matrix with 0s on the diagonal.

/* Assumes g[u][ v] is initially the cost of the edge (u,v), INFINITY (i.e. some big number) if no such edge. Also need g[v][ v] = 0 for all v. */ for ( int k = 0 ; k < n ; k++) for ( int u = 0 ; u < n ; u++) for ( int v = 0 ; v < n ; v++) g [ u ] [ v ] = min ( g [ u ] [ v ], g [ u ] [ k ] + g [ k ] [ v ] ) ;

/* Assumes g[u][ v] is initially the cost of the edge (u,v), INFINITY (i.e. some big number) if no such edge. Also need g[v][ v] = 0 for all v. */ for ( int k = 0 ; k < n ; k++) for ( int u = 0 ; u < n ; u++) for ( int v = 0 ; v < n ; v++) g [ u ] [ v ] = min ( g [ u ] [ v ], g [ u ] [ k ] + g [ k ] [ v ] ) ; If there is a negative weight cycle C, g[v][v] < 0 for some v C. Otherwise, g[u][v] is the weight of the min-weight u v path for any u, v V.

/* Assumes g[u][ v] is initially the cost of the edge (u,v), INFINITY (i.e. some big number) if no such edge. Also need g[v][ v] = 0 for all v. */ for ( int k = 0 ; k < n ; k++) for ( int u = 0 ; u < n ; u++) for ( int v = 0 ; v < n ; v++) g [ u ] [ v ] = min ( g [ u ] [ v ], g [ u ] [ k ] + g [ k ] [ v ] ) ; If there is a negative weight cycle C, g[v][v] < 0 for some v C. Otherwise, g[u][v] is the weight of the min-weight u v path for any u, v V. Invariant After iteration k, g[u][v] is the minimum possible weight of a u v path using only uses 0,..., k as intermediate vertices.

/* Assumes g[u][ v] is initially the cost of the edge (u,v), INFINITY (i.e. some big number) if no such edge. Also need g[v][ v] = 0 for all v. */ for ( int k = 0 ; k < n ; k++) for ( int u = 0 ; u < n ; u++) for ( int v = 0 ; v < n ; v++) g [ u ] [ v ] = min ( g [ u ] [ v ], g [ u ] [ k ] + g [ k ] [ v ] ) ; If there is a negative weight cycle C, g[v][v] < 0 for some v C. Otherwise, g[u][v] is the weight of the min-weight u v path for any u, v V. Invariant After iteration k, g[u][v] is the minimum possible weight of a u v path using only uses 0,..., k as intermediate vertices. Running Time: O(n 3 ).

Bellman-Ford A (potentially) faster way to handle negative-weight edges. Let s V, let bf [s] = 0 and bf [v] = for all v s. The values bf [v] represent the shortest s v path seen so far.

Bellman-Ford A (potentially) faster way to handle negative-weight edges. Let s V, let bf [s] = 0 and bf [v] = for all v s. The values bf [v] represent the shortest s v path seen so far. Try to find an edge (u, v) with bf [u] + weight(u, v) < bf [v] to find a shorter path to v. Iterate until no more changes to bf [].

int b f [MAXN] ; // MAXN = max # nodes possible v e c t o r <Edge> edges ; // list of all edges... for ( int v = 0 ; v < n ; ++v ) b f [ v ] = ( v == s? 0 : INFINITY ) ; for ( int i t e r = 0 ; i t e r < n ; ++i t e r ) for ( auto& e : edges ) b f [ e. v ] = min ( b f [ e. v ], b f [ e. u ] + e. w eight ) ;

int b f [MAXN] ; // MAXN = max # nodes possible v e c t o r <Edge> edges ; // list of all edges... for ( int v = 0 ; v < n ; ++v ) b f [ v ] = ( v == s? 0 : INFINITY ) ; for ( int i t e r = 0 ; i t e r < n ; ++i t e r ) for ( auto& e : edges ) b f [ e. v ] = min ( b f [ e. v ], b f [ e. u ] + e. w eight ) ; If there is still an edge e with bf [e.u] + e.weight < bf [e.v] then e lies on a negative weight cycle.

int b f [MAXN] ; // MAXN = max # nodes possible v e c t o r <Edge> edges ; // list of all edges... for ( int v = 0 ; v < n ; ++v ) b f [ v ] = ( v == s? 0 : INFINITY ) ; for ( int i t e r = 0 ; i t e r < n ; ++i t e r ) for ( auto& e : edges ) b f [ e. v ] = min ( b f [ e. v ], b f [ e. u ] + e. w eight ) ; If there is still an edge e with bf [e.u] + e.weight < bf [e.v] then e lies on a negative weight cycle. Running Time: O(n m)

int b f [MAXN] ; // MAXN = max # nodes possible v e c t o r <Edge> edges ; // list of all edges... for ( int v = 0 ; v < n ; ++v ) b f [ v ] = ( v == s? 0 : INFINITY ) ; for ( int i t e r = 0 ; i t e r < n ; ++i t e r ) for ( auto& e : edges ) b f [ e. v ] = min ( b f [ e. v ], b f [ e. u ] + e. w eight ) ; If there is still an edge e with bf [e.u] + e.weight < bf [e.v] then e lies on a negative weight cycle. Running Time: O(n m) Loop Invariants: 1. If bf [v], it is the length of some s v path. 2. After k iterations, bf [v] shortest path that uses k steps.

All-pairs shortest paths with negative-weight edges Initialize bf [v] = 0, v V and run the Bellman-Ford algorithm. Note: bf [u] + weight(u, v) < bf [v] < 0 for some edge uv iff G has a negative-weight cycle.

All-pairs shortest paths with negative-weight edges Initialize bf [v] = 0, v V and run the Bellman-Ford algorithm. Note: bf [u] + weight(u, v) < bf [v] < 0 for some edge uv iff G has a negative-weight cycle. If there is no negative weight cycle: bf [u] + weight(u, v) bf [v] 0, uv E

All-pairs shortest paths with negative-weight edges Initialize bf [v] = 0, v V and run the Bellman-Ford algorithm. Note: bf [u] + weight(u, v) < bf [v] < 0 for some edge uv iff G has a negative-weight cycle. If there is no negative weight cycle: bf [u] + weight(u, v) bf [v] 0, uv E Find all-pairs shortest paths by running Dijkstra s from each v V, but use edge weights bf [u] + weight(u, v) bf [v].

All-pairs shortest paths with negative-weight edges Initialize bf [v] = 0, v V and run the Bellman-Ford algorithm. Note: bf [u] + weight(u, v) < bf [v] < 0 for some edge uv iff G has a negative-weight cycle. If there is no negative weight cycle: bf [u] + weight(u, v) bf [v] 0, uv E Find all-pairs shortest paths by running Dijkstra s from each v V, but use edge weights bf [u] + weight(u, v) bf [v]. Running time: O(nm log m). Better than O(n 3 ) in sparse graphs.

Minimum Spanning Tree Let G = (V, E) be an undirected, connected, and weighted graph. Spanning Tree A subset of edges T E forming a connected tree. Find the spanning tree with minimum total edge weight.

Kruskal s Algorithm Sort the edges e 1,..., e m by weight. T = For each e i = (u, v) in this order, if u is not connected to v in T then add e i to T.

Kruskal s Algorithm Sort the edges e 1,..., e m by weight. T = For each e i = (u, v) in this order, if u is not connected to v in T then add e i to T. Exercise The final T will always be connected (assuming G is connected). There are no cycles in T. So, T is a spanning tree.

Greedy algorithms like this often fail to find optimum solutions. Why does it work in this case?

Greedy algorithms like this often fail to find optimum solutions. Why does it work in this case? Lemma (good coffee shop math) A spanning tree T is a minimum-weight spanning tree if and only if for every cycle C, some heaviest edge of C is not in T. 5 2 3 3 2 1 4 2 2 3

Greedy algorithms like this often fail to find optimum solutions. Why does it work in this case? Lemma (good coffee shop math) A spanning tree T is a minimum-weight spanning tree if and only if for every cycle C, some heaviest edge of C is not in T. 5 2 3 3 2 1 4 2 2 3 So, when the algorithm considers the heaviest edge e i of some cycle, its endpoints are already connected so e i will not be added!

To efficiently detect if T connects the endpoints of an edge e i, we use the union-find data structure. It represents a connected component in T by a directed tree, the root is the representative of the tree. a e c b d g f e a g b c d f The endpoints of e i have the same representative if and only if they are already connected.

Before adding any edges (i.e. T = ): a b c d e f

Before adding any edges (i.e. T = ): a b c d e f To merge two components, just point one representative at the other. a e c b d g f e a g b c d f

Crawl up the tree to find representatives. b d g c h find h's representative

Crawl up the tree to find representatives. b d g c h find h's representative Path Compression To speed up future calculations, also point all nodes seen to the rep. b d g c h

int u f [MAXN] ; // union find pointers int f i n d ( int u ) { if ( u f [ u ]!= u ) u f [ u ] = f i n d ( u f [ u ] ) ; return u f [ u ] ; } bool merge ( int u, int v ) { u = f i n d ( u ) ; v = f i n d ( v ) ; u f [ u ] = v ; return u == v ; // true iff this merged different components }... for ( int v = 0 ; v < n ; ++i ) u f [ v ] = v ; // initialization // suppose edges are stored in vector <Edge > edges; s o r t ( edges. b e g i n ( ), edges. end ( ) ) ; int mst = 0 ; for ( auto& e : edges ) if ( merge ( e. u, e. v ) ) mst += e. w e i g h t ;

Running time: O(m log m) to sort the edges m union-find merges has total running time O(m log m) Overall: O(m log m).

Running time: O(m log m) to sort the edges m union-find merges has total running time O(m log m) Overall: O(m log m). Union Find by Rank (in textbook) k merge calls has running time O(α(k) k) where α(k) is a crazy slow function. α(k) 5 for k 100 100100 Though, α(k) so it s technically not a constant.

Open Kattis - borg https://open.kattis.com/problems/borg

Open Kattis - borg https://open.kattis.com/problems/borg The path of edges taken looks like a tree and the nodes are either S or A nodes. Compute the shortest path distance between any two of these characters: O(a x y) time using a BFS from each of the a aliens. Then compute the minimum spanning tree of the shortest-path graph in O(a log a) time. Overall: O(a x y) time

Open Kattis - allpairspath https://open.kattis.com/problems/allpairspath

Open Kattis - allpairspath https://open.kattis.com/problems/allpairspath Recall the running times. What was the fastest one for this task? Not Bellman-Ford, not Floyd-Warshall, not Dijkstra s. The Dijkstra + Bellman-Ford hybrid! Precomputes all-pairs shortest path distances in O(nm log m) time, each query can be answered in O(1) time.

Open Kattis - redbluetree https://open.kattis.com/problems/redbluetree

Open Kattis - redbluetree https://open.kattis.com/problems/redbluetree Compute any spanning tree T, first by processing the red edges. Return no if no spanning tree or if > k blue edges are used. Re-run Kruskal s algorithm by first adding blue edges of T, then the remaining edges until exactly k are added, then adding red. Correctness Idea: The first pass identifies the minimum number of blue edges required. Let T be a feasible solution. For any blue e T T, there is some blue f T T with T f + e, T e + f being spanning trees. So T f + e is another feasible solution agreeing more with T on blue edges. Iterating shows B T can be extended to a feasible solution.