public void SimpleAcyclic_A1()
    {
        // Simple acyclic graph.
        var connList = new LightweightList <DirectedConnection>
        {
            new DirectedConnection(0, 3),
            new DirectedConnection(1, 3),
            new DirectedConnection(2, 3),
            new DirectedConnection(2, 4),
            new DirectedConnection(4, 3)
        };

        // Create graph.
        var connSpan = connList.AsSpan();

        connSpan.Sort();
        var digraph = DirectedGraphBuilder.Create(connSpan, 3, 2);

        // Perform depth analysis.
        GraphDepthInfo depthInfo = new CyclicGraphDepthAnalysis().CalculateNodeDepths(digraph);

        // Assertions.
        Assert.Equal(3, depthInfo._graphDepth);
        Assert.Equal(5, depthInfo._nodeDepthArr.Length);

        // Node depths.
        Assert.Equal(0, depthInfo._nodeDepthArr[0]);
        Assert.Equal(0, depthInfo._nodeDepthArr[1]);
        Assert.Equal(0, depthInfo._nodeDepthArr[2]);
        Assert.Equal(2, depthInfo._nodeDepthArr[3]);
        Assert.Equal(1, depthInfo._nodeDepthArr[4]);
    }
        public void TestIsConnectionCyclic1()
        {
            var cyclicTest = new CyclicConnectionTest();

            var connArr = new DirectedConnection[3];

            connArr[0] = new DirectedConnection(0, 2);
            connArr[1] = new DirectedConnection(2, 3);
            connArr[2] = new DirectedConnection(3, 1);

            DirectedGraph digraph = DirectedGraphBuilder.Create(connArr, 1, 1);

            // True tests (cycle).
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(0, 0)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 1)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(2, 2)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(3, 3)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(2, 0)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(3, 0)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 0)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(3, 2)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 2)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 3)));

            // False tests (no cycle).
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(0, 3)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(0, 1)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(2, 1)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(3, 1)));
        }
        public void SimpleAcyclic_DefinedNodes_NodeIdGap()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(100, 103),
                new DirectedConnection(101, 103),
                new DirectedConnection(102, 103),
                new DirectedConnection(102, 104)
            };

            // Create graph.
            var digraph = DirectedGraphBuilder.Create(connList, 0, 10);

            // The gaps in the node IDs should be removed such that node IDs form a contiguous span starting from zero.
            var connListExpected = new List <DirectedConnection>
            {
                new DirectedConnection(10, 13),
                new DirectedConnection(11, 13),
                new DirectedConnection(12, 13),
                new DirectedConnection(12, 14)
            };

            CompareConnectionLists(connListExpected, digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.Equal(15, digraph.TotalNodeCount);
        }
        public void Regression1()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(0, 2),
                new DirectedConnection(0, 3),
                new DirectedConnection(0, 4),
                new DirectedConnection(2, 5),
                new DirectedConnection(3, 6),
                new DirectedConnection(4, 7),
                new DirectedConnection(5, 8),
                new DirectedConnection(6, 4),
                new DirectedConnection(6, 9),
                new DirectedConnection(7, 10),
                new DirectedConnection(8, 1),
                new DirectedConnection(9, 1),
                new DirectedConnection(10, 1)
            };

            // Create graph.
            connList.Sort();
            var digraph = DirectedGraphBuilder.Create(connList, 0, 0);

            // Test if cyclic.
            var  cyclicGraphAnalysis = new CyclicGraphAnalysis();
            bool isCyclic            = cyclicGraphAnalysis.IsCyclic(digraph);

            Assert.IsFalse(isCyclic);
        }
        public void TestIsConnectionCyclic2()
        {
            var cyclicTest = new CyclicConnectionTest();

            var connArr = new DirectedConnection[8];

            connArr[0] = new DirectedConnection(0, 5);
            connArr[1] = new DirectedConnection(0, 2);
            connArr[2] = new DirectedConnection(0, 3);
            connArr[3] = new DirectedConnection(5, 4);
            connArr[4] = new DirectedConnection(4, 2);
            connArr[5] = new DirectedConnection(2, 1);
            connArr[6] = new DirectedConnection(3, 6);
            connArr[7] = new DirectedConnection(3, 2);
            Array.Sort(connArr);

            DirectedGraph digraph = DirectedGraphBuilder.Create(connArr, 1, 1);

            // True tests (cycle).
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(2, 5)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(2, 4)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 2)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 0)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(1, 4)));
            Assert.IsTrue(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(6, 0)));

            // False tests (no cycle).
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(3, 1)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(5, 3)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(6, 5)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(6, 2)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(6, 4)));
            Assert.IsFalse(cyclicTest.IsConnectionCyclic(digraph, new DirectedConnection(3, 4)));
        }
        public void Regression1()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(0, 2),
                new DirectedConnection(0, 3),
                new DirectedConnection(0, 4),
                new DirectedConnection(2, 5),
                new DirectedConnection(3, 6),
                new DirectedConnection(4, 7),
                new DirectedConnection(5, 8),
                new DirectedConnection(6, 4),
                new DirectedConnection(6, 9),
                new DirectedConnection(7, 10),
                new DirectedConnection(8, 1),
                new DirectedConnection(9, 1),
                new DirectedConnection(10, 1)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort();
            var digraph = DirectedGraphBuilder.Create(connSpan, 0, 0);

            // Test if cyclic.
            var  cyclicGraphCheck = new CyclicGraphCheck();
            bool isCyclic         = cyclicGraphCheck.IsCyclic(digraph);

            Assert.False(isCyclic);
        }
        public void SimpleCyclic_DefinedNodes_NodeIdGap()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(100, 103),
                new DirectedConnection(101, 103),
                new DirectedConnection(102, 103),
                new DirectedConnection(102, 104),
                new DirectedConnection(104, 101),
                new DirectedConnection(101, 102)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort();
            var digraph = DirectedGraphBuilder.Create(connSpan, 0, 10);

            // Test if cyclic.
            var  cyclicGraphCheck = new CyclicGraphCheck();
            bool isCyclic         = cyclicGraphCheck.IsCyclic(digraph);

            Assert.True(isCyclic);
        }
        public void SimpleCyclic()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(0, 3),
                new DirectedConnection(1, 3),
                new DirectedConnection(2, 3),
                new DirectedConnection(2, 4),
                new DirectedConnection(4, 1),
                new DirectedConnection(1, 2)
            };

            // Create graph.
            var connSpan = connList.AsSpan();

            connSpan.Sort();
            var digraph = DirectedGraphBuilder.Create(connSpan, 0, 0);

            // Test if cyclic.
            var  cyclicGraphCheck = new CyclicGraphCheck();
            bool isCyclic         = cyclicGraphCheck.IsCyclic(digraph);

            Assert.True(isCyclic);
        }
