当前位置: 首页>>代码示例>>C#>>正文


C# Graph.AddVertex方法代码示例

本文整理汇总了C#中Graph.AddVertex方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.AddVertex方法的具体用法?C# Graph.AddVertex怎么用?C# Graph.AddVertex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


在下文中一共展示了Graph.AddVertex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MultipleCyclesDirected

        public void MultipleCyclesDirected()
        {
            var graph = new Graph<int>(true);
            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);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex3, vertex1);

            graph.AddEdge(vertex4, vertex5);
            graph.AddEdge(vertex5, vertex4);

            var cycles = graph.FindCycles(true);

            Assert.AreEqual(2, cycles.Count, "There are two cycles");

            IList<Vertex<int>> cycle1 = cycles[0];
            IList<Vertex<int>> cycle2 = cycles[1];

            Assert.IsTrue(((cycle1.Count == 3) && (cycle2.Count == 2)) || ((cycle1.Count == 2) && (cycle2.Count == 3)), "Wrong number of items in the cycles");

            var index = (cycle1.Count == 3) ? 0 : 1;
            Assert.IsTrue(cycles[index].Any(v => v.Data == 1), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycles[index].Any(v => v.Data == 2), "Vertex 2 missing from the cycle");
            Assert.IsTrue(cycles[index].Any(v => v.Data == 3), "Vertex 3 missing from the cycle");

            index = (cycle1.Count == 2) ? 0 : 1;
            Assert.IsTrue(cycles[index].Any(v => v.Data == 4), "Vertex 1 missing from the cycle");
            Assert.IsTrue(cycles[index].Any(v => v.Data == 5), "Vertex 2 missing from the cycle");
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:34,代码来源:FindCycles.cs

示例2: Simple

        public void Simple()
        {
            var graph = new Graph<int>(true);
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);
            var vertex3 = graph.AddVertex(3);
            var vertex4 = graph.AddVertex(4);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex2, vertex4);
            graph.AddEdge(vertex3, vertex4);

            Assert.IsFalse(graph.IsCyclic());

            // Add a cycle
            graph.AddEdge(vertex4, vertex1);

            Assert.IsTrue(graph.IsCyclic());

            // Remove the cycle again
            graph.RemoveEdge(vertex4, vertex1);

            Assert.IsFalse(graph.IsCyclic());

            // Add a cycle again
            graph.AddEdge(vertex2, vertex1);

            Assert.IsTrue(graph.IsCyclic());

            // Remove the cycle
            graph.RemoveEdge(vertex2, vertex1);

            Assert.IsFalse(graph.IsCyclic());
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:35,代码来源:IsCyclic.cs

示例3: Simple

        public void Simple()
        {
            var vertex1 = new Vertex<int>(1);
            var vertex2 = new Vertex<int>(2);
            var vertex3 = new Vertex<int>(3);
            var vertex4 = new Vertex<int>(4);

            var graph = new Graph<int>(true);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);
            graph.AddVertex(vertex4);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex2, vertex3);
            graph.AddEdge(vertex3, vertex1);
            graph.AddEdge(vertex4, vertex2);

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

            foreach (var vertex in graph.Vertices)
            {
                vertexList.Add(vertex);
            }

            Assert.IsTrue(vertexList.Contains(vertex1));
            Assert.IsTrue(vertexList.Contains(vertex2));
            Assert.IsTrue(vertexList.Contains(vertex3));
            Assert.IsTrue(vertexList.Contains(vertex4));
        }
开发者ID:havok,项目名称:ngenerics,代码行数:31,代码来源:Vertices.cs

示例4: Undirected

        public void Undirected()
        {
            var graph = new Graph<int>(false);
            var vertex1 = new Vertex<int>(1);
            var vertex2 = new Vertex<int>(2);
            var vertex3 = new Vertex<int>(3);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);

            graph.AddEdge(vertex1, vertex2);

            Assert.AreEqual(vertex1.IncomingEdgeCount, 0);
            Assert.AreEqual(vertex2.IncomingEdgeCount, 0);

            graph.AddEdge(vertex3, vertex2);

            Assert.AreEqual(vertex3.IncomingEdgeCount, 0);
            Assert.AreEqual(vertex2.IncomingEdgeCount, 0);

            graph.AddEdge(vertex1, vertex3);

            Assert.AreEqual(vertex1.IncomingEdgeCount, 0);
            Assert.AreEqual(vertex3.IncomingEdgeCount, 0);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:26,代码来源:IncomingEdgeCount.cs

