Exemplo n.º 1
0
        public void DijkstraTest3_Success()
        {
            var graph = new DirectedWeightedGraph <char>(5);
            var a     = graph.AddVertex('A');
            var b     = graph.AddVertex('B');
            var c     = graph.AddVertex('C');

            graph.AddEdge(a, b, 1);
            graph.AddEdge(b, a, 1);

            graph.AddEdge(a, c, 3);
            graph.AddEdge(c, a, 3);

            var shortestPathList = DijkstraAlgorithm.GenerateShortestPath(graph, a);

            shortestPathList.Length.Should().Be(3);
            shortestPathList[0].Vertex.Should().Be(a);
            shortestPathList[0].Distance.Should().Be(0);
            shortestPathList[0].PreviousVertex.Should().Be(a);
            shortestPathList[0].ToString().Should()
            .Be($"Vertex: {a} - Distance: {0} - Previous: {a}");

            shortestPathList[1].Vertex.Should().Be(b);
            shortestPathList[1].Distance.Should().Be(1);
            shortestPathList[1].PreviousVertex.Should().Be(a);
            shortestPathList[1].ToString().Should()
            .Be($"Vertex: {b} - Distance: {1} - Previous: {a}");

            shortestPathList[2].Vertex.Should().Be(c);
            shortestPathList[2].Distance.Should().Be(3);
            shortestPathList[2].PreviousVertex.Should().Be(a);
            shortestPathList[2].ToString().Should()
            .Be($"Vertex: {c} - Distance: {3} - Previous: {a}");
        }
        public void VisitAll_ShouldCountNumberOfVisitedVertix_ResultShouldBeTheSameAsNumberOfVerticesInGraph()
        {
            //Arrange
            var graph = new DirectedWeightedGraph <int>(10);

            var vertex1 = graph.AddVertex(1);

            var vertex2 = graph.AddVertex(20);

            var vertex3 = graph.AddVertex(40);

            var vertex4 = graph.AddVertex(40);

            graph.AddEdge(vertex1, vertex2, 1);

            graph.AddEdge(vertex2, vertex3, 1);

            graph.AddEdge(vertex2, vertex4, 1);

            graph.AddEdge(vertex4, vertex1, 1);

            var dfsSearcher = new DepthFirstSearch <int>();

            long countOfVisitedVertices = 0;

            //Act
            dfsSearcher.VisitAll(graph, vertex1, (Vertex <int> vertex) => countOfVisitedVertices++);

            //Assert
            Assert.AreEqual(countOfVisitedVertices, graph.Count);
        }
        public void GraphAddVertexTest_Success()
        {
            var graph = new DirectedWeightedGraph <char>(10);

            graph.AddVertex('A');
            graph.AddVertex('B');
            graph.AddVertex('C');

            graph.Count.Should().Be(3);
        }
        public void GraphAddEdgeTest_ShouldThrowZeroWeight()
        {
            var graph   = new DirectedWeightedGraph <char>(10);
            var vertexA = graph.AddVertex('A');
            var vertexB = graph.AddVertex('B');

            Action addZeroEdge = () => graph.AddEdge(vertexA, vertexB, 0);

            addZeroEdge.Should().Throw <InvalidOperationException>()
            .WithMessage("Edge weight cannot be zero.");
        }
        public void GraphRemoveEdgeTest_Success()
        {
            var graph   = new DirectedWeightedGraph <char>(10);
            var vertexA = graph.AddVertex('A');
            var vertexB = graph.AddVertex('B');

            graph.AddEdge(vertexA, vertexB, 5);

            graph.RemoveEdge(vertexA, vertexB);

            graph.AreAdjacent(vertexA, vertexB).Should().BeFalse();
        }
        public void GraphAddEdgeTest_ShouldThrowEdgeExists()
        {
            var       graph             = new DirectedWeightedGraph <char>(10);
            var       vertexA           = graph.AddVertex('A');
            var       vertexB           = graph.AddVertex('B');
            const int currentEdgeWeight = 5;

            graph.AddEdge(vertexA, vertexB, currentEdgeWeight);

            Action addZeroEdge = () => graph.AddEdge(vertexA, vertexB, 10);

            addZeroEdge.Should().Throw <InvalidOperationException>()
            .WithMessage($"Vertex already exists: {currentEdgeWeight}");
        }
