- Merged with old version of warshall.c. Some minor improvements.

This commit is contained in:
ccremers 2004-08-17 09:48:29 +00:00
parent f384042bfe
commit 9ec1bdc8eb

View File

@ -16,42 +16,76 @@ graph_fill (int *graph, int nodes, int value)
} }
} }
/** //! Show a graph
* return 1 if no cycle void graph_display (int *graph, int nodes)
* return 0 if cycle
*/
int
warshall (int *graph, int size)
{ {
int i; int i;
int index2 (i, j) int index (const int i, const int j)
{ {
return (i * size + j); return (i * nodes + j);
} }
i = 0; i = 0;
while (i < size) while (i < nodes)
{
int j;
j = 0;
while (j<nodes)
{
eprintf ("%i ", graph[index(i,j)]);
j++;
}
eprintf ("\n");
i++;
}
}
//! Apply warshall's algorithm to determine the closure of a graph
/**
* If j<i and k<j, then k<i.
* Could be done more efficiently but that is irrelevant here.
*
*@param graph A pointer to the integer array of nodes*nodes elements.
*@param nodes The number of nodes in the graph.
*@Returns 0 if there is a cycle; and the algorithm aborts, 1 if there is no cycle and the result is okay.
*/
int
warshall (int *graph, int nodes)
{
int i;
int index (const int i, const int j)
{
return (i * nodes + j);
}
i = 0;
while (i < nodes)
{ {
int j; int j;
j = 0; j = 0;
while (j < size) while (j < nodes)
{ {
if (graph[index2 (j, i)] == 1) if (graph[index (j, i)] == 1)
{ {
int k; int k;
k = 0; k = 0;
while (k < size) while (k < nodes)
{ {
if (graph[index2 (k, j)] == 1) if (graph[index (k, j)] == 1)
{ {
if (k == i) if (k == i)
{ {
// Oh no! A cycle.
graph [index (k,i)] = 2;
graph_display (graph, nodes);
return 0; return 0;
} }
graph[index2 (k, i)] = 1; graph[index (k, i)] = 1;
} }
k++; k++;
} }