Week 12: Minimum Spanning trees and Shortest Paths

Similar documents
Week 11: Minimum Spanning trees

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

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

Single Source Shortest Path (SSSP) Problem

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

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

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

Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Minimum Spanning Trees

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

Introduction to Algorithms. Lecture 11

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

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Announcements Problem Set 5 is out (today)!

Outline. Computer Science 331. Analysis of Prim's Algorithm

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

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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

Minimum Spanning Trees

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

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

Minimum Spanning Trees My T. UF

Parallel Graph Algorithms

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

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

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

Algorithms and Data Structures 2014 Exercises and Solutions Week 9

CHAPTER 13 GRAPH ALGORITHMS

Single Source Shortest Path

CS4800: Algorithms & Data Jonathan Ullman

Minimum Spanning Trees

Algorithm Design and Analysis

Minimum Spanning Trees

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

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

6.1 Minimum Spanning Trees

Lecture 5: Graph algorithms 1

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

CSC 8301 Design & Analysis of Algorithms: Warshall s, Floyd s, and Prim s algorithms

COMP 182: Algorithmic Thinking Prim and Dijkstra: Efficiency and Correctness

Single Source Shortest Paths

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Lecture 11: Analysis of Algorithms (CS ) 1

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

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

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

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

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

Minimum Spanning Trees

Minimum Spanning Tree

Minimum Spanning Trees Ch 23 Traversing graphs

Recitation 13. Minimum Spanning Trees Announcements. SegmentLab has been released, and is due Friday, November 17. It s worth 135 points.

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Outline. Computer Science 331. Definitions: Paths and Their Costs. Computation of Minimum Cost Paths

Minimum-Cost Spanning Tree. Example

2 A Template for Minimum Spanning Tree Algorithms

CS 310 Advanced Data Structures and Algorithms

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

Shortest Path Algorithms

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

Algorithm Design and Analysis

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

CS 270 Algorithms. Oliver Kullmann. Analysing BFS. Depth-first search. Analysing DFS. Dags and topological sorting.

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

Shortest Path Problem

Three Graph Algorithms

Three Graph Algorithms

Dijkstra s Shortest Path Algorithm

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

Exam 3 Practice Problems

Analysis of Algorithms, I

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

Lecture 6 Basic Graph Algorithms

Introduction: (Edge-)Weighted Graph

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

Minimum Spanning Trees

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

Data Structures and Algorithms. Werner Nutt

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Undirected Graphs. Hwansoo Han

CSE 502 Class 23. Jeremy Buhler Steve Cole. April BFS solves unweighted shortest path problem. Every edge traversed adds one to path length

CS 6783 (Applied Algorithms) Lecture 5

Unit 2: Algorithmic Graph Theory

Parallel Graph Algorithms

Week 5. 1 Analysing BFS. 2 Depth-first search. 3 Analysing DFS. 4 Dags and topological sorting. 5 Detecting cycles. CS 270 Algorithms.

Prim & Kruskal Algorithm

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

Algorithm Design and Analysis

Introduction to Algorithms

Minimum Spanning Trees

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

CSci 231 Final Review

Introduction to Algorithms

Graph Traversal CSCI Algorithms I. Andrew Rosenberg

Transcription:

Agenda: Week 12: Minimum Spanning trees and Shortest Paths Kruskal s Algorithm Single-source shortest paths Dijkstra s algorithm for non-negatively weighted case Reading: Textbook : 61-7, 80-87, 9-601 1

Week 12: Kruskal s MST algorithm Kruskal s algorithm for the MST problem: Input: an edge-weighted (simple, undirected, connected) graph (positive weights) Output: an MST Idea: Start with a forest T on all the vertices and no edges Grow the forest T to become a tree by adding one edge at a time The edges are considered in non-decreasing order of their weight an edge can be added if it joins two different connected components (i.e. two trees of T ) So an edge is added if it does not create a cycle, otherwise it is discarded For each vertex we keep an index which tells the index of the cluster to which it belongs. When we add an edge, we merge the clusters (i.e. the sub-trees) that it connects. 2

Week 12: Kruskal s MST algorithm Kruskal s algorithm for the MST problem: procedure kruskal (G) T for each v V (G) do Define cluster C(v) v sort edges in E(G) into non-decreasing weight order for each edge e i = (u, v) E(G) do if C(u) C(v) then T T {e i } merge clusters C(u) and C(v) return T An example: 2 1 9 2 12 8 1 1 17 1 19 6 18 7 kruskalmst(g, w) returns: 2 1 2 12 8 1 6 7

