-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskalMST.c
127 lines (112 loc) · 2.77 KB
/
KruskalMST.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
#include "stdio.h"
#include "stdlib.h"
#define SIZE 500
struct edge{
char so;
char dis;
int w;
};
typedef struct edge* edgeptr;
struct qu{
edgeptr data[SIZE];
int last;
};
typedef struct qu* queue;
int compare(edgeptr x, edgeptr y){
return x->w < y->w ? 1 : 0;
}
void reheapup(queue q, int pos){
edgeptr temp;
if(pos <= 0){
return;
}
if(compare(q->data[pos],q->data[(pos-1)/2])){
temp = q->data[pos];
q->data[pos] = q->data[(pos-1)/2];
q->data[(pos-1)/2] = temp;
reheapup(q, (pos-1)/2);
}
}
int reheapdown(queue q, int pos){
edgeptr temp;
if(pos*2+1 >= q->last){
return;
}
if(pos*2+2 == q->last || compare(q->data[pos*2+1], q->data[pos*2+2])){
if(compare(q->data[pos*2+1], q->data[pos])){
temp = q->data[pos];
q->data[pos] = q->data[pos*2+1];
q->data[pos*2+1] = temp;
reheapdown(q, pos*2+1);
}
}else{
if(compare(q->data[pos*2+2], q->data[pos])){
temp = q->data[pos];
q->data[pos] = q->data[pos*2+2];
q->data[pos*2+2] = temp;
reheapdown(q, pos*2+2);
}
}
}
void enqueue(queue q, edgeptr e){
q->data[q->last] = e;
reheapup(q, q->last);
q->last++;
}
edgeptr dequeue(queue q){
edgeptr temp = q->data[0];
q->last--;
q->data[0] = q->data[q->last];
reheapdown(q, 0);
return temp;
}
void replaceDisjoin(int disjoinSet[], int size, int from, int to){
int i = 0;
for(i = 0; i < size; i++){
if(disjoinSet[i] == from){
disjoinSet[i] = to;
}
}
}
int main(){
int adjMartix[SIZE][SIZE];
int disjoinSet[SIZE];
int n,m;
char s,d;
int w;
int i,j,isALL;
queue edgeList = (queue)malloc(sizeof(struct qu));
edgeptr e;
edgeList->last = 0;
scanf("%d%d", &n, &m);
for(i = 0; i < m; i++){
scanf(" %c %c %d", &s, &d, &w);
e = (edgeptr)malloc(sizeof(struct edge));
e->so = s - 'a';
e->dis = d - 'a';
e->w = w;
enqueue(edgeList, e);
}
for(i = 0; i < n; i++){
disjoinSet[i] = i;
}
while(edgeList->last > 0){
e = dequeue(edgeList);
if(disjoinSet[e->so] != disjoinSet[e->dis]){
adjMartix[e->so][e->dis] = e->w;
adjMartix[e->dis][e->so] = e->w;
if(disjoinSet[e->so] < disjoinSet[e->dis]){
replaceDisjoin(disjoinSet, n, disjoinSet[e->dis], disjoinSet[e->so]);
}else{
replaceDisjoin(disjoinSet, n, disjoinSet[e->so], disjoinSet[e->dis]);
}
}
free(e);
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("%d ", adjMartix[i][j]);
}
printf("\n");
}
}