Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8
Minimum Spanning Tree Input: graph G with weights on the edges Output: connected sub graph G’ of G that includes all the vertices of G, of minimum total weight
Exhaustive Search List all connected sub-graphs of G Return sub-graph with least weight Number of vertices, N, and number of edges, M O(m n-1 )
Greedy Algorithm Start with a graph containing just the vertices, G’={V,Ø} Add edges with the least weight until the graph is connected but no cycles are created To do this: – Order the edges in increasing weight – While the graph is not connected, add an edge – End when all vertices are connected
Greedy Algorithm - Example Initial Graph:List of Edges: EdgeWeight AB1 AF5 BC8 BD4 BF5 CD2 CF3 DE10 DF1 EF4
Greedy Algorithm - Example Graph:Sorted List of Edges: EdgeWeight AB1 DF1 CD2 CF3 BD4 EF4 AF5 BF5 BC8 DE10 Start with G’={V,Ø} and sorted list of edges
Greedy Algorithm - Example Graph:Sorted List of Edges: EdgeWeight AB1 DF1 CD2 CF3 BD4 EF4 AF5 BF5 BC8 DE10 Add edge from list to graph s.t. no cycles are created Remove edge from list
Greedy Algorithm - Example Graph:Sorted List of Edges: EdgeWeight AB1 DF1 CD2 CF3 BD4 EF4 AF5 BF5 BC8 DE10 Add edge from list to graph s.t. no cycles are created Remove edge from list
Greedy Algorithm - Example Graph:Sorted List of Edges: EdgeWeight CF3 BD4 EF4 AF5 BF5 BC8 DE10 Add edge from list to graph s.t. no cycles are created Remove edge from list Check for cycles using Depth First Search starting at a vertex in the added edge: Start at C C-> D -> F -> C C gets visited twice => a cycle exists and CF should not be added to the graph
Greedy Algorithm - Example Graph:Sorted List of Edges: EdgeWeight BD4 EF4 AF5 BF5 BC8 DE10 Add edge from list to graph s.t. no cycles are created Remove edge from list End when all vertices can be visited (graph is connected) Check for cycles using Depth First Search starting at a vertex in the added edge: Start at B B ->D ->C -> F -> A If all vertices are visited by DFS, then the graph is connected and we are done
Greedy Algorithm - Example Graph:Sorted List of Edges: EdgeWeight EF4 AF5 BF5 BC8 DE10 Add edge from list to graph s.t. no cycles are created Remove edge from list End when all vertices can be visited (graph is connected) Check for cycles using Depth First Search starting at a vertex in the added edge: Start at E E -> F -> D -> B -> A ->C All vertices were visited and there were no cycles => we have found a minimum spanning tree with weight 12.
Greedy Algorithm- Psuedocode Given G = (V, E) G’ <- (V,Ø) While G’ is not connected – Add e Є E to G’ s.t. G’ is acyclic and e is minimum – Remove e from E
Greedy Algorithm – Run Time Initialization – constant While loop – O(n-1) – Connected graph has n-1 edges – Must add n-1 edges to the graph for it to be connected Find a minimum e Є E – O(m) Make sure G’ is acyclic – O(2n) – DFS is O(m+n)<O(2n) where m<=n-1 Test connectivity of G’ – O(n) – Can use DFS; could be done in same step as testing acyclicity Remove e from E - constant Total Runtime: O(n*(m+2n+n)) ~ O(nm+ n 2 ) -> POLYNOMIAL
Error? Let X be any subset of the vertices of G, and let edge e be the smallest edge connecting X to G-X. Then e is part of the minimum spanning tree.
Error? 1.T does not contain e and is a spanning tree 2.e is smallest edge connecting X = {C,D} to G-X ={A,B,F,E} 3.Adding e creates a cycle in T 4.Another spanning tree exists, T 2 = T+e-f, where f is another edge that could connect X to G-x 5.Because e < f, T was not the MST and e must be part of the MST 1 2 4
Error? In the greedy algorithm, the edge added was always the smallest. Because the smallest edge to connect two parts of the original graph must be included, each edge added must be a part of the final minimum spanning tree. In this case, the greedy algorithm is always correct (but we know this is not the case for ALL greedy algorithms).