Tarjan cycle detection help C#

后端 未结 3 1310
深忆病人
深忆病人 2020-12-28 13:21

Here is a working C# implementation of tarjan\'s cycle detection.

The algorithm is found here: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_

3条回答
  •  天命终不由人
    2020-12-28 14:24

    As of 2008 quickgraph has supported this algorithm. See the StronglyConnectedComponentsAlgorithm class for the implementation, or AlgorithmExtensions.StronglyConnectedComponents method for a usage shortcut.

    Example:

    // Initialize result dictionary
    IDictionary comps = new Dictionary();
    
    // Run the algorithm
    graph.StronglyConnectedComponents(out comps);
    
    // Group and filter the dictionary
    var cycles = comps
        .GroupBy(x => x.Value, x => x.Key)
        .Where(x => x.Count() > 1)
        .Select(x => x.ToList())
    

提交回复
热议问题