Exemplo n.º 1
0
        public void Should_RemoveEdge_Throw_If_Out_Of_Range()
        {
            //arrange
            var graph = new MyGraphAdj <int>(5);

            //act
            Action actLowerFrom  = () => graph.RemoveEdge(-1, 1);
            Action actHigherFrom = () => graph.RemoveEdge(6, 1);

            Action actLowerTo  = () => graph.RemoveEdge(1, -1);
            Action actHigherTo = () => graph.RemoveEdge(1, 6);

            //assert
            actLowerFrom.ShouldThrow <ArgumentOutOfRangeException>();
            actHigherFrom.ShouldThrow <ArgumentOutOfRangeException>();
            actLowerTo.ShouldThrow <ArgumentOutOfRangeException>();
            actHigherTo.ShouldThrow <ArgumentOutOfRangeException>();

            graph.Capacity.ShouldBeEquivalentTo(5);
            graph.Count.ShouldBeEquivalentTo(0);
        }
Exemplo n.º 2
0
        private void UpdateWeights(MyGraphAdj <T> tempGraph, IEnumerable <int> newWeights)
        {
            List <int[]> edgesToDelete = tempGraph.GetAllEdges().Where(x => x[2] == 0).ToList();

            foreach (var edge in edgesToDelete)
            {
                tempGraph.RemoveEdge(edge[0], edge[1]);
            }

            tempGraph.Remove((tempGraph.GetAllVertexes().Last().Key));

            var restEdges = tempGraph.GetAllEdges();

            foreach (var edge in restEdges)
            {
                int newWeight = edge[2] + newWeights.ElementAt(edge[0]) - newWeights.ElementAt(edge[1]);
                tempGraph.UpdateWeight(edge[0], edge[1], newWeight);
            }
        }
Exemplo n.º 3
0
        public void Should_RemoveEdge_With_Weight()
        {
            //arrange
            var graph = new MyGraphAdj <int>(6, true);

            graph.AddVertex(0, 0);
            graph.AddVertex(1, 1);
            graph.AddVertex(2, 2);
            graph.AddVertex(3, 3);
            graph.AddVertex(4, 4);
            graph.AddVertex(5, 5);

            graph.AddEdge(0, 5, 1);
            graph.AddEdge(0, 1, 2);
            graph.AddEdge(0, 4, 3);
            graph.AddEdge(1, 3, 4);
            graph.AddEdge(1, 4, 5);
            graph.AddEdge(2, 1, 6);
            graph.AddEdge(3, 2, 7);
            graph.AddEdge(3, 4, 8);

            var resBfs = new[] { 0, 4, 5 };

            //act
            graph.RemoveEdge(0, 1);
            var result = graph.BreadthFirstSearch(0).ToArray();

            //assert
            result.Length.ShouldBeEquivalentTo(resBfs.Length);
            for (int i = 0; i < result.Length; i++)
            {
                result[i].ShouldBeEquivalentTo(resBfs[i]);
            }

            graph.Capacity.ShouldBeEquivalentTo(6);
            graph.Count.ShouldBeEquivalentTo(6);
        }