Basically the point of using the Floyd-Warshall algorithm is to determine the shortest path between two nodes in a connected graph. What I am attempting to do is instead of simply finding the shortest path, I want the shortest path that is also an even weight.
For instance, this is a simple implementation of the Floyd-Warshall algorithm:
#include <stdio.h>
main()
{
int dist[10][10],succ[10][10],n,i,j,k;
int newDist;
scanf("%d",&n);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
dist[i][j]=999;
succ[i][j]=j;
}
while (1)
{
scanf("%d %d %d",&i,&j,&k);
if (i==(-1))
break;
dist[i][j]=k;
distOdd[i][j]=k;
distEven[i][j]=k;
}
printf(" ");
for (i=0;i<n;i++)
printf("%3d ",i);
printf("\n");
for (i=0;i<n;i++)
{
printf("%3d ",i);
for (k=0;k<n;k++)
printf("%3d %d ",dist[i][k],succ[i][k]);
printf("\n");
}
printf("-------------------------------\n");
/* Floyd-Warshall */
for (j=0;j<n;j++)
{
for (i=0;i<n;i++)
if (dist[i][j]<999)
for (k=0;k<n;k++)
{
newDist=dist[i][j]+dist[j][k];
if (newDist<dist[i][k])
{
dist[i][k]=newDist;
succ[i][k]=succ[i][j];
}
}
printf(" ");
for (i=0;i<n;i++)
printf("%3d ",i);
printf("\n");
for (i=0;i<n;i++)
{
printf("%3d ",i);
for (k=0;k<n;k++)
printf("%3d %d ",dist[i][k],succ[i][k]);
printf("\n");
}
printf("-------------------------------\n");
}
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (dist[i][j]==999)
printf("No path from %d to %d\n",i,j);
else
{
printf("Distance %d for %d ",dist[i][j],i);
for (k=succ[i][j];
k!=j;
k=succ[k][j])
printf("%d ",k);
printf("%d\n",j);
}
}
Given the following input:
6
0 1 1
1 2 1
2 3 1
3 1 1
1 4 1
4 5 1
-1 -1 -1
I want the following output (ignore the formatting, I simply need a way to find the "odd matrix at each step)
initial odd matrix
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 0
odd matrix
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 1
odd matrix
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 2
odd matrix
999 0 1 1 999 2 3 1 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 3 1 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 2 2 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 3
odd matrix
999 0 1 1 5 1 3 1 5 1 999 5
999 0 3 2 1 2 5 2 1 4 999 5
999 0 5 3 3 3 1 3 3 3 999 5
999 0 1 1 5 1 3 1 5 1 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 4 1 2 1 6 1 2 1 999 5
999 0 6 2 4 2 2 2 4 2 999 5
999 0 2 3 6 3 4 3 6 3 999 5
999 0 4 1 2 1 6 1 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 4
odd matrix
999 0 1 1 5 1 3 1 5 1 3 1
999 0 3 2 1 2 5 2 1 4 5 2
999 0 5 3 3 3 1 3 3 3 7 3
999 0 1 1 5 1 3 1 5 1 3 1
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 4 1 2 1 6 1 2 1 6 1
999 0 6 2 4 2 2 2 4 2 2 4
999 0 2 3 6 3 4 3 6 3 4 3
999 0 4 1 2 1 6 1 2 1 6 1
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 5
odd matrix
999 0 1 1 5 1 3 1 5 1 3 1
999 0 3 2 1 2 5 2 1 4 5 2
999 0 5 3 3 3 1 3 3 3 7 3
999 0 1 1 5 1 3 1 5 1 3 1
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 4 1 2 1 6 1 2 1 6 1
999 0 6 2 4 2 2 2 4 2 2 4
999 0 2 3 6 3 4 3 6 3 4 3
999 0 4 1 2 1 6 1 2 1 6 1
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
What my code currently does is it gets the most optimal weight which is represented in each of the separate "odd" and "even" matrices.
My lack of understanding is how the "odd" and "even" matrices come up with their non-optimal values when the optimal value is located in the opposite matrix (odd/even). It seems to me that there would have to be some sort of looping or recursion in order to do it, but I'm lost as to how I would accomplish this.