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; }
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); }
public void Execute() { TVertex u = new TVertex(); u.SetFromString(_args.Vertex.VertexStr); u = G.AdjList.Keys.ToList().Where(v => v.Equals(u)).First(); SortedDictionary <TVertex, int> nPeiphery = new SortedDictionary <TVertex, int>(); G.AdjList.Keys.ToList().ForEach(v => { if (!v.Equals(u)) { BellmanFord(v); if (u.d < GGF <TVertex> .INF && !(u.d > v.d + GGF <TVertex> .W(G, v, u)) && u.d > _args.N) { nPeiphery[v] = u.d; } } }); nPeiphery.ToList().ForEach(v => { _args.NPeripheryOut.Add($"{v.Key.GetRepresentation()} = {v.Value}"); Field.SetColorAndWidth(v.Key.GetRepresentation(), RGBcolor.DarkGreen, 2); Thread.Sleep(500); }); _args.AlgorithmNameOut = "Форд-Беллман"; _args.TaskNameOut = $"{_args.N}-периферия вершины {u}"; _args.SuccsessOut = true; }