1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.

Slides:



Advertisements
Similar presentations
Greed is good. (Some of the time)
Advertisements

1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Shortest Paths1 C B A E D F
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Shortest Paths1 C B A E D F
© 2004 Goodrich, Tamassia Shortest Paths1 C B A E D F
Parallel Programming – Graph Algorithms David Monismith CS599 Notes are primarily based upon Introduction to Parallel Programming, Second Edition by Grama,
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Weighted Graphs In a weighted graph, each edge has an associated numerical value, called the weight of the edge Edge weights may represent, distances,
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Graph Algorithms Shortest path problems. Graph Algorithms Shortest path problems.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program,
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Shortest Paths C B A E D F
SPANNING TREES Lecture 21 CS2110 – Spring
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Lecture 16. Shortest Path Algorithms
Minimum weight spanning trees
Shortest Path Graph Theory Basics Anil Kishore.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
CSC 213 – Large Scale Programming. Today’s Goals  Discuss what is meant by weighted graphs  Where weights placed within Graph  How to use Graph ’s.
Weighted Graphs Computing 2 COMP s1 Sedgewick Part 5: Chapter
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum- Spanning Trees
1 Weighted Graphs. 2 Outline (single-source) shortest path –Dijkstra (Section 4.4) –Bellman-Ford (Section 4.6) (all-pairs) shortest path –Floyd-Warshall.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
© 2010 Goodrich, Tamassia Shortest Paths1 C B A E D F
Shortest Paths C B A E D F Shortest Paths
Minimum Spanning Trees
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Greedy Technique.
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Short paths and spanning trees
Shortest Paths C B A E D F
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Shortest Paths C B A E D F Shortest Paths
Shortest Paths C B A E D F Shortest Paths
Shortest Paths.
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Minimum-Cost Spanning Tree
Presentation transcript:

1 Prim’s algorithm

2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum of the weights on the edges, that is w(T) = Σ e є T w(e) We want a spanning tree of minimum cost

3 Prim’s algorithm The edges in the set A always forms a single tree The tree starts from an arbitrary vertex and grows until the tree spans all the vertices in V At each step, a light edge is added to the tree A that connects A to an isolated vertex of G A =(V, A) “Greedy” because the tree is augmented at each step with an edge that contributes the minimum amount possible to the tree’s weight

4 Prim’s vs. Dijkstra’s Prim’s strategy similar to Dijkstra’s Grows the MST T one edge at a time Cloud covering the portion of T already computed Label D[u] associated with each vertex u outside the cloud (distance to the cloud)

5 Prim’s algorithm For any vertex u, D[u] represents the weight of the current best edge for joining u to the rest of the tree in the cloud (as opposed to the total sum of edge weights on a path from start vertex to u) Use a priority queue Q whose keys are D labels, and whose elements are vertex-edge pairs

6 Prim’s algorithm Any vertex v can be the starting vertex We still initialize D[v]=0 and all the D[u] values to +∞ We can reuse code from Dijkstra’s, just change a few things

7 Prim’s algorithm, Example

8

9

10 Prim’s algorithm, Example

11 Prim’s algorithm, Example

12 Prim’s algorithm, Example

13 Prim’s algorithm, Example

14 Prim’s algorithm, Example

15 Another Example

16 Example

17 Example

18 Example

19 Example

20 Pseudo Code

21 Time complexity Initializing the queue takes O(n log n) [binary heap] Each iteration of the while, we spend O(log n) time to remove vertex u from Q and O(deg(u) log n) to perform the relaxation step Overall, O(n log n + Σ v (deg(v) log n)) which is O((n+m) log n) [if using a binary heap]

22 Summary Time complexityNotes DijkstraO((n+m) log n) using b.q. O(n 2 +m) using array Non-negative weights Bellman-FordO(m n) Negative weights ok DAGsO(n+m) Only for acyclic graphs All-pairsO(n 3 ) Negative weights ok KruskalO((n+m) log n) using p.q. Prim-JarnikO((n+m) log n) using p.q.

23 Reading Assignment Dasgupta –single-source shortest path (4.4, 4.6 and 4.7) –all-pairs shortest path (6.6) –minimum spanning tree (5.1.1 – 5.1.3, 5.1.5)

24 Revisiting Floyd-Warshall’s algorithm

25 All-pairs shortest path We want to compute the shortest path distance between every pair of vertices in a directed graph G (n vertices, m edges) We want to know D[i,j] for all i,j, where D[i,j]=shortest distance from v i to v j

26 All-pairs shortest path If G has no negative-weight edges, How can we solve the all-pair shortest path problem?

27 All-pairs shortest path If G has no negative-weight edges, we could use Dijkstra’s algorithm repeatedly from each vertex It would take O(n (m+n) log n) time, that is O(n 2 log n + nm log n) time, which could be as large as O(n 3 log n)

28 All-pairs shortest path If G has negative-weight edges (but no negative-weight cycles) How can we solve the all-pair shortest path problem?

29 All-pairs shortest path If G has negative-weight edges (but no negative-weight cycles) we could use Bellman-Ford’s algorithm repeatedly from each vertex Recall that Bellman-Ford’s algorithm runs in O(nm) It would take O(n 2 m) time, which could be as large O(n 4 ) time

30 All-pairs shortest path We will see an algorithm to solve the all-pairs shortest path in O(n 3 ) time The graph can contain negative-weight edges (but no negative-weight cycles)

Floyd-Warshall Algorithm - Idea D i,j (k) – the shortest path from v i to v j containing only vertices v 1,..., v k D i,j (0) = w((v i, v j )) D i,j (k) = w((v i, v j ))if k = 0 min{D i,j (k-1), D i,k (k-1) + D k,j (k-1) }if k > 0

32 D i,j (k) – the shortest path from v i to v j containing only vertices v 1,..., v k

33 D i,j (k) – the shortest path from v i to v j containing only vertices v 1,..., v k

34 D i,j (k) – the shortest path from v i to v j containing only vertices v 1,..., v k

35 D i,j (k) – the shortest path from v i to v j containing only vertices v 1,..., v k