Cse2202 LabReport05

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Lab Report 03

Rajshahi University of Engineering & Technology

Department of Computer Science and Engineering

CSE 2202

Sessional Based on CSE 2202

Lab Report 05

Name of the Experiment: Minimum spanning tree.


Date of the Experiment: 07/11/2022
Date of Submission: 14/10/2022

Submitted to: Submitted by:


Name: Rizoan Toufiq, Name: Rukaiya Haque
Assistant Professor, Roll: 1903120
Department of CSE, Section: B
RUET 2nd Year Odd Semester

1|Page
Lab Report 03

INDEX

Sl.No Topic Page No.

1. 3

Title

2. Objective 3

3. Theory 3-5

4. Code and Output 6


 Prim’s Algorithm
 Kruskal’s Algorithm

6. Discussion 7

2|Page
Lab Report 03

Title: Minimum spanning tree.

Objective:

 Finding minimum spanning tree using Prim’s Algorithm.


 Finding minimum spanning tree using Kruskal Algorithm.

Theory:
Minimum Spanning Tree:

A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the edges of a connected,
edge-weighted undirected graph that connects all the vertices together, without any cycles and with the
minimum possible total edge weight.

Prim’s Algorithm:

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a connected weighted undirected
graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the
edges in the tree is minimized.
This algorithm is directly based on the MST( minimum spanning tree) property.

MST-PRIM(G, w, r)

1. for each u V [G]

2. do key[u] ← ∞ 

3. π[u] ← NIL

4. key[r] ← 0

5. Q ← V [G]

6. while Q ≠ Ø

7. do u ← EXTRACT-MIN(Q)

8. for each v Adj[u] 

9. do if v Q and w(u, v) < key[v]


3|Page
Lab Report 03

10. then π[v] ← u

11. key[v] ← w(u, v)

Kruskal’s algorithm:

Kruskal's algorithm is a greedy algorithm in graph theory that finds a minimum spanning tree for a connected
weighted graph.
It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges
in the tree is minimized.
This algorithm is directly based on the MST( minimum spanning tree) property.

MST-KRUSKAL(G, w)

1. A ← Ø

2. for each vertex v V[G]

3. do MAKE-SET(v)

4. sort the edges of E into nondecreasing order by weight w

5. for each edge (u, v) E, taken in nondecreasing order by weight

6. do if FIND-SET(u) ≠ FIND-SET(v)

7. then A ← A {(u, v)}

8. UNION(u, v)

9. return A

4|Page
Lab Report 03

Code and Output:


Prim’s Algorithm:

Code:

1 #include<bits/stdc++.h>
2 #define pii pair<int , int>
3 #define N 1000000
4 using namespace std;
5
6 int n,par[N+1],key[N+1];
7 int visMst[N+1];
8 vector<pii>graph[N+1];
9
10 void Prims(int src)
11 {
12 for(int i=0;i<n;i++)
13 {
14 key[i]=INT_MAX;
15 par[i]=-1;
16 visMst[i]=false;
17 }
18
19 priority_queue<pii,vector<pii >, greater<pii > > pq;
20 key[src]=0;
21 pq.push({0, src});
22 while(!pq.empty())
23 {
24 int u = pq.top().second;
25 pq.pop();
26
27 if(visMst[u]==true)
28 continue;
29 visMst[u]=true;
30
31 for(int i = 0 ; i<graph[u].size() ; i++)
32 {
33 int v = graph[u][i].first;
34 int w = graph[u][i].second;
35
36 if(visMst[v]==false && key[v]>w)
37 {
38 key[v] = w;
39 par[v] = u;
40 pq.push({key[v],v});
41 }
42 }
43 }
44 int minimumCost=0;
45 cout << "Edges and cost of minimum Spanning Tree:\n";
46 for(int i=1;i<n;i++)
47 {

5|Page
Lab Report 03

cout << par[i] << " " << i << "-----> " << key[i] << "\n";
48
minimumCost+=key[i];
49
}
50
cout <<"Minimum cost is "<< minimumCost << endl;
51
}
52
53
int main()
54
{
55
int m,u,v,i,w,s;
56
cout << "Enter number of nodes: ";
57
cin >> n;
58
cout << "Enter number of edges: ";
59
cin >> m;
60
cout << "Enter edges and their weights: \n";
61
for(i=0;i<m;i++)
62
{
63
cin >> u >> v >> w;
64
graph[u].push_back({v,w});
65
graph[v].push_back({u,w});
66
}
67
68
cout << "Enter source node: ";
69
cin >> s;
70
Prims(s);
71
72
return 0;
73
}
74

6|Page
Lab Report 03

Output:

7|Page
Lab Report 03

Kruskal’s algorithm:

Code:

1 #include<bits/stdc++.h>
2 #define N 1000005
3 using namespace std;
4
5 int parent[N],n,e;
6 pair <int, pair<int, int> > p[N];
7
8 int find1(int x)
9{
10 while(parent[x] != x)
11 {
12 parent[x] = parent[parent[x]];
13 x = parent[x];
14 }
15 return x;
16 }
17 void union1(int x, int y)
18 {
19 int p = find1(x);
20 int q = find1(y);
21 parent[p] = parent[q];
22 }
23 int kruskal()
24 {
25 int x, y;
26 int cost, minimumCost = 0;
27 for(int i = 1;i<=n;i++)
28 parent[i] = i;
29 for(int i = 0;i < e;i++)
30 {
31 x = p[i].second.first;
32 y = p[i].second.second;
33 cost = p[i].first;
34 if(find1(x) != find1(y))
35 {
36 minimumCost += cost;
37 union1(x, y);
38 cout << x << " " << y << "-----> " << cost << "\n";
39 }
40 }
41 return minimumCost;
42 }
43 int main()
44 {
45 int u,v,i,w, cost, minimumCost;
46 cout <<"Enter number of Nodes and edges: ";
47 cin >> n >> e;
48
49 cout<<"Enter edges and weight:\n";

8|Page
Lab Report 03

for(i = 0;i < e;i++)


50
{
51
cin >> u >> v >> w;
52
p[i] = make_pair(w, make_pair(u, v));
53
}
54
sort(p, p + e);
55
56
cout << "Edges and cost of minimum Spanning Tree:\n";
57
minimumCost = kruskal();
58
cout <<"Minimum cost is "<< minimumCost << endl;
59
return 0;
60
}
61

Output:

9|Page
Lab Report 03

Discussion:

In this lab, we have learnt to find out minimum spanning tree using Prim’s Algorithm and Kruskal’s Algorithm.
We have implemented the algorithm using code by C++.

Both of the algorithms are greedy approach. In Prim’s Algorithm the spanning tree is not sorted but in
Kruskal’s Algorithm the spanning tree is sorted.

The advantage of Prim's algorithm is its complexity, which is better than Kruskal's algorithm. Therefore, Prim's
algorithm is helpful when dealing with dense graphs that have lots of edges. However, Prim's algorithm
doesn't allow us much control over the chosen edges when multiple edges with the same weight occur.

10 | P a g e

You might also like