示例#1
0
        private static int[,] RunBellmanFord(Vertex[] vertices)
        {
            int[,] paths = new int[vertices.Length, vertices.Length];

            for (int i = 0; i < vertices.Length; i++)
            {
                ShortestPathTestClass.ResetAllVertices(vertices);
                BellmanFord.Run(vertices, i);
                for (int j = 0; j < vertices.Length; j++)
                {
                    paths[i, j] = vertices[j].Depth;
                }
            }

            return(paths);
        }
        public void GrokkinAlgortihms_Ex_7_1_C()
        {
            // Arrange
            var startNode = new Node("Start");
            var aNode     = new Node("A");
            var bNode     = new Node("B");
            var cNode     = new Node("C");
            var endNode   = new Node("End");

            var graph = new Graph(startNode, endNode);
            var edges = graph.Add(startNode);

            edges.Add(new Edge {
                Node = aNode, Weight = 2
            });
            edges.Add(new Edge {
                Node = bNode, Weight = 2
            });
            edges = graph.Add(aNode);
            edges.Add(new Edge {
                Node = cNode, Weight = 2
            });
            edges.Add(new Edge {
                Node = endNode, Weight = 2
            });
            edges = graph.Add(bNode);
            edges.Add(new Edge {
                Node = aNode, Weight = 2
            });
            edges = graph.Add(cNode);
            edges.Add(new Edge {
                Node = bNode, Weight = -1
            });
            edges.Add(new Edge {
                Node = endNode, Weight = 2
            });
            graph.Add(endNode);

            // Act
            var shortestPath = BellmanFord.Run(graph);

            // Assert
            Assert.Equal(new[] { startNode, aNode, endNode }, shortestPath);
        }
示例#3
0
        static void Main(string[] args)
        {
            var graph       = new Graph(dataSets[CURRENT_DATASET_INDEX]);
            var source      = graph.NodeMap[START_LABEL];
            var destination = graph.NodeMap[END_LABEL];

            switch (CURRENT_TASK)
            {
            case Task.RoyFloyd:
            {
                var solver = new RoyFloyd(graph.Nodes.Count);

                solver.Run(graph);
                solver.PrintMinPath(graph, source, destination);

                break;
            }

            case Task.Dijkstra:
            {
                var solver   = new Dijkstra(graph);
                var distance = solver.Run(source);

                PrintDistance(graph, distance);

                break;
            }

            case Task.BellmanFord:
            {
                var solver   = new BellmanFord(graph);
                var distance = solver.Run(source);

                PrintDistance(graph, distance);

                break;
            }

            default: break;
            }
        }
示例#4
0
            public void Solve()
            {
                //ABC061 D
                int N = NextInt(), M = NextInt();
                var bf = new BellmanFord(N);

                M.REP(i => {
                    int ai = NextInt() - 1, bi = NextInt() - 1, ci = NextInt();
                    bf.Add(ai, bi, -ci);
                });
                bf.Run(0);

                if (bf.HasCycle)
                {
                    "inf".WL();
                }
                else
                {
                    (-bf.Distance[N - 1]).WL();
                }
                return;
            }
 private static bool[] RunBellmanFord(Vertex[] vertices, int source)
 {
     BellmanFord.Run(vertices, source);
     return(vertices.Select(v => v.Depth != int.MaxValue).ToArray());
 }
 private static int[] RunBellmanFord(Vertex[] vertices, int source)
 {
     BellmanFord.Run(vertices, source);
     return(vertices.Select(v => v.Depth).ToArray());
 }