CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Similar documents
Minimum Spanning Trees

Minimum Spanning Trees

Shortest Paths and Minimum Spanning Trees

Advanced algorithms. topological ordering, minimum spanning tree, Union-Find problem. Jiří Vyskočil, Radek Mařík 2012

Analysis of Algorithms, I

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

Data Structures and Algorithms. Werner Nutt

Minimum Spanning Trees

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

Minimum Spanning Trees My T. UF

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Week 11: Minimum Spanning trees

2 A Template for Minimum Spanning Tree Algorithms

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

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

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

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

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

Minimum Spanning Trees

Shortest Paths and Minimum Spanning Trees

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

Minimum spanning trees

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

Department of Information Technology

Lecture Notes for Chapter 23: Minimum Spanning Trees

Unit 2: Algorithmic Graph Theory

Quiz 2 CS 3510 October 22, 2004

CS 4349 Lecture October 23rd, 2017

CS583 Lecture 09. Jana Kosecka. Graph Algorithms Topological Sort Strongly Connected Component Minimum Spanning Tree

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

Minimum Spanning Trees

Week 12: Minimum Spanning trees and Shortest Paths

Announcements Problem Set 5 is out (today)!

Introduction to Algorithms I

Algorithms (I) Introduction. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Minimum Spanning Tree

Introduction to Algorithms. Lecture 11

SRI VENKATESWARA COLLEGE OF ENGINEERING. COURSE DELIVERY PLAN - THEORY Page 1 of 6

Design and Analysis of Algorithms (I)

Introduction to Algorithms

Lecture 1. Introduction

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Partha Sarathi Manal

Breadth-First Search, 1. Slides for CIS 675 DPV Chapter 4. Breadth-First Search, 3. Breadth-First Search, 2

Shortest path problems

Design and Analysis of Algorithms

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

Title. Ferienakademie im Sarntal Course 2 Distance Problems: Theory and Praxis. Nesrine Damak. Fakultät für Informatik TU München. 20.

Minimum Spanning Tree (undirected graph)

Graph Algorithms. A Brief Introduction. 高晓沨 (Xiaofeng Gao) Department of Computer Science Shanghai Jiao Tong Univ.

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

Example of why greedy step is correct

Three Graph Algorithms

Three Graph Algorithms

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

Lecture 5: Graph algorithms 1

Algorithms for Data Science

Lecture 10: Analysis of Algorithms (CS ) 1

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Algorithms (V) Path in Graphs. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Randomized Graph Algorithms

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

6.1 Minimum Spanning Trees

Minimum Spanning Trees Ch 23 Traversing graphs

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Algorithms (VI) Greedy Algorithms. Guoqiang Li. School of Software, Shanghai Jiao Tong University

Algorithm Design and Analysis

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

CMSC351 - Fall 2014, Homework #6

Solution. violating the triangle inequality. - Initialize U = {s} - Add vertex v to U whenever DIST[v] decreases.

Trees, Glorious Trees. From one new year to another new year, and from one new academic year to another new academic year ראש השנה לאילנות

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

CS 161: Design and Analysis of Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Tree. number of vertices. Connected Graph. CSE 680 Prof. Roger Crawfis

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

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

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

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

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Single Source Shortest Path

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

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

Introduction to Algorithms

Lecture 6 Basic Graph Algorithms

Minimum Spanning Trees Outline: MST

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

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

Introduction to Algorithms

Transcription:

CS161 - Minimum Spanning Trees and Single Source Shortest Paths David Kauchak Single Source Shortest Paths Given a graph G and two vertices s, t what is the shortest path from s to t? For an unweighted graph, BFS gives us a solution to this problem. For weighted graphs, as it turns out, we can calculate the shortest distance from s to all vertices t V in worst case the same amount of time for any particular t, so we ll look at this problem, which is the single source shortest paths. Shortest path property If the path v 1,v 2,v 3,...,v k where v i V is the shortest path from v 1 to v k then for all 1 i j k, v i,v i+1,...,v j is the shortest path from v i to v j Proof: Consider that a shorter path exists between v i and v j, then we could use this path instead of the path v i,v i+1,...,v j in the path from v 1 to v k, resulting in a shorter path from v 1 to v k, but this is a contradiction. General idea for all the algorithms mark each vertex with an upper bound on the distance from the source to that node. Decrease that value until it is correct. Dijkstra s algorithm Assume that all of the weights are positive 1

Like BFS, exept our frontier that we expand is based on the weights of the edges not the number of edges Dijkstra(G, s) 1 for all v V 2 dist[v] 3 prev[v] null 4 dist[s] 0 5 Q MakeHeap(V ) 6 while!empty(q) 7 u ExtractMin(Q) 8 for all edges (u,v) E 9 if dist[v] > dist[u] + w(u,v) 10 dist[v] dist[u] + w(u,v) 11 DecreaseKey(Q, v, dist[v]) 12 prev[v] u Why doesn t this hold with negative weights? Consider the graph: A B : 1,C : 10 B D : 1 C D : 10 D E : 5 What is the shortest path from A to E? Is it correct? Invariant: For every vertex that has been visited/removed from the heap, dist[v] is the actual shortest distance from s to v The only time a vertex u gets visited is when the distance from s to that vertex is smaller than any remaining vertex. In addition, because we enforce positive weights, there cannot be any other 2

