-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprim.c
140 lines (127 loc) · 2.12 KB
/
prim.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
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "limits.h"
struct list
{
int wt;
int vertex;
};
struct node
{
int key;
struct list list;
struct node* next;
};
int parent(int i) { return (i-1)/2; }
void top_down_heapify(struct node* a[], int len, int i){
int l=2*i +1;
int r=2*i +2,j;
struct node *t = (struct node *)malloc(sizeof(struct node));
if(l==len-1 && a[i]->key>a[l]->key){
t=a[l];
a[l]=a[i];
a[i]=t;
return;
}
while(r<len){
if(a[l]->key<a[r]->key)
j=l;
else
j=r;
if(a[i]->key<a[j]->key)
break;
else{
t=a[j];
a[j]=a[i];
a[i]=t;
}
i=j;
l=2*i+1;
r=2*i+1;
if(l==len-1&&a[i]->key>a[l]->key){
t=a[l];
a[l]=a[i];
a[i]=t;
break;
}
}
}
void build_heap(struct node* a[],int n){
int i=n/2;
while(i>=0){
top_down_heapify(a,n,i);
i--;
}
}
struct node* delete_min(struct node *a[],int *n)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp=a[*n-1];
a[*n-1]=a[0];
a[0]=temp;
temp=a[*n-1];
(*n)--;
top_down_heapify(a,*n,0);
return temp;
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void decreaseKey(int harr[],int i, int new_val)
{
harr[i] = new_val;
while (i != 0 && harr[parent(i)] > harr[i])
{
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
}
void addedge(struct node* a[],int u,int v,int wt){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->list.vertex=v;
temp->list.wt=wt;
temp->list.key=INT_MAX;
temp->next=a[u];
a[u]=temp;
struct node* temp1=(struct node*)malloc(sizeof(struct node));
temp1->list.vertex=u;
temp1->list.wt=wt;
temp1->list.key=INT_MAX;
temp1->next=a[v];
a[v]=temp1;
}
void prim(struct node* a[],int r){
int color[10];
int pi[10]''
for (int i = 0; i < 10; ++i)
{
color[i]=0;
a[i].key=INT_MAX;
pi[i]=-1;
}
a[r]->key=0;
pi[r]=0;
build_heap(a,n);
}
int main()
{
struct node* a[10];
for (int i = 0; i < 10; ++i)
{
a[i]=NULL;
}
addedge(a,0,1,2);
addedge(a,0,3,6);
addedge(a,1,2,3);
addedge(a,1,3,8);
addedge(a,1,4,5);
addedge(a,2,4,7);
addedge(a,3,4,9);
int key[10];
int pi[10];
return 0;
}