How to find all chordless cycles in an undirected graph?
For example, given the graph
0 --- 1
| | \\
| | \\
4 --- 3 - 2
th
Assign numbers to nodes from 1 to n.
Pick the node number 1. Call it 'A'.
Enumerate pairs of links coming out of 'A'.
Pick one. Let's call the adjacent nodes 'B' and 'C' with B less than C.
If B and C are connected, then output the cycle ABC, return to step 3 and pick a different pair.
If B and C are not connected:
Repeat until you run out of vectors.
Repeat steps 3-5 with all pairs.
Remove node 1 and all links that lead to it. Pick the next node and go back to step 2.
Edit: and you can do away with one nested loop.
This seems to work at the first sight, there may be bugs, but you should get the idea:
void chordless_cycles(int* adjacency, int dim)
{
for(int i=0; i > candidates;
for(int k=j+1; k v;
v.resize(3);
v[0]=j;
v[1]=i;
v[2]=k;
candidates.push_back(v);
}
while(!candidates.empty())
{
vector v = candidates.front();
candidates.pop_front();
int k = v.back();
for(int m=i+1; m w = v;
w.push_back(m);
candidates.push_back(w);
}
}
}
}
}