Example #1
0
        public void TestAddED_RemoveED_5()
        {
            SimpleGraph graph = new SimpleGraph(3);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            Assert.IsTrue(graph.m_adjacency[0, 1] == 1);
            Assert.IsTrue(graph.m_adjacency[0, 2] == 1);

            graph.RemoveEdge(0, 2);
            Assert.IsTrue(graph.m_adjacency[0, 1] == 1);
            Assert.IsTrue(graph.m_adjacency[0, 2] == 0);

            graph.AddEdge(2, 0);
            Assert.IsTrue(graph.m_adjacency[0, 1] == 1);
            Assert.IsTrue(graph.m_adjacency[0, 2] == 1);

            graph.RemoveEdge(2, 0);
            Assert.IsTrue(graph.m_adjacency[0, 1] == 1);
            Assert.IsTrue(graph.m_adjacency[2, 0] == 0);
        }
Example #2
0
        public void AddEdge()
        {
            int[] arr = { 10, 15, 21, 23 };

            foreach (int item in arr)
            {
                graph.AddVertex(item);
            }
            int[] j = { 0, 0, 1, 0, 2 };
            int[] k = { 1, 2, 2, 3, 3 };
            for (int i1 = 0; i1 < j.Length; i1++)
            {
                if (!graph.IsEdge(j[i1], k[i1]))
                {
                    graph.AddEdge(j[i1], k[i1]);
                    if (!graph.IsEdge(j[i1], k[i1]))
                    {
                        Assert.Fail();
                    }
                }
                else
                {
                    Assert.Fail();
                }
            }
        }
Example #3
0
        public void TestAddED_RemoveED_3()
        {
            SimpleGraph graph = new SimpleGraph(4);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(2, 3);
            Assert.IsFalse(graph.IsEdge(1, 3));
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(0, 2));
            Assert.IsTrue(graph.IsEdge(0, 3));
            Assert.IsTrue(graph.IsEdge(2, 3));

            graph.RemoveVertex(2);
            Assert.IsTrue(graph.vertex[2] == null);
            Assert.IsFalse(graph.IsEdge(0, 2));
            Assert.IsFalse(graph.IsEdge(2, 0));

            Assert.IsFalse(graph.IsEdge(3, 2));
        }
Example #4
0
        public void WeakVertices_in_3v_where_Graph_Is_Triangle()
        {
            int size = 3;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(0); // добавляем вершины
            testGraph.AddVertex(10);
            testGraph.AddVertex(20);

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(1, 2);

            List <Vertex <int> > WeakList = testGraph.WeakVertices();

            int expectedCount = 0;
            int actualCount   = WeakList.Count;

            if (WeakList.Count != 0)
            {
                WeakList.ForEach((item) => Console.Write("{0} ", item.Value));
            }
            else
            {
                Console.WriteLine("[]");
            }

            Assert.IsNotNull(WeakList);
            Assert.AreEqual(expectedCount, actualCount);
        }
