-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim's_Algorithm.cpp
More file actions
142 lines (130 loc) · 3.1 KB
/
Prim's_Algorithm.cpp
File metadata and controls
142 lines (130 loc) · 3.1 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
#include <iostream.h>
#include <conio.h>
#include <timer.h>
#include <stdlib.h>
#define MAX 20
class Edge
{
public :
int start,end,weight;
Edge()
{
start = end = weight = 0;
}
void print()
{
cout << "From " << start << " To " << end << " (wt : "<< weight<<")\n";
}
};
class Graph
{
int edges[MAX][MAX],nodes,sides,x,*visited,v,Wsum,*counter;
Edge *e;
public :
Graph()
{
nodes = sides = Wsum = 0;
x = v = -1;
}
void input();
void start_PRIMS_frm(int);
void printTree();
void randomize();
};
void Graph :: input()
{
while(1)
{
cout << "\nGive the number of nodes : ";cin >> nodes;
if(nodes >= MAX) cout << "Give number of nodes less than 20\n";
else break;
}
visited = new int [nodes];counter = new int [nodes];
for(int k = 0;k < nodes;k++)
for(int l = 0;l < nodes;l++)
{
visited[k] = 0;
edges[k][l] = 999;
}
cout << "Give the number of edges : ";cin >> sides;
e = new Edge [sides];
cout << "\nNow give edges one-by-one\n";
while(sides--)
{
int start,end,weight;
cout << "From : ";cin >> start;
cout << "To : ";cin >> end;
cout << "Weight : ";cin >> weight;cout << "\n";
edges[start - 1][end - 1] = weight;edges[end - 1][start - 1] = weight;
}
}
void Graph :: start_PRIMS_frm(int source)
{
int min = 999,st,en;
counter[++v] = source - 1;visited[source - 1] = 1;
while(x < nodes - 2)
{
for(int i = 0;i <= v;i++)
for(int j = 0;j < nodes;j++)
{
if(visited[j]) continue;
if(edges[counter[i]][j] < min)
{
min = edges[counter[i]][j];
st = counter[i] + 1;en = j + 1;
}
}
e[++x].start = st;e[x].end = en;e[x].weight = min;
Wsum += min;min = 999;
counter[++v] = en - 1;visited[en - 1] = 1;
}
}
void Graph :: printTree()
{
cout << "\nWrite all the nodes first\n";
for(int i = 0;i <= x;i++)
{
cout << "Then draw ";
e[i].print();cout << "\n";
}
cout << "\nWeight of the minimum spanning tree thus formed : " << Wsum;
cout << "\n";Wsum = 0;
}
void Graph :: randomize()
{
Timer t;
for(int i = 1;i < MAX;i++)
{
nodes = i;
visited = new int [nodes];counter = new int [nodes];
x = v = -1;
for(int k = 0;k < nodes;k++) visited[k] = 0;
for(int row = 0;row < nodes;row++)
for(int col = 0;col < nodes;col++)
edges[row][col] = -1;
for(int w = 0;w < nodes;w++)
for(int s = 0;s < nodes;s++)
{
int tmp = (rand() % 999) + 1;
if(edges[w][s] != -1) continue;
else edges[w][s] = edges[s][w] = tmp;
}
t.start();start_PRIMS_frm(1);t.stop();
cout << "\nFor operation : " << nodes;
cout << " time taken is : " << t.time() << " seconds";
t.reset();
}
}
int main()
{
Graph G;int src = 1;
Timer t;clrscr();
G.input();
cout << "Give the source node (where to start) : ";cin >> src;
t.start();G.start_PRIMS_frm(src);t.stop();
G.printTree();
cout << "\nTime taken for this is : " << t.time() << " seconds\n";
//G.randomize(); //uncomment this only for bulk operation
getch();
return 0;
}