Exemplo n.º 7
0
        public void DijkstraTest4_Success()
        {
            var graph = new DirectedWeightedGraph <char>(5);
            var a     = graph.AddVertex('A');
            var b     = graph.AddVertex('B');
            var c     = graph.AddVertex('C');
            var d     = graph.AddVertex('D');

            graph.AddEdge(a, b, 1);
            graph.AddEdge(b, a, 1);

            graph.AddEdge(a, c, 3);
            graph.AddEdge(c, a, 3);

            graph.AddEdge(c, d, 5);
            graph.AddEdge(d, c, 5);

            var shortestPathList = DijkstraAlgorithm.GenerateShortestPath(graph, a);

            shortestPathList.Length.Should().Be(4);
            shortestPathList[0].Vertex.Should().Be(a);
            shortestPathList[0].Distance.Should().Be(0);
            shortestPathList[0].PreviousVertex.Should().Be(a);
            shortestPathList[0].ToString().Should()
            .Be($"Vertex: {a} - Distance: {0} - Previous: {a}");

            shortestPathList[1].Vertex.Should().Be(b);
            shortestPathList[1].Distance.Should().Be(1);
            shortestPathList[1].PreviousVertex.Should().Be(a);
            shortestPathList[1].ToString().Should()
            .Be($"Vertex: {b} - Distance: {1} - Previous: {a}");

            shortestPathList[2].Vertex.Should().Be(c);
            shortestPathList[2].Distance.Should().Be(3);
            shortestPathList[2].PreviousVertex.Should().Be(a);
            shortestPathList[2].ToString().Should()
            .Be($"Vertex: {c} - Distance: {3} - Previous: {a}");

            // Vertex D won't be visited in this dijkstra implementation which is valid only for cyclic graphs,
            // since it is necessary to backtrack all unvisited vertices and place them
            // to the priority queue, which is not implemented yet in this repository.
            // If algo goes to the next vertex with minimal distance and this vertex is leaf -- algorithm stops.
            shortestPathList[3].Vertex.Should().Be(d);
            shortestPathList[3].Distance.Should().Be(double.MaxValue);
            shortestPathList[3].PreviousVertex.Should().BeNull();
            shortestPathList[3].ToString().Should()
            .Be($"Vertex: {d} - Distance: {double.MaxValue} - Previous: {null}");
        }
        public void GraphAddVertexTest_ShouldThrowOverflow()
        {
            var graph = new DirectedWeightedGraph <char>(10);

            for (var i = 0; i < 10; i++)
            {
                graph.AddVertex('A');
            }

            Action addOverflow = () => graph.AddVertex('A');

            graph.Count.Should().Be(10);
            graph.Vertices.Should().OnlyContain(x => x != null && x.Data == 'A');
            addOverflow.Should().Throw <InvalidOperationException>()
            .WithMessage("Graph overflow.");
        }
        public void Test()
        {
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i <= 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 1);
            graph1.AddEdge("b", "c", 2);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("a", "g", 600);
            graph1.AddEdge("b", "f", 0);
            graph1.AddEdge("e", "b", 7);


            Stack <Vertex> stack = new Stack <Vertex>();

            stack.Push(graph1.GetVertex("d"));
            stack.Push(graph1.GetVertex("c"));
            stack.Push(graph1.GetVertex("f"));
            stack.Push(graph1.GetVertex("b"));
            stack.Push(graph1.GetVertex("g"));
            stack.Push(graph1.GetVertex("a"));
            stack.Push(graph1.GetVertex("e"));
            stack.Push(graph1.GetVertex("h"));

            var testStack = TopologicalSort <DirectedWeightedGraph> .Sort(graph1);

            for (int i = 'a'; i <= 'h'; i++)
            {
                Assert.AreEqual(stack.Pop(), testStack.Pop());
            }
        }
Exemplo n.º 10
0
        public void Test()
        {
            // orientovaný ohodnocený.
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 1);
            graph1.AddEdge("b", "c", 2);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("d", "e", -4);
            graph1.AddEdge("f", "c", 5);
            graph1.AddEdge("a", "g", 600);
            graph1.AddEdge("b", "f", 0);
            graph1.AddEdge("b", "e", 7);

            DepthFirstSearch <DirectedWeightedGraph> .Search(graph1, "a");

            Assert.AreEqual(0, graph1.GetVertex("a").distance);
            Assert.AreEqual(1, graph1.GetVertex("b").distance);
            Assert.AreEqual(2, graph1.GetVertex("c").distance);
            Assert.AreEqual(3, graph1.GetVertex("d").distance);
            Assert.AreEqual(4, graph1.GetVertex("e").distance);
            Assert.AreEqual(2, graph1.GetVertex("f").distance);
            Assert.AreEqual(1, graph1.GetVertex("g").distance);
        }