Example #5
0
        public static void Main(string[] args)
        {
            var graph = new SimpleGraph <bool>();

            var node1       = new SimpleGraphNode <bool>(1);
            var Node1Input1 = new SimpleGraphTerminal <bool>(node1);
            var Node1Input2 = new SimpleGraphTerminal <bool>(node1);
            var Node1Output = new SimpleGraphTerminal <bool>(node1);

            node1.Inputs.Add(Node1Input1);
            node1.Inputs.Add(Node1Input2);
            node1.Outputs.Add(Node1Output);

            var node2       = new SimpleGraphNode <bool>(2);
            var Node2Input1 = new SimpleGraphTerminal <bool>(node2);
            var Node2Output = new SimpleGraphTerminal <bool>(node2);

            node2.Inputs.Add(Node2Input1);
            node2.Outputs.Add(Node2Output);

            var node3       = new SimpleGraphNode <bool>(3);
            var Node3Input1 = new SimpleGraphTerminal <bool>(node3);
            var Node3Output = new SimpleGraphTerminal <bool>(node3);

            node3.Inputs.Add(Node3Input1);
            node3.Outputs.Add(Node3Output);

            var node4       = new SimpleGraphNode <bool>(4);
            var Node4Output = new SimpleGraphTerminal <bool>(node4);

            node4.Outputs.Add(Node4Output);

            var node5       = new SimpleGraphNode <bool>(5);
            var Node5Output = new SimpleGraphTerminal <bool>(node5);

            node5.Outputs.Add(Node5Output);

            var node6       = new SimpleGraphNode <bool>(6);
            var Node6Input1 = new SimpleGraphTerminal <bool>(node6);

            node6.Inputs.Add(Node6Input1);

            graph.AddNode(node1);
            graph.AddNode(node2);
            graph.AddNode(node3);
            graph.AddNode(node4);
            graph.AddNode(node5);
            graph.AddNode(node6);

            graph.AddEdge(new SimpleGraphEdge <bool>(Node3Output, Node6Input1));
            graph.AddEdge(new SimpleGraphEdge <bool>(Node1Output, Node2Input1));
            graph.AddEdge(new SimpleGraphEdge <bool>(Node2Output, Node3Input1));
            graph.AddEdge(new SimpleGraphEdge <bool>(Node4Output, Node1Input1));
            graph.AddEdge(new SimpleGraphEdge <bool>(Node5Output, Node1Input2));

            graph.CalculateFromNode(node3);

            Console.ReadKey();
        }
Example #6
0
 public void TestAddEdge()
 {
     Assert.IsFalse(_graph.IsEdge(1, 2));
     Assert.IsFalse(_graph.IsEdge(2, 1));
     _graph.AddEdge(1, 2);
     Assert.IsTrue(_graph.IsEdge(1, 2));
     Assert.IsTrue(_graph.IsEdge(2, 1));
 }
Example #7
0
    static PPATH()
    {
        // 10000 because zero-based indices. This isn't great as we don't need 10000
        // (as most #s don't represent primes), but hopefully it doesn't matter. Edges
        // exist between primes (a vertex whose index is prime) and other primes,
        // when there's a one-digit swap to go between them (swaps are reversible).
        _primeGraph = new SimpleGraph(10000);
        var primeDecider = new SieveOfEratosthenesDecider(9999);

        // If n is a prime, connect to the primes greater than it, within a one-digit swap. Only
        // greater than because lesser primes were already connected to it earlier in the loop.
        for (int n = 1001; n <= 9999; n += 2)
        {
            if (!primeDecider.IsOddPrime(n)) continue;

            int nSwapped = n + 1000;
            while (nSwapped % 10000 > n)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 1000;
            }

            nSwapped = n + 100;
            while (nSwapped % 1000 > n % 1000)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 100;
            }

            nSwapped = n + 10;
            while (nSwapped % 100 > n % 100)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 10;
            }

            nSwapped = n + 2;
            while (nSwapped % 10 > n % 10)
            {
                if (primeDecider.IsOddPrime(nSwapped))
                    _primeGraph.AddEdge(n, nSwapped);

                nSwapped += 2;
            }
        }
    }
