You are on page 1of 18

# Minimum Spanning Trees

Problem: Laying Telephone Wire Central office 2 .

Wiring: Naive Approach Central office Expensive! 3 .

Wiring: Better Approach Central office Minimize the total length of wire connecting the customers 4 .

Minimum Cost spanning Tree • A connected/directed graph [ Cyclic]  Kruskal Algorithm • Un connected/ un directed graph [acyclic]  Prims Algorithm .

Minimum-cost spanning trees • Suppose you have a connected undirected graph with a weight (or cost) associated with each edge • The cost of a spanning tree would be the sum of the costs of its edges • A minimum-cost spanning tree is a spanning tree that has the lowest cost A 19 16 21 11 B 5 6 A 16 11 B 5 6 33 F 18 14 C 10 F E 18 C E 6 D D A connected. undirected graph A minimum-cost spanning tree .

e.Minimum Spanning Tree (MST) A minimum spanning tree is a subgraph of an undirected weighted graph G.1 edges  7  the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique .. such that   it is a tree (i. it is acyclic) it covers all the vertices V  contains |V| .

How Can We Generate a MST? 9 a 5 c 2 4 6 d 4 5 e c 5 b a 2 4 d 4 5 e 9 6 b 5 5 8 .

Prim’s algorithm T = a spanning tree containing a single node s. Prim’s algorithm is far simpler to implement than Kruskal’s algorithm (presented below) 9 . w) else { add edge (v. E = set of edges adjacent to s. w) and node w to T add to E the edges adjacent to w } } • An edge of lowest cost can be found with a priority queue • Testing for a cycle is automatic – Hence. while T does not contain all the nodes { remove an edge (v. w) of lowest cost from E if w is already in T then discard edge (v.

Example 2 2 A B 5 8 7 D  4 2 F E 3 7 7 4 2  2 A B 5 8 0 5 C 8 C 7 7 8  B 5 8 7 D 7 4 2 A 5 C 7 7 7 8 E F 3 7  0 0 2 2 A B 5 8 5 C 7 7 D D 9 8 7 4 F E 3 7 4 8 7 E 7 F 3 0 10 7 .Prim’s algorithm .

Example (contd.) 2 2 A B 5 5 C 7 7 8 7 E 3 2 2 A B 5 C 8 0 11 D 7 4 F 3 7 D 7 8 7 E 3 7 4 F 3 4 4 8 0 5 .

and we update the distances and parent for its adjacent vertices 12 .Prim’s algorithm e 9 a 5 c 2 4 5 6 d 4 e 5 d 4 b 5 c a d b c a 0     b Vertex Parent e b c d Vertex Parent e b e c e d e 5  The MST initially consists of the vertex e.

Prim’s algorithm d 9 a 5 c 2 4 5 6 d 4 e a 2 c 4 b 5 5 4 b b 5 c a 5  Vertex Parent e b e c e d e Vertex Parent e b e c d d e a d 13 .

Prim’s algorithm a 2 9 a 5 c c b 2 4 5 6 d 4 e 5 b c 4 b 5 Vertex Parent e b e c d d e a d 4 5 Vertex Parent e b e c d d e a d 14 .

Prim’s algorithm c 9 a 5 c b 5 2 4 5 6 d 4 e 5 b 4 b 5 Vertex Parent e b e c d d e a d Vertex Parent e b e c d d e a d 15 .

Prim’s algorithm b 9 a 5 c 2 4 5 6 d 4 e 5 5 b Vertex Parent e b e c d d e a d The final minimum spanning tree Vertex Parent e b e c d d e a d 16 .

Define Point class and an Arc class. Write a method to find a minimum cost spanning tree in a graph. . Define a Graph class which represents graph as a collection of Point objects and Arc objects.1.

Write a c++ program to Design and implement queue class with necessary exception handling. 5. Write a c++ program to Design and implement stack class with necessary exception handling. 4. Write a method to find a minimum cost spanning tree in a graph. Write a c++ program to develop a template of linkedlist class and its methods. Define Point class and an Arc class. Write a c++ program to develop templates of standard sorting algorithms for quick sort. Write a c++ program to develop templates of standard sorting algorithms for merge sort. 2.Assignment :02 – Submit on 25th Sep 2013 1. 3. 6. . Define a Graph class which represents graph as a collection of Point objects and Arc objects.