-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule5.h
205 lines (178 loc) · 4.44 KB
/
module5.h
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include "Graph.h"
#define infi 2147483647
using namespace std;
struct k_Edge
{
int u;
int v;
int weight;
};
int root(int arr[],int x);
int find(int arr[],int x,int y);
void union_set(int arr[],int x,int y);
void kedge_sort(struct k_Edge arr[],int start,int end);
void kedge_swap(struct k_Edge *x,struct k_Edge *y);
//Main functions
void mst_Prim(Graph G);
void mst_kruskal(Graph G);
void mst_kruskal(Graph G)
{
struct k_Edge edges[G.edge_count];
int k = 0;
for(int i = 1;i <= G.vertex_count;i++)
{
struct edgelist *temp = G.edges[i];
while(temp != NULL)
{
edges[k].u = i;
edges[k].v = temp->value;
edges[k].weight = temp->weight;
k++;
temp = temp->next;
}
}
kedge_sort(edges,0,k - 1);
Graph MST;
MST.weighted = 1;
MST.addVertex(G.vertex_count);
int disjoint[G.vertex_count + 1];
for(int i = 1; i <= G.vertex_count; i++)
disjoint[i] = i;
printf("MST of Given graph by krushkal's algorithm is\nu -> v == weight\n");
int count_krushkal = 0;
int sum_krushkal = 0;
for(int i = 0; i < G.edge_count; i++)
{
if(count_krushkal == G.vertex_count - 1)
break;
if(find(disjoint,edges[i].u,edges[i].v))
{
continue;
}
else
{
union_set(disjoint,edges[i].u,edges[i].v);
MST.addEdge(edges[i].u,edges[i].v,edges[i].weight);
printf("%d -> %d == %d\n",edges[i].u,edges[i].v,edges[i].weight);
sum_krushkal += edges[i].weight;
count_krushkal++;
}
}
printf("Total weight = %d\n",sum_krushkal);
}
//Enter a undirected weighted graph G
void mst_Prim(Graph G)
{
int n = G.vertex_count;
int added[n+1],distance[n+1],mst[n+1],parent[n+1];
int u = 1,v;
int minDistance,count = 0;
struct edgelist *temp;
for(int i = 1; i <= n; i++)
{
added[i] = 0;
distance[i] = infi;
parent[i] = 0;
}
u = 1;
distance[u] = 0;
parent[u] = 0;
while(added[u] == 0)
{
added[u] = 1;
mst[++count] = u;
temp = G.edges[u];
int wei = temp->weight;
while(temp != NULL)
{
//Update the distances from edge u to its neighbourhood if it is minimum edge
v = temp->value;
//IF we have not added v in added and also if he edgecost u-v is less distance[v]
if(added[v] == 0 && temp->weight < distance[v])
{
distance[v] = temp->weight;
parent[v] = u;
}
temp = temp->next;
}
v = 1;
//For selecting nearest available node
minDistance = infi;
for(int i = 1; i <= n; i++)
{
if(added[i] == 0 && minDistance > distance[i])
{
v = i;
minDistance = distance[i];
}
}
u = v;
}
int sum = 0;
for(int i = 1; i <= n; i++)
{
cout << i << " - " << parent[i] << " = " << distance[i] << endl;
sum += distance[i];
}
cout << "Total distance = " << sum << endl;
}
void kedge_swap(struct k_Edge *x,struct k_Edge *y)
{
struct k_Edge temp;
temp.u = x->u;
temp.v = x->v;
temp.weight = x->weight;
x->u = y->u;
x->v = y->v;
x->weight = y->weight;
y->u = temp.u;
y->v = temp.v;
y->weight = temp.weight;
}
//Sort edges for k_edges in krushkal
void kedge_sort(struct k_Edge arr[],int start,int end)
{
if(start < end)
{
int p_weight = arr[end].weight;
int b = start;
for(int i = start; i < end ;i++)
{
if(arr[i].weight < p_weight)
{
kedge_swap(&arr[i],&arr[b]);
b++;
}
}
kedge_swap(&arr[end],&arr[b]);
kedge_sort(arr,start,b-1);
kedge_sort(arr,b + 1,end);
}
}
//return root of x
int root(int arr[],int x)
{
while(arr[x] != x)
{
arr[x] = arr[arr[x]];
x = arr[x];
}
return x;
}
//joins both set
void union_set(int arr[],int x,int y)
{
int root_x = root(arr,x);
int root_y = root(arr,y);
arr[root_x] = root_y;
}
//Return 1 when both have same root
int find(int arr[],int x, int y)
{
int root_x = root(arr,x);
int root_y = root(arr,y);
if(root_x == root_y)
return 1;
return 0;
}