示例5: Main

    public static void Main(string[] args)
    {
        Graph edgeColorTestGraph = new Graph();
        Vertex v1 = new Vertex();
        v1.name = "v1";
        Vertex v2 = new Vertex();
        v2.name = "v2";
        Vertex v3 = new Vertex();
        v3.name = "v3";
        Vertex v4 = new Vertex();
        v4.name = "v4";
        Vertex v5 = new Vertex();
        v5.name = "v5";

        edgeColorTestGraph.AddVertex(v1);
        edgeColorTestGraph.AddVertex(v2);
        edgeColorTestGraph.AddVertex(v3);
        edgeColorTestGraph.AddVertex(v4);
        edgeColorTestGraph.AddVertex(v5);

        edgeColorTestGraph.AddEdge(v1, v2);
        edgeColorTestGraph.AddEdge(v1, v3);
        edgeColorTestGraph.AddEdge(v1, v4);
        edgeColorTestGraph.AddEdge(v2, v5);
        edgeColorTestGraph.AddEdge(v3, v4);
        edgeColorTestGraph.AddEdge(v5, v4);

        edgeColorTestGraph.display();
        edgeColorTestGraph.edgeColoring();
        edgeColorTestGraph.display();
    }
开发者ID:RoDaniel,项目名称:featurehouse,代码行数:31,代码来源:EdgeColoringTest.cs

示例6: VertexAndEdge

        public void VertexAndEdge()
        {
            var graph = new Graph<int>(true);

            var vertex1 = new Vertex<int>(3);

            var vertex2 = new Vertex<int>(4);

            var vertex3 = new Vertex<int>(5);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);

            graph.AddEdge(vertex2, vertex1);

            vertex2 = SerializeUtil.BinarySerializeDeserialize(vertex1);

            TestIsCopy(vertex1, vertex2);

            Assert.AreEqual(vertex2.IncidentEdges.Count, 1);
            Assert.AreEqual(vertex1.IncidentEdges.Count, 1);

            Assert.AreEqual(vertex2.EmanatingEdges.Count, 0);
            Assert.AreEqual(vertex1.EmanatingEdges.Count, 0);

            TestIsCopy(vertex2.IncidentEdges[0], vertex1.IncidentEdges[0]);
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:28,代码来源:Serializable.cs

示例7: AddEdgeFromVerticesExample

        public void AddEdgeFromVerticesExample()
        {
            // Initialize a new graph instance
            var graph = new Graph<int>(true);

            // Add two vertices to the graph
            var vertex1 = graph.AddVertex(1);
            var vertex2 = graph.AddVertex(2);

            // Add the edge to the graph
            var edge = graph.AddEdge(vertex1, vertex2);

            // The from vertex will be vertex1
            Assert.AreEqual(edge.FromVertex, vertex1);

            // The to vertex will be vertex2
            Assert.AreEqual(edge.ToVertex, vertex2);

            // Since the graph is directed, the edge will
            // be directed as well
            Assert.AreEqual(edge.IsDirected, true);

            // The edge will be accessible though the edges collection
            Assert.AreEqual(graph.Edges.Count, 1);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:25,代码来源:GraphExamples.cs

示例8: Directed

        public void Directed()
        {
            var vertex1 = new Vertex<int>(3);
            var vertex2 = new Vertex<int>(5);
            var vertex3 = new Vertex<int>(8);

            var graph = new Graph<int>(true);
            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex3, vertex1);

            var edgeList = vertex3.EmanatingEdges;

            Assert.AreEqual(edgeList.Count, 1);
            AssertContainsEdges(edgeList, true,
                                vertex3.GetEmanatingEdgeTo(vertex1)
                );

            edgeList = vertex1.EmanatingEdges;

            Assert.AreEqual(edgeList.Count, 1);
            AssertContainsEdges(edgeList, true,
                                vertex1.GetEmanatingEdgeTo(vertex2)
                );
        }
