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


C# Graph.GetShortestPath方法代码示例

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


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

示例1: TestRedundandGraphWorks

        public void TestRedundandGraphWorks()
        {
            // mirror relation:
            nodeList[1].adjacentNodes.Add(1);
            nodeList[3].adjacentNodes.Add(1);

            Graph g = new Graph(nodeList);
            var result = g.GetShortestPath(nodeList[0], nodeList[3]);

            Assert.AreEqual(new int[2] { 1, 4 }, result.Select(n => n.id).ToArray());
        }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:11,代码来源:GraphTest.cs

示例2: Main

    static void Main(string[] args)
    {
        string[] inputs;
        inputs = Console.ReadLine().Split(' ');
        int nodeNumber = int.Parse(inputs[0]); // the total number of nodes in the level, including the gateways
        int linksNumber = int.Parse(inputs[1]); // the number of links
        int gatewayNumbers = int.Parse(inputs[2]); // the number of exit gateways

        var graph = new Graph(nodeNumber);
        for (int i = 0; i < linksNumber; i++)
        {
            inputs = Console.ReadLine().Split(' ');
            int nodeNumber1 = int.Parse(inputs[0]); // N1 and N2 defines a link between these nodes
            int nodeNumber2 = int.Parse(inputs[1]);

            graph.AddLink(nodeNumber1, nodeNumber2);
            Console.Error.Write(nodeNumber1 + "  " + nodeNumber2 + " | ");
        }

        for (int i = 0; i < gatewayNumbers; i++)
        {
            int eI = int.Parse(Console.ReadLine()); // the index of a gateway node
            graph.gatewayNumbers.Add(eI);
            Console.Error.Write(eI + " | ");
        }
        Console.Error.WriteLine("node numbers :{0} | Gateway number :{1}", graph.nodes.Count, graph.gatewayNumbers.Count);
        var path = new int[] {0, 1};
        // game loop
        while (true)
        {
            int sI = int.Parse(Console.ReadLine()); // The index of the node on which the Skynet agent is positioned this turn

            var minPathLengthArray = graph.GetNodePathLengthArray(sI);
            var nearestGatewayNumber = -1;

            foreach (var gatewayNumber in graph.gatewayNumbers)
            {
                var exitNodeNumber = graph.GetNodeIndex(gatewayNumber);

                Console.Error.WriteLine("miPathLEngthArray : {0}", minPathLengthArray[exitNodeNumber]);
                if (minPathLengthArray[exitNodeNumber] == -1)
                    continue;

                if (nearestGatewayNumber == -1)
                {
                    nearestGatewayNumber = gatewayNumber;
                }
                else
                {
                    var minExitNodeIndex = graph.GetNodeIndex(nearestGatewayNumber);
                    if (minPathLengthArray[exitNodeNumber] < minPathLengthArray[minExitNodeIndex])
                        nearestGatewayNumber = gatewayNumber;
                }
            }
            Console.Error.WriteLine("agent Node: {0} | nearestGateway number: {1} | minPathLength : {2}", sI, nearestGatewayNumber, minPathLengthArray[graph.GetNodeIndex(nearestGatewayNumber)] + 1);
            path = graph.GetShortestPath(sI, nearestGatewayNumber, minPathLengthArray[graph.GetNodeIndex(nearestGatewayNumber)] + 1);
            Console.Error.WriteLine("Number of nodes in path:{0} | agentIndex: {1} | path[0]: {2}", path.Count(), sI, path[0]);
            graph.RemoveLink(path[0], path[1]);

            Console.WriteLine("{0} {1}", path[0], path[1]); // Example: 0 1 are the indices of the nodes you wish to sever the link between
        }
    }
开发者ID:yann510,项目名称:CodinGame,代码行数:62,代码来源:SkynetTheVirus.cs

示例3: TestUndirectedGraph

 public void TestUndirectedGraph()
 {
     Graph g = new Graph(nodeList);
     var result1 = g.GetShortestPath(nodeList[0], nodeList[4]);
     var result2 = g.GetShortestPath(nodeList[0], nodeList[3]);
     Assert.IsNotNull(result1);
     Assert.IsNotNull(result2);
     Assert.AreEqual(new int[3] { 1, 3, 5 }, result1.Select(n => n.id).ToArray());
     Assert.AreEqual(new int[2] { 1, 4 }, result2.Select(n => n.id).ToArray());
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:10,代码来源:GraphTest.cs

示例4: TestUndirectedCyclicalGraph

 public void TestUndirectedCyclicalGraph()
 {
     nodeList[4].adjacentNodes.Add(3);
     Graph g = new Graph(nodeList);
     var result = g.GetShortestPath(nodeList[0], nodeList[4]);
     Assert.IsNotNull(result);
     Assert.AreEqual(new int[3] { 1, 3, 5 }, result.Select(n => n.id).ToArray());
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:8,代码来源:GraphTest.cs

示例5: TestEmptyGraph

 public void TestEmptyGraph()
 {
     Graph g = new Graph(new List<node>());
     var result = g.GetShortestPath(new node(), new node());
     Assert.IsEmpty(result);
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:6,代码来源:GraphTest.cs

示例6: TestUndirectedGraphWithIsolatedNode

 public void TestUndirectedGraphWithIsolatedNode()
 {
     nodeList.Add(new node()
     {
         id = 6,
         label = "uber",
         adjacentNodes = new node.adjacentNodesType() { 6 }
     });
     Graph g = new Graph(nodeList);
     var result = g.GetShortestPath(nodeList[5], nodeList[4]);
     var resultSelf = g.GetShortestPath(nodeList[5], nodeList[5]);
     Assert.IsEmpty(result);
     Assert.IsNotNull(resultSelf);
     Assert.AreEqual(new int[1] { 6 }, resultSelf.Select(n => n.id).ToArray());
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:15,代码来源:GraphTest.cs

示例7: TestUndirectedGraphTwoShortestPaths

 public void TestUndirectedGraphTwoShortestPaths()
 {
     nodeList[0].adjacentNodes.Remove(4);
     nodeList[2].adjacentNodes.Add(4);
     nodeList[1].adjacentNodes.Add(4);
     Graph g = new Graph(nodeList);
     var result = g.GetShortestPath(nodeList[0], nodeList[3]);
     Assert.IsNotNull(result);
     Assert.AreEqual(new int[3] { 1, 2, 4 }, result.Select(n => n.id).ToArray());
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:10,代码来源:GraphTest.cs

示例8: TestUndirectedGraphSelfPath

 public void TestUndirectedGraphSelfPath()
 {
     Graph g = new Graph(nodeList);
     var result = g.GetShortestPath(nodeList[4], nodeList[4]);
     Assert.IsEmpty(result);
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:6,代码来源:GraphTest.cs

示例9: TestUndirectedGraphInvalidNodes

 public void TestUndirectedGraphInvalidNodes()
 {
     Graph g = new Graph(nodeList);
     var result = g.GetShortestPath(nodeList[4], new node() { id = 100 } );
     Assert.IsEmpty(result);
 }
开发者ID:florindr,项目名称:MassiveHomework,代码行数:6,代码来源:GraphTest.cs


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