Example #8
0
        public void AddEdge_5e_in_5v_Graph()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(19);
            testGraph.AddVertex(18);
            testGraph.AddVertex(17);
            testGraph.AddVertex(16);
            testGraph.AddVertex(15);

            Assert.IsTrue(testGraph.vertex[0].Value == 19);
            Assert.IsTrue(testGraph.vertex[1].Value == 18);
            Assert.IsTrue(testGraph.vertex[2].Value == 17);
            Assert.IsTrue(testGraph.vertex[3].Value == 16);
            Assert.IsTrue(testGraph.vertex[4].Value == 15);
            Assert.AreEqual(size, testGraph.max_vertex);
            Assert.AreEqual(size, testGraph.vertex.Length);
            Assert.AreEqual(size, testGraph.m_adjacency.GetLength(0));
            Assert.AreEqual(size, testGraph.m_adjacency.GetLength(1));
            Assert.AreEqual(0, testGraph.m_adjacency[0, 2]);
            Assert.AreEqual(0, testGraph.m_adjacency[2, 0]);


            testGraph.AddEdge(0, 1); // добавление ребра между вершинами
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(0, 3);
            testGraph.AddEdge(2, 1);
            testGraph.AddEdge(2, 3);

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }

            Assert.AreEqual(1, testGraph.m_adjacency[0, 1]);
            Assert.AreEqual(1, testGraph.m_adjacency[0, 2]);
            Assert.AreEqual(1, testGraph.m_adjacency[0, 3]);
            Assert.AreEqual(1, testGraph.m_adjacency[1, 0]);
            Assert.AreEqual(1, testGraph.m_adjacency[1, 2]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 0]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 1]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 3]);
            Assert.AreEqual(1, testGraph.m_adjacency[3, 0]);
            Assert.AreEqual(1, testGraph.m_adjacency[3, 2]);
        }
Example #9
0
    static NAKANJ()
    {
        _knightMoveGraph = new SimpleGraph(vertexCount: 64);

        // The problem's chessboard has rows from 8 to 1 and columns from a to h,
        // starting in the upper left corner. This one will have rows from 0 to 7
        // and columns from 0 to 7, starting in the upper left hand corner.
        for (int r = 0; r < 8; ++r)
        {
            for (int c = 0; c < 8; ++c)
            {
                int thisVertexID = GetVertexID(r, c);

                foreach (var knightMoveTransformation in _knightMoveTransformations)
                {
                    int rowTransformation    = knightMoveTransformation.Item1;
                    int columnTransformation = knightMoveTransformation.Item2;

                    int movedToVertexID;
                    if (TryGetVertexID(r + rowTransformation, c + columnTransformation,
                                       out movedToVertexID))
                    {
                        _knightMoveGraph.AddEdge(thisVertexID, movedToVertexID);
                    }
                }
            }
        }
    }
Example #10
0
        public void AddEdge_in_5v_Graph_If_v1_Is_not_Exists()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(19);
            testGraph.AddVertex(18);
            testGraph.AddVertex(17);
            testGraph.AddVertex(16);

            testGraph.AddEdge(0, 4);

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }
            Assert.IsTrue(testGraph.vertex[0].Value == 19); // вершина не изменились
            Assert.IsTrue(testGraph.vertex[1].Value == 18);
            Assert.IsTrue(testGraph.vertex[2].Value == 17);
            Assert.IsTrue(testGraph.vertex[3].Value == 16);
            Assert.IsNull(testGraph.vertex[4]);              // вершина не существует
            Assert.AreEqual(0, testGraph.m_adjacency[0, 4]); // ребро не добавлено к v0
            Assert.AreEqual(0, testGraph.m_adjacency[4, 0]); // ребро не добавлено к v4
        }
Example #11
0
    private static void Main()
    {
        var output    = new StringBuilder();
        int testCount = FastIO.ReadNonNegativeInt();

        for (int t = 1; t <= testCount; ++t)
        {
            int bugCount         = FastIO.ReadNonNegativeInt();
            int interactionCount = FastIO.ReadNonNegativeInt();
            var interactionGraph = new SimpleGraph(bugCount);

            for (int i = 0; i < interactionCount; ++i)
            {
                interactionGraph.AddEdge(
                    firstVertexID: FastIO.ReadNonNegativeInt() - 1,
                    secondVertexID: FastIO.ReadNonNegativeInt() - 1);
            }

            output.AppendLine($"Scenario #{t}:");
            output.AppendLine(BUGLIFE.Solve(interactionGraph)
                ? "No suspicious bugs found!" : "Suspicious bugs found!");
        }

        Console.Write(output);
    }
