Exemplo n.º 1
0
        public void CheckIfBFSIsCorrect()
        {
            var vertices = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

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

            graph.AddVertices(vertices);

            // Graph is:
            // 1 -> 3, 4
            // 1 <- 2
            // 2 -> 1, 5
            // 2 <- 3, 5, 6
            // 3 -> 2, 6
            // 3 <- 1, 4
            // 4 -> 3, 6
            // 4 <- 1, 7
            // 5 -> 2, 7, 8
            // 5 <- 2, 8
            // 6 -> 2
            // 6 <- 3, 4
            // 7 -> 4
            // 7 <- 5, 8
            // 8 -> 5, 7
            // 8 <- 5
            // 9 ->
            // 9 <-
            // With each edge having a weight of the diffrenece between the source and destination vertex (source - destination)
            graph.AddEdge(1, 3, -2);
            graph.AddEdge(1, 4, -3);
            graph.AddEdge(2, 1, 1);
            graph.AddEdge(2, 5, -3);
            graph.AddEdge(3, 2, 1);
            graph.AddEdge(3, 6, -3);
            graph.AddEdge(4, 3, 1);
            graph.AddEdge(4, 6, -2);
            graph.AddEdge(5, 2, 3);
            graph.AddEdge(5, 7, -2);
            graph.AddEdge(5, 8, -3);
            graph.AddEdge(6, 2, 4);
            graph.AddEdge(7, 4, 3);
            graph.AddEdge(8, 5, 3);
            graph.AddEdge(8, 7, 1);

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

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

            // Expected bfs edges for vertex 1:
            // 1 1 | 3 3 | 2 | 5 5
            // 3 4 | 2 6 | 5 | 7 8
            var bfsExpectedSources      = new int[] { 1, 1, 3, 3, 2, 5, 5 };
            var bfsExpectedDestinations = new int[] { 3, 4, 2, 6, 5, 7, 8 };
            var bfsEdges = graph.BreadthFirstSearchEdges(1).ToList();

            Assert.IsTrue(bfsEdges.Count == bfsExpectedSources.Length);
            for (int i = 0; i < bfsEdges.Count; i++)
            {
                Assert.IsTrue(bfsEdges[i].Source == bfsExpectedSources[i]);
                Assert.IsTrue(bfsEdges[i].Destination == bfsExpectedDestinations[i]);
                Assert.IsTrue(bfsEdges[i].Weight == bfsEdges[i].Source - bfsEdges[i].Destination);
            }

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

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

            // Expected bfs edges for vertex 8:
            // 8 8 | 5 | 7 | 2 | 4 4
            // 5 7 | 2 | 4 | 1 | 3 6
            bfsExpectedSources      = new int[] { 8, 8, 5, 7, 2, 4, 4 };
            bfsExpectedDestinations = new int[] { 5, 7, 2, 4, 1, 3, 6 };
            bfsEdges = graph.BreadthFirstSearchEdges(8).ToList();

            Assert.IsTrue(bfsEdges.Count == bfsExpectedSources.Length);
            for (int i = 0; i < bfsEdges.Count; i++)
            {
                Assert.IsTrue(bfsEdges[i].Source == bfsExpectedSources[i]);
                Assert.IsTrue(bfsEdges[i].Destination == bfsExpectedDestinations[i]);
                Assert.IsTrue(bfsEdges[i].Weight == bfsEdges[i].Source - bfsEdges[i].Destination);
            }
        }