Example #9
0
        public void ConnectThroughInput()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(0, 3),
                new DirectedConnection(1, 3),
                new DirectedConnection(2, 3),
                new DirectedConnection(2, 4),
                new DirectedConnection(4, 1)
            };

            // Create graph.
            connList.Sort();
            var digraph = DirectedGraphBuilder.Create(connList, 3, 2);

            // Assert is acyclic.
            var cyclicGraphAnalysis = new CyclicGraphAnalysis();

            Assert.IsTrue(!cyclicGraphAnalysis.IsCyclic(digraph));

            // Depth analysis.
            GraphDepthInfo depthInfo = AcyclicGraphDepthAnalysis.CalculateNodeDepths(digraph);

            // Assertions.
            Assert.AreEqual(4, depthInfo._networkDepth);
            Assert.AreEqual(5, depthInfo._nodeDepthArr.Length);

            // Node depths.
            Assert.AreEqual(0, depthInfo._nodeDepthArr[0]);
            Assert.AreEqual(2, depthInfo._nodeDepthArr[1]);
            Assert.AreEqual(0, depthInfo._nodeDepthArr[2]);
            Assert.AreEqual(3, depthInfo._nodeDepthArr[3]);
            Assert.AreEqual(1, depthInfo._nodeDepthArr[4]);
        }
    public void Random1()
    {
        // Simple acyclic graph.
        var connList = new LightweightList <DirectedConnection>
        {
            new DirectedConnection(0, 2),
            new DirectedConnection(0, 3),
            new DirectedConnection(0, 4),
            new DirectedConnection(2, 5),
            new DirectedConnection(2, 10),
            new DirectedConnection(3, 1),
            new DirectedConnection(3, 4),
            new DirectedConnection(3, 6),
            new DirectedConnection(3, 7),
            new DirectedConnection(4, 1),
            new DirectedConnection(4, 6),
            new DirectedConnection(4, 7),
            new DirectedConnection(4, 10),
            new DirectedConnection(5, 4),
            new DirectedConnection(5, 8),
            new DirectedConnection(6, 9),
            new DirectedConnection(7, 9),
            new DirectedConnection(7, 10),
            new DirectedConnection(8, 1),
            new DirectedConnection(8, 3),
            new DirectedConnection(8, 9),
            new DirectedConnection(9, 1),
            new DirectedConnection(10, 1),
        };

        // Create graph.
        var connSpan = connList.AsSpan();

        connSpan.Sort();
        var digraph = DirectedGraphBuilder.Create(connSpan, 1, 1);

        // Depth analysis.
        GraphDepthInfo depthInfo = new AcyclicGraphDepthAnalysis().CalculateNodeDepths(digraph);

        // Assertions.
        Assert.Equal(9, depthInfo._graphDepth);
        Assert.Equal(11, depthInfo._nodeDepthArr.Length);

        // Node depths.
        Assert.Equal(0, depthInfo._nodeDepthArr[0]);
        Assert.Equal(8, depthInfo._nodeDepthArr[1]);
        Assert.Equal(1, depthInfo._nodeDepthArr[2]);
        Assert.Equal(4, depthInfo._nodeDepthArr[3]);
        Assert.Equal(5, depthInfo._nodeDepthArr[4]);
        Assert.Equal(2, depthInfo._nodeDepthArr[5]);
        Assert.Equal(6, depthInfo._nodeDepthArr[6]);
        Assert.Equal(6, depthInfo._nodeDepthArr[7]);
        Assert.Equal(3, depthInfo._nodeDepthArr[8]);
        Assert.Equal(7, depthInfo._nodeDepthArr[9]);
        Assert.Equal(7, depthInfo._nodeDepthArr[10]);
    }
        public void SimpleAcyclic_DefinedNodes()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(10, 13),
                new DirectedConnection(11, 13),
                new DirectedConnection(12, 13),
                new DirectedConnection(12, 14)
            };

            // Create graph.
            var digraph = DirectedGraphBuilder.Create(connList, 0, 10);

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connList, digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.Equal(15, digraph.TotalNodeCount);
        }
