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.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Graphs: MSTs and Shortest Paths David Kauchak cs161 Summer 2009.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CSE 421 Algorithms Richard Anderson Dijkstra’s algorithm.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Shortest Paths1 C B A E D F
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Dijkstra's algorithm.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph (II) Shortest path, Minimum spanning tree GGuy
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
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.
Minimum weight spanning trees
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.
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Short paths and spanning trees
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Minimum-Cost Spanning Tree
Minimum Spanning Tree.
Autumn 2015 Lecture 11 Minimum Spanning Trees (Part II)
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
Shortest Paths and Minimum Spanning Trees
Minimum Spanning Trees
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
Winter 2019 Lecture 11 Minimum Spanning Trees (Part II)
Winter 2019 Lecture 10 Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Minimum Spanning Trees
Lecture 14 Minimum Spanning Tree (cont’d)
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Topological Sorting Minimum Spanning Trees Shortest Path
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Autumn 2019 Lecture 11 Minimum Spanning Trees (Part II)
Presentation transcript:

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 acyclic subset of edges of G.

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 acyclic subset of edges of G. Def: Given is a weighted (undirected) graph G=(V,E,w) where w:E->Reals defines a weight of every edge in E. A minimum spanning tree of G is a spanning tree with the minimum total weight of edges

Kruskal ( G=(V,E,w) ) 1. Let T= ; 2. Sort the edges in increasing order of weight 3. For edge e do 4. If T [ e does not contain a cycle then 5. Add e to T 6. Return T Minimum spanning trees (MST) - Kruskal

Kruskal ( G=(V,E,w) ) 1. Let T= ; 2. Sort the edges in increasing order of weight 3. For edge e do 4. If T [ e does not contain a cycle then 5. Add e to T 6. Return T Minimum spanning trees (MST) - Kruskal Lemma: Algo is correct.

Kruskal ( G=(V,E,w) ) 1. Let T= ; 2. Sort the edges in increasing order of weight 3. For edge e do 4. If T [ e does not contain a cycle then 5. Add e to T 6. Return T Minimum spanning trees (MST) - Kruskal Implementation?

Kruskal ( G=(V,E,w) ) 1. Let T= ; 2. Sort the edges in increasing order of weight 3. For edge e do 4. If T [ e does not contain a cycle then 5. Add e to T 6. Return T Minimum spanning trees (MST) - Kruskal Implementation?

Init (V) 1. for every vertex v do 2. boss[v]=v 3. size[v]=1 4. set[v]={v} Union (u,v) 1. if size[boss[u]]>size[boss[v]] then 2. set[boss[u]]=set[boss[u]] union set[boss[v]] 3. size[boss[u]]+=size[boss[v]] 4. for every z in set[boss[v]] do 5. boss[z]=boss[u] 6. else do steps with u,v switched Minimum spanning trees (MST) - Kruskal Implementation? - Union-Find datastructure

Union (u,v) 1. if size[boss[u]]>size[boss[v]] then 2. set[boss[u]]=set[boss[u]] union set[boss[v]] 3. size[boss[u]]+=size[boss[v]] 4. for every z in set[boss[v]] do 5. boss[z]=boss[u] 6. else do steps with u,v switched Minimum spanning trees (MST) - Kruskal Analysis of Union-Find Lemma: k Unions take O(k log k) time

Minimum spanning trees (MST) - Kruskal Analysis of Union-Find Lemma: k Unions take O(k log k) time Corollary: The running time of Kruskal is: O(|E| log |E|) + O(|V| log |V|)

Prim ( G=(V,E,w) ) 1. Let T= ;, H= ; 2. For every vertex v do 3. cost[v]= 1, parent[v]=null 4. Let u be a vertex 5. Update (u) 6. For i=1 to n-1 do 7. u=vertex from H of smallest cost (remove) Add (u,parent[u]) to T Update(u) Return T Minimum spanning trees (MST) - Prim Update (u) 1. For every neighbor v of u 2. If cost[v]>w(u,v) then 3. cost[v]=w(u,v), parent[v]=u 4. If v not in H then 5. Add v to H

Minimum spanning trees (MST) - Prim Lemma: Prim is correct. Running time:

Single source shortest paths - Dijkstra G=(V,E,w) and a vertex s (w non-negative) shortest paths from s to every other vertex Input: Output: Can use similar idea to Prim?

Dijkstra ( G=(V,E,w), s ) 1. Let H= ; 2. For every vertex v do 3. dist[v]= 1 4. dist[s]=0 5. Update (s) 6. For i=1 to n-1 do 7. u=extract vertex from H of smallest cost 8. Update(u) Return dist[] Update (u) 1. For every neighbor v of u 2. If dist[v]>dist[u]+w(u,v) then 3. dist[v]=dist[u]+w(u,v) 4. If v not in H then 5. Add v to H Single source shortest paths - Dijkstra

Single source shortest paths - Dijkstra Lemma: Dijkstra is correct. Running time:

All pairs shortest paths – Floyd-Warshall G=(V,E,w), w non-negative shortest paths between all pairs of vertices Input: Output:

All pairs shortest paths – Floyd-Warshall Idea 1: Use Dijkstra from every vertex G=(V,E,w), w non-negative shortest paths between all pairs of vertices Input: Output:

All pairs shortest paths – Floyd-Warshall Idea 1: Use Dijkstra from every vertex Idea 2: How about dynamic programming? G=(V,E,w), w non-negative shortest paths between all pairs of vertices Input: Output:

All pairs shortest paths – Floyd-Warshall the length of the shortest path from i to j using only vertices · k Heart of the algorithm: S[i,j,k] =

All pairs shortest paths – Floyd-Warshall Heart of the algorithm: S[i,j,k] = How to compute S[i,j,k] ? the length of the shortest path from i to j using only vertices · k

Floyd-Warshall ( G=(V,E,w) ) 1. For i=1 to |V| do 2. For j=1 to |V| do 3. S[i,j,0] = w(i,j) 4. For k=1 to |V| do 5. For i=1 to |V| do 6. For j=1 to |V| do 7. S[i,j,k] = min { 8. S[i,j,k-1], 9. S[i,k,k-1]+S[k,j,k-1] } 10.Return ? All pairs shortest paths – Floyd-Warshall S[i,j,k] = w(i,j) if k = 0 min { S[i,j,k-1], S[i,k,k-1] + S[k,j,k-1] } if k > 0

Input: directed G=(V,E,w) and a vertex s Output: FALSE if exists reachable negative-weight cycle, distance to every vertex, otherwise Single source shortest paths – Bellman-Ford 4

Bellman-Ford ( G=(V,E,w), s ) 1. For every vertex v 2. d[v] = 1 3. d[s]=0 4. For i=1 to |V|-1 do 5. For every edge (u,v) in E do 6. If d[v]>d[u]+w(u,v) then 7. d[v]=d[u]+w(u,v) 8. For every edge (u,v) in E do 9. If d[v]>d[u]+w(u,v) then 10. Return NEGATIVE CYCLE 11.Return d[] Single source shortest paths – Bellman-Ford 4

Bellman-Ford ( G=(V,E,w), s ) 1. For every vertex v 2. d[v] = 1 3. d[s]=0 4. For i=1 to |V|-1 do 5. For every edge (u,v) in E do 6. If d[v]>d[u]+w(u,v) then 7. d[v]=d[u]+w(u,v) 8. For every edge (u,v) in E do 9. If d[v]>d[u]+w(u,v) then 10. Return NEGATIVE CYCLE 11.Return d[] Single source shortest paths – Bellman-Ford Lemma: Bellman-Ford is correct. Running time: 4