MST & Shortest Path -Prim s -Djikstra s

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

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

UNIT Name the different ways of representing a graph? a.adjacencymatrix b. Adjacency list

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

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

Introduction Single-source shortest paths All-pairs shortest paths. Shortest paths in graphs

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Minimum Spanning Trees. CSE 373 Data Structures

Shortest Path Algorithms

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

Shortest Paths. CSE 373 Data Structures Lecture 21

Shortest path problems

Lecture 11: Analysis of Algorithms (CS ) 1

Graphs. The ultimate data structure. graphs 1

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

Minimum-Cost Spanning Tree. Example

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

Dijkstra s Algorithm

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

L22-23: Graph Algorithms

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

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

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

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

Discrete Mathematics. Chapter 7. trees Sanguk Noh

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Spanning Tree. Lecture19: Graph III. Minimum Spanning Tree (MSP)

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

Minimum Spanning Trees

CHAPTER 13 GRAPH ALGORITHMS

Lecture 17: Weighted Graphs, Dijkstra s Algorithm. Courtesy of Goodrich, Tamassia and Olga Veksler

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

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

CS302 - Data Structures using C++

Week 12: Minimum Spanning trees and Shortest Paths

Week 11: Minimum Spanning trees

CSC 421: Algorithm Design & Analysis. Spring 2015

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

How to find a minimum spanning tree

CS200: Graphs. Prichard Ch. 14 Rosen Ch. 10. CS200 - Graphs 1

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

Minimum Spanning Trees

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

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

Lecture 18: Implementing Graphs

Description of The Algorithm

Graph Algorithms shortest paths, minimum spanning trees, etc.


CS2210 Data Structures and Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

CS 310 Advanced Data Structures and Algorithms

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

Algorithm Design and Analysis

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

CS/COE

Routing Protocols and the IP Layer

CSE 21: Mathematics for Algorithms and Systems Analysis

CS350: Data Structures Dijkstra s Shortest Path Alg.

Lecture 34 Fall 2018 Wednesday November 28

CMPSC 250 Analysis of Algorithms Spring 2018 Dr. Aravind Mohan Shortest Paths April 16, 2018

Introduction to Algorithms. Lecture 11

