0% found this document useful (0 votes)
166 views5 pages

19BCS099 - Assignment On Prims and Kruskal Algorithm

The cost of the minimum spanning tree (MST) for an undirected graph G(V,E) with n nodes where nodes vi and vj are connected if 0 < |i-j| <= 2 and edge weight is i + j, is n^2 - n + 1. This can be shown through examples for small values of n and deriving the recurrence relation as T(n) = T(n-1) + (2n-2), which has the closed form solution of n^2 - n + 1.

Uploaded by

Rohit Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
166 views5 pages

19BCS099 - Assignment On Prims and Kruskal Algorithm

The cost of the minimum spanning tree (MST) for an undirected graph G(V,E) with n nodes where nodes vi and vj are connected if 0 < |i-j| <= 2 and edge weight is i + j, is n^2 - n + 1. This can be shown through examples for small values of n and deriving the recurrence relation as T(n) = T(n-1) + (2n-2), which has the closed form solution of n^2 - n + 1.

Uploaded by

Rohit Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. What is a minimum spanning tree?

Ans :

The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be
many spanning trees. Minimum spanning tree is the spanning tree where the cost is minimum
among all the spanning trees. There also can be many minimum spanning trees.

Minimum spanning tree has direct application in the design of networks. It is used in algorithms
approximating the travelling salesman problem, multi-terminal minimum cut problem and
minimum-cost weighted perfect matching. Other practical applications are:

1. Cluster Analysis
2. Handwriting recognition
3. Image segmentation

2. Write Prim’s and Kruskal’s algorithm to solve minimum spanning tree problems.

Ans:

Kruskal's algorithm to solve minimum spanning tree problem is :

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;


const int MAX = 1e4 + 5;
int id[MAX], nodes, edges;
pair <long long, pair<int, int> > p[MAX];

void initialize()
{
for(int i = 0;i < MAX;++i)
id[i] = i;
}

int root(int x)
{
while(id[x] != x)
{
id[x] = id[id[x]];
x = id[x];
}
return x;
}

void union1(int x, int y)


{
int p = root(x);
int q = root(y);
id[p] = id[q];
}

long long kruskal(pair<long long, pair<int, int> > p[])


{
int x, y;
long long cost, minimumCost = 0;
for(int i = 0;i < edges;++i)
{
// Selecting edges one by one in increasing order from the beginning
x = p[i].second.first;
y = p[i].second.second;
cost = p[i].first;
// Check if the selected edge is creating a cycle or not
if(root(x) != root(y))
{
minimumCost += cost;
union1(x, y);
}
}
return minimumCost;
}

int main()
{
int x, y;
long long weight, cost, minimumCost;
initialize();
cin >> nodes >> edges;
for(int i = 0;i < edges;++i)
{
cin >> x >> y >> weight;
p[i] = make_pair(weight, make_pair(x, y));
}
sort(p, p + edges);
minimumCost = kruskal(p);
cout << minimumCost << endl;
return 0;
}

Prim's algorithm to solve minimum spanning tree problem is :

#include <iostream>
#include <vector>
#include <queue>
#include <functional>
#include <utility>

using namespace std;


const int MAX = 1e4 + 5;
typedef pair<long long, int> PII;
bool marked[MAX];
vector <PII> adj[MAX];

long long prim(int x)


{
priority_queue<PII, vector<PII>, greater<PII> > Q;
int y;
long long minimumCost = 0;
PII p;
Q.push(make_pair(0, x));
while(!Q.empty())
{
// Select the edge with minimum weight
p = Q.top();
Q.pop();
x = p.second;
// Checking for cycle
if(marked[x] == true)
continue;
minimumCost += p.first;
marked[x] = true;
for(int i = 0;i < adj[x].size();++i)
{
y = adj[x][i].second;
if(marked[y] == false)
Q.push(adj[x][i]);
}
}
return minimumCost;
}

int main()
{
int nodes, edges, x, y;
long long weight, minimumCost;
cin >> nodes >> edges;
for(int i = 0;i < edges;++i)
{
cin >> x >> y >> weight;
adj[x].push_back(make_pair(weight, y));
adj[y].push_back(make_pair(weight, x));
}
// Selecting 1 as the starting node
minimumCost = prim(1);
cout << minimumCost << endl;
return 0;
}

3. Perform complexity analysis of Prim’s and Kruskal’s algorithm.

Ans:

Kruskal’s algorithms complexity Analysis:

In Kruskal’s algorithm, most time consuming operation is sorting because the total complexity of
the Disjoint-Set operations will be O(ElogV), which is the overall Time Complexity of the
algorithm.
Prim’s algorithms complexity Analysis:

The time complexity of the Prim’s Algorithm is O((V+E)logV) because each vertex is inserted in
the priority queue only once and insertion in priority queue takes logarithmic time.

4. The number of distinct minimum spanning trees for the weighted graph below
is ____

Ans:

The number of distinct minimum spanning trees for the weighted graph below is 6

5. An undirected graph G(V, E) contains n ( n > 2 ) nodes named v1 , v2 ,….vn.


Two nodes vi , vj are connected if and only if 0 < |i – j| <= 2. Each edge (vi, vj ) is
assigned a weight i + j. A sample graph with n = 4 is shown below. What will be the
cost of the minimum spanning tree (MST) of such a graph with n nodes?

Ans :

Minimum spanning tree for 2 nodes would be


(v1) _ (v2)
Total weight 3

Minimum spanning tree for 3 nodes would be

(v1) _ (v2)
|
(v3)
Total weight= 3 + 4 = 7

Minimum spanning tree for 4 nodes would be

(v1) _ (v2) _ (v4)


|
(v3)
Total weight= 3 + 4 + 6 = 13

Minimum spanning tree for 5 nodes would be

(v1) _ (v2) _ (v4)


|
(v3)
|
(v5)
Total weight= 3 + 4 + 6 + 8 = 21

Minimum spanning tree for 6 nodes would be

(v1) _ (v2) _ (v4) _ (v6)


|
(v3)
|
(v5)
Total weight= 3 + 4 + 6 + 8 + 10 = 31

We can observe from the above examples that when we add kth nodes,
the weight of the spanning tree increases by 2k-2. Let T(n) be the weight
of the minimum spanning tree. T(n) can be written as

T(n) = T(n-1) + (2n-2) for n > 2


T(1) = 0, T(2) = 0 and T(2) = 3

The recurrence can be written as sum of series (2n – 2) + (2n-4) + (2n-6)


+ (2n-8) + …. 3 and the solution of this recurrence is n^2 – n + 1.

You might also like