开发者ID:havok,项目名称:ngenerics,代码行数:28,代码来源:EmanatingEdges.cs

示例9: Undirected

        public void Undirected()
        {
            var graph = new Graph<int>(false);
            var vertex1 = new Vertex<int>(1);
            var vertex2 = new Vertex<int>(2);
            var vertex3 = new Vertex<int>(3);
            var vertex4 = new Vertex<int>(4);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);
            graph.AddVertex(vertex4);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex3, vertex2);
            graph.AddEdge(vertex1, vertex3);

            Assert.IsFalse(graph.IsStronglyConnected());

            graph.AddEdge(vertex2, vertex4);

            Assert.IsTrue(graph.IsStronglyConnected());

            graph.RemoveEdge(vertex2, vertex3);

            Assert.IsTrue(graph.IsStronglyConnected());

            graph.RemoveEdge(vertex1, vertex3);

            Assert.IsFalse(graph.IsStronglyConnected());
        }
开发者ID:GTuritto,项目名称:ngenerics,代码行数:31,代码来源:IsStronglyConnected.cs

示例10: TestAddRemove1

        public void TestAddRemove1()
        {
            using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000, Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
            {
                uint tagsId = 10;
                var vertex1 = graph.AddVertex(51, 1);
                var vertex2 = graph.AddVertex(51, 2);

                graph.AddEdge(vertex1, vertex2, new Edge()
                {
                    Forward = true,
                    Tags = tagsId
                }, null);

                // test forward edge.
                var edges =  graph.GetEdges(vertex1).ToKeyValuePairs();
                Assert.AreEqual(1, edges.Length);
                Assert.AreEqual(tagsId, edges[0].Value.Tags);
                Assert.AreEqual(vertex2, edges[0].Key);
                Assert.AreEqual(true, edges[0].Value.Forward);

                // remove edge again.
                graph.RemoveEdge(vertex1, vertex2);

                // check if the edge is gone.
                edges = graph.GetEdges(vertex1).ToKeyValuePairs();
                Assert.AreEqual(0, edges.Length);
            }
        }
开发者ID:UnifyKit,项目名称:OsmSharp,代码行数:29,代码来源:MemoryMappedGraphTests.cs

示例11: DirectedValue

        public void DirectedValue()
        {
            var graph = new Graph<int>(true);
            var vertex1 = new Vertex<int>(1);
            var vertex2 = new Vertex<int>(2);
            var vertex3 = new Vertex<int>(3);

            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);
            graph.AddVertex(vertex3);

            graph.AddEdge(vertex1, vertex2);
            graph.AddEdge(vertex3, vertex2);
            graph.AddEdge(vertex1, vertex3);

            Assert.AreEqual(graph.Edges.Count, 3);
            Assert.AreEqual(graph.Vertices.Count, 3);

            Assert.IsTrue(graph.RemoveVertex(1));

            Assert.AreEqual(graph.Edges.Count, 1);
            Assert.AreEqual(graph.Vertices.Count, 2);

            Assert.IsFalse(graph.RemoveVertex(4));
            Assert.AreEqual(graph.Edges.Count, 1);
            Assert.AreEqual(graph.Vertices.Count, 2);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:27,代码来源:RemoveVertex.cs

示例12: DegreeExample

        public void DegreeExample()
        {
            // Create two new vertices
            var vertex1 = new Vertex<int>(2);
            var vertex2 = new Vertex<int>(4);

            // The degree of the vertex is the number
            // of edges emanating from the vertex.  Thus,
            // a newly created vertex will have a degree
            // of 0.
            Assert.AreEqual(vertex1.Degree, 0);
            Assert.AreEqual(vertex2.Degree, 0);

            // Create a graph, and add the vertices to
            // the graph
            var graph = new Graph<int>(true);
            graph.AddVertex(vertex1);
            graph.AddVertex(vertex2);

            // Add an edge from vertex1 to vertex vertex2
            graph.AddEdge(vertex1, vertex2);

            // Since the edge is emanating from vertex1
            // (and the graph is directed), vertex1's
            // degree will be 1 and vertex2's degree 0.
            Assert.AreEqual(vertex1.Degree, 1);
            Assert.AreEqual(vertex2.Degree, 0);
        }
