コード例 #1
0
ファイル: WeightedALGraphTests.cs プロジェクト: zack7891/DSA
        public void UpdatingAndGettingEdgeWeight()
        {
            var vertices = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

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

            graph.AddVertices(vertices);

            Assert.IsTrue(graph.VerticesCount == vertices.Length);

            foreach (var vertex in vertices)
            {
                Assert.IsTrue(graph.ContainsVertex(vertex));
            }

            // 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);

            int weight = 0;

            Assert.IsTrue(graph.TryGetEdgeWeight(1, 2, out weight));
            Assert.IsTrue(weight == 3);
            Assert.IsTrue(graph.UpdateEdgeWeight(1, 2, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 1, out weight));
            Assert.IsTrue(weight == 6);
            Assert.IsTrue(graph.TryGetEdgeWeight(1, 3, out weight));
            Assert.IsTrue(weight == 4);
            Assert.IsTrue(graph.UpdateEdgeWeight(1, 3, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(3, 1, out weight));
            Assert.IsTrue(weight == 8);
            Assert.IsTrue(graph.TryGetEdgeWeight(1, 4, out weight));
            Assert.IsTrue(weight == 5);
            Assert.IsTrue(graph.UpdateEdgeWeight(1, 4, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(4, 1, out weight));
            Assert.IsTrue(weight == 10);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 3, out weight));
            Assert.IsTrue(weight == 5);
            Assert.IsTrue(graph.UpdateEdgeWeight(2, 3, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(3, 2, out weight));
            Assert.IsTrue(weight == 10);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 5, out weight));
            Assert.IsTrue(weight == 7);
            Assert.IsTrue(graph.UpdateEdgeWeight(2, 5, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(5, 2, out weight));
            Assert.IsTrue(weight == 14);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 6, out weight));
            Assert.IsTrue(weight == 8);
            Assert.IsTrue(graph.UpdateEdgeWeight(2, 6, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(6, 2, out weight));
            Assert.IsTrue(weight == 16);
            Assert.IsTrue(graph.TryGetEdgeWeight(3, 6, out weight));
            Assert.IsTrue(weight == 9);
            Assert.IsTrue(graph.UpdateEdgeWeight(3, 6, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(6, 3, out weight));
            Assert.IsTrue(weight == 18);
            Assert.IsTrue(graph.TryGetEdgeWeight(4, 6, out weight));
            Assert.IsTrue(weight == 10);
            Assert.IsTrue(graph.UpdateEdgeWeight(4, 6, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(6, 4, out weight));
            Assert.IsTrue(weight == 20);
            Assert.IsTrue(graph.TryGetEdgeWeight(4, 7, out weight));
            Assert.IsTrue(weight == 11);
            Assert.IsTrue(graph.UpdateEdgeWeight(4, 7, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(7, 4, out weight));
            Assert.IsTrue(weight == 22);
            Assert.IsTrue(graph.TryGetEdgeWeight(5, 7, out weight));
            Assert.IsTrue(weight == 12);
            Assert.IsTrue(graph.UpdateEdgeWeight(5, 7, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(7, 5, out weight));
            Assert.IsTrue(weight == 24);
            Assert.IsTrue(graph.TryGetEdgeWeight(5, 8, out weight));
            Assert.IsTrue(weight == 13);
            Assert.IsTrue(graph.UpdateEdgeWeight(5, 8, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(8, 5, out weight));
            Assert.IsTrue(weight == 26);
            Assert.IsTrue(graph.TryGetEdgeWeight(7, 8, out weight));
            Assert.IsTrue(weight == 15);
            Assert.IsTrue(graph.UpdateEdgeWeight(7, 8, weight * 2));
            Assert.IsTrue(graph.TryGetEdgeWeight(8, 7, out weight));
            Assert.IsTrue(weight == 30);
        }
コード例 #2
0
ファイル: WeightedALGraphTests.cs プロジェクト: zack7891/DSA
        public void CheckingIfVerticesAndEdgesAreAddedProperly()
        {
            var vertices = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

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

            graph.AddVertices(vertices);

            Assert.IsTrue(graph.VerticesCount == vertices.Length);

            foreach (var vertex in vertices)
            {
                Assert.IsTrue(graph.ContainsVertex(vertex));
            }

            // 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);

            Assert.IsTrue(graph.EdgesCount == 12);

            int weight = 0;

            Assert.IsTrue(graph.TryGetEdgeWeight(1, 2, out weight));
            Assert.IsTrue(weight == 3);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 1, out weight));
            Assert.IsTrue(weight == 3);
            Assert.IsTrue(graph.TryGetEdgeWeight(1, 3, out weight));
            Assert.IsTrue(weight == 4);
            Assert.IsTrue(graph.TryGetEdgeWeight(3, 1, out weight));
            Assert.IsTrue(weight == 4);
            Assert.IsTrue(graph.TryGetEdgeWeight(1, 4, out weight));
            Assert.IsTrue(weight == 5);
            Assert.IsTrue(graph.TryGetEdgeWeight(4, 1, out weight));
            Assert.IsTrue(weight == 5);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 3, out weight));
            Assert.IsTrue(weight == 5);
            Assert.IsTrue(graph.TryGetEdgeWeight(3, 2, out weight));
            Assert.IsTrue(weight == 5);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 5, out weight));
            Assert.IsTrue(weight == 7);
            Assert.IsTrue(graph.TryGetEdgeWeight(5, 2, out weight));
            Assert.IsTrue(weight == 7);
            Assert.IsTrue(graph.TryGetEdgeWeight(2, 6, out weight));
            Assert.IsTrue(weight == 8);
            Assert.IsTrue(graph.TryGetEdgeWeight(6, 2, out weight));
            Assert.IsTrue(weight == 8);
            Assert.IsTrue(graph.TryGetEdgeWeight(3, 6, out weight));
            Assert.IsTrue(weight == 9);
            Assert.IsTrue(graph.TryGetEdgeWeight(6, 3, out weight));
            Assert.IsTrue(weight == 9);
            Assert.IsTrue(graph.TryGetEdgeWeight(4, 6, out weight));
            Assert.IsTrue(weight == 10);
            Assert.IsTrue(graph.TryGetEdgeWeight(6, 4, out weight));
            Assert.IsTrue(weight == 10);
            Assert.IsTrue(graph.TryGetEdgeWeight(4, 7, out weight));
            Assert.IsTrue(weight == 11);
            Assert.IsTrue(graph.TryGetEdgeWeight(7, 4, out weight));
            Assert.IsTrue(weight == 11);
            Assert.IsTrue(graph.TryGetEdgeWeight(5, 7, out weight));
            Assert.IsTrue(weight == 12);
            Assert.IsTrue(graph.TryGetEdgeWeight(7, 5, out weight));
            Assert.IsTrue(weight == 12);
            Assert.IsTrue(graph.TryGetEdgeWeight(5, 8, out weight));
            Assert.IsTrue(weight == 13);
            Assert.IsTrue(graph.TryGetEdgeWeight(8, 5, out weight));
            Assert.IsTrue(weight == 13);
            Assert.IsTrue(graph.TryGetEdgeWeight(7, 8, out weight));
            Assert.IsTrue(weight == 15);
            Assert.IsTrue(graph.TryGetEdgeWeight(8, 7, out weight));
            Assert.IsTrue(weight == 15);

            Assert.IsFalse(graph.ContainsEdge(1, 7));
            Assert.IsFalse(graph.ContainsEdge(1, 5));
            Assert.IsFalse(graph.ContainsEdge(1, 8));
            Assert.IsFalse(graph.ContainsEdge(2, 4));
            Assert.IsFalse(graph.ContainsEdge(2, 7));
            Assert.IsFalse(graph.ContainsEdge(2, 8));
            Assert.IsFalse(graph.ContainsEdge(3, 4));
            Assert.IsFalse(graph.ContainsEdge(3, 5));
            Assert.IsFalse(graph.ContainsEdge(3, 7));
            Assert.IsFalse(graph.ContainsEdge(3, 8));
            Assert.IsFalse(graph.ContainsEdge(4, 2));
            Assert.IsFalse(graph.ContainsEdge(4, 3));
            Assert.IsFalse(graph.ContainsEdge(4, 5));
            Assert.IsFalse(graph.ContainsEdge(4, 8));
            Assert.IsFalse(graph.ContainsEdge(5, 1));
            Assert.IsFalse(graph.ContainsEdge(5, 3));
            Assert.IsFalse(graph.ContainsEdge(5, 4));
            Assert.IsFalse(graph.ContainsEdge(5, 6));
            Assert.IsFalse(graph.ContainsEdge(6, 1));
            Assert.IsFalse(graph.ContainsEdge(6, 5));
            Assert.IsFalse(graph.ContainsEdge(6, 7));
            Assert.IsFalse(graph.ContainsEdge(6, 8));
            Assert.IsFalse(graph.ContainsEdge(7, 1));
            Assert.IsFalse(graph.ContainsEdge(7, 2));
            Assert.IsFalse(graph.ContainsEdge(7, 3));
            Assert.IsFalse(graph.ContainsEdge(7, 6));
            Assert.IsFalse(graph.ContainsEdge(8, 1));
            Assert.IsFalse(graph.ContainsEdge(8, 2));
            Assert.IsFalse(graph.ContainsEdge(8, 3));
            Assert.IsFalse(graph.ContainsEdge(8, 4));
            Assert.IsFalse(graph.ContainsEdge(8, 6));
        }