Example #12
0
        public void SimpleAcyclic()
        {
            // Simple acyclic graph.
            var connList = new LightweightList <DirectedConnection>
            {
                new DirectedConnection(0, 3),
                new DirectedConnection(1, 3),
                new DirectedConnection(2, 3),
                new DirectedConnection(2, 4)
            };

            // Create graph.
            var digraph = DirectedGraphBuilder.Create(connList.AsSpan(), 0, 0);

            // The graph should be unchanged from the input connections.
            CompareConnectionLists(connList.AsSpan(), digraph.ConnectionIdArrays);

            // Check the node count.
            Assert.Equal(5, digraph.TotalNodeCount);
        }
        public void SimpleAcyclic_DefinedNodes_NodeIdGap()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(100, 103),
                new DirectedConnection(101, 103),
                new DirectedConnection(102, 103),
                new DirectedConnection(102, 104),
                new DirectedConnection(104, 101)
            };

            // Create graph.
            connList.Sort();
            var digraph = DirectedGraphBuilder.Create(connList, 0, 10);

            // Test if cyclic.
            var  cyclicGraphAnalysis = new CyclicGraphAnalysis();
            bool isCyclic            = cyclicGraphAnalysis.IsCyclic(digraph);

            Assert.IsFalse(isCyclic);
        }
        public void SimpleAcyclic()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(0, 3),
                new DirectedConnection(1, 3),
                new DirectedConnection(2, 3),
                new DirectedConnection(2, 4),
                new DirectedConnection(4, 1)
            };

            // Create graph.
            connList.Sort();
            var digraph = DirectedGraphBuilder.Create(connList, 0, 0);

            // Test if cyclic.
            var  cyclicGraphAnalysis = new CyclicGraphAnalysis();
            bool isCyclic            = cyclicGraphAnalysis.IsCyclic(digraph);

            Assert.IsFalse(isCyclic);
        }
    public void ShortAndLongPath()
    {
        // Simple acyclic graph.
        var connList = new LightweightList <DirectedConnection>
        {
            new DirectedConnection(0, 4),
            new DirectedConnection(4, 5),
            new DirectedConnection(5, 2),
            new DirectedConnection(1, 2),
            new DirectedConnection(2, 3)
        };

        // Create graph.
        var connSpan = connList.AsSpan();

        connSpan.Sort();
        var digraph = DirectedGraphBuilder.Create(connSpan, 2, 2);

        // Assert is acyclic.
        var cyclicGraphCheck = new CyclicGraphCheck();

        Assert.False(cyclicGraphCheck.IsCyclic(digraph));

        // Depth analysis.
        GraphDepthInfo depthInfo = new AcyclicGraphDepthAnalysis().CalculateNodeDepths(digraph);

        // Assertions.
        Assert.Equal(5, depthInfo._graphDepth);
        Assert.Equal(6, depthInfo._nodeDepthArr.Length);

        // Node depths.
        Assert.Equal(0, depthInfo._nodeDepthArr[0]);
        Assert.Equal(0, depthInfo._nodeDepthArr[1]);
        Assert.Equal(3, depthInfo._nodeDepthArr[2]);
        Assert.Equal(4, depthInfo._nodeDepthArr[3]);
        Assert.Equal(1, depthInfo._nodeDepthArr[4]);
        Assert.Equal(2, depthInfo._nodeDepthArr[5]);
    }
        public void SimpleCyclic_DefinedNodes()
        {
            // Simple acyclic graph.
            var connList = new List <DirectedConnection>
            {
                new DirectedConnection(10, 13),
                new DirectedConnection(11, 13),
                new DirectedConnection(12, 13),
                new DirectedConnection(12, 14),
                new DirectedConnection(14, 11),
                new DirectedConnection(11, 12)
            };

            // Create graph.
            connList.Sort();
            var digraph = DirectedGraphBuilder.Create(connList, 0, 10);

            // Test if cyclic.
            var  cyclicGraphAnalysis = new CyclicGraphAnalysis();
            bool isCyclic            = cyclicGraphAnalysis.IsCyclic(digraph);

            Assert.IsTrue(isCyclic);
        }