Exemplo n.º 11
0
        public void CorrectMatrixTest()
        {
            var graph = new DirectedWeightedGraph <int>(10);

            var vertex1 = graph.AddVertex(1);

            var vertex2 = graph.AddVertex(2);

            var vertex3 = graph.AddVertex(3);

            var vertex4 = graph.AddVertex(4);

            var vertex5 = graph.AddVertex(5);

            graph.AddEdge(vertex1, vertex2, 3);

            graph.AddEdge(vertex1, vertex5, -4);

            graph.AddEdge(vertex1, vertex3, 8);

            graph.AddEdge(vertex2, vertex5, 7);

            graph.AddEdge(vertex2, vertex4, 1);

            graph.AddEdge(vertex3, vertex2, 4);

            graph.AddEdge(vertex4, vertex3, -5);

            graph.AddEdge(vertex4, vertex1, 2);

            graph.AddEdge(vertex5, vertex4, 6);

            var actualDistances = new double[, ]
            {
                { 0, 1, -3, 2, -4 },
                { 3, 0, -4, 1, -1 },
                { 7, 4, 0, 5, 3 },
                { 2, -1, -5, 0, -2 },
                { 8, 5, 1, 6, 0 },
            };

            var floydWarshaller = new FloydWarshall <int>();

            floydWarshaller.Run(graph).Should().Equal(actualDistances);
        }
Exemplo n.º 12
0
        public void VisitAll_ReturnTheSuqenceOfVertices_ShouldBeTheSameAsExpected()
        {
            //Arrange
            var graph = new DirectedWeightedGraph <int>(10);

            var vertex1 = graph.AddVertex(1);

            var vertex2 = graph.AddVertex(20);

            var vertex3 = graph.AddVertex(40);

            var vertex4 = graph.AddVertex(40);

            var vertex5 = graph.AddVertex(40);

            graph.AddEdge(vertex1, vertex2, 1);

            graph.AddEdge(vertex1, vertex5, 1);

            graph.AddEdge(vertex2, vertex3, 1);

            graph.AddEdge(vertex2, vertex5, 1);

            graph.AddEdge(vertex2, vertex4, 1);

            var dfsSearcher = new BreadthFirstSearch <int>();

            var expectedSequenceOfVisitedVertices = new List <Vertex <int> >
            {
                vertex1,
                vertex2,
                vertex5,
                vertex3,
                vertex4,
            };

            var sequenceOfVisitedVertices = new List <Vertex <int> >();

            //Act
            dfsSearcher.VisitAll(graph, vertex1, vertex => sequenceOfVisitedVertices.Add(vertex));

            //Assert
            CollectionAssert.AreEqual(expectedSequenceOfVisitedVertices, sequenceOfVisitedVertices);
        }
