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

Similar documents
Weighted Graphs and Greedy Algorithms

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Week 12: Minimum Spanning trees and Shortest Paths

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

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

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

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

Week 11: Minimum Spanning trees

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

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

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

Lecture 5: Graph algorithms 1

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

Single Source Shortest Path (SSSP) Problem

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

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

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

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

(Refer Slide Time: 00:18)

CS 310 Advanced Data Structures and Algorithms

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

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Greedy algorithms is another useful way for solving optimization problems.

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

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

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

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

23.2 Minimum Spanning Trees

Minimum Spanning Trees and Prim s Algorithm

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

Shortest path problems

tree follows. Game Trees

Lecture Summary CSC 263H. August 5, 2016

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

Algorithms for Minimum Spanning Trees

Solutions to relevant spring 2000 exam problems

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

MC 302 GRAPH THEORY 10/1/13 Solutions to HW #2 50 points + 6 XC points

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

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

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

Complexity of Prim s Algorithm

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Shortest Path Algorithms

SHORTEST PATHS. Weighted Digraphs. Shortest paths. Shortest Paths BOS ORD JFK SFO LAX DFW MIA

Depth First Search A B C D E F G A B C 5 D E F 3 2 G 2 3

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

CS521 \ Notes for the Final Exam

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

1 Dijkstra s Algorithm

CHAPTER 13 GRAPH ALGORITHMS

Lecture 4: Graph Algorithms

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

GIAN Course on Distributed Network Algorithms. Spanning Tree Constructions

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

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

Parallel Breadth First Search

Minimum-Cost Spanning Tree. Example

CSE 100: GRAPH ALGORITHMS

Graphs - II. Announcements. Where did I leave that book? Where did I leave that book? Where did I leave that book? CS 2110, Fall 2016

Homework Assignment 3

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

Lecture 11: Analysis of Algorithms (CS ) 1

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

CMSC351 - Fall 2014, Homework #6

CS4800: Algorithms & Data Jonathan Ullman

CS 170 Second Midterm ANSWERS 7 April NAME (1 pt): SID (1 pt): TA (1 pt): Name of Neighbor to your left (1 pt):

Data Structures Brett Bernstein

Theorem 2.9: nearest addition algorithm

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

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

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

Algorithms and Data Structures

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

Minimum Spanning Trees

Minimum Spanning Trees My T. UF

Minimum spanning trees

GIAN Course on Distributed Network Algorithms. Spanning Tree Constructions

6.1 Minimum Spanning Trees

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

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

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

Analysis of Algorithms, I

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

Treewidth and graph minors

Minimum Spanning Trees

Introduction: (Edge-)Weighted Graph

Lecture 10 Graph algorithms: testing graph properties

Lecture 25 Spanning Trees

CSC 421: Algorithm Design & Analysis. Spring 2015

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

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Transcription:

Prim and Dijkstra: Efficiency and Correctness Luay Nakhleh 1 Prim s Algorithm In class we saw Prim s algorithm for computing a minimum spanning tree (MST) of a weighted, undirected graph g. The pseudo-code is given in Algorithm 1. Algorithm 1: Prim Input: Weighted, undirected graph g = (V, E, w). 1 V T {x}; // x is an arbitrary node in V 3 for i 1 to V 1 do 4 Let e = {u, v} be a minimum-weight edge such that u V T and v (V \ V T ); 5 V T V T {v}; 6 E T E T {e}; 7 return E T ; 1.1 Running Time: Without Using a Min-heap Let us first assume that g is given by its adjacency matrix (with value in entry [i, j] if there is no edge between i and j). We show two different running-time analyses. We denote by n and m the numbers of nodes and edges, respectively, in g. Clearly, Lines 1, 2, 5, 6, and 7 are all O(1) operations, and the loop on Line 3 iterates n 1 = O(n) times. So, the two analyses will differ in terms of how Line 4 is implemented. The most naive implementation of Line 4. Go through every node u V T, and for each such node, go through all neighbors of u, and finally return as v the node that is not in V T and that has the lightest edge connecting it to one of the u s in V T. Since the graph is given by its adjacency matrix, going through the neighbors of a node takes n 1 = O(n) time. Furthermore, since V T = O(n) is the worst case, we have the Line 4 takes O(n 2 ) time, for a total running time of O(n 3 ) for the algorithm. A better implementation of Line 4. Assume the nodes in V as numbered {0, 1, 2,..., n 1}. Then, Prim s algorithm could be rewritten as in Algorithm 2. In this algorithm, we maintain a list S of size V \ V T such an element in S is a pair (j, q, k) where j is a node in V \ V T and q is the smallest weight of an edge that connects node j to a node in V T, and k is the node in V T that has an edge {k, j} of weight q. Line 5 in Algorithm 2 finds the smallest-weight edge that connects a node j (V \ V T ) to a node k V T. This node and its associated edge are added to V T and E T on Lines 6 and 7, respectively. The loop at Line 9 checks for every node a (V \ V T ) if its smallest-weight connecting to nodes in V T is affected by the addition of node j to V T ; if it is, then the nodes that a connects to, along with the weight of the corresponding edge, are updated on Line 11. In this new algorithm, Lines 1, 2, 6, 7, 8, 10, 11, and 12 takes O(1) operations each. Lines 3 and 5 take O(n) operations each, in the worst case. The loop on Line 4 iterates O(n) times, and the loop on Line 9 iterates O(n) times in the worst case. Therefore, with this implementation of Prim s algorithm, the algorithm takes O(n 2 ) time. 1

