The minimum spanning tree problem MST is a minimum cost connection problem on graphs The graph can model the connection in a (hydraulic, electric, telecommunication) network: the nodes are the points that must be connected and the edges the possible connections The problem consists in selecting edges so that all nodes are connected with the minimum total cost The solution is a Minimum Spanning Tree (MST) of the graph Formally, consider an undirected graph G=(V,E) A cost c i is associated with each edges e i, i=1,...,m of G The problem: find MST(G, c) the spanning tree of G with the minimum cost Two algorithms: Kruskal s algorithm (Greedy Algorithm) Prim s algorithm
1. Given graph G=(V,E) with n nodes and m edges, sort the edges as e 1, e 2,...,e m such that the associated costs are not decreasing (c 1 c 2... c m ). Set E 0 =, k=1 and the spanning tree ST 0 =(V, E 0 ) 2. If (V, E k-1 {e k }) is an acyclic graph then ST k =(V, E k ) with E k =E k-1 {e k }, otherwise E k =E k-1 e ST k =ST k-1 3. if E k =n-1 stop and ST k is the minimum spanning tree, otherwise k=k+1 and go to step 2.
It is a greedy algorithm: at each step it tries to insert in the solution the component with the minimum cost (local best decision) Easy to implement with a difficulty: how to check if the solution graph remains acyclic after the insertion of an edge? An alternative: Build the MST progressively joining disjoint trees that initially form a forest Start with a forest composed of n single nodes A selected edge is inserted only if the connected nodes belong to two distinct trees in the forest
Alternative Kruskal s algorithm 1. Initial spanning tree ST=(V, X); X = 2. Build n trees T k k=1,...,n each including a single node v V 3. Sort the edges in E such that the costs are non decreasing 4. For each edge e=(u,v) E T r = Tree_including(u) T s = Tree_including(v) If r s then X = X {e} Join_tree(T r, T s, e)
Example Consider the following graph V =9 E =17 Edge with cost 3 cannot be included since it closes a cycle 7 8 11 12 6 14 4 10 17 3 2 1 9 16 The MST cost is 44 13 5 15
The alternative implementation V =9 E =17 7 Edge with cost 3 cannot be inserted because it connects two nodes belonging to the same tree (so it closes a cycle) 8 11 12 6 14 4 10 17 3 2 1 9 16 When a single tree remains then the MST is found 13 5 15
Algorithm complexity: the number of needed operations in the worst case grows as a polynomial of order O( E log 2 E + E 2 ) that is, O( E 2 ): The algorithm sorts the edges (sorting requires a number of steps of order O( E log 2 E )) Then it performs E steps... And at each step it verifies that the solution remain acyclic (possible with procedure of order O( E )) Implementations exist with complexity O( E log 2 E ) The second algorithm for MST: Prim s algorithm is similar to the second version of Kruskal s algoritm It starts from a tree of the initial forest (a node) and progressively expand such a tree so that it includes all the other nodes
Prim s algorithm for MST 1. Given G=(V,E) with n nodes and m edges Select a node of G, V 0 ={v s } and set E 0 = and k=1 2. Connect a node v i V k-1 with a node v h V \V k-1 such that the cost of the edge (v i, v h ) be c( v i, v h ) = v j V ( v, v j k 1 min, v ) E V V [ c( v then update V k =V k-1 {v h } and E k =E k-1 {(v i, v h )} e 3. If E k =n-1 stop and ST k =(V k, E k ) is the MST, otherwise k=k+1 and go to 2. e k 1 j, v e )]
Prim s algorithm for MST Polynomial complexity: simple implementation is O( V E ): step 2 executed V -1 times and the edge selection has O( E ) complexity Example V =6 E =12 Initial node arbitrary chosen 12 9 7.5 7 8 16 10 17 11 9.5 19 10.5