Exemplo n.º 13
0
        public void DijkstraMethodTest_ShouldThrow_GraphIsNull()
        {
            var graph = new DirectedWeightedGraph <char>(5);
            var a     = graph.AddVertex('A');

            Func <DistanceModel <char>[]> action = () => DijkstraAlgorithm.GenerateShortestPath(null !, a);

            action.Should().Throw <ArgumentNullException>()
            .WithMessage($"Value cannot be null. (Parameter '{nameof(graph)}')");
        }
        public void GraphGetNeighborsTest_Success()
        {
            var graph   = new DirectedWeightedGraph <char>(10);
            var vertexA = graph.AddVertex('A');
            var vertexB = graph.AddVertex('B');
            var vertexC = graph.AddVertex('C');
            var vertexD = graph.AddVertex('D');

            graph.AddEdge(vertexA, vertexB, 5);
            graph.AddEdge(vertexA, vertexC, 5);
            graph.AddEdge(vertexA, vertexD, 5);

            var neighborsA = graph.GetNeighbors(vertexA).ToArray();

            neighborsA.Should().HaveCount(3);
            neighborsA[0].Should().Be(vertexB);
            neighborsA[1].Should().Be(vertexC);
            neighborsA[2].Should().Be(vertexD);
        }
        public void GraphRemoveEdgeTest_ShouldThrowVertexNotInGraph()
        {
            var graph   = new DirectedWeightedGraph <char>(10);
            var vertexA = graph.AddVertex('A');
            var vertexB = new Vertex <char>('B', 1);

            Action removeEdge = () => graph.RemoveEdge(vertexA, vertexB);

            removeEdge.Should().Throw <InvalidOperationException>()
            .WithMessage($"Vertex does not belong to graph: {vertexB}.");
        }
        public void GraphRemoveVertexTest_Success()
        {
            var graph   = new DirectedWeightedGraph <char>(10);
            var vertexA = graph.AddVertex('A');
            var vertexB = graph.AddVertex('B');
            var vertexC = graph.AddVertex('C');

            graph.AddEdge(vertexB, vertexA, 5);
            graph.AddEdge(vertexC, vertexA, 5);
            var neighborsB = graph.GetNeighbors(vertexB).ToList();
            var neighborsC = graph.GetNeighbors(vertexC).ToList();

            graph.RemoveVertex(vertexA);

            neighborsB.Should().HaveCount(1);
            neighborsB[0].Should().Be(vertexA);
            neighborsC.Should().HaveCount(1);
            neighborsC[0].Should().Be(vertexA);
            graph.GetNeighbors(vertexB).Should().HaveCount(0);
            graph.GetNeighbors(vertexC).Should().HaveCount(0);
        }
        public void Test()
        {
            #region Orientovaná kružnice

            // orientovaný ohodnocený.
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 1);
            graph1.AddEdge("b", "c", 2);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("d", "e", -4);
            graph1.AddEdge("f", "c", 5);
            graph1.AddEdge("a", "g", 600);
            graph1.AddEdge("b", "f", 0);
            graph1.AddEdge("e", "b", 7);


            Assert.IsTrue(Cycle.ContainsCycle <DirectedWeightedGraph>(graph1));

            graph1.Clear();

            #endregion

            #region Orientovaný bez kružnice

            // orientovaný ohodnocený.
            var graph3 = new DirectedUnweightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph3.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }


            graph3.AddEdge("a", "b");
            graph3.AddEdge("b", "c");
            graph3.AddEdge("c", "d");
            graph3.AddEdge("a", "g");
            graph3.AddEdge("b", "f");
            graph3.AddEdge("e", "b");

            Assert.IsFalse(Cycle.ContainsCycle <DirectedUnweightedGraph>(graph3));

            graph3.Clear();

            #endregion
        }
Exemplo n.º 18
0
        private static DirectedWeightedGraph <string> GenerateGraph(IEnumerable <string> testData)
        {
            var graph = new DirectedWeightedGraph <string>();

            // Build the thing - complete directed weighted graph
            foreach (var line in testData)
            {
                //Alice would gain 2 happiness units by sitting next to Bob.
                var parts = line.Split(' ');
                var n1    = parts[0];
                var n2    = parts.Last().Substring(0, parts.Last().Length - 1);

                var v1 = graph.GetVertex(n1);
                var v2 = graph.GetVertex(n2);

                if (v1 == null)
                {
                    v1 = graph.AddVertex(n1);
                }

                if (v2 == null)
                {
                    v2 = graph.AddVertex(n2);
                }

                //Happiness cost
                var hCost = int.Parse(parts[3]);

                if (parts[2].Equals("lose"))
                {
                    hCost *= -1;
                }

                graph.AddEdge(v1, v2, hCost);
            }

            return(graph);
        }
        public void Test()
        {
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i <= 'e'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 1);
            graph1.AddEdge("b", "a", 2);
            graph1.AddEdge("c", "a", 3);
            graph1.AddEdge("c", "b", 600);
            graph1.AddEdge("c", "d", 0);
            graph1.AddEdge("d", "c", 7);
            graph1.AddEdge("d", "b", 7);

            var list = new List <List <Vertex> >();

            var subList1 = new List <Vertex>();
            var subList2 = new List <Vertex>();
            var subList3 = new List <Vertex>();

            subList1.Add(graph1.GetVertex("b"));
            subList1.Add(graph1.GetVertex("a"));

            subList2.Add(graph1.GetVertex("d"));
            subList2.Add(graph1.GetVertex("c"));

            subList3.Add(graph1.GetVertex("e"));

            list.Add(subList1);
            list.Add(subList2);
            list.Add(subList3);

            var testList = StronglyConnectedComponents <DirectedWeightedGraph> .GetComponents(graph1);


            Assert.AreEqual(list.Count, testList.Count);

            for (int subList = 0; subList <= 1; subList++)
            {
                for (int vertex = 0; vertex <= 1; vertex++)
                {
                    Assert.AreEqual(list[subList][vertex], testList[subList][vertex]);
                }
            }

            Assert.AreEqual(list[2][0], testList[2][0]);
        }