Outline. (single-source) shortest path. (all-pairs) shortest path. minimum spanning tree. Dijkstra (Section 4.4) Bellman-Ford (Section 4.

Minimum Spanning Trees My T. UF

looking ahead to see the optimum

Lecture 5: Graph algorithms 1

More Graph Algorithms: Topological Sort and Shortest Distance

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

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

Greedy Approach: Intro

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Graph Algorithms shortest paths, minimum spanning trees, etc.

Routing & Congestion Control

Three Graph Algorithms

Three Graph Algorithms

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

CSS 343 Data Structures, Algorithms, and Discrete Math II. Graphs II. Yusuf Pisan

Single-Source Shortest Paths. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

problem statement Prim s algorithm a multiple key map representation for a tree code for Prim s algorithm

Graphs. The ultimate data structure. graphs 1

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

Parallel Graph Algorithms

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

Topological Sort. Here a topological sort would label A with 1, B and C with 2 and 3, and D with 4.

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

COMP 251 Winter 2017 Online quizzes with answers

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

Minimum Spanning Tree (undirected graph)

Minimum Spanning Trees

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

Section authors: Hamilton Clower, David Scott, Ian Dundore.

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

Parallel Graph Algorithms

Lecture 28: Graphs: Shortest Paths (Part 1)

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

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

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

Minimum Spanning Trees and Shortest Paths

Algorithms and Theory of Computation. Lecture 7: Priority Queue

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Spanning Trees. Lecture 22 CS2110 Spring 2017

Transcription:

MST & Shortest Path -Prim s -Djikstra s

PRIM s - Minimum Spanning Tree - A spanning tree of a graph is a tree that has all the vertices of the graph connected by some edges. - A graph can have one or more number of spanning trees. - If the graph has N vertices then the spanning tree will have N-1 edges.

Pseudocode: MarkedSet = {A}; (0 or starting vertex) UnMarkedSet = {B,C,D}; (1 to N-1 where N is the total number of vertices and 0 is the starting(marked) vertex) while( UnMarkedSet!= empty) { Find edge e = (x,y) such that: 1. x ϵ MarkedSet. y ϵ UnMarkedSet. e has the smallest weight SpanningTree = Spanningtree U {e}; } MarkedSet = MarkedSet U {y}; UnMarkedSet = UnMarkedSet {y};

Here is our Graph 4 B 11 D 4 5 5 8 A 10 C 1

Step 1: Remove all loops 4 B 11 D 4 5 5 8 A 10 C 1

Step 1: Remove all loops B 11 D 4 5 5 A 10 C 1

Step : Remove all parallel edges connecting the same vertices except for the least weighted one. B 11 D 4 5 5 A 10 C 1

Step : Remove all parallel edges connecting the same vertices except for the least weighted one. B 11 D 4 5 5 A 10 C

Step : We are ready to create our MST. Draw a table with rows = columns = number of vertices B 11 D 4 5 5 A 10 C

Step : We are ready to create our MST. Draw a table with rows = columns = number of vertices B 11 D A A B C D 4 5 5 B C A 10 C D

Step 4: Put 0 in cells having the same row and column name B 11 D A 0 A B C D 4 5 5 B 0 C 0 A 10 C D 0

Step 5: Find the edge directly connecting vertex A to other vertices and fill up the table values for cells AB as well as BA B 11 D A B C D A 0 5 4 5 5 B 5 0 C 0 A 10 C D 0

Step 5: Similarly for AC and CA. B 11 D A B C D A 0 5 10 4 5 5 B 5 0 C 10 0 A 10 C D 0

Step 5: The cell AD is put as since no direct path from A to D B 11 D A B C D A 0 5 10 4 5 5 B 5 0 C 10 0 A 10 C D 0

Step 5: Fill other rows similarly B 11 D A B C D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step : Find the smallest value in row A except for 0 and mark it as a minimum edge for both vertices that it connects to. B 11 D A B C D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step : Find the smallest value in row A except for 0 and mark it as a minimum edge for both vertices that it connects to. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step 7: Now looked for the smallest unmarked value in all rows that have a marked edge. In this case, rows A and B that have marked edge 5. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step 7: Smallest unmarked value out of all values in rows A and B is 4 corresponding to BC and CB. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step 7: Mark BC = CB = 4 A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step 7: Now look for the smallest unmarked value in all three rows A,B,C since all now have marked values in them. In this case, CD = 5. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step 7: Mark CD = DC = 5. A B C D B 11 D A 0 5 10 4 5 5 B 5 0 4 11 C 10 4 0 5 A 10 C D 11 5 0

Step 7: We have our final Minimum Spanning Tree. B 11 D 4 5 5 A 10 C

Weight of MST = Sum of marked edges. AB + BC + CD = 5 + 4 + 5 = 14 B 11 D 4 5 5 A 10 C

In-class practice Compute the Minimum Spanning Tree using Prim s algorithm for the following graph:

Answer or

Time Complexity: Minimum edge weight data structure Time complexity (total) adjacency matrix, searching O( V ) binary heap and adjacency list O(( V + E ) log V ) = O( E log V ) Fibonacci heap and adjacency list O( E + V log V )

Dijkstra s Shortest Path: - Given: weighted graph, G, and source vertex, v - Compute: shortest path to every other vertex in G - Path length is sum of edge weights along path. Shortest path has smallest length among all possible paths

Algorithm: Grow a collection of vertices for which shortest path is known - paths contain only vertices in the set - add as new vertex the one with the smallest distance to the source - shortest path to an outside vertex must contain a current shortest path as a prefix Use a greedy algorithm

Vertex Relaxation: Maintain value D[u] for each vertex Each starts at infinity, and decreases as we find out about a shorter path from v to u (D[v] = 0) Maintain priority queue, Q, of vertices to be relaxed use D[u] as key for each vertex remove min vertex from Q, and relax its neighbors Relaxation for each neighbor of u: If D[u] + w(u,z) < D[z] then, D[z] = D[u] + w(u,z)

Pseudocode : ShortestPath(G, v) init D array entries to infinity D[v]=0 add all vertices to priority queue Q while Q not empty do u = Q.removeMin() for each neighbor, z, of u in Q do if D[u] + w(u,z) < D[z] then D[z] = D[u] + w(u,z) Change key of z in Q to D[z] return D as shortest path lengths

Example: a 5 c e 7 5 b d 9 f 1 g

Step 1: Draw a table with the set of vertices (v) a 5 c e 7 5 b d 9 f 1 g

Step 1: Draw a table with the set of vertices (v) a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a

Step : Mark all vertices starting from a. Subscript denotes the vertex we connect from. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a

Step : The lowest weight is marked red (shortest weight determined) and the next lowest weight in the row is looked for a in this case which is the weight from a to b. So, the next row in the table starts with b and a marked red. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a

Step : The lowest weight is marked red (shortest weight determined) and the next lowest weight in the row is looked for a in this case which is the weight from a to b. So, the next row in the table starts with b and a marked red. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a

Step : The next adjacent vertex from b is looked for and compared with the weight when directly reached from a a. When coming via vertex b, the total weight is a->b( a )+b->d( b ) = 5b. This new weight will replace the older weight in the d column. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a

Step : The next adjacent vertex from b is looked for and compared with the weight when directly reached from a a. When coming via vertex b, the total weight is a->b( a )+b->d( b ) = 5b. This new weight will replace the older weight in the d column. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b

Step : Cannot reach anywhere else from b, so rest of the weights c, e, f, g are copied down from the first row as they were a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b

Step : Cannot reach anywhere else from b, so rest of the weights c, e, f, g are copied down from the first row as they were a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b

Step 4: Next lowest value is selected. So any of 5a or 5b will be valid. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b

Step 4: Next lowest unmarked weight is selected. So any of 5a or 5b will be valid. a b 5 c d 9 7 5 f 1 e g - c->d cost: 5a+c = 7c which is!< 5b, so weight in column d remains 5b. - c->e cost = 5a+c = 11c which is <, so new weight in column e becomes 11c - Similarly for f and g, new weights are 8c and 1c respectively. - Remember: Subscript is where I m coming from! V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c

Step 5: Next lowest 5b. d->f = 5b+9d = 14d!< 11c. f and g are unreachable so their weights remain as they are. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c

Step : Next lowest 8c. f->e = 8c+5f = 1f!< 11c. f->g = 8c+1f = 9f <1c a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f

Step 7: No more scope for improvement so the last row gives me the shortest path from a to any of the other vertices. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f

Shortest Path weight from a to g is = 9f, where f=8c, where c = 5a. a 5 c e 7 5 b d 9 f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f

So, the shortest path from a to g would be: a 5 c f 1 g V a b c d e f g a 0 a a b 0 a 5 b c 0 a 5 b 11 c 8 c 1 c d 0 a 5 b 11 c 8 c 1 c f 0 a 5 b 11 c 8 c 9 f

In-class practice Compute the Shortest Path using Dijkstra s algorithm from node 0 to every node in the following graph:

Answer

Djikstra Analysis: O(nlogn) time to build priority queue O(nlogn) time removing vertices from queue O(mlogn) time relaxing edges Changing key can be done in O(logn) time Total time: O( (n + m)logn ) which can be O(n logn) for dense graph

Prim s vs Dijkstra s Prim builds up MST for each adjacent vertex so, will have to go through all previous vertices to get to the desired vertex Dijkstra s builds up the MST for the shortest possible path to any of the vertices from the source vertex which may or may not include all vertices occurring before the destination vertex. Train that connects to several cities would then use Prim s Algorithm. But a train that could go from one city to another in the shortest possible time, would use Dijkstra s.