Using the pseudo code found on wikipedia to implement a floyd warshall algorithm on a adjacency list representation the following code was created. The graph is a grid, so if it is a 3 x 3 grid vertex 0 has two edges and vertex 1 has 3 while vertex 2 has two and so on.
self-> V = number of vertex in the graph!!
void floyd(Graph *self, int** dist, int** next)
{
int i, j, k;
EdgeNodePtr current;
current = malloc(sizeof(current));
for (i = 0; i < self->V; i++)
{
for (j = 0; j < self->V; j++) {
dist[i][j] = INT_MAX; // Sets all minimun distances to infintiy
next[i][j] = -1; // Sets all next vertex to a non existant vertex
}
}
for (i = 0; i < self->V; i++)
{
for (j = 0; j < self->V; j++)
{
current = self->edges[i].head;
while (current != NULL) // Cycles through all the edges in edgelist
{
if (current->edge.to_vertex == j) // If an edge to the correct vertex is found then adds distance
{
dist[i][j] = current->edge.weight;
next[i][j] = j; // Updates next node
}
current = current->next;
}
}
}
PRINT
// Standard implemnation of floyds algorithm
for (k = 0; k < self->V; k++)
{
for (i = 0; i < self->V; i++)
{
for (j = 0; j < self->V; j++)
{
if (dist[i][j] > dist[i][k] + dist[k][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
next[i][j] = next[i][k];
}
}
}
}
PRINT
}
What happens is the edges are all inserted into the distance array correctly, checked by a simple print. The problem is faced when the algorithm runs it turns all the distances into INT_MINS or similar numbers. While not actually calculating the distances.
I believe the end distance graph of a grid should have every possible distance filled in the array except having the distances from a vertex to itself as infinity.
A picture of the output from printing the list graph where its says PRINT