Example #12
0
        public void TestAddED_RemoveED_2()
        {
            SimpleGraph graph = new SimpleGraph(4);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);
            graph.AddEdge(0, 3);
            graph.AddEdge(2, 3);
            Assert.IsFalse(graph.IsEdge(1, 3));
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(0, 2));
            Assert.IsTrue(graph.IsEdge(0, 3));
            Assert.IsTrue(graph.IsEdge(2, 3));

            graph.RemoveVertex(2);
            Assert.IsTrue(graph.vertex[2] == null);
            Assert.IsFalse(graph.IsEdge(0, 2));
            Assert.IsFalse(graph.IsEdge(2, 0));

            Assert.IsFalse(graph.IsEdge(2, 3));
            Assert.IsFalse(graph.IsEdge(3, 2));

            Assert.IsTrue(graph.vertex[0] != null);
            Assert.IsTrue(graph.vertex[1] != null);
            Assert.IsTrue(graph.vertex[2] == null);
            Assert.IsTrue(graph.vertex[3] != null);

            Assert.IsTrue(graph.m_adjacency[0, 0] == 0);
            Assert.IsTrue(graph.m_adjacency[0, 1] == 1);
            Assert.IsTrue(graph.m_adjacency[0, 2] == 0);
            Assert.IsTrue(graph.m_adjacency[0, 3] == 1);
            Assert.IsTrue(graph.m_adjacency[1, 0] == 1);
            Assert.IsTrue(graph.m_adjacency[1, 2] == 0);
            Assert.IsTrue(graph.m_adjacency[1, 3] == 0);
            Assert.IsTrue(graph.m_adjacency[2, 0] == 0);
            Assert.IsTrue(graph.m_adjacency[2, 1] == 0);
            Assert.IsTrue(graph.m_adjacency[2, 3] == 0);
            Assert.IsTrue(graph.m_adjacency[3, 0] == 1);
            Assert.IsTrue(graph.m_adjacency[3, 1] == 0);
            Assert.IsTrue(graph.m_adjacency[3, 2] == 0);
        }
        public void TestFirstSilly_1()
        {
            SimpleGraph <int> graph = new SimpleGraph <int>(5);

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

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 4);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 3);
            graph.AddEdge(3, 4);

            List <Vertex <int> > list = graph.BreadthFirstSearch(0, 4);
        }
Example #14
0
        public void AddEdge()
        {
            var g = new SimpleGraph(4);

            g.AddVertex(1);
            g.AddVertex(2);

            Assert.True(!g.IsEdge(1, 2));
            Assert.True(!g.IsEdge(1, 1));

            g.AddEdge(1, 2);
            Assert.True(g.IsEdge(1, 2));
            Assert.True(!g.IsEdge(1, 1));

            g.AddEdge(1, 1);
            Assert.True(g.IsEdge(1, 2));
            Assert.True(g.IsEdge(1, 1));
        }
Example #15
0
        public void DepthFirstSearch_5_Vertex_where_1v_has_not_Adj()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(19); // добавляем вершины
            testGraph.AddVertex(18);
            testGraph.AddVertex(17);
            testGraph.AddVertex(16);
            testGraph.AddVertex(15);

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 4);
            testGraph.AddEdge(1, 3);
            testGraph.AddEdge(3, 4);

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }

            List <Vertex <int> > vList  = testGraph.DepthFirstSearch(0, 2); // попытка построения пути из 0 (ключ 19) в 2 (ключ 17).
            List <Vertex <int> > vList2 = testGraph.DepthFirstSearch(1, 2); // 1 (ключ 18) в 2 (ключ 17).
            List <Vertex <int> > vList3 = testGraph.DepthFirstSearch(3, 2); // 3 (ключ 16) в 2 (ключ 17).
            List <Vertex <int> > vList4 = testGraph.DepthFirstSearch(4, 2); // 4 (ключ 15) в 2 (ключ 17).

            vList.ForEach((item) => Console.WriteLine(item.Value));
            vList2.ForEach((item) => Console.WriteLine(item.Value));
            vList3.ForEach((item) => Console.WriteLine(item.Value));
            vList4.ForEach((item) => Console.WriteLine(item.Value));

            Assert.IsNotNull(vList);
            Assert.IsNotNull(vList2);
            Assert.IsNotNull(vList3);
            Assert.IsNotNull(vList4);
            Assert.IsTrue(vList.Count == 0);
            Assert.IsTrue(vList2.Count == 0);
            Assert.IsTrue(vList3.Count == 0);
            Assert.IsTrue(vList4.Count == 0);
        }