path to u that hasn t been visited already that would result in a shorter path, since all paths visited in the future are longer. Runtime Depends on the heap implementation 1 call to MakeHeap V calls to ExtractMax E calls to DecreaseKey 1. array V + V V + E = O(V 2 ) 2. binary heap V + V log V + E log V = O((V + E)log V ) = O(E log V ) if E < V 2 /log V then this is an improvement 3. fibonacci heap V + V log V + E = O(V log V + E) negative cycles positive cycles - if a positive cycle exists can a path through that cycle be the shortest path? negative cycles - What happens when a negative cycles exists along the path to a negative cycle? Bellman-Ford algorithm (general case) 3

Bellman-Ford(G, s) 1 for all v V 2 dist[v] 3 prev[v] null 4 dist[s] 0 5 for i 1 to V 1 6 for all edges (u,v) E 7 if dist[v] > dist[u] + w(u,v) 8 dist[v] dist[u] + w(u,v) 9 prev[v] u 10 for all edges (u,v) E 11 if dist[v] > dist[u] + w(u,v) 12 return f alse Is it correct? Assuming no negative cycles (along the paths from s), Invariant: After any iteration i, all i edge paths from the source s to any vertex are the shortest possible path of i edges or less. For i = 1 this is true, since we re only traversing one edge, so the distance for any vertex v, 1 edge away from s will be w(s,v), which is the shortest path. Consider the difference between paths of length i 1 and paths of length i. There are two options: Adding another edge decreases the length of a particular path In this case, the comparison at line 6 will notice this difference (since it iterates over all edges) and the new distance for v will be updated accordingly Adding another edge doesn t decrease the length of a particular path In this case, the comparison at line 6 will not be true and no changes will be made, so the invariant still holds Does it identify negative cycles? The check in lines 9-11, see if we can continue to decrease the shortest path to a node. The only time this can happen, is if 4

Dags there is a negative cycle exists since all paths of length V 1 should already have the correct values. Any path longer than this must contain a cycle. A positive cycle would not decrease the value, so it must be a negative cycle. Running time V 1 loops and each loop iterates over all edges, O(V E) Is there any way we can speed this up slightly? What happens if at a given iteration we don t update any distances? Adds the constraint that there are no cycles Minimum Spanning Trees what is the problem? What is the lowest weight set of edges that connects all vertices of an undirected graph with positive weights? Can there be cycles? what are the applications Network connectivity Wiring connectivity Cut property What is a cut? Let S be a subset of the vertices and let edge e = (u,v) be the minimum cost edge with u S and v V S. Every minimum spanning tree contains the edge e. Proof: Consider a minimum spanning tree T that does not contain e. There must be come cycle in the graph that contains an edge 5

e = (u,v ) with u S and v V S with a higher weight (otherwise e would be the only option for creating an spanning tree). If we remove e from the spanning tree and include e, we will still have a spanning tree since we still connect sets S and V S. However, this new tree will have a lower weight since the weight of e is less than the weight of e, so T is not a minimum spanning tree. We ll use this property to prove the correctness of the MST algorithms. Kruskal s algorithm Add the lowest weight edge to the tree as long as that edge does not connect two vertices that are already connected via some other path. Kruskal(G) 1 for all v V 2 MakeSet(v) 3 T {} 4 sort the edges of E by weight 5 for all edges (u,v) E in increasing order of weight 6 if Find-Set(u) Find-Set(v) 7 add edge to T 8 Union(Find-Set(u),Find-Set(v)) 1. Is it correct? Let S be the set Find-Set(u). The edge (u,v) is the minimum edge from S to V S since we re visiting edge in increasing order and if S were connected to S V then Find-Set(u) Find-Set(v) would not be true. Therefore, by the cut propery, e must be part of the MST. 2. Running time V calls to MakeSet Sort the edges: O(E log E) 6

2E calls to Find-Set V 1 calls to Union Depends on the implementation of the sets Linked lists V + E log E + E V + V = O(E V ) Linked lists + heuristics (see section 21.3 of [1]) Prim s algorithm V + E log E + E log V + V = O(E log E + V log V ) = O(E log V + V log V ) = O((E + V )log V = O(E log V ) Start at some root node and build out the MST by adding the lowest weighted edge at the frontier. Prim(G, r) 1 for all v V 2 key[v] 3 prev[v] null 4 key[r] 0 5 H MakeHeap(key) 6 while!empty(h) 7 u Extract-Min(H) 8 visited[u] true 9 for each edge (u,v) E 10 if!visited[v] and w(u, v) < key(v) 11 Decrease-Key(v, w(u, v)) 12 prev[v] u Is it correct? Let S be the set of vertices visited so far (i.e. v : visited[v] = true). The only time a new edge is added to the MST is when it 7

is the lowest weight edge from S to V S because we use a heap and we only add edges from nodes in S. Therefore, by the cut property, this added edge is part of the MST. Runtime V initialization operation of Θ(1) 1 call to MakeHeap V calls to Extract-Min E calls to Decrease-Key 1. Binary heap V + E + V log V + E log V = O((V + E)log V ) = O(E log V ) 2. Fibonacci heap V + E + V log V + E = O(V log V ) These notes are adapted from material found in chapters 22,23 of [1], chapter 4 of [2] and chapters 4,5 of [3] References [1] Thomas H. Cormen, Charles E. Leiserson Ronald L. Rivest and Clifford Stein. 2007. Introduction to Algorithms, 2nd ed. MIT Press. [2] Jon Kleinberg and Eva Tardos. 2006. Algorithm Design. Pearson Education, Inc. [3] Sanjoy Dasgupta, Christos Papadimitiou and Umesh Vazirani. 2008. Algorithms. McGraw-Hill Companies, Inc. 8