Esempio n. 1
0
        public void FindStrongConnectedComponentsFindsExpectedComponents()
        {
            // See https://upload.wikimedia.org/wikipedia/commons/6/60/Tarjan%27s_Algorithm_Animation.gif
            var graph = new Graph <object, object>(
                Enumerable.Range(1, 8).Select(vertexId => new Vertex <object>((uint)vertexId)),
                new []
            {
                new Edge <object>(0, 1, 2, isDirected: true),
                new Edge <object>(1, 2, 3, isDirected: true),
                new Edge <object>(2, 3, 1, isDirected: true),
                new Edge <object>(3, 4, 2, isDirected: true),
                new Edge <object>(4, 4, 3, isDirected: true),
                new Edge <object>(5, 4, 5, isDirected: true),
                new Edge <object>(6, 5, 4, isDirected: true),
                new Edge <object>(7, 5, 6, isDirected: true),
                new Edge <object>(8, 6, 3, isDirected: true),
                new Edge <object>(9, 6, 7, isDirected: true),
                new Edge <object>(10, 7, 6, isDirected: true),
                new Edge <object>(11, 8, 6, isDirected: true),
                new Edge <object>(12, 8, 7, isDirected: true),
                new Edge <object>(13, 8, 8, isDirected: true)
            });
            var expected = new List <IList <uint> >
            {
                new uint[] { 1, 2, 3 },
                new uint[] { 4, 5 },
                new uint[] { 6, 7 },
                new uint[] { 8 }
            };
            var actual = GraphAlgorithms.FindStronglyConnectedComponents(graph);

            Assert.That(actual.Count, Is.EqualTo(expected.Count));
            foreach (var vertexIds in expected)
            {
                var referenceVertex = graph.GetVertexFromId(vertexIds[0]);
                var matchingActual  = actual.Single(c => c.Contains(referenceVertex));
                CollectionAssert.AreEquivalent(vertexIds, matchingActual.Select(c => c.Id));
            }
        }