Example #16
0
    // For example, edges like (1, 2), (2, 3) => there's an edge between vertices 0 and 1 and 1 and 2.
    public static SimpleGraph CreateFromOneBasedEdges(int vertexCount, int[,] edges)
    {
        var graph = new SimpleGraph(vertexCount);

        for (int i = 0; i < edges.GetLength(0); ++i)
        {
            graph.AddEdge(edges[i, 0] - 1, edges[i, 1] - 1);
        }

        return(graph);
    }
Example #17
0
        public void AddsEdges()
        {
            var graph = new SimpleGraph(5);

            Assert.AreEqual(0, graph.Vertices[0].Degree);
            Assert.AreEqual(0, graph.Vertices[1].Degree);

            graph.AddEdge(0, 1);
            Assert.AreEqual(1, graph.Vertices[0].Degree);
            Assert.AreEqual(1, graph.Vertices[1].Degree);
            Assert.AreEqual(1, graph.Vertices[0].Neighbors.Single().ID);
            Assert.AreEqual(0, graph.Vertices[1].Neighbors.Single().ID);

            graph.AddEdge(1, 4);
            Assert.AreEqual(1, graph.Vertices[0].Degree);
            Assert.AreEqual(2, graph.Vertices[1].Degree);
            Assert.AreEqual(1, graph.Vertices[4].Degree);
            Assert.AreEqual(1, graph.Vertices[0].Neighbors.Single().ID);
            CollectionAssert.AreEquivalent(new[] { 0, 4 }, graph.Vertices[1].Neighbors.Select(n => n.ID).ToArray());
            Assert.AreEqual(1, graph.Vertices[4].Neighbors.Single().ID);
        }
Example #18
0
        public bool CheckTransportSystems(AlgorithmConfig cfg)
        {
            InitDataFromConfig(cfg);
            var graph = new SimpleGraph(_cities.Select(c => c.Id), null);

            foreach (var road in _roads)
            {
                graph.AddEdge(road.ToCityId, road.FromCityId, road.Id);
            }

            return(graph.ValidationCheck(_centralCities.Select(c => c.Id)));
        }
Example #19
0
        public void BreadthFirstSearch_5_Vertex_Where_path_has_2v()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(0); // добавляем вершины
            testGraph.AddVertex(10);
            testGraph.AddVertex(20);
            testGraph.AddVertex(30);
            testGraph.AddVertex(40);

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 3);
            testGraph.AddEdge(0, 4);

            testGraph.AddEdge(1, 2);
            testGraph.AddEdge(2, 3);
            testGraph.AddEdge(3, 4);

            List <Vertex <int> > vList = testGraph.BreadthFirstSearch(0, 4); // попытка построения пути из 0 (0) в 4 (40).

            vList.ForEach((item) => Console.Write(" {0}", item.Value));

            Assert.IsNotNull(vList);
            Assert.IsTrue(vList.Count == 2);
        }
Example #20
0
        public void TestAdd_Edge_1()
        {
            SimpleGraph graph = new SimpleGraph(5);

            graph.AddVertex(22);
            graph.AddVertex(5);
            graph.AddVertex(7);

            graph.AddEdge(0, 2);
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsFalse(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(0, 2));
        }
