Professional Documents
Culture Documents
Prim Krus Algo 20105077
Prim Krus Algo 20105077
Vinayak Sahni
20105125
ECE
Kruskal's algorithm
#include <bits/stdc++.h>
#define li long long
int using namespace
std;
class Edge{
public:
li start;
li destination;
li weight;
};
bool cmp(const Edge e1, const Edge e2){ return e1.weight < e2.weight;
}
li getparent(li* parent, li index){ while(parent[index] != index){
index = parent[index];
}
return index;
}
OUTPUT:
Prim’s Algorithm
#include <bits/stdc++.h>
#define li long long
int using namespace
std;
signed main() {
li n;
li e;
cin >> n >> e;
li** edges = new li*[n];
for (li i = 0; i < n; i++) {
edges[i] = new li[n];
for (li j = 0; j < n; j++) {
edges[i][j] = 0;
}
}
li minVertex = -1;
for(li i = 0; i < n; i++){
if(!visited[i] && (minVertex == - 1 || weights[i]
< weights[minVertex])){
minVertex = i;
}
}
return minVertex;
}
void prims(li** edges, li n){