Algorithm 2: Prim Input: Weighted, undirected graph g = (V, E, w) with V = {0, 1,..., n 1}. 1 V T {0}; 3 Let S be a list of size n 1 initialized to have an element (j, w({0, j}), 0) for every j (V \ V T ); 4 for i 1 to V 1 do 5 Let (j, q, k) be an element with the smallest value q of all elements in S; 6 V T V T {j}; 7 E T E T {{j, k}}; 8 Remove (j, q, k) from S; 9 foreach (a, b, c) S do 10 if w({j, a}) < b then 11 (a, b, c) (a, w({j, a}), j); 12 return E T ; These two analyses illustrate important aspects of pseudo-code and running-time analysis. While Algorithm 2 gives more implementation details than Algorithm 1, it does not affect one s understanding of what the algorithm does. It only changes the details of how it does it. So, the pseudo-code gives sufficient details so that one understand what it does. When it comes to analyzing the running time, how an algorithm performs a certain step could impact the running time. A standard textbook description of Prim s algorithm is what is given in Algorithm 1. The extra details given in Algorithm 2 would be found in the analysis of the running time. 1.2 Running Time: Using a Min-heap Assume now that the graph g is given by its adjacency list and that the list S in Algorithm 2 is maintained as a min-heap based on the second value; that is, for the elements (a, b, c) in S, the value b is the key that determines the parental dominance property of the heap. The pseudo-code, with minor changes, is given in Algorithm 3. With this new implementation, Line 3 takes O(n) time (the time to construct a heap), Line 5 takes O(log n) (the time to remove the minimum element), and Lines 11 and 12 take O(log n) each (removing and adding an element to the heap). Lines 4, 9, 10, 11, and 12 take a combined O(m log n) amount of time (again, think about how many times each edge in the graph g will be visited). Lines 4 and 5 take a combined O(n log n) amount of time. Therefore, the running time of Algorithm 3 is O(n+(m+n) log n) = O((m+n) log n) = O(m log n) for a connected graph (in a connected graph, n = O(m)). The reason I focus on connected graphs is because there is no spanning tree of a disconnected graph. So, in the case of Prim s algorithm, graphs with fewer than n 1 edges are not of interest (they d be disconnected). 1.3 Correctness We now prove that Prim s algorithm is correct. That is, we prove that given a weighted undirected graph g = (V, E, w), Prim s algorithm as given in Algorithm 1 returns an MST of g. We prove this by mathematical induction. Notice that the for-loop on Line 3 in Algorithm 1 iterates n 1 times, where n = V, each time growing a spanning tree T of g by adding one edge to it. Let us denote by T 0 the spanning tree that the algorithm starts with before entering the for-loop. That is, the tree T 0 consists of only node 0 and no edges. Let us now denote by T i, for 1 i n 1 the tree obtained by adding to T i 1 the minimum-weight edge e identified on Line 4 in the i-th iteration of the algorithm. Obviously, tree T n 1 is the tree that Prim s algorithm returns. We now prove, by induction, that T i, for 0 i n 1, is a subtree of an MST of g. Base case: T 0 is part of an MST since T 0 consists of only node 0 and no edges. In fact, every MST of g must contain T 0 as a subtree. Inductive step: Assume that T i 1 is a subtree of an MST T of g (this is the inductive hypothesis) and let us prove that T i is a subtree of an MST of g. We prove this by contraction. Assume T i 1 is a subtree of an MST of g but that T i is not a subtree of any MST of g. Let e i = (u, v) be the minimum-weight edge that Prim s 2

