public void FindSCC_WithSingleNode() { var graph = new List <int>[] { new List <int>() { }, }; var expected = new List <List <int> > { new List <int> { 0 } }; var result = StronglyConnectedComponents.FindStronglyConnectedComponents(graph); result.ForEach(scc => scc.Sort()); result = result.OrderBy(scc => scc.Count).ThenBy(scc => scc.First()).ToList(); Assert.AreEqual(expected.Count, result.Count, "Incorrect amount of strongly connected components."); for (int i = 0; i < expected.Count; i++) { CollectionAssert.AreEqual( expected[i], result[i], $"Expected component to be [{string.Join(", ", expected[i])}], but was [{string.Join(", ", result[i])}]."); } }
static void Main() { var graph = new List <int>[] { new List <int>() { 1, 11, 13 }, // children of node 0 new List <int>() { 6 }, // children of node 1 new List <int>() { 0 }, // children of node 2 new List <int>() { 4 }, // children of node 3 new List <int>() { 3, 6 }, // children of node 4 new List <int>() { 13 }, // children of node 5 new List <int>() { 0, 11 }, // children of node 6 new List <int>() { 12 }, // children of node 7 new List <int>() { 6, 11 }, // children of node 8 new List <int>() { 0 }, // children of node 9 new List <int>() { 4, 6, 10 }, // children of node 10 new List <int>() { }, // children of node 11 new List <int>() { 7 }, // children of node 12 new List <int>() { 2, 9 }, // children of node 13 }; var result = StronglyConnectedComponents.FindStronglyConnectedComponents(graph); Console.WriteLine("Strongly Connected Components:"); foreach (var component in result) { Console.WriteLine("{{{0}}}", string.Join(", ", component)); } }
public void FindSCC_WithMultipleComponents() { var graph = new List <int>[] { new List <int>() { 1, 11, 13 }, // children of node 0 new List <int>() { 6 }, // children of node 1 new List <int>() { 0 }, // children of node 2 new List <int>() { 4 }, // children of node 3 new List <int>() { 3, 6 }, // children of node 4 new List <int>() { 13 }, // children of node 5 new List <int>() { 0, 11 }, // children of node 6 new List <int>() { 12 }, // children of node 7 new List <int>() { 6, 11 }, // children of node 8 new List <int>() { 0 }, // children of node 9 new List <int>() { 4, 6, 10 }, // children of node 10 new List <int>() { }, // children of node 11 new List <int>() { 7 }, // children of node 12 new List <int>() { 2, 9 }, // children of node 13 }; var expected = new List <List <int> > { new List <int> { 5 }, new List <int> { 8 }, new List <int> { 10 }, new List <int> { 11 }, new List <int> { 3, 4 }, new List <int> { 7, 12 }, new List <int> { 0, 1, 2, 6, 9, 13 }, }; var result = StronglyConnectedComponents.FindStronglyConnectedComponents(graph); result.ForEach(scc => scc.Sort()); result = result.OrderBy(scc => scc.Count).ThenBy(scc => scc.First()).ToList(); Assert.AreEqual(expected.Count, result.Count, "Incorrect amount of strongly connected components."); for (int i = 0; i < expected.Count; i++) { CollectionAssert.AreEqual( expected[i], result[i], $"Expected component to be [{string.Join(", ", expected[i])}], but was [{string.Join(", ", result[i])}]."); } }