-
Notifications
You must be signed in to change notification settings - Fork 3
/
floyd_apsp.c
142 lines (125 loc) · 3.98 KB
/
floyd_apsp.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
/* ============================================================= */
/* === MATLAB/floyd mexFunction ============================= */
/* ============================================================= */
/* ----------------------------------------------------------------
* Compute all-pair shortest paths using Floyd's algorithm.
*
* Copyright (c) 2005-2006 Yin Zhang <yzhang@cs.utexas.edu>
* ----------------------------------------------------------------
*/
/* $Header: /u/yzhang/PrivacyTE/src/Package/RCS/floyd_apsp.c,v 1.1 2005/12/29 05:24:18 yzhang Exp $ */
#include "mex.h"
#include "matrix.h"
void FloydAPSP (int N, double** C, double** D, double** P)
{
int i,j,k;
#ifndef USE_ROW_ORDER /* USE_COLUMN_ORDER */
/*
* When C, D, P are stored in column order (as in MATLAB), we have
*
* C[i][j] gives the weight from j to i
* D[i][j] gives the distance from j to i
* P[i][j] gives the predecessor of i (from j to i)
*/
/* initialization */
for (j = 0; j < N; j++) {
for (i = 0; i < N; i++) {
D[j][i] = C[j][i];
P[j][i] = i;
}
D[j][j] = 0.0;
P[j][j] = -1;
}
for (k = 0; k < N; k++) { /* k-> is the intermediate point */
for (j = 0; j < N; j++) { /* reaching j */
for (i = 0; i < N; i++) { /* start from i */
/* if i-->k + k-->j is smaller than original i-->j */
if (mxIsInf(D[j][k])) continue;
if (D[k][i] + D[j][k] < D[j][i]) {
/* reduce the i-->j distance to the smaller one i->k->j */
D[j][i] = D[k][i] + D[j][k];
/* and update the predecessor matrix */
P[j][i] = P[j][k];
}
}
}
}
#else /* USE_ROW_ORDER */
/* initialization */
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
D[i][j] = C[i][j];
P[i][j] = i;
}
D[i][i] = 0.0;
P[i][i] = -1;
}
for (k = 0; k < N; k++) { /* k-> is the intermediate point */
for (i = 0; i < N; i++) { /* start from i */
if (mxIsInf(D[i][k])) continue;
for (j = 0; j < N; j++) { /* reaching j */
/* if i-->k + k-->j is smaller than original i-->j */
if (D[i][k] + D[k][j] < D[i][j]) {
/* reduce the i-->j distance to the smaller one i->k->j */
D[i][j] = D[i][k] + D[k][j];
/* and update the predecessor matrix */
P[i][j] = P[k][j];
}
}
}
}
#endif
} /* FloydAPSP */
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
/* Declare variables */
int m,n,i,j;
double *Cx,*Dx,*Px,**C,**D,**P;
/* Check for proper number of input and output arguments. */
if (nrhs != 1) {
mexErrMsgTxt("One input arguments required: [D,P] = floyd(C)");
}
if (nlhs > 2) {
mexErrMsgTxt("Too many output arguments.");
}
/* Check data type of input argument. */
if (mxGetNumberOfDimensions(prhs[0]) != 2) {
mexErrMsgTxt("Input argument must be two dimensional\n");
}
if (mxIsChar(prhs[0]) || mxIsSparse(prhs[0]) || mxIsComplex(prhs[0])) {
mexErrMsgTxt("Input argument must be a full real matrix.");
}
/* Get the size and pointers to input data. */
m = mxGetM(prhs[0]);
n = mxGetN(prhs[0]);
if (m != n) {
mexErrMsgTxt("Input argument must be square.");
}
Cx = mxGetPr(prhs[0]);
/* create output matrices */
plhs[0] = mxCreateDoubleMatrix(m, n, mxREAL);
Dx = mxGetPr(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(m, n, mxREAL);
Px = mxGetPr(plhs[1]);
/* set up the 2-d arrays */
C = (double**) mxMalloc(n*sizeof(double*));
D = (double**) mxMalloc(n*sizeof(double*));
P = (double**) mxMalloc(n*sizeof(double*));
for (i = 0, j = 0; i < n; i++, j+=n) {
C[i] = Cx + j;
D[i] = Dx + j;
P[i] = Px + j;
}
FloydAPSP(n, C, D, P);
/* translate base 0 into base 1 */
for (i = 0; i < n*n; i++) {
Px[i]++;
}
/* garbage collection */
mxFree(C);
mxFree(D);
mxFree(P);
}