CSE 5311 Notes 8: Minimum Spanning Trees

Similar documents
CSE 5311 Notes 10: Medians/Selection

Introduction to Algorithms

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Minimum Spanning Trees My T. UF

Introduction to Algorithms

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

(Dijkstra s Algorithm) Consider the following positively weighted undirected graph for the problems below: 8 7 b c d h g f

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

) $ f ( n) " %( g( n)

1. To reduce the probability of having any collisions to < 0.5 when hashing n keys, the table should have at least this number of elements.

n 2 ( ) ( ) Ο f ( n) ( ) Ω B. n logn Ο

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

( ). Which of ( ) ( ) " #& ( ) " # g( n) ( ) " # f ( n) Test 1

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

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

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

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

( D. Θ n. ( ) f n ( ) D. Ο%

9. The expected time for insertion sort for n keys is in which set? (All n! input permutations are equally likely.)

Week 11: Minimum Spanning trees

Introduction to Algorithms

n 2 ( ) ( ) + n is in Θ n logn

Minimum Spanning Trees

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

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

Minimum Spanning Trees

Shortest path problems

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

Review of course COMP-251B winter 2010

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

Graph Algorithms (part 3 of CSC 282),

CS 310 Advanced Data Structures and Algorithms

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

Multiple Choice. Write your answer to the LEFT of each problem. 3 points each

Algorithms for Minimum Spanning Trees

Introduction: (Edge-)Weighted Graph

DESIGN AND ANALYSIS OF ALGORITHMS GREEDY METHOD

( ) 1 B. 1. Suppose f x

CSci 231 Final Review

( ) n 3. n 2 ( ) D. Ο

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

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

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

Introduction to Parallel & Distributed Computing Parallel Graph Algorithms

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Lecture 4: Graph Algorithms

9. Which situation is true regarding a cascading cut that produces c trees for a Fibonacci heap?

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

Lecture 11: Analysis of Algorithms (CS ) 1

CS4800: Algorithms & Data Jonathan Ullman

Test 1 Last 4 Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. 2 points each t 1

2 A Template for Minimum Spanning Tree Algorithms

Lecture 6 Basic Graph Algorithms

Problem Score Maximum MC 34 (25/17) = 50 Total 100

Shortest Paths. Nishant Mehta Lectures 10 and 11

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

Minimum-Cost Spanning Tree. Example

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

Shortest Paths. Nishant Mehta Lectures 10 and 11

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

Analysis of Algorithms, I

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

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

COS 226 Lecture 19: Minimal Spanning Trees (MSTs) PFS BFS and DFS examples. Classic algorithms for solving a natural problem

Dijkstra s Algorithm Last time we saw two methods to solve the all-pairs shortest path problem: Min-plus matrix powering in O(n 3 log n) time and the

Module 5 Graph Algorithms

Minimum Spanning Trees Ch 23 Traversing graphs

Data Structures Brett Bernstein

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

COMP 251 Winter 2017 Online quizzes with answers

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

Introduction to Algorithms. Lecture 11

Algorithm Design (8) Graph Algorithms 1/2

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

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

Minimum Spanning Trees

CS521 \ Notes for the Final Exam

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

CSE 100: GRAPH ALGORITHMS


CHAPTER 23. Minimum Spanning Trees

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

Design and Analysis of Algorithms - - Assessment

Chapter 9 Graph Algorithms

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

Graph Algorithms (part 3 of CSC 282),

Exam 3 Practice Problems

CISC 320 Midterm Exam

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

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

Course Review for Finals. Cpt S 223 Fall 2008

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

looking ahead to see the optimum

Algorithms. All-Pairs Shortest Paths. Dong Kyue Kim Hanyang University

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

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

Transcription:

CSE Notes 8: Minimum Spanning Trees (Last updated /8/7 :0 PM) CLRS, Chapter CONCEPTS Given a weighted, connected, undirected graph, find a minimum (total) weight free tree connecting the vertices. (AKA bottleneck shortest path tree) Cut Property: Suppose S and T partition V such that. S T =. S T = V. S > 0 and T > 0 then there is some MST that includes a minimum weight edge s, t with s S and t T. Proof: Suppose there is a partition with a minimum weight edge s, t. A spanning tree without s, t must still have a path between s and t. Since s S and t T, there must be at least one edge x, y on this path with x S and y T. By removing x, y and including s, t, a spanning tree whose total weight is no larger is obtained. T x y S s t Cycle Property: Suppose a given spanning tree does not include the edge u, v. If the weight of u, v is no larger than the weight of an edge x, y on the unique spanning tree path between u and v, then replacing x, y with u, v yields a spanning tree whose weight does not exceed that of the original spanning tree. Proof: Including u, v into the spanning tree introduces a cycle, but removing x, y will remove the cycle to yield a modified tree whose weight is no larger.

Does not directly suggest an algorithm, but all algorithms avoid including an edge that violates. Prove or give counterexample: The MST path beween two vertices is a shortest path. True or False? Choosing the V - edges with smallest weights gives a MST. Fill in the blank: Multiple MSTs occur only if. MODIFIED REACHABILITY CONDITION Towards an algorithm:. Assume unique edge weights (easily forced by lexicographically breaking ties).. Consider all (cycle-free) paths between some pair of vertices.. Consider the maximum weight edge on each path.. The edge that is the minimum of the maximums must be included in the MST. Any edge that is never a must is not in the MST. Example: A D 6 7 B C E 8 F Original Adjacency Matrix Unchanged Entries ( ) are MST A B C D E F A B C D E F A A 7 B B 7 C C 7 D 6 7 D 7 E 6 8 E 7 F 7 8 F 7 7 7 7 7 7

Implementation - Based on Warshall s Algorithm. Directed reachability - existence of path: for (j=0; j<v; j++) for (i=0; i<v; i++) if (A[i][j]) for (k=0; k<v; k++) if (A[j][k]) A[i][k]=; j k j i j i k Correctness proof is by math. induction. (CSE 0 Notes 6.C) Successor Matrix (CLRS uses predecessor) 7- directions: 00 0 7 00 s[00][00]=0 s[0][00]=7 s[7][00]=00 Initialize: x y s[x][y]=y Warshall Matrix Update: (- otherwise) j B A i? k s[i][j] = A s[j][k] = B s[i][k] =?

for (j=0; j<v; j++) for (i=0; i<v; i++) if (s[i][j]!= (-)) for (k=0; k<v; k++) if (s[i][k] == (-) && s[j][k]!= (-)) s[i][k] = s[i][j];. All-pairs shortest paths - Floyd-Warshall for (j=0; j<v; j++) for (i=0; i<v; i++) if (dist[i][j] < 999) for (k=0; k<v; k++) newdist = dist[i][j] + dist[j][k]; if (newdist < dist[i][k]) dist[i][k] = newdist; s[i][k] = s[i][j];. Minimum spanning tree: http://ranger.uta.edu/~weems/notes/mstwarshall.c // MST based on Warshall's algorithm (Maggs & Plotkin, // Information Processing Letters 6, Jan 988, 9-9) // /6/0 BPW // Modified 7//0 to make more robust, especially edges with same weight #include <stdio.h> #define maxsize (0) struct edge int weight,smalllabel,largelabel; ; typedef struct edge edgetype; edgetype min(edgetype x,edgetype y) // Returns smaller-weighted edge, using lexicographic tie-breaker if (x.weight<y.weight) return x; if (x.weight>y.weight) return y; if (x.smalllabel<y.smalllabel) return x; if (x.smalllabel>y.smalllabel) return y; if (x.largelabel<y.largelabel) return x; return y;

edgetype max(edgetype x,edgetype y) // Returns larger-weighted edge, using lexicographic tie-breaker if (x.weight>y.weight) return x; if (x.weight<y.weight) return y; if (x.smalllabel>y.smalllabel) return x; if (x.smalllabel<y.smalllabel) return y; if (x.largelabel>y.largelabel) return x; return y; main() int numvertices,numedges, i, j, k; edgetype matrix[maxsize][maxsize]; int count; printf("enter # of vertices and edges: "); fflush(stdout); scanf("%d %d",&numvertices,&numedges); printf("enter undirected edges u v weight\n"); for (j=0;j<numvertices;j++) matrix[i][j].weight=999; if (i<=j) matrix[i][j].smalllabel=i; matrix[i][j].largelabel=j; else matrix[i][j].smalllabel=j; matrix[i][j].largelabel=i; for (k=0;k<numedges;k++) scanf("%d %d",&i,&j); scanf("%d",&matrix[i][j].weight); matrix[j][i].weight=matrix[i][j].weight; printf("input matrix\n"); for (j=0;j<numvertices;j++) printf("%d ",matrix[i][j].weight); printf("\n");

// MST by Warshall for (k=0;k<numvertices;k++) for (j=0;j<numvertices;j++) matrix[i][j]=min(matrix[i][j],max(matrix[i][k],matrix[k][j])); 6 printf("output matrix\n"); for (j=0;j<numvertices;j++) printf("%d(%d,%d) ",matrix[i][j].weight,matrix[i][j].smalllabel, matrix[i][j].largelabel); printf("\n"); count=0; for (j=i+;j<numvertices;j++) if (matrix[i][j].weight<999 && i==matrix[i][j].smalllabel && j==matrix[i][j].largelabel) count++; printf("%d %d %d\n",i,j,matrix[i][j].weight); if (count<numvertices-) printf("result is a spanning forest\n"); else if (count>=numvertices) printf("error?... \n"); PRIM S ALGORITHM Outline:. Vertex set S: tree that grows to MST.. T = V - S: vertices not yet in tree.. Initialize S with arbitrary vertex.. Each step moves one vertex from T to S: the one with the minimum weight edge to an S vertex. Different data structures lead to various performance characteristics.

Which edge does Prim s algorithm select next? 7 A E 0 I D 6 7 6 8 H 7 J B 0 C F G 9 8 9 K. Maintains T-table that provides the closest vertex in S for each vertex in T. Scans the list of the last vertex moved from T to S. Place any vertex x V in S. T = V x for each t T Initialize T-table entry with weight of t, x (or if non-existent) and x as best-s-neighbor. while T Scan T-table entries for the minimum weight edge t, best-s-neighbor[t] over all t T and all s S. Include edge t, best-s-neighbor[t] in MST. T = T t S = S t for each vertex x in adjacency list of t if x T and weight of x, t is smaller than T-weight[x] T-weight[x] = weight of x, t best-s-neighbor[x] = t What are the T-table contents before and after the next MST vertex is selected? A E 0 I D 6 7 6 8 H 7 J B 0 C F G 9 8 9 K

Analysis: 8 Initializing the T-table takes Θ(V). Scans of T-table entries contribute Θ(V ). Traversals of adjacency lists contribute Θ(E). Θ(V + E) overall worst-case.. Replace T-table by a heap. The time for updating for best-s-neighbor increases, but the time for selection of the next vertex to move from T to S improves. Place any vertex x V in S. T = V x for each t T Load T-heap entry with weight (as the priority) of t, x (or if non-existent) and x as best-s-neighbor BUILD-MIN-HEAP(T-heap) while T Use HEAP-EXTRACT-MIN to obtain T-heap entry with the minimum weight edge over all t T and all s S. Include edge t, best-s-neighbor[t] in MST. T = T t S = S t for each vertex x in adjacency list of t if x T and weight of x, t is smaller than T-weight[x] T-weight[x] = weight of x, t best-s-neighbor[x] = t MIN-HEAP-DECREASE-KEY(T-heap) Analysis for binary heap: Initializing the T-heap takes Θ(V). Total cost for HEAP-EXTRACT-MINs is Θ(V log V). Traversals of adjacency lists and MIN-HEAP-DECREASE-KEYs contribute Θ(E log V). Θ(E log V) overall worst-case, since E > V. Analysis (amortized) for Fibonacci heap: Initializing the T-heap takes Θ(V). Total cost for HEAP-EXTRACT-MINs is Θ(V log V). Traversals of adjacency lists and MIN-HEAP-DECREASE-KEYs contribute Θ(E). Θ(E + V log V) overall worst-case, since E > V. Which version is the fastest?

( ) Dense % E = Ω% V Sparse E = Ο( V ) # $ # $ && (( '' 9 table binary heap Fibonacci heap # Θ% V & + E $ ' ( Θ # % V & $ ' ( Θ # % V & ( $ ' # Θ( E logv ) Θ V logv % $ # ( ) Θ% V & ( $ ' Θ( E + V logv ) Θ V logv ( ) Θ V logv & ( ' Analysis also applies to Dijkstra s shortest path. KRUSKAL S ALGORITHM (Discussed in Notes 7 as an application of UNION-FIND trees.) http://ranger.uta.edu/~weems/notes/kruskal.c... main()... qsort(edgetab,numedges,sizeof(edgetype),weightascending); for (i=0;i<numedges;i++) root=find(edgetab[i].tail); root=find(edgetab[i].head); if (root==root) printf("%d %d %d discarded\n",edgetab[i].tail,edgetab[i].head, edgetab[i].weight); else printf("%d %d %d included\n",edgetab[i].tail,edgetab[i].head, edgetab[i].weight); MSTweight+=edgeTab[i].weight; makeequivalent(root,root); if (numtrees!=) printf("mst does not exist\n"); printf("sum of weights of spanning edges %d\n",mstweight); Incremental sorting (e.g. QUICKSORT or HEAPSORT) may be used.

Can adapt to determine if MST is unique: 0 http://ranger.uta.edu/~weems/notes/kruskaldup.c... main()... numtrees=numvertices; qsort(edgetab,numedges,sizeof(edgetype),weightascending); i=0; while (i<numedges) for (k=i; k<numedges && edgetab[k].weight==edgetab[i].weight; k++) ; for (j=i;j<k;j++) root=find(edgetab[j].tail); root=find(edgetab[j].head); if (root==root) printf("%d %d %d discarded\n",edgetab[j].tail,edgetab[j].head, edgetab[j].weight); edgetab[j].weight=(-); for (j=i;j<k;j++) if (edgetab[j].weight!=(-)) root=find(edgetab[j].tail); root=find(edgetab[j].head); if (root==root) printf("%d %d %d alternate\n",edgetab[j].tail,edgetab[j].head, edgetab[j].weight); else printf("%d %d %d included\n",edgetab[j].tail,edgetab[j].head, edgetab[j].weight); makeequivalent(root,root); i=k; if (numtrees!=) printf("mst does not exist\n"); A C B D G F E H

BORUVKA S ALGORITHM Similar to Kruskal:. Initially, each vertex is a component.. Each component has a best edge to some other component.. Boruvka step: For each best edge from a component x to a component y: a = FIND(x) b = FIND(y) if a b UNION(a,b) Worst-case: number of components decreases by at least half in each phase. Gives O(E log V) time. In some cases a cluster of several components may collapse: 0 6 7 8 9 0 0 7 6

http://ranger.uta.edu/~weems/notes/boruvka.c... main()... numtrees=numvertices; // Each vertex is initially in its own subtree usefuledges=numedges; // An edge is useful if the two vertices are separate while (numtrees> && usefuledges>0) bestedgenum[i]=(-); usefuledges=0; for (i=0;i<numedges;i++) root=find(edgetab[i].tail); root=find(edgetab[i].head); if (root==root) printf("%d %d %d useless\n",edgetab[i].tail,edgetab[i].head, edgetab[i].weight); else usefuledges++; if (bestedgenum[root]==(-) edgetab[bestedgenum[root]].weight>edgetab[i].weight) bestedgenum[root]=i; // Have a new best edge from this component if (bestedgenum[root]==(-) edgetab[bestedgenum[root]].weight>edgetab[i].weight) bestedgenum[root]=i; // Have a new best edge from this component if (bestedgenum[i]!=(-)) root=find(edgetab[bestedgenum[i]].tail); root=find(edgetab[bestedgenum[i]].head); if (root==root) continue; // This round has already connected these components. MSTweight+=edgeTab[bestEdgeNum[i]].weight; printf("%d %d %d included in MST\n", edgetab[bestedgenum[i]].tail,edgetab[bestedgenum[i]].head, edgetab[bestedgenum[i]].weight); makeequivalent(root,root); printf("numtrees is %d\n",numtrees); if (numtrees!=) printf("mst does not exist\n"); printf("sum of weights of spanning edges %d\n",mstweight); ASIDE - COUNTING SPANNING TREES https://en.wikipedia.org/wiki/kirchhoff's_theorem