Exemplo n.º 20
0
        public void Test()
        {
            // orientovaný ohodnocený.
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 10);
            graph1.AddEdge("b", "c", 20);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("d", "e", 4);
            graph1.AddEdge("f", "c", 2);
            graph1.AddEdge("b", "f", 3);
            graph1.AddEdge("e", "b", 1);

            DijkstraShortestPath <DirectedWeightedGraph> .Search(graph1, "a");

            Assert.AreEqual(0, graph1.GetVertex("a").distance);
            Assert.AreEqual(10, graph1.GetVertex("b").distance);
            Assert.AreEqual(15, graph1.GetVertex("c").distance);
            Assert.AreEqual(18, graph1.GetVertex("d").distance);
            Assert.AreEqual(22, graph1.GetVertex("e").distance);
            Assert.AreEqual(13, graph1.GetVertex("f").distance);
            Assert.AreEqual(long.MaxValue, graph1.GetVertex("g").distance);


            Stack <Vertex> stack = new Stack <Vertex>();

            stack.Push(graph1.GetVertex("e"));
            stack.Push(graph1.GetVertex("d"));
            stack.Push(graph1.GetVertex("c"));
            stack.Push(graph1.GetVertex("f"));
            stack.Push(graph1.GetVertex("b"));
            stack.Push(graph1.GetVertex("a"));

            var stackTest = DijkstraShortestPath <DirectedWeightedGraph> .GetShortestPath(graph1, "e");

            while (stack.Count > 0)
            {
                Assert.AreEqual(stack.Pop(), stackTest.Pop());
            }
        }
Exemplo n.º 21
0
        public void Test()
        {
            // orientovaný ohodnocený.
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 1);
            graph1.AddEdge("b", "c", 2);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("d", "e", -4);
            graph1.AddEdge("f", "c", 5);
            graph1.AddEdge("a", "g", 600);
            graph1.AddEdge("b", "f", 0);
            graph1.AddEdge("b", "e", 7);

            BreathFirstSearch <DirectedWeightedGraph> .Search(graph1, "a");

            Assert.AreEqual(0, graph1.GetVertex("a").distance);
            Assert.AreEqual(1, graph1.GetVertex("b").distance);
            Assert.AreEqual(2, graph1.GetVertex("c").distance);
            Assert.AreEqual(3, graph1.GetVertex("d").distance);
            Assert.AreEqual(2, graph1.GetVertex("e").distance);
            Assert.AreEqual(2, graph1.GetVertex("f").distance);
            Assert.AreEqual(1, graph1.GetVertex("g").distance);


            Stack <Vertex> stack = new Stack <Vertex>();

            stack.Push(graph1.GetVertex("e"));
            stack.Push(graph1.GetVertex("b"));
            stack.Push(graph1.GetVertex("a"));

            var stackTest = BreathFirstSearch <DirectedWeightedGraph> .GetShortestPath(graph1, "e");

            while (stack.Count > 0)
            {
                Assert.AreEqual(stack.Pop(), stackTest.Pop());
            }
        }
        public void VisitAll_ShouldCountNumberOfVisitedVerices_TwoSeparatedGraphInOne()
        {
            //Arrange
            var graph = new DirectedWeightedGraph <int>(10);

            var vertex1 = graph.AddVertex(1);

            var vertex2 = graph.AddVertex(20);

            var vertex3 = graph.AddVertex(40);

            var vertex4 = graph.AddVertex(40);

            var vertex5 = graph.AddVertex(40);

            var vertex6 = graph.AddVertex(40);

            graph.AddEdge(vertex1, vertex2, 1);

            graph.AddEdge(vertex2, vertex3, 1);

            graph.AddEdge(vertex4, vertex5, 1);

            graph.AddEdge(vertex5, vertex6, 1);

            var dfsSearcher = new DepthFirstSearch <int>();

            long countOfVisitedVerticesPerFirstGraph = 0;

            long countOfVisitedVerticesPerSecondGraph = 0;

            //Act
            dfsSearcher.VisitAll(graph, vertex1, (Vertex <int> vertex) => countOfVisitedVerticesPerFirstGraph++);

            dfsSearcher.VisitAll(graph, vertex4, (Vertex <int> vertex) => countOfVisitedVerticesPerSecondGraph++);

            //Assert
            Assert.AreEqual(countOfVisitedVerticesPerFirstGraph, 3);

            Assert.AreEqual(countOfVisitedVerticesPerSecondGraph, 3);
        }
