-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfloyd.c
More file actions
125 lines (102 loc) · 3.01 KB
/
Copy pathfloyd.c
File metadata and controls
125 lines (102 loc) · 3.01 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
/* floyd.c
*
* Compute all-pairs shortest paths in weighted graphs.
*
* by: Steven Skiena
*
* 2015-05-19 13:07
* Altered by James Shi
*/
#include <stdio.h>
#include <stdlib.h>
#include "bool.h"
#define MAXV 100 /* maximum number of vertices */
#define MAXDEGREE 50 /* maximum number 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;
/* never used */
typedef struct {
edge edges[MAXV+1][MAXV+1]; /* adjacency info */
int degree[MAXV+1]; /* out degree of each vertex */
int nvertices; /* number of vertices in the graph */
int nedges; /* number of edges in the graph */
} graph;
typedef struct {
int weight[MAXV+1][MAXV+1]; /* adjacency/weight info */
int nvertices;
} adjacency_matrix;
void initialize_adjacency_matrix(adjacency_matrix *g)
{
int i, j; /* counter */
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;
int m;
int x, y, w;
initialize_adjacency_matrix(g);
scanf("%d %d", &(g->nvertices), &m);
for (i=0; i<m; ++i) {
scanf("%d%d%d", &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("%3d:", i);
for (j=1; j<=(g->nvertices); ++j)
if (g->weight[i][j] < MAXINT) printf(" %3d", 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");
}
}
void floyd(adjacency_matrix *g)
{
int i, j;
int k;
int through_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()
{
bool directed = TRUE; /* FALSE for floyd.in01, TRUE for floyd.in02 */
adjacency_matrix g;
scanf("%d", &directed);
read_adjacency_matrix(&g, directed); /* or you can build a adjacency matrix based on graph */
printf("graph is:\n");
print_graph(&g);
floyd(&g);
printf("all-pairs shorest path by floyd:\n");
print_adjacency_matrix(&g);
if (directed == FALSE)
printf("you must find the path form i to i is not 0,\nthe value is the smallest weight of its edges\nmultiply by 2.\n");
else
printf("what's the meaning of i to i? the shorest directed cycle, right?\n");
return 0;
}