-
Notifications
You must be signed in to change notification settings - Fork 0
/
main21.c
130 lines (116 loc) · 3.19 KB
/
main21.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
//
// Created by ge on 2023/12/20.
//
#include <stdlib.h>
#include <stdio.h>
#define MAX 32767
typedef struct Graph {
char *vexs;
int **arcs;
int vexNum;
int arcNum;
} Graph;
typedef struct Edge {
int start;
int end;
int weight;
} Edge;
Edge *initEdge(Graph *G) {
Edge *edge = (Edge *) malloc(sizeof(Edge) * G->arcNum);
int index = 0;
for (int i = 0; i < G->vexNum; i++) {
for (int j = i + 1; j < G->vexNum; j++) {
if (G->arcs[i][j] != MAX) {
edge[index].start = i;
edge[index].end = j;
edge[index].weight = G->arcs[i][j];
index++;
}
}
}
return edge;
}
void sortEdge(Edge *edge, Graph *G) {
Edge temp;
for (int i = 0; i < G->arcNum - 1; i++) {
for (int j = 0; j < G->arcNum - 1 - i; j++) {
if (edge[j].weight > edge[j + 1].weight) {
temp = edge[j];
edge[j] = edge[j + 1];
edge[j + 1] = temp;
}
}
}
}
void kruskal(Graph *G) {
int *connected = (int *) malloc(sizeof(int) * G->vexNum);
for (int i = 0; i < G->vexNum; i++) {
connected[i] = i;
}
Edge *edge = initEdge(G);
sortEdge(edge, G);
for (int i = 0; i < G->arcNum; i++) {
int start = connected[edge[i].start];
int end = connected[edge[i].end];
if (start != end) {
printf("v%c --> v%c weight = %d\n",G->vexs[edge[i].start],
G->vexs[edge[i].end],edge[i].weight);
for (int j = 0; j < G->vexNum; j++) {
if (connected[j] == end){
connected[j] = start;
}
}
}
}
}
Graph *initGraph(int vexNum) {
Graph *G = (Graph *) malloc(sizeof(Graph));
G->vexs = (char *) malloc(sizeof(char) * vexNum);
G->arcs = (int **) malloc(sizeof(int *) * vexNum);
for (int i = 0; i < vexNum; i++) {
G->arcs[i] = (int *) malloc(sizeof(int) * vexNum);
}
G->vexNum = vexNum;
G->arcNum = 0;
return G;
}
void createGraph(Graph *G, char *vexs, int *arcs) {
for (int i = 0; i < G->vexNum; i++) {
G->vexs[i] = vexs[i];
for (int j = 0; j < G->vexNum; j++) {
G->arcs[i][j] = *(arcs + i * G->vexNum + j);
if (G->arcs[i][j] != 0 && G->arcs[i][j] != MAX) {
G->arcNum++;
}
}
}
G->arcNum /= 2;
}
void DFS(Graph *G, int *visited, int vexIndex) {
printf("%c\t", G->vexs[vexIndex]);
visited[vexIndex] = 1;
for (int i = 0; i < G->vexNum; i++) {
if (G->arcs[vexIndex][i] > 0 && G->arcs[vexIndex][i] != MAX && !visited[i]) {
DFS(G, visited, i);
}
}
}
int main() {
Graph *G = initGraph(6);
int *visited = malloc(sizeof(int) * G->vexNum);
for (int i = 0; i < G->vexNum; i++) {
visited[i] = 0;
}
int arcs[6][6] = {
0, 6, 1, 5, MAX, MAX,
6, 0, 5, MAX, 3, MAX,
1, 5, 0, 5, 6, 4,
5, MAX, 5, 0, MAX, 2,
MAX, 3, 6, MAX, 0, 6,
MAX, MAX, 4, 2, 6, 0
};
createGraph(G, "123456", (int *) arcs);
DFS(G, visited, 0);
printf("\n");
kruskal(G);
}