Exemplo n.º 23
0
        public void DijkstraTest1_Success()
        {
            // here test case is from https://www.youtube.com/watch?v=pVfj6mxhdMw

            var graph = new DirectedWeightedGraph <char>(5);
            var a     = graph.AddVertex('A');
            var b     = graph.AddVertex('B');
            var c     = graph.AddVertex('C');
            var d     = graph.AddVertex('D');
            var e     = graph.AddVertex('E');

            graph.AddEdge(a, b, 6);
            graph.AddEdge(b, a, 6);

            graph.AddEdge(a, d, 1);
            graph.AddEdge(d, a, 1);

            graph.AddEdge(d, e, 1);
            graph.AddEdge(e, d, 1);

            graph.AddEdge(d, b, 2);
            graph.AddEdge(b, d, 2);

            graph.AddEdge(e, b, 2);
            graph.AddEdge(b, e, 2);

            graph.AddEdge(e, c, 5);
            graph.AddEdge(c, e, 5);

            graph.AddEdge(c, b, 5);
            graph.AddEdge(b, c, 5);

            var shortestPathList = DijkstraAlgorithm.GenerateShortestPath(graph, a);

            shortestPathList.Length.Should().Be(5);

            shortestPathList[0].Vertex.Should().Be(a);
            shortestPathList[0].Distance.Should().Be(0);
            shortestPathList[0].PreviousVertex.Should().Be(a);
            shortestPathList[0].ToString().Should()
            .Be($"Vertex: {a} - Distance: {0} - Previous: {a}");

            shortestPathList[1].Vertex.Should().Be(b);
            shortestPathList[1].Distance.Should().Be(3);
            shortestPathList[1].PreviousVertex.Should().Be(d);
            shortestPathList[1].ToString().Should()
            .Be($"Vertex: {b} - Distance: {3} - Previous: {d}");

            shortestPathList[2].Vertex.Should().Be(c);
            shortestPathList[2].Distance.Should().Be(7);
            shortestPathList[2].PreviousVertex.Should().Be(e);
            shortestPathList[2].ToString().Should()
            .Be($"Vertex: {c} - Distance: {7} - Previous: {e}");

            shortestPathList[3].Vertex.Should().Be(d);
            shortestPathList[3].Distance.Should().Be(1);
            shortestPathList[3].PreviousVertex.Should().Be(a);
            shortestPathList[3].ToString().Should()
            .Be($"Vertex: {d} - Distance: {1} - Previous: {a}");

            shortestPathList[4].Vertex.Should().Be(e);
            shortestPathList[4].Distance.Should().Be(2);
            shortestPathList[4].PreviousVertex.Should().Be(d);
            shortestPathList[4].ToString().Should()
            .Be($"Vertex: {e} - Distance: {2} - Previous: {d}");
        }
        public void Test()
        {
            DirectedWeightedGraph graph1 = new DirectedWeightedGraph(false);

            #region Basic stuff
            Assert.AreEqual(true, graph1.directed);
            Assert.AreEqual(true, graph1.weighted);
            Assert.AreEqual(false, graph1.multigraph);
            #endregion

            #region Vertex stuff
            for (char i = 'a'; i <= 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(i));
            }
            graph1.AddVertex("a");

            Assert.AreEqual(8, graph1.verticesCount);

            graph1.RemoveVertex("h");
            graph1.RemoveVertex("z");

            Assert.AreEqual(7, graph1.verticesCount);
            Assert.AreEqual(true, graph1.ExistVertex("a"));
            Assert.AreEqual(false, graph1.ExistVertex("z"));
            #endregion

            #region Edge stuff
            graph1.AddEdge("a", "b", 10);
            graph1.AddEdge("a", "b", 2);

            Assert.AreEqual(1, graph1.edgesCount);

            graph1.AddEdge("a", "a");
            graph1.AddEdge("a", "a", 2);

            Assert.AreEqual(1, graph1.edgesCount);

            graph1.AddEdge("a", "z", 2);
            graph1.AddEdge("z", "a", 2);
            graph1.AddEdge("y", "z", 2);

            Assert.AreEqual(1, graph1.edgesCount);

            graph1.AddEdge("b", "c", 1);
            graph1.AddEdge("c", "d", 1);
            graph1.AddEdge("d", "e", 1);
            graph1.AddEdge("e", "f", 1);
            graph1.AddEdge("f", "g", 1);

            Assert.AreEqual(6, graph1.edgesCount);

            graph1.RemoveEdge("a", "b");
            graph1.RemoveEdge("a", "c", 1);
            graph1.RemoveEdge("f", "g", 1);

            Assert.AreEqual(5, graph1.edgesCount);
            Assert.AreEqual(true, graph1.ExistEdge("a", "b"));

            Assert.AreEqual(10, graph1.GetEdgeWeight("a", "b"));

            graph1.ChangeWeight("a", "b", 10, 20);

            Assert.AreEqual(20, graph1.GetEdgeWeight("a", "b"));

            graph1.RemoveVertex("d");
            Assert.AreEqual(3, graph1.edgesCount);
            #endregion

            graph1.Clear();

            Assert.AreEqual(0, graph1.verticesCount);
            Assert.AreEqual(0, graph1.edgesCount);


            DirectedWeightedGraph graph2 = new DirectedWeightedGraph(true);

            #region Basic stuff
            Assert.AreEqual(true, graph2.directed);
            Assert.AreEqual(true, graph2.weighted);
            Assert.AreEqual(true, graph2.multigraph);
            #endregion

            #region Vertex stuff
            for (char i = 'a'; i <= 'h'; i++)
            {
                graph2.AddVertex(Convert.ToString(i));
            }
            graph2.AddVertex("a");

            Assert.AreEqual(8, graph2.verticesCount);

            graph2.RemoveVertex("h");
            graph2.RemoveVertex("z");

            Assert.AreEqual(7, graph2.verticesCount);
            Assert.AreEqual(true, graph2.ExistVertex("a"));
            Assert.AreEqual(false, graph2.ExistVertex("z"));
            #endregion

            #region Edge stuff
            graph2.AddEdge("a", "b", 10);
            graph2.AddEdge("a", "b", 2);

            Assert.AreEqual(2, graph2.edgesCount);

            graph2.AddEdge("a", "a");
            graph2.AddEdge("a", "a", 2);

            Assert.AreEqual(2, graph2.edgesCount);

            graph2.AddEdge("a", "z", 2);
            graph2.AddEdge("z", "a", 2);
            graph2.AddEdge("y", "z", 2);

            Assert.AreEqual(2, graph2.edgesCount);

            graph2.AddEdge("b", "c", 1);
            graph2.AddEdge("c", "d", 1);
            graph2.AddEdge("d", "e", 1);
            graph2.AddEdge("e", "f", 1);
            graph2.AddEdge("f", "g", 1);

            Assert.AreEqual(7, graph2.edgesCount);

            graph2.RemoveEdge("a", "b");
            graph1.RemoveEdge("a", "c", 1);
            graph2.RemoveEdge("f", "g", 1);

            Assert.AreEqual(6, graph2.edgesCount);
            Assert.AreEqual(true, graph2.ExistEdge("a", "b"));

            Assert.AreEqual(1, graph2.GetEdgeWeight("a", "b"));

            graph2.ChangeWeight("a", "b", 10, 20);

            Assert.AreEqual(1, graph2.GetEdgeWeight("a", "b"));

            graph2.RemoveVertex("d");
            Assert.AreEqual(4, graph2.edgesCount);
            #endregion

            graph2.Clear();

            Assert.AreEqual(0, graph2.verticesCount);
            Assert.AreEqual(0, graph2.edgesCount);
        }
