0% found this document useful (0 votes)
59 views

Minimum Cost Spanning Tree Problem

The document discusses the minimum cost spanning tree problem and provides algorithms to solve it. It defines key terms like tree, spanning tree, minimum cost spanning tree. It then explains Kruskal's algorithm and Prim's algorithm to find the minimum cost spanning tree, including their steps, pseudocode, and time complexities of O(ElogE) for Kruskal's and O(V^2) for Prim's, where E is the number of edges and V is the number of vertices. The document compares the two algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Minimum Cost Spanning Tree Problem

The document discusses the minimum cost spanning tree problem and provides algorithms to solve it. It defines key terms like tree, spanning tree, minimum cost spanning tree. It then explains Kruskal's algorithm and Prim's algorithm to find the minimum cost spanning tree, including their steps, pseudocode, and time complexities of O(ElogE) for Kruskal's and O(V^2) for Prim's, where E is the number of edges and V is the number of vertices. The document compares the two algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Minimum Cost Spanning Tree Problem

1
Insights
• Tree
• Spanning Tree
• Minimum Cost Spanning Tree
• Minimum Cost Spanning Tree Problem
• Solution to the Problem
• KRUSKAL’s Algorithm
• Steps
• Algorithm
• Time Complexity
• PRIM’s Algorithm
• Steps
• Algorithm
• Time Complexity
• Differences between KRUSKAL’s and PRIM’s Algorithm
Tree
• Tree represents the nodes (N) connected by edges (E)
• Example:

• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
Graph
• A graph is a pictorial representation of a set of objects where some pairs of objects
are connected by links.
• The interconnected objects are represented by points termed as vertices (V), and
the links that connect the vertices are called edges (E).
• Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set
of edges, connecting the pairs of vertices. Take a look at the following graph −

Types:
• Directed Graph
• Undirected Graph
• Connected Graph
• In the above graph, • Connected Graph with weights
• V = {a, b, c, d, e}
• E = {ab, ac, bd, cd, de}
Spanning Tree
• A spanning tree is a subset of an undirected Graph that has all the vertices connected by minimum
number of edges.
• If all the vertices are connected in a graph, then there exists at least one spanning tree.
• In a graph, there may exist more than one spanning tree.
• Properties
• A spanning tree does not have any cycle.
• Any vertex can be reached from any other vertex.

Example:
In the above graph, the highlighted edges form a spanning tree.
Minimum Cost Spanning Tree (MST)
• A Minimum Spanning Tree (MST) is a subset of edges of a connected weighted
undirected graph that connects all the vertices together with the minimum possible total
edge weight.
• one graph may have more than one spanning tree.
• If there are n number of vertices, the spanning tree should have n - 1 number of edges. In
this context, if each edge of the graph is associated with a weight and there exists more
than one spanning tree, we need to find the minimum spanning tree of the graph.
• Moreover, if there exist any duplicate weighted edges, the graph may have multiple
minimum spanning tree.
In the beside graph, It shows a spanning tree though it’s
not the minimum spanning tree.

The cost of this spanning tree is (5 + 7 + 3 + 3 + 5 + 8 + 3


+ 4) = 38.

MST will be computed Next………….


Minimum Cost Spanning Tree (MST)
Minimum Cost Spanning Tree - Problem
• Find MST for the below GRAPH
8 7
2 3 4
1 9
2
1 11 9 4 14 5
7
16
8 10
8 7 2
6
4
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm

• Start with a forest that has no edges.


• Add the next minimum cost edge to the
forest if it will not cause a cycle.
• Continue this process until the tree has n - 1
edges.

4 -9
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
8 7
2 3 4
1 9
2
1 11 9 4 14 5


8

7 16
10
8 7 2
6
4

2 3 4

1 9 5

8 7 6

4 -10
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm 4
9

5 5
3 8
4 9
3 6 8 6
2

1 2 2 4 4 7 7 8 8 9 14 16
10

1
9 7 7 3

3 8 2 1
Does not form cycle 4 6
6
7
3 4
2

9 5
1

8 7 6

Forms cycle
4 -11
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm
Algorithm kruskal(E,cost,n,t) while((i<n-1) and (heap not empty)) do
// E is the set of edges in G.G has n vertices.cost[u,v] {
is the cost of edge(u,v). delete a minimum cost edge (u,v) from the heap and
reheapify using Adjust;
//t is the set of edges in the minimum –cost spanning j=Find(u);
tree.the final cost
K=Find(v);
// is returned. if(j!=k) then
// {
{ i=i+1;
Construct a heap out of the edge costs t[I,1]=u;
using Hepify; t[I,2]=v;
mincost=mincost+cos[u,v];
for i=1 to n do parent[i]=-1; Union(j,k);
//each vertex is in a different set. }
i=0; }
If(i!=n-1) then
mincost=0; write(“no spanning tree”);
else
return mincost;
}

4 -12
Minimum Cost Spanning Tree – KRUSKAL’s Algorithm

Time complexity of kruskal’s algorithm


• With an efficient Find-set and union algorithms, the running
time of kruskal’s algorithm will be dominated by the time
needed for sorting the edge costs of a given graph.

• Hence, with an efficient sorting algorithm(merge sort), the


complexity of kruskal’s algorithm is O( ElogE).

4 -13
Minimum Cost Spanning Tree – PRIM’s Algorithm
t [1:n-1,1:2]
8 7
2 3 4 1 2
1 9
2 1 1 2
1 11 9i 4 14 5 2 2 3
7 16
8 10
8 7 6 3 9
4 2
. 3 6
. 3 4
2 3 4 .
4 5
1 9 5 6 7
n-1 7 8
8 7 6

4 -14
Minimum Cost Spanning Tree – PRIM’s Algorithm
Prim’s Algorithm 14 for i=2 to n-1 do
1 Algorithm Prim(E, cost, n, t) 15 {
2 // E is the set of edges in G. 16 // Find n-1 additional edges for t.
3 //cost[1:n,1:n] is the cost matrix such that cost[i,j] is
either
17 Let j be an index such that
4 // positive real number or ∞ if no edge (i,j) exists. near[j]≠0 and
cost[i,j]=0, if i=j.
18 cost[j,near[j]] is minimum;
5 // A minimum spanning tree is computed and stored
6 // as a set of edges in the array t[1:n-1,1:2]
19 t[i,1]=j; t[i,2]=near[j];
7 {
20 mincost=mincost+cost[j,near[j]];
8 Let (k,l) be an edge of minimum cost in E
21 near[j]=0;
9 mincost=cost[k,l];
22 for k=1 to n do // update near[]
10 t[1,1]=k; t[1,2]=l;
23 if( ( near[k] ≠0 ) and
11 for i=1 to n do // initialize near (cost[k,near[k]>cost[k,j])) then
12 if( cost[i,l]< cost[i, k] then near[i]=l; 24 near[k]=j;
else near[i]= k; 25 }
13 near[k]=near[l]=0; 26 return mincost;
27 }
4 -15
Minimum Cost Spanning Tree – PRIM’s Algorithm
Time complexity of Prims algorithm
• Line 8 takes o(E).
• The for loop of line 12 takes o(n).
• 17 and 18 and the for loop of line 22 require o(n)
time.
• Each iteration of the for loop of line 14 takes o(n)
time.
• Therefore, the total time for the for loop of line 14
is o(n2).
• Hence, time complexity of Prim is o(n2).

16
Differences between KRUSKAL’s and PRIM’s
Algorithm

17
Thank You

18

You might also like