-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_bf.c
110 lines (98 loc) · 2.56 KB
/
st_bf.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
#include <stdio.h>
#include <stdlib.h>
#define MAX 100001
//for edge
typedef int vtype;
typedef struct Edge{
vtype x, y;
int weight;
}edge, *edge_ptr;
typedef struct Graph{
vtype **map;
int num_e, num_v;
}graph, *graph_ptr;
graph_ptr create_graph(int size){
graph_ptr graph = (graph_ptr)malloc(sizeof(graph));
graph->map = (vtype **)malloc(size * sizeof(vtype*));
for (int i = 0; i < size; i++){
graph->map[i] = (vtype *)malloc(size * sizeof(vtype));
}
graph->num_e = 0;
graph->num_v = size;
return graph;
}
edge_ptr create_edge(vtype x, vtype y, int weight){
edge_ptr edge = (edge_ptr)malloc(sizeof(edge));
edge->x = x;
edge->y = y;
edge->weight = weight;
return edge;
}
void insert_edge(graph_ptr graph, edge_ptr edge, edge_ptr list[]){
list[graph->num_e] = edge;
graph->num_e++;
graph->map[edge->x][edge->y] = edge->weight;
}
void bellman_ford(int size, int start, graph_ptr graph, edge_ptr list[]){
int cost[size], pre[size];
for (int i = 0; i < size; i++){
pre[i] = -1;
cost[i] = MAX;
}
cost[start] = 0;
for (int i = 0; i < size-1; i++){
for (int j = 0; j < graph->num_e; j++){
if (cost[list[j]->x] + list[j]->weight < cost[list[j]->y]){
cost[list[j]->y] = cost[list[j]->x] + list[j]->weight;
pre[list[j]->y] = list[j]->x;
}
}
}
for (int i = 0; i < size; i++){
int temp = i;
if (temp != start){
while (temp != start && pre[temp] != -1){
printf("%d -> ", temp);
temp = pre[temp];
}
printf("%d\n", start);
}
}
printf("cost\n");
for (int i = 0; i < size; i++){
printf("%d ", cost[i]);
}
putchar('\n');
printf("pre\n");
for (int i = 0; i < size; i++){
printf("%d ", pre[i]);
}
putchar('\n');
}
int main(void){
int size = 7;
graph_ptr graph = create_graph(size);
edge_ptr list[20];
list[20] = (edge_ptr)malloc(sizeof(edge)*20);
insert_edge(graph, create_edge(0, 1, 2), list);
insert_edge(graph, create_edge(0, 3, 1), list);
insert_edge(graph, create_edge(1, 3, 3), list);
insert_edge(graph, create_edge(1, 4, 10), list);
insert_edge(graph, create_edge(2, 0, 4), list);
insert_edge(graph, create_edge(2, 5, 5), list);
insert_edge(graph, create_edge(3, 2, 2), list);
insert_edge(graph, create_edge(3, 4, 7), list);
insert_edge(graph, create_edge(3, 5, 8), list);
insert_edge(graph, create_edge(3, 6, 4), list);
insert_edge(graph, create_edge(4, 6, 6), list);
insert_edge(graph, create_edge(6, 5, 1), list);
for (int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
printf("%2d ", graph->map[i][j]);
}
putchar('\n');
}
printf("%d\n", size);
bellman_ford(size, 0, graph, list);
return 0;
}