- Merged with old version of warshall.c. Some minor improvements.
This commit is contained in:
parent
f384042bfe
commit
9ec1bdc8eb
@ -16,42 +16,76 @@ graph_fill (int *graph, int nodes, int value)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return 1 if no cycle
|
||||
* return 0 if cycle
|
||||
*/
|
||||
int
|
||||
warshall (int *graph, int size)
|
||||
//! Show a graph
|
||||
void graph_display (int *graph, int nodes)
|
||||
{
|
||||
int i;
|
||||
|
||||
int index2 (i, j)
|
||||
int index (const int i, const int j)
|
||||
{
|
||||
return (i * size + j);
|
||||
return (i * nodes + j);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
j = 0;
|
||||
while (j < size)
|
||||
while (j < nodes)
|
||||
{
|
||||
if (graph[index2 (j, i)] == 1)
|
||||
if (graph[index (j, i)] == 1)
|
||||
{
|
||||
int k;
|
||||
|
||||
k = 0;
|
||||
while (k < size)
|
||||
while (k < nodes)
|
||||
{
|
||||
if (graph[index2 (k, j)] == 1)
|
||||
if (graph[index (k, j)] == 1)
|
||||
{
|
||||
if (k == i)
|
||||
{
|
||||
// Oh no! A cycle.
|
||||
graph [index (k,i)] = 2;
|
||||
graph_display (graph, nodes);
|
||||
return 0;
|
||||
}
|
||||
graph[index2 (k, i)] = 1;
|
||||
graph[index (k, i)] = 1;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user