danielrbradley / CycleDetection

Graph Cycle Detection Algorithm in C#
34 stars 45 forks source link

Quadratic Time Complexity #1

Open ekzhang opened 6 years ago

ekzhang commented 6 years ago

On line 53 of StronglyConnectedComponentFinder, the Stack.Contains method is being used, which runs in O(V) time, which is a bottleneck for the algorithm that can cause it to run in worst-case O(V^2) time. This small bug unfortunately renders the implementation useless, as it can be superseded easily by a naive solution.

https://github.com/danielrbradley/CycleDetection/blob/10cf461b73e51ee387e14db4f7d226f5f5165af1/StronglyConnectedComponents/StronglyConnectedComponentFinder.cs#L53

@danielrbradley Could you modify the code to use, for example, an array of boolean flags to see if the vertex is in the stack or not? This would fix the bug and make the algorithm run in linear time as expected. Sorry that I am not able to make this change myself; I do not have enough experience working with C#.

danielrbradley commented 6 years ago

Thanks for the suggestion. I'm not actively maintaining this project (haven't opened it in 5 years now), but am happy to merge any PRs if someone wants to take a stab at it 🙂