-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim.cpp
More file actions
171 lines (145 loc) · 3.77 KB
/
Copy pathPrim.cpp
File metadata and controls
171 lines (145 loc) · 3.77 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/****************************************
* Prim's implementation for finding MST
****************************************/
#include<iostream>
#include<fstream>
#include<list>
#include<vector>
#include<set>
#include<string>
#include <sstream> //For stringstream()
using namespace std;
//Def of a node
typedef struct nodes
{
int dest;
int cost;
}node;
class Graph
{
int n;
list<node> *adjList;
int total_cost;
private:
void showList(int src, list<node> lt) {
list<node> :: iterator i;
node tempNode;
for(i = lt.begin(); i != lt.end(); i++) {
tempNode = *i;
cout << "Edge (" << src <<","<<tempNode.dest<<") " << "Edge cost-"<<tempNode.cost<<"\t";
}
cout << endl;
}
public:
Graph()
{
total_cost=0;
n = 0;
}
Graph(int nodeCount)
{
total_cost=0;
n = nodeCount;
adjList = new list<node>[n];
}
void addEdge(int source, int dest, int cost) {
node newNode;
newNode.dest = dest;
newNode.cost = cost;
adjList[source].push_back(newNode);
}
void displayEdges() {
for(int i = 0; i<n; i++) {
list<node> tempList = adjList[i];
showList(i, tempList);
}
cout<<"Total most of MST is "<<this->total_cost;
}
friend Graph primsMST(Graph g, int start);
};
set<int> difference(set<int> first, set<int> second)
{
set<int> :: iterator it;
set<int> res;
for(it = first.begin(); it != first.end(); it++)
{
if(second.find(*it) == second.end())
res.insert(*it); //add those item which are not in the second list
}
return res; //the set (first-second)
}
Graph primsMST(Graph g, int start)
{
int n = g.n;
set<int> B, N, diff;
Graph tree(n); //make tree with same node as graph
B.insert(start); //insert start node in the B set
for(int u = 0; u<n; u++)
N.insert(u); //add all vertices in the N set
while(B != N)
{
int min = INT_MAX; //set as infinity
int v, par;
diff = difference(N, B); //find the set N - B
for(int u = 0; u < n; u++)
{
if(B.find(u) != B.end())
{
list<node>::iterator it;
for(it = g.adjList[u].begin(); it != g.adjList[u].end(); it++)
{
if(diff.find(it->dest) != diff.end())
{
if(min > it->cost)
{
min = it->cost; //update cost
par = u;
v = it->dest;
}
}
}
}
}
//Add node v in closed set B
B.insert(v);
tree.addEdge(par, v, min);
tree.addEdge(v, par, min);
//update total MST cost till node v
tree.total_cost+=min;
}
return tree;
}
int main()
{
/* Take input from test file */
std::ifstream read("test.txt");
string str;
vector<int> vect;
if (read.is_open())
{
while (read)
{
getline(read, str);
stringstream ss(str);
int i;
while(ss >> i)
{
vect.push_back(i);
if(ss.peek() == ',')
ss.ignore();
}
}
}
//Total number of nodes in Graph
int n=vect.at(0);
//Initialize Graph and MST with total number of nodes
Graph g(n), tree(n);
//Add edges as per given input
for(int j=1;j < vect.size();j=j+3)
g.addEdge(vect.at(j),vect.at(j+1),vect.at(j+2));
int src=0;
//Call MST for g with src=0 as starting node
tree = primsMST(g, src);
//Display entire tree and total cost which is also a form of gaph
tree.displayEdges();
}