开发者ID:havok,项目名称:ngenerics,代码行数:28,代码来源:VertexExamples.cs

示例13: TestGraphAddEdge

        public void TestGraphAddEdge()
        {
            var graph = new Graph<Edge>();
            var vertex1 = graph.AddVertex(51, 1);
            var vertex2 = graph.AddVertex(51, 2);

            graph.AddEdge(vertex1, vertex2, new Edge()
                                               {
                                                   Forward = true,
                                                   Tags = 0
                                               }, null);

            var edges =  graph.GetEdges(vertex1).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(0, edges[0].Value.Tags);
            Assert.AreEqual(vertex2, edges[0].Key);

            edges = graph.GetEdges(vertex2).ToKeyValuePairs();
            Assert.AreEqual(1, edges.Length);
            Assert.AreEqual(0, edges[0].Value.Tags);
            Assert.AreEqual(vertex1, edges[0].Key);

            Edge edge;
            Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
            Assert.AreEqual(0, edge.Tags);
            Assert.AreEqual(true, edge.Forward);
            Assert.IsTrue(graph.GetEdge(vertex2, vertex1, out edge));
            Assert.AreEqual(0, edge.Tags);
            Assert.AreEqual(false, edge.Forward);
        }
开发者ID:UnifyKit,项目名称:OsmSharp,代码行数:30,代码来源:GraphTests.cs

示例14: Main

        static void Main(string[] args)
        {
            Graph UG = new Graph(true);
            Vertex a = new Vertex("a");
            Vertex b = new Vertex("b");
            Vertex c = new Vertex("c");
            Vertex d = new Vertex("d");
            Vertex e = new Vertex("e");

            Vertex f = new Vertex("f");

            // Add vertexes
            UG.AddVertex(a);
            UG.AddVertex(b);
            UG.AddVertex(c);
            UG.AddVertex(d);
            UG.AddVertex(e);

            UG.AddEdge(a, b, 3);
            UG.AddEdge(a, c, 3);
            UG.AddEdge(b, d, 3);
            UG.AddEdge(d, e, 3);

            UG.BreadthFirstSearch(d);

            Console.ReadLine();
        }
开发者ID:bharathkumarms,项目名称:graphDS,代码行数:27,代码来源:Program.cs

示例15: TestSparseRemoval1

        public void TestSparseRemoval1()
        {
            // use one edge definition everywhere.
            var edge = new Edge();
            edge.Forward = true;
            edge.Tags = 1;

            var graph = new Graph<Edge>();
            uint vertex1 = graph.AddVertex(0, 0);
            uint vertex2 = graph.AddVertex(1, 1);
            uint vertex3 = graph.AddVertex(2, 2);

            graph.AddEdge(vertex1, vertex2, edge, null);
            graph.AddEdge(vertex2, vertex1, edge, null);
            graph.AddEdge(vertex2, vertex3, edge, null);
            graph.AddEdge(vertex3, vertex2, edge, null);

            // execute pre-processor.
            var preProcessor = new GraphSimplificationPreprocessor(graph);
            preProcessor.Start();

            // test resulting graph.
            Assert.AreEqual(2, graph.VertexCount);
            Assert.AreEqual(1, graph.GetEdges(1).ToKeyValuePairs().Length);
            Assert.AreEqual(2, graph.GetEdges(1).ToKeyValuePairs()[0].Key);
            Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs().Length);
            Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs()[0].Key);
        }
开发者ID:cmberryau,项目名称:routing,代码行数:28,代码来源:PreProcessorTests.cs


注:本文中的Graph.AddVertex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。