Presentation is loading. Please wait.

Presentation is loading. Please wait.

WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.

Similar presentations


Presentation on theme: "WEEK 12 Graphs IV Minimum Spanning Tree Algorithms."— Presentation transcript:

1 WEEK 12 Graphs IV Minimum Spanning Tree Algorithms

2 Minimum Spanning Tree (MST) Spanning Tree (Minimum) Spanning Tree

3 Minimum Spanning Tree (MST) A minimum spanning tree is a tree (i.e., it is acyclic) covers all the vertices V –contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree of an undirected graph G is a tree formed from graph edges that connects all the vertices of G at a lowest cost.

4 Minimum Spanning Tree (MST) How to find the Minimum Spanning tree of a given undirected graph ??? 1. Prim’s Algorithm (~Dijsktra) 2. Kruskal’s Algorithm

5 a. Pick a vertex r to be the root b. Set r.distance = 0 (distance of vertex r), r.parent = null (parent of vertex r) c. For all vertices v  V, which are not r, set v.distance= , v.known =FALSE, v.parent=NULL d. Set r.known= TRUE and list all adjacent vertices of r, Set adjacent.parent =r and adjacent.distance=cost of edge e. Select vertex m with minimum distance Set m.known=TRUE If the distance of any adjacent vertex of m is less than the current value change the distance and the parent. f. Repeat Step e till all the vertices have known =TRUE. class Vertex { List adj; bool known; int distance; Vertex parent; } Prim’s Algorithm

6 Prim’s Algorithm Example I a c e d b 2 45 9 6 4 5 5 v.distancev.parentv.known v.distancev.parentv.known v.distancev.parentv.known e0-FALSEe0-TRUEe0- a  -FALSEa  - a2d b  - b5e b5e c  - c5e c4d d  - d4e d4eTRUE v.distancev.parentv.known v.distancev.parentv.known v.distancev.parentv.known e0-TRUEe0- e0- a2d a2d a2d b5eFALSEb5e b5eTRUE c4dFALSEc4dTRUEc4d d4e d4e d4e

7 Prim’s Algorithm Example I a c e d b 2 45 9 6 4 5 5 v.distancev.parentv.known e0-TRUE a2d b5e c4d d4e a c e d b 2 4 45

8 Prim’s Algorithm – Example II

9 Prim’s Algorithm – Time Complexity ● The running time is O(|V| 2 ) (|V| iterations in each of which a sequential scan is performed to find the minimum cost unknown edge ) without heaps, which is optimal for dense graphs. O(|E|*log|V|) ( an underlying binary heap with |V| elements is used, and |V| deleteMin ( for picking min cost vertex) and at most |E| decreaseKey (for updates) are performed each taking O(log|V|) time ) using binary heaps,which is good for sparse graphs

10 Kruskal’s Algorithm // Create the list of all edges E solution = { } // will include the list of MST edges while ( more edges in E) do // Selection select minimum weight(cost) edge remove edge from E // Feasibility if (edge closes a cycle with solution so far) then reject edge else add edge to solution // Solution check if |solution| = |V | - 1 return solution or use a sorted list of edges in increasing order

11 Kruskal’s Algorithm (once more in other words)!! EXAMPLE I a c e d b 2 45 9 6 4 5 5 Create a forest of trees from the vertices Repeatedly merge trees by adding “safe edges” until only one tree remains A “safe edge” is an edge of minimum weight which does not create a cycle forest: {a}, {b}, {c}, {d}, {e}

12 Kruskal’s Algorithm : Example I Initialization a. Create a set for each vertex v  V b. Initialize the set of “safe edges” S (Solution set) comprising the MST to the empty set c. Sort edges by increasing weight a c e d b 2 45 9 6 4 5 5 F = {a}, {b}, {c}, {d}, {e} S =  E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)}

13 Kruskal’s algorithm For each edge (u,v)  E in increasing order while more than one set remains: If u and v, belong to different sets U and V (if find(u)!=find(v)) a. Add edge (u,v) to the safe edge set (Solution set) S = S  {(u,v)} b. Merge the sets U and V (union(u,v)) Return S ● Running time bounded by sorting (or findMin) ● O( |E|log|E| )

14 Kruskal’s algorithm: Example I E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} Forest {a}, {b}, {c}, {d}, {e} {a,d}, {b}, {c}, {e} {a,d,c}, {b}, {e} {a,d,c,e}, {b} {a,d,c,e,b} S  {(a,d)} {(a,d), (c,d)} {(a,d), (c,d), (d,e)} {(a,d), (c,d), (d,e), (b,e)} a c e d b 2 45 9 6 4 5 5 We will use the sorted list of all edges  E

15 Kruskal’s Algorithm: Example II EdgeWeightAction {v1}{v2}{v3}{v4}{v5}{v6}{v7} (v1,v4)1Accepted{v1, v4}{v2}{v3}{v5}{v6}{v7} (v6,v7)1Accepted{v1, v4}{v2}{v3}{v5}{v6, v7} (v1,v2)2Accepted{v1, v4, v2}{v3}{v5}{v6, v7} (v3,v4)2Accepted{v1, v4, v2, v3}{v5}{v6, v7} (v2,v4)3Rejected{v1, v4, v2, v3}{v5}{v6, v7} (v1,v3)4Rejected{v1, v4, v2, v3}{v5}{v6, v7} (v4,v7)4Accepted{v1, v4, v2, v3, v6, v7}{v5} (v3,v6)5Rejected{v1, v4, v2, v3, v6, v7}{v5} (v5,v7)6Accepted{v1, v4, v2, v3, v6, v7,v5}  SOLUTION INCLUDES ALL VERTICES Minimum spanning tree is the list of accepted edges : (v1,v4), (v6,v7),(v1,v2), (v3,v4), (v4,v7)

16 Kruskal’s Algorithm : Pseudocode


Download ppt "WEEK 12 Graphs IV Minimum Spanning Tree Algorithms."

Similar presentations


Ads by Google