Example #21
0
        public void BreadthFirstSearch_10_Vertex_Where_Path_is_not_Exists()
        {
            int size = 10;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(0); // добавляем вершины
            testGraph.AddVertex(10);
            testGraph.AddVertex(20);
            testGraph.AddVertex(30);
            testGraph.AddVertex(40);
            testGraph.AddVertex(50);
            testGraph.AddVertex(60);
            testGraph.AddVertex(70);
            testGraph.AddVertex(80);

            testGraph.AddVertex(90); // ребро без связей

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(0, 3);

            testGraph.AddEdge(1, 4);
            testGraph.AddEdge(2, 5);
            testGraph.AddEdge(3, 6);

            testGraph.AddEdge(4, 7);
            testGraph.AddEdge(5, 7);
            testGraph.AddEdge(5, 8);

            testGraph.AddEdge(6, 8);

            List <Vertex <int> > vList = testGraph.BreadthFirstSearch(0, 9); // попытка построения пути из 0 (0) в  9 (90).

            vList.ForEach((item) => Console.Write(" {0}", item.Value));

            Assert.IsNotNull(vList);
            Assert.IsTrue(vList.Count == 0);
        }
Example #22
0
    private static void Main()
    {
        int treeSize = FastIO.ReadNonNegativeInt();
        var tree     = new SimpleGraph(treeSize);

        for (int e = 0; e < treeSize - 1; ++e)
        {
            int firstVertexID  = FastIO.ReadNonNegativeInt() - 1;
            int secondVertexID = FastIO.ReadNonNegativeInt() - 1;
            tree.AddEdge(firstVertexID, secondVertexID);
        }

        Console.Write(PT07X.Solve(tree));
    }
Example #23
0
        private static IUndirectedGraph <String, Edge <String> > CreateStringGraph()
        {
            var g = new SimpleGraph <String, Edge <String> >(new EdgeFactory <string, Edge <string> >());

            var vertex1 = "1";
            var vertex2 = "2";
            var vertex3 = "3";
            var vertex4 = "4";

            // add the vertices
            g.AddVertex(vertex1);
            g.AddVertex(vertex2);
            g.AddVertex(vertex3);
            g.AddVertex(vertex4);

            // add edges to create a circuit
            g.AddEdge(vertex1, vertex2);
            g.AddEdge(vertex2, vertex3);
            g.AddEdge(vertex3, vertex4);
            g.AddEdge(vertex4, vertex1);

            return(g);
        }
Example #24
0
        public void TestAdd_Edge_2()
        {
            SimpleGraph graph = new SimpleGraph(5);

            graph.AddVertex(22);
            graph.AddVertex(5);
            graph.AddVertex(7);
            graph.AddVertex(11);
            graph.AddVertex(52);
            graph.AddVertex(27);
            for (int i = 0; i < graph.max_vertex; i++)
            {
                Assert.IsTrue(graph.vertex[i].Value != 27);
            }


            graph.AddEdge(1, 4);
            graph.AddEdge(3, 4);
            graph.AddEdge(0, 1);
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsFalse(graph.IsEdge(1, 3));
            Assert.IsFalse(graph.IsEdge(2, 0));
            Assert.IsFalse(graph.IsEdge(2, 1));
            Assert.IsFalse(graph.IsEdge(2, 3));
            Assert.IsFalse(graph.IsEdge(3, 0));
            Assert.IsFalse(graph.IsEdge(3, 1));
            Assert.IsFalse(graph.IsEdge(3, 2));
            Assert.IsFalse(graph.IsEdge(4, 0));
            Assert.IsFalse(graph.IsEdge(4, 2));

            Assert.IsTrue(graph.IsEdge(4, 1));
            Assert.IsTrue(graph.IsEdge(1, 4));
            Assert.IsTrue(graph.IsEdge(3, 4));
            Assert.IsTrue(graph.IsEdge(4, 3));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(1, 0));
        }
