-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarshall.cpp
More file actions
87 lines (62 loc) · 1.71 KB
/
warshall.cpp
File metadata and controls
87 lines (62 loc) · 1.71 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
/* Warshall-floyd algorithm
*
* Shortest path between all pairs of vertices
* strategy: dynamic programming
* order: n **3
*/
#include <limits.h>
#include <stdio.h>
typedef int adj_matrix [10][10];
#define MAX_VERTEX 9
#define FOUND 0
adj_matrix gr = {
{ 0, 5, 3, INT_MAX, 2 },
{ INT_MAX, 0, 2, 6, INT_MAX },
{ INT_MAX, 1, 0, 2, INT_MAX },
{ INT_MAX, INT_MAX, INT_MAX, 0, INT_MAX },
{ INT_MAX, 6, 10, 4, 0 }
};
// This directed graph has five vertices.
#define NUM_VERT 5
// as on gr
char first_hop[NUM_VERT][NUM_VERT];
// first_hop[i][j] contains the first hop in the path between i and j
void warshall() {
int k,i,j;
int hopdist;
for( k = 0; k < NUM_VERT; k ++ ) {
for( i = 0; i < NUM_VERT; i ++ ) { // row index
for( j = 0; j < NUM_VERT; j ++ ) { // column index
if(gr[i][k] == INT_MAX || gr[k][j] == INT_MAX )
hopdist = INT_MAX;
else // can hop to k
hopdist = gr[i][k] + gr[k][j];
if( hopdist < gr[i][j]) { // if hopping to k is better
first_hop[i][j] = k; // if you want to go from i to j then first go to k
gr[i][j] = hopdist;
}
}
}
}
}
void print_first_hop() {
int i,j;
for( i = 0; i < NUM_VERT; i ++ ) {
for( j = 0 ; j < NUM_VERT; j ++ )
printf("%d ",first_hop[i][j]);
printf("\n");
}
}
int main() {
int i, j;
for( i = 0; i < NUM_VERT; i ++ ) {
for( j = 0; j < NUM_VERT; j ++ ) {
if( gr[i][j] != INT_MAX ) // if edge exists
first_hop[i][j] = j;
else first_hop[i][j] = -1; // not known yet
}
}
warshall();
printf("the first hop in the shortest path from any vertex i to j is \n");
print_first_hop();
}