done with serial floyd.cc
git-svn-id: svn://anubis/gvsu@194 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
fcb800e858
commit
70f6fbbde5
@ -8,6 +8,7 @@
|
|||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@ -35,13 +36,36 @@ int main(int argc, char * argv[])
|
|||||||
int D[2][num_verts][num_verts];
|
int D[2][num_verts][num_verts];
|
||||||
convertToMatrix(v, num_verts, (int *) &D[0]);
|
convertToMatrix(v, num_verts, (int *) &D[0]);
|
||||||
|
|
||||||
|
/* Run Floyd's Algorithm on D */
|
||||||
|
for (int k = 1; k <= num_verts; k++)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < num_verts; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < num_verts; j++)
|
||||||
|
{
|
||||||
|
int distWithoutK = D[(k-1) & 1][i][j];
|
||||||
|
int distItoK = D[(k-1) & 1][i][k-1];
|
||||||
|
int distKtoJ = D[(k-1) & 1][k-1][j];
|
||||||
|
int distWithK =
|
||||||
|
(distItoK == INT_MAX || distKtoJ == INT_MAX)
|
||||||
|
? INT_MAX
|
||||||
|
: distItoK + distKtoJ;
|
||||||
|
D[k & 1][i][j] = min(
|
||||||
|
distWithoutK,
|
||||||
|
distWithK
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Print out the final matrix */
|
||||||
for (int i = 0; i < num_verts; i++)
|
for (int i = 0; i < num_verts; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < num_verts; j++)
|
for (int j = 0; j < num_verts; j++)
|
||||||
{
|
{
|
||||||
cout << D[0][i][j] << " ";
|
printf("%2d ", D[num_verts & 1][i][j]);
|
||||||
}
|
}
|
||||||
cout << endl;
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -77,6 +101,8 @@ void convertToMatrix(const vector<int> & v, int num_verts, int * vals)
|
|||||||
for (int j = 0; j < num_verts; j++)
|
for (int j = 0; j < num_verts; j++)
|
||||||
{
|
{
|
||||||
(*V)[i][j] = v[vidx++];
|
(*V)[i][j] = v[vidx++];
|
||||||
|
if ((*V)[i][j] == 0)
|
||||||
|
(*V)[i][j] = INT_MAX;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user