forked from SkienaBooks/Algorithm-Design-Manual-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloyd.c
147 lines (110 loc) · 3.59 KB
/
floyd.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
140
141
142
143
144
145
146
147
/* floyd.c
Compute all-pairs shortest paths in weighted graphs.
by: Steven Skiena
begun: March 26, 2002
*/
/*
Copyright 2003 by Steven S. Skiena; all rights reserved.
Permission is granted for use in non-commerical applications
provided this copyright notice remains intact and unchanged.
This program appears in my book:
"Programming Challenges: The Programming Contest Training Manual"
by Steven Skiena and Miguel Revilla, Springer-Verlag, New York 2003.
See our website www.programming-challenges.com for additional information.
This book can be ordered from Amazon.com at
http://www.amazon.com/exec/obidos/ASIN/0387001638/thealgorithmrepo/
*/
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
/************************************************************/
#define MAXV 100 /* maximum number of vertices */
#define MAXDEGREE 50 /* maximum outdegree of a vertex */
#define MAXINT 100007
typedef struct {
int v; /* neighboring vertex */
int weight; /* edge weight */
bool in; /* is the edge "in" the solution? */
} edge;
typedef struct {
edge edges[MAXV+1][MAXDEGREE]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
} graph;
/* [[[ amatrix_cut */
typedef struct {
int weight[MAXV+1][MAXV+1]; /* adjacency/weight info */
int nvertices; /* number of vertices in the graph */
} adjacency_matrix;
/* ]]] */
void initialize_adjacency_matrix(adjacency_matrix *g) {
int i, j; /* counters */
g->nvertices = 0;
for (i = 1; i <= MAXV; i++) {
for (j = 1; j <= MAXV; j++) {
g->weight[i][j] = MAXINT;
}
}
}
void read_adjacency_matrix(adjacency_matrix *g, bool directed) {
int i; /* counter */
int m; /* number of edges */
int x, y, w; /* placeholder for edge and weight */
initialize_adjacency_matrix(g);
scanf("%d %d\n", &(g->nvertices), &m);
for (i = 1; i <= m; i++) {
scanf("%d %d %d\n", &x, &y, &w);
g->weight[x][y] = w;
if (directed == FALSE) {
g->weight[y][x] = w;
}
}
}
void print_graph(adjacency_matrix *g) {
int i, j; /* counters */
for (i = 1; i <= g->nvertices; i++) {
printf("%d: ",i);
for (j = 1; j <= g->nvertices; j++) {
if (g->weight[i][j] < MAXINT) {
printf(" %d",j);
}
}
printf("\n");
}
}
void print_adjacency_matrix(adjacency_matrix *g) {
int i, j; /* counters */
for (i = 1; i <= g->nvertices; i++) {
printf("%3d: ",i);
for (j = 1; j <= g->nvertices; j++) {
printf(" %3d", g->weight[i][j]);
}
printf("\n");
}
}
/* [[[ floyd_cut */
void floyd(adjacency_matrix *g) {
int i, j; /* dimension counters */
int k; /* intermediate vertex counter */
int through_k; /* distance through vertex k */
for (k = 1; k <= g->nvertices; k++) {
for (i = 1; i <= g->nvertices; i++) {
for (j = 1; j <= g->nvertices; j++) {
through_k = g->weight[i][k]+g->weight[k][j];
if (through_k < g->weight[i][j]) {
g->weight[i][j] = through_k;
}
}
}
}
}
/* ]]] */
int main(void) {
adjacency_matrix g;
read_adjacency_matrix(&g, FALSE);
print_graph(&g);
floyd(&g);
print_adjacency_matrix(&g);
return 0;
}