-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathall_pairs_shortest_paths.cpp
More file actions
133 lines (114 loc) · 3.55 KB
/
Copy pathall_pairs_shortest_paths.cpp
File metadata and controls
133 lines (114 loc) · 3.55 KB
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
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include "graph.h"
using namespace std;
int** allocate_square_matrix(int n, int init_value = NULL){
int** a = (int**)malloc(n * sizeof(int*));
for(int i=0; i<n; i++){
a[i] = (int*)malloc(n * sizeof(int));
for(int j=0; j<n; j++)
a[i][j] = init_value;
}
return a;
}
// free the matrix
void deallocate_square_matrix(int** a, int n){
int i;
for(i=0; i<n; i++){
free(a[i]);
}
free(a);
}
void initialize_all_pairs(int** graph, int V, int** dis, int** parent){
int i, j;
for(i=0; i<V; i++){
for(j=0; j<V; j++){
dis[i][j] = graph[i][j];
if(graph[i][j] < MAX_DIST)
parent[i][j] = i;
}
dis[i][i] = 0;
parent[i][i] = -1;
}
}
/*
最终的结算结果会存放入L矩阵
*/
void extend_shortest_paths(int** graph, int V, int** L, int** parent){
int i,j,k;
int** L2 = allocate_square_matrix(V, MAX_DIST);
for(i=0; i<V; i++)
for(j=0; j<V; j++){
int tmp = MAX_DIST;
int tmp_p = -1;
for(k=0; k<V; k++){
if(k!=j && L[i][k] + graph[k][j] < tmp){
tmp_p = k;
tmp = L[i][k] + graph[k][j];
}
}
if(tmp < L[i][j]){
parent[i][j] = tmp_p;
L2[i][j] = tmp; // 此时还不可以修改L中的值
}else{
// parent值不变
L2[i][j] = L[i][j];
}
}
for(i=0; i<V; i++)
for(j=0; j<V; j++)
L[i][j] = L2[i][j];
deallocate_square_matrix(L2, V);
}
void slow_all_pairs_shortest_paths(int** graph, int V, int** dis, int** parent){
// init L(1)
initialize_all_pairs(graph, V, dis, parent);
for(int i=2; i<V+1; i++){
extend_shortest_paths(graph, V, dis, parent);
}
}
void Floyd_Warshall(int** graph, int V, int** dis, int** parent){
initialize_all_pairs(graph, V, dis, parent);
int i, j, k;
for(k=0; k<V; k++)
for(i=0; i<V; i++)
for(j=0; j<V; j++){
if(dis[i][j] > dis[i][k] + dis[k][j]){ // k is on the path
dis[i][j] = dis[i][k] + dis[k][j];
parent[i][j] = parent[k][j];
}else if(dis[i][j] == dis[i][k] + dis[k][j]){
/*
// 研究在相等时的动作
cout << "i = " << i << " j = " << j << " k = " << k << endl;
cout << "dis[i][j] = " << dis[i][j] << " ";
cout << "dis[i][k] = " << dis[i][k] << " ";
cout << "dis[k][j] = " << dis[k][j] << endl;
*/
}else{
; // k is not on the shortest path
}
}
}
int main(int argc, const char* argv[]){
if(argc < 2){
printf("./all_pairs_shortest_paths <graph-file-data>");
exit(0);
}
int V;
// 采用邻接矩阵的方式表示
int** graph = read_directed_weighted_graph(argv[1], V);
int** dis = allocate_square_matrix(V, MAX_DIST);
int** parent = allocate_square_matrix(V, -1);
//slow_all_pairs_shortest_paths(graph, V, dis, parent);
Floyd_Warshall(graph, V, dis, parent);
print_all_pair_shortest_distance(dis, V);
print_all_pair_shortest_paths(parent, V);
cout << "Raw values in parent: " << endl;
for(int i=0; i<V; i++){
for(int j=0; j<V; j++){
cout << parent[i][j] << " ";
}
cout << endl;
}
}