You are on page 1of 48

Minimum Spanning Tree

Chapter-6

Objectives
• To Study Spanning Tree • To Study Minimal Spanning Tree • To Study Algorithm for construction of Minimal Spanning Tree • Kruskal’s Algorithm • Prim’s Algorithm

Example of a Problem that Translates into a MST
The Problem • Several pins of an electronic circuit must be connected using the least amount of wire.

Modeling the Problem • The graph is a complete, undirected graph G = ( V, E ,W ), where V is the set of pins, E is the set of all possible interconnections between the pairs of pins and w(e) is the length of the wire needed to connect the pair of vertices. • Find a minimum spanning tree.

.

then a cycle is formed. . • It is a tree containing all the nodes of the graph. • If we append any edge into the spanning tree.Spanning Tree • A spanning tree is a sub graph of a graph.

then a spanning tree of the graph is a connected subgraph in which there are no cycles A connected.. undirected graph Four of the spanning trees of the graph .Spanning trees • Suppose you have a connected undirected graph – Connected: every node is reachable from every other node – Undirected: edges do not have an associated direction • ..

Spanning Tree .

Undirected graph and 3 of its spanning trees Spanning Trees Undirected Graph .

you could connect these houses with a spanning tree (of.Minimizing costs • Suppose you want to supply a set of houses (say. for example. in a new subdivision) with: – – – – electric power water sewage lines telephone lines • To keep costs down. you could connect the houses with a minimum-cost spanning tree . power lines) – However. the houses are not all equal distances apart • To reduce costs even further.

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 that 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 D D A connected. undirected graph A minimum-cost spanning tree .

e. .g. • A Spanning tree with minimum cost is known as minimal spanning tree.Minimal Spanning Tree • E. the length of the links should be minimum.: To connect the cities via communication link so that the cost of the project is minimum i.

Applications of Spanning Trees • Minimal path routing in all kinds of settings – circuits. networks. roads and sewers .

Minimal Spanning Tree • Two algorithms to construct Minimal Spanning Tree: • Kruskal’s Algorithm • Prim’s Algorithm .

Kruskal’s Algorithm To find the minimal spanning tree Step 1: Lengths of the graph are arranged in ascending order .

Step 2: The first edge is included in the spanning tree .

4) .Step 3: The next edge to be included is (1.

7) .Step 4: Now. include edge (6.

Step 5: The next edge to be included is (1.3) .

Step 6: Now. include edge (2. 5) .

Step 7: By including (2. 3) it forms a cycle. So it is rejected .

Step 8: Now include edge (5. 6) in the minimal spanning tree .

7) forms cycle. 6) & (5. (4. So they are rejected . 2). 5). (4.Step 9: Edges (1.

.Step 10: This is the final Minimal Spanning Tree.

The tree is constructed by choosing a sequence of edges.Prim’s Algorithm To find the Minimal Spanning Tree starting with a tree consisting of a single vertex. .

Step 1: Include the smallest edge in the tree .

6) or (5. 5). (2. 7) . 3). (4. So we can include (1. 3).Step 2: We have to include the edges whose one vertex is already in the tree And other vertex is new. 5). (2. (5.

Step 3: So. 3) as its length is minimum. . we will insert edge (1.

7) . 4). we can insert (1. (2. 5). (2. (4. (1.Step 4: Now. 5). (5. 6) or (5. 3). 2).

Step 5: So insert edge (1. 4) in the minimal spanning tree .

5) .Step 6: Next insert edge (2.

So we insert edge (5.Step 7: By including (2. 3) it forms a cycle. 6) .

7) This is the Final Minimal Spanning Tree .Step 8: Next insert edge (6.

Prim’s algorithm 1 10 6 25 24 5 18 22 12 28 2 14 7 16 3 10 1 2 6 5 7 3 4 4 .

Prim’s algorithm 1 10 6 25 24 5 18 22 12 28 2 14 7 16 3 10 1 2 6 25 5 7 3 4 4 .

Prim’s algorithm 1 10 6 25 24 5 18 22 12 28 2 14 7 16 3 10 1 2 6 25 5 7 3 4 22 4 .

Prim’s algorithm 1 10 6 25 24 5 18 22 12 28 2 14 7 16 3 10 1 2 6 25 5 7 3 12 4 4 22 .

Prim’s algorithm 1 10 6 25 24 5 18 22 12 28 2 14 7 16 3 10 1 2 16 6 25 5 7 3 12 4 4 22 .

Prim’s algorithm 1 10 6 25 24 5 18 22 12 28 2 14 7 16 3 10 1 2 14 16 6 25 5 7 3 12 4 4 22 Cost = 99 .

D} {B.D} 12 C A B C E D F K .E} {D.F} {E.C} {B.Kruskal’s Algorithm 5 A 1 B 8 9 E 2 7 D 5 F {A.D} {A.D} {C.B} 1 2 5 5 7 8 9 12 G {A.

B} {A.F} {E.D} .D} A 1 B C F E D 1 2 5 5 7 8 9 12 {A.Kruskal’s Algorithm {A.D} {C.D} {B.E} K {D.C} {B.

C} {B.F} {E.D} .E} D K {D.D} A 1 B C F E 2 1 2 5 5 7 8 9 12 {A.D} {C.B} {A.D} {B.Kruskal’s Algorithm {A.

E} D K {D.D} .Kruskal’s Algorithm {A.C} {B.D} {B.D} {C.B} {A.F} {E.D} 5 A 1 B C F E 2 1 2 5 5 7 8 9 12 {A.

E} D 5 F K {D.D} {C.D} {B.B} {A.D} 5 A 1 B C E 2 1 2 5 5 7 8 9 12 {A.D} .Kruskal’s Algorithm {A.C} {B.F} {E.

D} will create a cycle .D} {C.D} 5 A 1 B C E 2 1 2 5 5 7 8 9 12 {A.D} {B.F} {E.D} {E.C} {B.E} D 5 F K {D.B} {A.Kruskal’s Algorithm {A.

D} {C.D} 8 C .D} 5 A 1 B E 2 1 2 5 5 7 8 9 12 {A.E} D 5 F K {D.Kruskal’s Algorithm {A.B} {A.F} {E.D} {B.C} {B.

D} {C.D} will create a cycle .D} 5 A 1 B E 2 1 2 5 5 7 8 9 12 {A.C} {B.D} {B.Kruskal’s Algorithm {A.E} D 5 F K {D.D} 8 C {B.F} {E.B} {A.

E} D 5 F K {D.D} 8 C {C.D} {C.D} 5 A 1 B E 2 1 2 5 5 7 8 9 12 {A.D} will create a cycle .B} {A.D} {B.Kruskal’s Algorithm {A.C} {B.F} {E.