Washi1337 / Rivers

A fast graphing library that allows for constructing, importing, exporting, and analysing graph structures in .NET.
MIT License
135 stars 25 forks source link

IsCyclic fails when node has multiple edges #6

Open holly-hacker opened 4 years ago

holly-hacker commented 4 years ago

I'm using IsCyclic(Graph) to check the attached graph and it returns true while I think it should return false. I assume this is because some nodes have multiple edges, since it reports true for simpler graphs.

Image ![graph](https://user-images.githubusercontent.com/13605369/69278791-e9b5c000-0bda-11ea-8890-428cb1772150.png)

graph.dot

Washi1337 commented 4 years ago

Good catch! It seems to already fail at a very simple graph of three nodes:

digraph G {
   1 -> 2
   1 -> 3
   2 -> 3
}

It seems it breaks because the DFS used in IsCyclic reaches node 3 twice (through edge 1->3, second through 2->3). Since IsCyclic bases its result on a set of all visited nodes, instead of just the nodes in the stack of the DFS, it leads to the conclusion that there was indeed a cycle.

This might be an indication the DFS algorithm (or the traversal interfaces in general) are too restricted and might need a change in API design, because there is currently no way to access the stack of DFS directly using the exposed events. Will have to think about changes there, as I'd rather not reimplement DFS just within the IsCyclic method. There are more algorithms that benefit from DFS and could perhaps benefit from access to the stack as well.