-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (78 loc) · 2.41 KB
/
main.cpp
File metadata and controls
78 lines (78 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
int no_of_nodes, no_of_edges;
map<string, int> node_map;
vector<pair<int,pair<int,int>>> edges;
vector<int> parent(1000);
int find(int x){
if(parent[x]==x){
return x;
}
return parent[x]=find(parent[x]);
}
int unite(int x,int y){
x=find(x);
y=find(y);
if(x!=y){
parent[x]=y;
}
}
void kruskal(){
sort(edges.begin(),edges.end());
for(int i=1;i<=no_of_nodes;i++){
parent[i]=i;
}
int cost=0;
vector<pair<pair<string,string>,int>> mst;
ofstream outfile("output.gv");
outfile << "graph MST{\n";
for(auto edge:edges){
int end1=edge.second.first;
int end2=edge.second.second;
int weight=edge.first;
string node1, node2;
for(auto node: node_map){
if(node.second == end1) node1 = node.first;
if(node.second == end2) node2 = node.first;
}
if(find(end1)!=find(end2)){
unite(end1,end2);
cost+=weight;
mst.push_back({{node1,node2},weight});
}
}
cout<<"Minimum Cost:"<<cost<<endl;
cout<<"Edges in MST:"<<endl;
for(auto edge:mst){
cout<< edge.first.first<<"--"<<edge.first.second<<"[label=" << edge.second << "];\n";
outfile << edge.first.first<<"--"<<edge.first.second<<"[label=" << edge.second << "];\n";
}
outfile << "}\n";
outfile.close();
}
int main(){
cout<<"Please enter number of nodes present in the Graph:";
cin>>no_of_nodes;
cout<<"\nPlease enter number of edges present in the Graph:";
cin>>no_of_edges;
ofstream originaloutfile("original.gv");
originaloutfile << "graph Original{\n";
for(int i=0;i<no_of_edges;i++){
string end1,end2;
int weight;
cin>>end1>>end2>>weight;
if(node_map.find(end1) == node_map.end()) node_map[end1] = node_map.size()+1;
if(node_map.find(end2) == node_map.end()) node_map[end2] = node_map.size()+1;
edges.push_back({weight,{node_map[end1],node_map[end2]}});
originaloutfile << end1 << "--" << end2 << " [label=" << weight << "];\n";
}
originaloutfile << "}\n";
originaloutfile.close();
kruskal();
system("dot -Tpng original.gv -o original.png > NUL 2>&1");
system("dot -Tpng output.gv -o output.png > NUL 2>&1");
system("powershell -c .\\original.png");
system("powershell -c .\\output.png");
return 0;
}