Example #25
0
        public void WeakVertices_in_5v_where_2v_Is_Weak()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(0); // добавляем вершины
            testGraph.AddVertex(10);
            testGraph.AddVertex(30);
            testGraph.AddVertex(50);
            testGraph.AddVertex(70);

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(0, 3);
            testGraph.AddEdge(0, 4);
            testGraph.AddEdge(3, 4);

            List <Vertex <int> > WeakList = testGraph.WeakVertices();

            int expectedCount = 2;
            int actualCount   = WeakList.Count;

            if (WeakList.Count != 0)
            {
                WeakList.ForEach((item) => Console.Write("{0} ", item.Value));
            }
            else
            {
                Console.WriteLine("[]");
            }

            Assert.IsNotNull(WeakList);
            Assert.AreEqual(expectedCount, actualCount);
            Assert.IsTrue(WeakList[0].Value == 10);
            Assert.IsTrue(WeakList[1].Value == 30);
        }
Example #26
0
        public void WeakVertices_in_3v_where_All_Is_Weak()
        {
            int size = 3;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(0); // добавляем вершины
            testGraph.AddVertex(10);
            testGraph.AddVertex(20);

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 2);

            List <Vertex <int> > WeakList = testGraph.WeakVertices();

            int expectedCount = 3;
            int actualCount   = WeakList.Count;

            WeakList.ForEach((item) => Console.Write("{0} ", item.Value));

            Assert.AreEqual(expectedCount, actualCount);
            Assert.IsTrue(WeakList[0].Value == 0);
            Assert.IsTrue(WeakList[1].Value == 10);
            Assert.IsTrue(WeakList[2].Value == 20);
        }
Example #27
0
    // For example, edges like (1, 2), (2, 3) => there's an edge between vertices 0 and 1 and 1 and 2.
    public static SimpleGraph CreateFromOneBasedEdges(int vertexCount, int[,] edges)
    {
        var graph = new SimpleGraph(vertexCount);

        for (int id = 0; id < vertexCount; ++id)
        {
            graph._vertices[id] = new Vertex(graph, id);
        }

        for (int i = 0; i < edges.GetLength(0); ++i)
        {
            graph.AddEdge(edges[i, 0] - 1, edges[i, 1] - 1);
        }

        return graph;
    }
        protected override IGraph <string> CreateGraph()
        {
            var graph = new SimpleGraph <string>();

            graph.AddEdge("6", "4");
            graph.AddEdge("4", "3");
            graph.AddEdge("4", "5");
            graph.AddEdge("5", "2");
            graph.AddEdge("5", "1");
            graph.AddEdge("3", "2");
            graph.AddEdge("1", "2");

            return(graph);
        }
Example #29
0
        public void RemoveVertex_1v_in_5v_Graph()
        {
            int size = 5;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(19);
            testGraph.AddVertex(18);
            testGraph.AddVertex(17);
            testGraph.AddVertex(16);
            testGraph.AddVertex(15);

            testGraph.AddEdge(0, 0); // добавление ребёр между вершинами
            testGraph.AddEdge(0, 1);
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(0, 3);
            testGraph.AddEdge(2, 1);
            testGraph.AddEdge(2, 3);

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine();

            testGraph.RemoveVertex(0); // удаление вершины

            for (int i = 0; i < testGraph.m_adjacency.GetLength(0); i++)
            {
                for (int j = 0; j < testGraph.m_adjacency.GetLength(1); j++)
                {
                    Console.Write(testGraph.m_adjacency[i, j] + " ");
                }
                Console.WriteLine();
            }

            Assert.AreEqual(0, testGraph.m_adjacency[0, 0]); // проверка удаления ребра вершины на саму себя
            Assert.AreEqual(0, testGraph.m_adjacency[0, 1]); // проверка удаления всех рёбер
            Assert.AreEqual(0, testGraph.m_adjacency[1, 0]);
            Assert.AreEqual(0, testGraph.m_adjacency[0, 2]);
            Assert.AreEqual(0, testGraph.m_adjacency[2, 0]);
            Assert.AreEqual(0, testGraph.m_adjacency[0, 3]);
            Assert.AreEqual(0, testGraph.m_adjacency[3, 0]);
            Assert.AreEqual(1, testGraph.m_adjacency[1, 2]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 1]);
            Assert.AreEqual(1, testGraph.m_adjacency[2, 3]);
            Assert.AreEqual(1, testGraph.m_adjacency[3, 2]);

            Assert.IsNull(testGraph.vertex[0]); // вершина удалена
        }