Exemplo n.º 25
0
        public void Test()
        {
            // orientovaný ohodnocený.
            var graph1 = new DirectedWeightedGraph(false);

            for (int i = 'a'; i < 'h'; i++)
            {
                graph1.AddVertex(Convert.ToString(Convert.ToChar(i)));
            }

            graph1.AddEdge("a", "b", 10);
            graph1.AddEdge("b", "c", 20);
            graph1.AddEdge("c", "d", 3);
            graph1.AddEdge("d", "e", 4);
            graph1.AddEdge("f", "c", -20);
            graph1.AddEdge("b", "f", 3);
            graph1.AddEdge("e", "b", 10);

            long[,] distances   = new long[graph1.verticesCount, graph1.verticesCount];
            Vertex[,] followers = new Vertex[graph1.verticesCount, graph1.verticesCount];

            for (int i = 0; i < graph1.verticesCount; i++)
            {
                for (int j = 0; j < graph1.verticesCount; j++)
                {
                    distances[i, j] = long.MaxValue;
                    followers[i, j] = null;
                }
            }

            for (int i = 0; i < graph1.verticesCount; i++)
            {
                distances[i, i] = 0;
            }

            #region distances
            // vzdálenosti z "a"
            distances[0, 1] = 10;
            distances[0, 2] = -7;
            distances[0, 3] = -4;
            distances[0, 4] = 0;
            distances[0, 5] = 13;

            // vzdálenosti z "b"
            distances[1, 2] = -17;
            distances[1, 3] = -14;
            distances[1, 4] = -10;
            distances[1, 5] = 3;

            //vzdálenposti z "c"
            distances[2, 1] = 17;
            distances[2, 3] = 3;
            distances[2, 4] = 7;
            distances[2, 5] = 20;

            //vzdálenposti z "d"
            distances[3, 1] = 14;
            distances[3, 2] = -3;
            distances[3, 4] = 4;
            distances[3, 5] = 17;

            //vzdálenposti z "e"
            distances[4, 1] = 10;
            distances[4, 2] = -7;
            distances[4, 3] = -4;
            distances[4, 5] = 13;

            //vzdálenposti z "f"
            distances[5, 1] = -3;
            distances[5, 2] = -20;
            distances[5, 3] = -17;
            distances[5, 4] = -13;

            //"g" je nedosažitelný

            #endregion

            #region followers
            // předchůdci k cestě z "a"
            followers[0, 0] = graph1.GetVertex("a");
            followers[0, 1] = graph1.GetVertex("b");
            followers[0, 2] = graph1.GetVertex("b");
            followers[0, 3] = graph1.GetVertex("b");
            followers[0, 4] = graph1.GetVertex("b");
            followers[0, 5] = graph1.GetVertex("b");

            // předchůdci k cestě z "b"
            followers[1, 1] = graph1.GetVertex("b");
            followers[1, 2] = graph1.GetVertex("f");
            followers[1, 3] = graph1.GetVertex("f");
            followers[1, 4] = graph1.GetVertex("f");
            followers[1, 5] = graph1.GetVertex("f");

            // předchůdci k cestě z "c"
            followers[2, 2] = graph1.GetVertex("c");
            followers[2, 1] = graph1.GetVertex("d");
            followers[2, 3] = graph1.GetVertex("d");
            followers[2, 4] = graph1.GetVertex("d");
            followers[2, 5] = graph1.GetVertex("d");

            // předchůdci k cestě z "d"
            followers[3, 3] = graph1.GetVertex("d");
            followers[3, 1] = graph1.GetVertex("e");
            followers[3, 2] = graph1.GetVertex("e");
            followers[3, 4] = graph1.GetVertex("e");
            followers[3, 5] = graph1.GetVertex("e");

            // předchůdci k cestě "e"
            followers[4, 4] = graph1.GetVertex("e");
            followers[4, 1] = graph1.GetVertex("b");
            followers[4, 2] = graph1.GetVertex("b");
            followers[4, 3] = graph1.GetVertex("b");
            followers[4, 5] = graph1.GetVertex("b");

            // předchůdci k cestě "f"
            followers[5, 5] = graph1.GetVertex("f");
            followers[5, 1] = graph1.GetVertex("c");
            followers[5, 2] = graph1.GetVertex("c");
            followers[5, 3] = graph1.GetVertex("c");
            followers[5, 4] = graph1.GetVertex("c");

            //"g" je nedosažitelný
            followers[6, 6] = graph1.GetVertex("g");

            #endregion

            var(distancesTest, followersTest) = FloydWarshallShortestPath <DirectedWeightedGraph> .Search(graph1).ToTuple();

            for (int i = 0; i < graph1.verticesCount; i++)
            {
                for (int j = 0; j < graph1.verticesCount; j++)
                {
                    Assert.AreEqual(distances[i, j], distancesTest[i, j]);
                    Assert.AreEqual(followers[i, j], followersTest[i, j]);
                }
            }

            Stack <Vertex> path = new Stack <Vertex>();

            path.Push(graph1.GetVertex("a"));
            path.Push(graph1.GetVertex("b"));
            path.Push(graph1.GetVertex("f"));
            path.Push(graph1.GetVertex("c"));
            path.Push(graph1.GetVertex("d"));
            path.Push(graph1.GetVertex("e"));


            Stack <Vertex> pathTest = FloydWarshallShortestPath <DirectedWeightedGraph> .GetShortestPath(graph1, "a", "e", ref followersTest);

            while (path.Count > 0)
            {
                Assert.AreEqual(path.Pop(), pathTest.Pop());
            }
        }