Professional Documents
Culture Documents
Ass3 Run But Output Problem
Ass3 Run But Output Problem
Description:
Spanning Tree:
A spanning tree is a subset of Graph G, which has all the vertices covered
with minimum possible number of edges.
A spanning tree does not have cycles and it cannot be disconnected.
1. Kruskal's Algorithm
2. Prim's Algorithm
Both are greedy algorithms. In this article, we will see the implementation of
Kruskal's Algorithm.
Kruskal's Algorithm:
1. Sort all the edges in increasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree
formed so far.
If cycle is not formed, include this edge. Else, discard it.
3. Repeat step 2 until there are (V-1) edges in the spanning tree.
Code:
/*Kruskal Algorithm to find minimum spanning tree (in
undirected
graph)
(Cycle detection using Union find Algorithm)*/
#include <bits/stdc++.h>
using namespace std;
class Edge{
public:
int src;
int dest;
int wt;
};
int main()
{
int n,E;
cin>>n>>E;
Edge edges[E];
for(int i=0;i<E;i++){
cin>>edges[i].src>>edges[i].dest>>edges[i].wt;
}
//Kruskal algo
Edge output[n-1];
return 0;
}
Input:
4 4
0 1 3
0 3 5
1 2 1
2 3 8
Output:
1 2 1
0 1 3
0 3 5