Example #30
0
        public void BreadthFirstSearch_6_Vertex_where_1v_is_Closured()
        {
            int size = 6;
            SimpleGraph <int> testGraph = new SimpleGraph <int>(size);

            testGraph.AddVertex(0); // добавляем вершины
            testGraph.AddVertex(10);
            testGraph.AddVertex(20);
            testGraph.AddVertex(30);
            testGraph.AddVertex(40);
            testGraph.AddVertex(50);

            testGraph.AddEdge(0, 1); // добавление рёбер между вершинами
            testGraph.AddEdge(0, 2);
            testGraph.AddEdge(0, 3);
            testGraph.AddEdge(1, 3);
            testGraph.AddEdge(1, 4);

            testGraph.AddEdge(2, 3);
            testGraph.AddEdge(3, 3);
            testGraph.AddEdge(3, 4);
            testGraph.AddEdge(4, 5);

            List <Vertex <int> > vList = testGraph.BreadthFirstSearch(3, 5); // попытка построения пути из 30 в 50.
            //Console.WriteLine("Путь 3 -> 5: ");
            //vList.ForEach((item) => Console.Write(" {0}", item.Value));

            List <Vertex <int> > vList2 = testGraph.BreadthFirstSearch(2, 5); // попытка построения пути из 20 в 50.

            //Console.WriteLine("Путь 2 -> 5: ");
            //vList2.ForEach((item) => Console.Write(" {0}", item.Value));

            Assert.IsNotNull(vList);
            Assert.IsNotNull(vList2);
            Assert.IsTrue(vList.Count == 3);
            Assert.IsTrue(vList2.Count == 4);
        }
Example #31
0
        private AlgorithmResult RunDijkstra(Func <ObjectId, Weight> weightFunction)
        {
            var graph = new SimpleGraph(_cities.Select(c => c.Id), weightFunction);

            foreach (var road in _roads)
            {
                graph.AddEdge(road.ToCityId, road.FromCityId, road.Id);
            }

            var startTime = DateTime.Now;

            graph.RunDijkstra(_centralCities.Select(c => c.Id));

            var algorithmResult = new AlgorithmResult()
            {
                RunDate = startTime,
                Nodes   = graph.Results
            };

            return(algorithmResult);
        }
Example #32
0
        public void TestAddRemove_Edge_1()
        {
            SimpleGraph graph = new SimpleGraph(5);

            graph.AddVertex(2);
            graph.AddVertex(5);

            graph.AddEdge(0, 1);
            Assert.IsFalse(graph.IsEdge(1, 2));
            Assert.IsFalse(graph.IsEdge(4, 2));
            Assert.IsTrue(graph.IsEdge(0, 1));
            Assert.IsTrue(graph.IsEdge(1, 0));

            graph.RemoveVertex(0);
            Assert.IsTrue(graph.vertex[0] == null);
            for (int i = 0; i < graph.vertex.Length; i++)
            {
                Assert.IsTrue(graph.m_adjacency[i, 0] == 0);
                Assert.IsTrue(graph.m_adjacency[0, i] == 0);
            }
        }