forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prims_Algorithm.c
162 lines (139 loc) · 4.38 KB
/
Prims_Algorithm.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node* ptrarr[1000005];
long long int vis[1000005];
struct node{
long long int value;
long long int weight;
struct node* next;
};
void adjinsert(struct node** head,long long int val,long long int weight){
struct node* first = (struct node*)malloc(sizeof(struct node));
first->value = val;
first->weight = weight;
first->next = *head;
*head = first;
}
struct node arr[1000005];
long long int heapsize;
void heapify(long long int i){
long long int left = arr[2*i].weight;
long long int right = arr[2*i+1].weight;
long long int root = arr[i].weight;
long long int lv = arr[2*i].value;
long long int rv = arr[2*i+1].value;
long long int mv = arr[i].value;
if(i>=1 && i<=heapsize && (2*i+1<=heapsize || 2*i<=heapsize )){
if( left <= right && left <= root){
arr[i].weight = left;
arr[i].value = lv;
arr[2*i].weight = root;
arr[2*i].value = mv;
heapify(2*i);
}
else if( right < left && right < root){
arr[i].weight = right;
arr[i].value = rv;
arr[2*i+1].weight = root;
arr[2*i+1].value = mv;
heapify(2*i+1);
}
}
}
void formHeap(){
long int i;
for(i=heapsize/2;i>=1;i--){
heapify(i);
}
}
void insert(long long int vertex, long long int val){
heapsize++;
arr[heapsize].weight = val;
arr[heapsize].value = vertex;
long long int parent = heapsize/2;
long long int child = heapsize;
while(arr[parent].weight > val && parent >= 1){
arr[child].weight = arr[parent].weight;
arr[parent].weight = val;
arr[child].value = arr[parent].value;
arr[parent].value = vertex;
child = parent;
parent = parent/2;
}
return;
}
struct node pop(){
struct node poped;
poped.weight = arr[1].weight;
poped.value = arr[1].value;
arr[1].weight = arr[heapsize].weight;
arr[1].weight = arr[heapsize].weight;
arr[1].value = arr[heapsize].value;
arr[1].value = arr[heapsize].value;
heapsize--;
heapify(1);
return poped;
}
void print(){
long long int i;
printf("-------------------HEAP-----------------\n");
for(i=1;i<=heapsize;i++){
printf("v: %lld w: %lld ",arr[i].value, arr[i].weight);
printf("\n");
}
printf("----------------------------------------\n");
}
long long int res,counter,spanedge;
struct node poped;
long long int prims(long long int v){
if(spanedge == counter)
return res;
struct node * current;
current = ptrarr[v];
while(current!=NULL){
if(vis[current->value]==0){
insert(current->value,current->weight);
}
current = current->next;
}
poped = pop();
while(vis[poped.value]==1){
poped = pop();
}
res = res + poped.weight;
counter++;
vis[poped.value] = 1;
return prims(poped.value);
}
int main(){
long long int total,t,x,i,v,e,v1,v2,w;
t = 1;
srand(time(NULL));
for(x=0;x<t;x++){
for(i=0;i<1000005;i++){
ptrarr[i] = NULL;
vis[i] = 0;
}
v = 5;
e = 10;
heapsize = 0;
total = 0;
spanedge = v-1;
res = 0;
counter = 0;
for(i=0;i<e;i++){
v1 = (long long int) (rand() % v + 1);
v2 = (long long int) (rand() % v + 1);
w = (long long int) rand()%1000;
if(v1!=v2){
adjinsert(&ptrarr[v1],v2,w);
adjinsert(&ptrarr[v2],v1,w);
}
total += w;
}
vis[1] = 1;
printf("Weight of minimum spanning tree : %lld\n", prims(1));
}
return 0;
}