Week 12: Kruskal s MST algorithm Kruskal s algorithm for the MST problem analysis: Correctness: We prove that after every step, where we have selected i edges and put into T, call it T i, there is a MST T opt which has all these i edges and has none of the edges we discarded. This is proved by induction on i, for all 0 i n 1. Once we prove it for i = n 1 it implies that the solution is a MST. The critical point is in the induction step when we select an edge e to be added to T i 1 to obtain T i but T opt does not have it. In this case, T = T opt + e has a cycle, C. This cycle contains at least one edge e that is not in T i (why?) Furthermore edge e is among the edges we have not considered yet, because up until edge e, all the decisions made were consisten with T opt. So w(e ) w(e). So T = T opt + e e is also a MST that extends T i. Running time analysis: C(u) and C(v)? how to implement Merge clusters

Week 12: Kruskal s MST algorithm Kruskal s algorithm for the MST problem analysis: Running time analysis: Each cluster will be an unordered linked list of vertices in that cluster Each vertex v also keeps the index of the cluster to which it belongs To find C(v) it takes O(1) time only (check the index) To merge C(v) and C(u): merge the smaller list into the larger one and update the index of the vertices whose list is merged. Thus, merging C(v) and C(u) takes O(min{ C(u), C(v) }) time. Observation: each time we update the reference for a vertex the size of the cluster to which it belongs at least doubles; starts from 1 and goes up to n Thus: number of times we update a vertex s reference is O(log n). Total time for all merges and cluster updates: O(n log n). Time for sorting edges: O(m log m) = O(m log n), time for the while loop: O(m) + O(n log n). Total time for Kruskal s algorithm O((m + n) log n), same as Prim s algorithm.

Week 12: Graph Algorithms Shortest path problems: BFS recall: outputs every s-to-v shortest path s start vertex v reachable vertex from s (residing in a same connected component) shortest # edges running time Θ(n + m) BFS solves the single-source-shortest-path problem on undirected unweighted graphs Single-Source-Shortest-Path (SSSP) problem: given a source s, find out for all vertices their shortest paths from s Variants: single source vs. all pairs graphs: undirected vs. directed edges: unweighted vs. weighted edge weights: non-negative vs. may have negative weights digraphs: acyclic vs. may have di-cycles Note: if there is no path, the distance is set to... SSSP problem on non-negatively weighted digraphs Dijkstra s algorithm (today) 6

Dijkstra s SSSP algorithm: Week 12: Graph Algorithms d[v] weight of the shortest path from source s to v if no such path, set to Idea in Dijkstra s algorithm: greedily grows an SSSP tree ensures that when adding a vertex, its shortest path in the current (induced) subgraph is determined records for every non-tree vertex v its best parent tree vertex p[v] Note: very similar to Prim s MST algorithm (the min-priority queue implementation) Pseudocode (use d[v] as the key): procedure dijkstra(g, w, s) **G = (V, E) for each v V (G) do **initialization d[v] p[v] NIL d[s] 0 Q V (G) while Q do u ExtractMin(Q) **s dequeued first for each v Adj[u] do if d[u] + w(u, v) < d[v] then **update v p[v] u decrease-key(v, d[u] + w(u, v)) **d[v] d[u] + w(u, v) 7

Dijkstra s SSSP algorithm an example: Week 12: Graph Algorithms Input graph G: 2 1 9 1 12 8 1 1 17 10 19 6 18 7 dijkstra(g, 1): 1 9 2 1 12 10 6 7 dijkstra(g, 1) trace: v 1 2 6 7 d[v]/p[v] 0/NIL /NIL /NIL /NIL /NIL /NIL /NIL 1 dequeued 0/NIL 9/1 /NIL /NIL /1 12/1 /NIL dequeued 0/NIL 9/1 11/ /NIL /1 12/1 /NIL 2 dequeued 0/NIL 9/1 10/2 /NIL /1 12/1 19/2 dequeued 0/NIL 9/1 10/2 2/ /1 12/1 19/2 6 dequeued 0/NIL 9/1 10/2 2/ /1 12/1 19/2 7 dequeued 0/NIL 9/1 10/2 2/7 /1 12/1 19/2 8

Dijkstra s SSSP algorithm analysis: Applies to undirected graphs too See the last example :-) Running time: Week 12: Graph Algorithms Same as the running time for Prim s MST algorithm Θ(m log n), assuming adjacency list graph representation and min-priority queue implemented by a heap Correctness: Let S = V Q (while) Loop Invariant: for every v S, d[v] records the weight of the shortest path from s to v in graph G Proof: initialization (S is empty): maintenance: Exercise: fill in the detail termination: S becomes V, so LI implies that for every v, d[v] records the weight of the shortest path from s to v in graph G 9