-
Notifications
You must be signed in to change notification settings - Fork 0
/
mst_k.c
155 lines (128 loc) · 2.67 KB
/
mst_k.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 12
#define NODE_N 7
#define MIN 100
typedef int vtype;
typedef struct MAP{
int nv;//number of vertexes
int ne;//number of edges
vtype v[MAX][MAX];//array -> weight & edge
}map, *map_ptr;
typedef struct ENode{//for each edge
vtype i, j;
int weight;
}en, *enp;
map_ptr create_map(int num){//create a map without any edges
vtype i, j;
map_ptr mst = (map_ptr)malloc(sizeof(map));
mst->nv = num;
mst->ne = 0;
for (i = 0; i < mst->nv; i++){
for (j = 0; j < mst->nv; j++){
mst->v[i][j] = 0;//initialize all edge as 0
}
}
return mst;
}
void insert_edge(map_ptr mst, enp enode){
(mst->ne)++;
mst->v[enode->i][enode->j] = enode->weight;
mst->v[enode->j][enode->i] = enode->weight;
}
enp create_node(vtype i, vtype j, int weight){
enp node = (enp)malloc(sizeof(en));
node->i = i;
node->j = j;
node->weight = weight;
return node;
}
int in_list(vtype a[], vtype n){//check whether is a ring
for (int i = 0; i < NODE_N; i++){
if (a[i] == n){
return 1;
}
}
return 0;
}
void insert(vtype b[], int *num, enp node){
vtype x = node->i;
vtype y = node->j;
if (!in_list(b, x)){
b[(*num)++] = x;
}
if (!in_list(b, y)){
b[(*num)++] = y;
}
}
void swap(enp a[], int i, int j){
enp temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void select_sort(enp a[], int num){
int i, j, min;
for (i = 0; i < num; i++){
min = i;
for (j = i+1; j < num; j++){
if (a[j]->weight < a[min]->weight){
min = j;
}
}
swap(a, i, min);
}
}
int f[100];
int find_f(int num){
while (num != f[num]){
num = f[num];
}
return num;
}
void join(int x, int y){
int fx = find_f(x);
int fy = find_f(y);
if (fx != fy){
f[fy] = fx;
}
}
int main(void){
map_ptr mst = (map_ptr)malloc(sizeof(map));
mst = create_map(NODE_N);
enp node[MAX];
int i;
for (i = 0; i < MAX; i++){
node[i] = NULL;
}
node[0] = create_node(0, 1, 2);
node[1] = create_node(0, 2, 4);
node[2] = create_node(0, 3, 1);
node[3] = create_node(1, 3, 3);
node[4] = create_node(1, 4, 10);
node[5] = create_node(2, 3, 2);
node[6] = create_node(2, 5, 5);
node[7] = create_node(3, 4, 7);
node[8] = create_node(3, 5, 8);
node[9] = create_node(3, 6, 4);
node[10] = create_node(4, 6, 6);
node[11] = create_node(5, 6, 1);
for (i = 0; i < MAX; i++){
insert_edge(mst, node[i]);
}
select_sort(node, MAX);
for (i = 0; i < NODE_N; i++){
f[i] = i;
}
int edge_num = 0, sum = 0;
for (i = 0; i < MAX && edge_num < NODE_N-1; i++){
if (find_f(node[i]->j) != find_f(node[i]->i)){
join(node[i]->i, node[i]->j);
printf("(%d, %d) ", node[i]->j, node[i]->i);
sum += node[i]->weight;
edge_num++;
}
}
putchar('\n');
printf("sum: %d\n", sum);
return 0;
}