Algorithm 3: Prim Input: Weighted, undirected graph g = (V, E, w) with V = {0, 1,..., n 1}. 1 V T {0}; 3 Let H be a heap of the n 1 elements (j, w({0, j}), 0) for every j (V \ V T ); 4 for i 1 to V 1 do 5 Let (j, q, k) be an element with the smallest value q of all elements in H; 6 V T V T {j}; 7 E T E T {{j, k}}; 8 Remove (j, q, k) from H; 9 foreach (a, b, c) H do 10 if w({j, a}) < b then 11 Remove (a, b, c) from H; 12 Add (a, w({j, a}), j) to H; 13 return E T ; algorithm identifies on Line 4 in the i-th iteration of the for-loop to add to T i 1 (see Fig. 1). Since we assume T i 1 is part of an MST of g but T i is not, this means that edge e i cannot be part of any MST of g. In other words, the presence of e i creating a cycle. Since T i 1 is a tree, and since e i connects a node in T i 1 (a blue node in Fig. 1) and a node not in T i 1 (a red node in Fig. 1), then for e i to create a cycle, there must be another edge e = (u, v ) in the MST, with u being a node in T i 1 and v being a node not in T i 1. If we delete edge e, then we break the cycle and have a spanning tree whose weight is at most the weight of MST T, since w(e i ) w(e ) (Line 4 in Prim s Algorithm). Therefore, the spanning tree created by removing edge e from MST T and replacing it by edge e i is also an MST, contradicting the assumption that T i is not a subtree of any MST. Therefore, T i is part of an MST of g. By induction, it follows that T n 1 is an MST of g. u e i v T i-1 u e v Figure 1: Correctness of Prim s Algorithm. T i 1 is the tree that Prim s Algorithm has built by the end of iteration (i 1) of the for-loop on Line 3 in Algorithm 1. Edge e i is the minimum-weight edge it identifies on Line 4 to add to T i 1 in order to create tree T i. 2 Dijkstra s Algorithm In class we saw Dijkstra s algorithm for computing shortest paths and their lengths from a source node to all other nodes in a weighted directed graph, where all edge weights are nonnegative. The pseudo-code is given in Algorithm 4. 3

Algorithm 4: Dijkstra. Input: Undirected, weighted graph g = (V, E, w) where are weights are nonnegative; source node i. Output: Shortest paths, as well as their lengths, from i to every other node in g. 1 X ; 2 foreach j V do 3 d j ; // d j is the geodesic distance between i and j 4 p j null ; // p j carries the node label of j s parent 5 X X {j} ; // Set X contains all nodes not visited yet 6 d i 0; 7 while X do 8 Let k be a node with the minimum value of d k in the set X; 9 if d k = then 10 break; 11 X X \ {k}; 12 foreach neighbor h of k in the set X do 13 if d k + w((k, h)) < d h then 14 d h d k + w((k, h)); 15 p h k; 16 return p, d; 2.1 Running Time: Using a Min-heap Let us assume that input graph g is represented by its adjacency list, and let n and m be the numbers of nodes and edges, respectively, of g. Lines 1 and 6 take O(1) time, and Lines 2 5 take O(n) time to initialize d and p for all nodes. Let us assume that the d k values are stored in a min-heap so that finding a node k with the minimum d k on Line 8 takes O(1) time and that removing that value from the heap and updating it takes O(log n) time. Analyzing Lines 7 15 carefully reveals two main components that determine the running time: Finding node k on Line 8 and removing it from the min-heap once k is removed from X on Line 11. This takes O(log n) in the worst case, and it is executed for every node (until X is empty) for a total of O(n log n). Line 14 results in an update to d h. Such an update might require updating the min-heap with respect to value d h, which costs O(log n) in the worst case. Similar to our analysis of BFS, the update on Line 14 happens O(m) times since the loop on Line 12 iterates for a total of m times. Therefore, in the worst case, Lines 7 15 take O(m log n) time. Therefore, the running time of Dijkstra s algorithm when g is represented by its adjacency list and a min-heap is used to store d, is O((m + n) log n). Since the algorithm is usually run on connected graphs (where m n), the running time is O(m log n). 2.2 Correctness Notice that Dijkstra s algorithm builds a tree of paths, where the nodes of the tree are the nodes of the graph (assuming all nodes are reachable from source node i) and the edges are given by the p values. Just as in proving the correctness of Prim s Algorithm, let us denote by T j, 1 j n, the tree with the first j nodes that Dijkstra s algorithm has finished processing (computing d and p thereof) after iteration j of the loop on Line 7. We demonstrate the correctness Dijkstra s algorithm by proving that T j contains j closest nodes to source node i (including node i itself). We prove this result by induction on the number of nodes in T j. Base case: For j = 1, the only node in T 1 is the source nodes i, and it is closest node to the source node, that is, itself. Inductive step: Assume T j contains j closest nodes to source node i (the inductive hypothesis). Let T j+1 be the tree formed by adding node v j+1 to tree T j. All the nodes that fall on shortest paths from i to v j+1 must be 4

already in T j since they are closer to i than v j+1 is. Hence, the (j + 1)-st closest node to i is a node h that is not in T j and that minimizes w((q, h)) + d q over all nodes q in T j. But this is precisely what Dijkstra s algorithm does. By induction, tree T n contains n closest nodes to source node i. 5