예제 #1
0
        public void CheckIfDFSIsCorrect()
        {
            var vertices = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            var graph = new WeightedALGraph <int, int>();

            graph.AddVertices(vertices);

            // Graph is:
            // 1 <-> 2, 3, 4
            // 2 <-> 1, 3, 5, 6
            // 3 <-> 1, 2, 6
            // 4 <-> 1, 6, 7
            // 5 <-> 2, 7, 8
            // 6 <-> 2, 3, 4
            // 7 <-> 4, 8
            // 8 <-> 7, 5
            // 9 <->
            // With each edge having a weight of the sum of its vertices
            graph.AddEdge(1, 2, 3);
            graph.AddEdge(1, 3, 4);
            graph.AddEdge(1, 4, 5);
            graph.AddEdge(2, 3, 5);
            graph.AddEdge(2, 5, 7);
            graph.AddEdge(2, 6, 8);
            graph.AddEdge(3, 6, 9);
            graph.AddEdge(4, 6, 10);
            graph.AddEdge(4, 7, 11);
            graph.AddEdge(5, 7, 12);
            graph.AddEdge(5, 8, 13);
            graph.AddEdge(7, 8, 15);

            // Expected dfs for vertex 1:
            // 1 | 2 | 3 | 6 | 4 | 7 | 5 | 8
            var dfsExpectedOutput = new int[] { 1, 2, 3, 6, 4, 7, 5, 8 };
            var dfs = graph.DepthFirstSearch(1).ToList();

            Assert.IsTrue(dfs.Count == dfsExpectedOutput.Length);
            for (int i = 0; i < dfs.Count; i++)
            {
                Assert.IsTrue(dfs[i] == dfsExpectedOutput[i]);
            }

            // Expected dfs edges for vertex 1:
            // 1 | 2 | 3 | 6 | 4 | 7 | 5
            // 2 | 3 | 6 | 4 | 7 | 5 | 8
            var dfsExpectedSources      = new int[] { 1, 2, 3, 6, 4, 7, 5 };
            var dfsExpectedDestinations = new int[] { 2, 3, 6, 4, 7, 5, 8 };
            var dfsEdges = graph.DepthFirstSearchEdges(1).ToList();

            Assert.IsTrue(dfsEdges.Count == dfsExpectedSources.Length);
            for (int i = 0; i < dfsEdges.Count; i++)
            {
                Assert.IsTrue(dfsEdges[i].Source == dfsExpectedSources[i]);
                Assert.IsTrue(dfsEdges[i].Destination == dfsExpectedDestinations[i]);
                Assert.IsTrue(dfsEdges[i].Weight == dfsEdges[i].Source + dfsEdges[i].Destination);
            }

            // Expected dfs for vertex 8:
            // 8 | 5 | 2 | 1 | 3 | 6 | 4 | 7
            dfsExpectedOutput = new int[] { 8, 5, 2, 1, 3, 6, 4, 7 };
            dfs = graph.DepthFirstSearch(8).ToList();

            Assert.IsTrue(dfs.Count == dfsExpectedOutput.Length);
            for (int i = 0; i < dfs.Count; i++)
            {
                Assert.IsTrue(dfs[i] == dfsExpectedOutput[i]);
            }

            // Expected dfs edges for vertex 8:
            // 8 | 5 | 2 | 1 | 3 | 6 | 4
            // 5 | 2 | 1 | 3 | 6 | 4 | 7
            dfsExpectedSources      = new int[] { 8, 5, 2, 1, 3, 6, 4 };
            dfsExpectedDestinations = new int[] { 5, 2, 1, 3, 6, 4, 7 };
            dfsEdges = graph.DepthFirstSearchEdges(8).ToList();

            Assert.IsTrue(dfsEdges.Count == dfsExpectedSources.Length);
            for (int i = 0; i < dfsEdges.Count; i++)
            {
                Assert.IsTrue(dfsEdges[i].Source == dfsExpectedSources[i]);
                Assert.IsTrue(dfsEdges[i].Destination == dfsExpectedDestinations[i]);
                Assert.IsTrue(dfsEdges[i].Weight == dfsEdges[i].Source + dfsEdges[i].Destination);
            }
        }