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


C# GraphNode.AddNeighbour方法代码示例

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


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

示例1: ShouldFindShortesPathWhen1PathExists

        public void ShouldFindShortesPathWhen1PathExists()
        {
            //http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
            var one = new GraphNode<int>(1);
            var two = new GraphNode<int>(2);
            var three = new GraphNode<int>(3);
            var four = new GraphNode<int>(4);
            var five = new GraphNode<int>(5);
            var six = new GraphNode<int>(6);

            one.AddNeighbour(six, 14);
            one.AddNeighbour(three, 9);
            one.AddNeighbour(two, 7);

            two.AddNeighbour(three, 10);
            two.AddNeighbour(four, 15);

            three.AddNeighbour(six, 2);
            three.AddNeighbour(four, 11);

            four.AddNeighbour(five, 6);

            five.AddNeighbour(six, 9);

            var graph = new List<GraphNode<int>> {one, two, three, four, five, six};

            var dijkstra = new Dijkstra<int>(graph);
            var path = dijkstra.FindShortestPathBetween(one, five);

            path[0].Value.ShouldBe(1);
            path[1].Value.ShouldBe(3);
            path[2].Value.ShouldBe(6);
            path[3].Value.ShouldBe(5);
        }
开发者ID:valantonini,项目名称:GraphCollection,代码行数:34,代码来源:TestDijkstra.cs

示例2: ShouldFindShortesPathWhen2PathExists

        public void ShouldFindShortesPathWhen2PathExists()
        {
            //2 paths exist
            //O A B D T
            //O A B E D T

            // ReSharper disable InconsistentNaming
            var O = new GraphNode<string>("O");
            var A = new GraphNode<string>("A");
            var B = new GraphNode<string>("B");
            var C = new GraphNode<string>("C");
            var D = new GraphNode<string>("D");
            var E = new GraphNode<string>("E");
            var F = new GraphNode<string>("F");
            var T = new GraphNode<string>("T");
            // ReSharper restore InconsistentNaming

            O.AddNeighbour(A, 2);
            O.AddNeighbour(B, 5);
            O.AddNeighbour(C, 4);

            A.AddNeighbour(F, 12);
            A.AddNeighbour(D, 7);
            A.AddNeighbour(B, 2);

            B.AddNeighbour(D, 4);
            B.AddNeighbour(E, 3);
            B.AddNeighbour(C, 1);

            C.AddNeighbour(E, 4);

            D.AddNeighbour(T, 5);
            D.AddNeighbour(E, 1);

            E.AddNeighbour(T, 7);

            F.AddNeighbour(T, 3);

            var graph = new List<GraphNode<string>> { O, A, B, C, D, E, F, T };

            var dijkstra = new Dijkstra<string>(graph);
            var path = dijkstra.FindShortestPathBetween(O, T);

            //The other alternate path
            //path[0].Value.ShouldBe("O");
            //path[1].Value.ShouldBe("A");
            //path[2].Value.ShouldBe("B");
            //path[3].Value.ShouldBe("E");
            //path[4].Value.ShouldBe("D");
            //path[5].Value.ShouldBe("T");

            path[0].Value.ShouldBe("O");
            path[1].Value.ShouldBe("A");
            path[2].Value.ShouldBe("B");
            path[3].Value.ShouldBe("D");
            path[4].Value.ShouldBe("T");
        }
开发者ID:valantonini,项目名称:GraphCollection,代码行数:57,代码来源:TestDijkstra.cs

示例3: ShouldAddNodes

        public void ShouldAddNodes()
        {
            var node1 = new GraphNode<string>("one");
            var node2 = new GraphNode<string>("two");
            var node3 = new GraphNode<string>("three");

            node1.AddNeighbour(node2, 2);
            node2.AddNeighbour(node3, 4);

            node1.IsNeighbourOf(node2).ShouldBe(true);
            node2.IsNeighbourOf(node1).ShouldBe(true);
            node2.IsNeighbourOf(node3).ShouldBe(true);
            node1.IsNeighbourOf(node3).ShouldBe(false);
            node3.IsNeighbourOf(node1).ShouldBe(false);
        }
开发者ID:valantonini,项目名称:GraphCollection,代码行数:15,代码来源:TestGraphNode.cs

示例4: ShouldKnowWhenNoPathEsists

        public void ShouldKnowWhenNoPathEsists()
        {
            var one = new GraphNode<int>(1);
            var two = new GraphNode<int>(2);
            var three = new GraphNode<int>(3);
            var four = new GraphNode<int>(4);
            var five = new GraphNode<int>(5);
            var six = new GraphNode<int>(6);

            var seven = new GraphNode<int>(7);

            one.AddNeighbour(six, 14);
            one.AddNeighbour(three, 9);
            one.AddNeighbour(two, 7);

            two.AddNeighbour(three, 10);
            two.AddNeighbour(four, 15);

            three.AddNeighbour(six, 2);
            three.AddNeighbour(four, 11);

            four.AddNeighbour(five, 6);

            five.AddNeighbour(six, 9);

            var graph = new List<GraphNode<int>> { one, two, three, four, five, six, seven };

            var dijkstra = new Dijkstra<int>(graph);
            var path = dijkstra.FindShortestPathBetween(one, seven);

            path.Count.ShouldBe(0);
        }
开发者ID:valantonini,项目名称:GraphCollection,代码行数:32,代码来源:TestDijkstra.cs


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