I'm trying to list all the paths in an undirected graph, and my question is similar to this question. I've tried to run this code, but it loops indefinitely -- I ran it with 60 nodes. Any ideas on how to produce the correct solution?
I added such a random graph and the code is now like:
#include<stdio.h>
static struct {
int value1;
int value2;
int used;
} data[] = {
{ 1, 2 },
{ 1, 5 },
{ 2, 3 },
{ 2, 6 },
{ 3, 7 },
{ 4, 0 },
{ 0, 4 },
{ 7, 3 },
{ 2, 1 },
};
enum { DATA_SIZE = sizeof data / sizeof *data };
static int output[DATA_SIZE];
int traverse(int from, int to, int depth) {
output[depth++] = from;
int i;
if (from == to) {
for (i = 0; i < depth; i++) {
if (i) {
printf("-");
}
printf("%d", output[i]);
}
printf("\n");
} else {
for (i = 0; i < DATA_SIZE; i++) {
if (!data[i].used) {
data[i].used = 1;
if (from == data[i].value1) {
traverse(data[i].value2, to, depth);
} else if (from == data[i].value2) {
traverse(data[i].value1, to, depth);
}
data[i].used = 0;
}
}
}
}
int main() {
traverse(1, 7, 0);
}`
And the output is: 1-2-3-7 1-2-3-7 1-2-3-7 1-2-3-7
Why do I get that path 4 times? Is it possible to fix? thanks