예제 #1
0
        public static BellmanFordResult[] Solve(double[][] grid)
        {
            var rows    = grid.Length;
            var cols    = grid[0].Length;
            var weights = new BellmanFordResult[rows];

            weights[0] = new BellmanFordResult {
                Weight = 0, Paths = new List <int> {
                    0
                }
            };
            for (var i = 1; i < rows; i++)
            {
                weights[i] = new BellmanFordResult {
                    Weight = double.MaxValue
                };
            }

            for (var k = 1; k <= rows - 1; k++)
            {
                for (var i = 0; i < rows; i++)
                {
                    for (var j = 0; j < cols; j++)
                    {
                        if (weights[j].Weight > weights[i].Weight + grid[i][j])
                        {
                            weights[j].Weight = weights[i].Weight + grid[i][j];
                            weights[j].Paths  = new List <int>(weights[i].Paths)
                            {
                                j
                            };
                        }
                    }
                }
            }

            for (var i = 0; i < rows; i++)
            {
                for (var j = 0; j < cols; j++)
                {
                    if (weights[j].Weight > weights[i].Weight + grid[i][j])
                    {
                        weights[j] = new NegativeCycleResult
                        {
                            Weight = weights[i].Weight + grid[i][j],
                            Paths  = new List <int>(weights[i].Paths)
                            {
                                j
                            }
                        };
                    }
                }
            }

            return(weights);
        }
예제 #2
0
        private static bool Relax(int left, int right, long weight, BellmanFordResult r)
        {
            var should = ShouldRelax(left, right, weight, r);

            if (should.Item1)
            {
                r.Distance.SetValue(right, should.Item2);
                r.VisitedFrom.SetValue(right, left);
            }
            return(should.Item1);
        }
예제 #3
0
        private static bool Relax(int left, int right, long weight, BellmanFordResult r)
        {
            var leftDistance    = r.Distance.GetValue(left);
            var relaxedDistance = leftDistance + weight;
            var currentDistance = r.Distance.GetValue(right);

            if (currentDistance <= relaxedDistance)
            {
                return(false);
            }

            r.Distance.SetValue(right, relaxedDistance);
            r.VisitedFrom.SetValue(right, left);
            return(true);
        }
예제 #4
0
        private static BellmanFordResult BellmanFord(int size, List <Edge <long> > edges)
        {
            var result = new BellmanFordResult(size);

            //Console.WriteLine("Initial: {0}",result.Distance);
            //IEnumerable<Edge> workingEdges = edges;
            for (var i = 0; i < size; i++)
            {
                foreach (var e in edges)
                {
                    Relax(e.Left, e.Right, e.Weight, result);
                }
                //workingEdges = workingEdges.Where(e => Relax(e, result) e.Weight == 0);
                //Console.WriteLine("{1}: {0}", result.Distance,i);
            }
            return(result);
        }
예제 #5
0
        private static Tuple <bool, long> ShouldRelax(int left, int right, long weight, BellmanFordResult r)
        {
            var leftDistance    = r.Distance.GetValue(left);
            var relaxedDistance = leftDistance == PositiveInfinity ? leftDistance : leftDistance + weight;
            var currentDistance = r.Distance.GetValue(right);

            return(new Tuple <bool, long>(currentDistance > relaxedDistance, relaxedDistance));
        }
예제 #6
0
 private static bool Relax(Edge e, BellmanFordResult r)
 {
     return(Relax(e.Left, e.Right, e.Weight, r));
 }