private bool BellmanFord(TVertex s)
        {
            GGF <TVertex> .InitializeSingleSource(G, s);

            for (int i = 1; i < G.Count; i++)
            {
                G.AdjList.ToList().ForEach(u =>
                {
                    u.Value.ForEach(v =>
                    {
                        GGF <TVertex> .Relax(G, u.Key, v.Vertex, GGF <TVertex> .W);
                    });
                });
            }
            foreach (var a in G.AdjList)
            {
                var u = a.Key;
                foreach (var b in a.Value)
                {
                    var v = b.Vertex;
                    if (v.d > u.d + GGF <TVertex> .W(G, u, v))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
示例#2
0
        public void Execute()
        {
            TVertex temp = new TVertex();

            temp.SetFromString(_args.StockModel.VertexStr);
            var stock = G.AdjList.Keys.Where(k => k.Equals(temp)).First();

            Field.RefreshDefault();
            G.AdjList.Keys.ToList().ForEach(s =>
            {
                GGF <TVertex> .InitializeSingleSource(G, s);
                var S = new SortedDictionary <TVertex, TVertex>();
                var Q = new List <TVertex>(G.AdjList.Keys.ToList());
                while (Q.Count != 0)
                {
                    var u = GGF <TVertex> .ExtractMin(Q);
                    S.Add(u, u);
                    G[u].ForEach(v => GGF <TVertex> .Relax(G, u, v.Vertex, GGF <TVertex> .W));
                }
                Field.RefreshDefault();
                MarkedWay(S, s, stock);
                Thread.Sleep(800);
            });
            _args.TaskNameOut      = $"Найти все кратчайщие пути до вершины {stock}!";
            _args.AlgorithmNameOut = "Дейкстра!";
            _args.SuccsessOut      = true;
        }
        private void BellmanFord(TVertex s)
        {
            GGF <TVertex> .InitializeSingleSource(G, s);

            for (int i = 1; i < G.Count; i++)
            {
                G.AdjList.ToList().ForEach(u =>
                {
                    u.Value.ForEach(v =>
                    {
                        GGF <TVertex> .Relax(G, u.Key, v.Vertex, GGF <TVertex> .W);
                    });
                });
            }


            //foreach (var a in G.AdjList)
            //{
            //    var u = a.Key;
            //    foreach (var b in a.Value)
            //    {
            //        var v = b.Vertex;
            //        if (v.d > u.d + GGF<TVertex>.W(G, u, v))
            //            return false;
            //    }
            //}
            //return true;
        }