1 / 17

WEEK 1 2 Graphs IV Minimum Spanning Tree Algorithms

WEEK 1 2 Graphs IV Minimum Spanning Tree Algorithms. CE222 – Data Structures & Algorithms II Chapter 9.5 (based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006). Minimum Spanning Tree (MST). Spanning Tree. Spanning Tree. (Minimum) Spanning Tree.

palma
Download Presentation

WEEK 1 2 Graphs IV Minimum Spanning Tree Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WEEK 12Graphs IVMinimum Spanning Tree Algorithms CE222 – Data Structures & Algorithms II Chapter 9.5 (based on the book by M. A. Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition, 2006)

  2. Minimum Spanning Tree (MST) Spanning Tree Spanning Tree (Minimum) Spanning Tree CE 222-Data Structures & Algorithms II, Izmir University of Economics

  3. Minimum Spanning Tree (MST) A minimum spanning treeof an undirected graph G is a tree formed from graph edges that connects all the vertices of G at a lowest cost. • 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 CE 222-Data Structures & Algorithms II, Izmir University of Economics

  4. Minimum Spanning Tree (MST) Howtofindthe Minimum Spanningtree of a givenundirectedgraph ??? Prim’sAlgorithm (~Dijsktra) Kruskal’sAlgorithm CE 222-Data Structures & Algorithms II, Izmir University of Economics

  5. Prim’s Algorithm 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 untill all the vertices have known =TRUE. class Vertex { Listadj; boolknown; intdistance; Vertex parent; } CE 222-Data Structures & Algorithms II, Izmir University of Economics

  6. Prim’s AlgorithmExample I 9 b a 6 2 d 4 5 5 4 e 5 c CE 222-Data Structures & Algorithms II, Izmir University of Economics

  7. Prim’s AlgorithmExample I b a 2 d 4 5 4 e c 9 b a 6 2 d 4 5 5 4 e 5 c CE 222-Data Structures & Algorithms II, Izmir University of Economics

  8. Prim’s Algorithm – Example II CE 222-Data Structures & Algorithms II, Izmir University of Economics

  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 CE 222-Data Structures & Algorithms II, Izmir University of Economics

  10. Kruskal’s Algorithm // Create the list of all edges E solution = { } // will include the list of MST edgeswhile ( more edges in E) do// Selection select minimum weight(cost) edgeremove edge from E// Feasibility if (edge closes a cycle with solution so far)then reject edgeelse add edge to solution// Solution check if |solution| = |V | - 1 return solution or use a sorted list of edges in increasing order CE 222-Data Structures & Algorithms II, Izmir University of Economics

  11. 9 b a 6 2 d 4 5 5 4 e 5 c Kruskal’s Algorithm (once more in other words)!!EXAMPLE I • 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} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  12. 9 b a 6 2 d 4 5 5 4 e 5 c 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 F = {a}, {b}, {c}, {d}, {e} S= E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} CE 222-Data Structures & Algorithms II, Izmir University of Economics

  13. Kruskal’s algorithm For each edge (u,v) E in increasing order while more than one set remains: Ifu 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|) CE 222-Data Structures & Algorithms II, Izmir University of Economics

  14. 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)} Kruskal’s algorithm: Example I We will use the sorted list of all edges E 9 b a 6 2 E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} d 4 5 5 4 e 5 c CE 222-Data Structures & Algorithms II, Izmir University of Economics

  15. Kruskal’s Algorithm: Example II Minimum spanning tree is the list of accepted edges : (v1,v4), (v6,v7),(v1,v2), (v3,v4) , (v4,v7) CE 222-Data Structures & Algorithms II, Izmir University of Economics

  16. Kruskal’s Algorithm : Pseudocode CE 222-Data Structures & Algorithms II, Izmir University of Economics

  17. Homework Assignments • 9.15, 9.16, 9.19, 9.20 • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics

More Related