private void MarkedWay(SortedDictionary <TVertex, TVertex> S, TVertex source, TVertex stock) { var way = new List <TVertex>(); var cost = stock.d; while (stock != null) { way.Add(stock); stock = (TVertex)stock.p; } if (way.Count < 2) { return; } way.Reverse(); StringBuilder sb = new StringBuilder(); way.ForEach(v => sb.Append($"{v} -> ")); _args.OutWays.Add(sb.ToString().TrimEnd(' ', '>', '-') + $" = {cost}"); for (int i = 0; i < way.Count - 1; i++) { Field.SetColorAndWidth(way[i].GetRepresentation(), RGBcolor.Green, 2); Thread.Sleep(500); if (!Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(way[i].GetRepresentation(), way[i + 1].GetRepresentation()), RGBcolor.Green, 2) && !Field.IsOrgraph) { Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(way[i + 1].GetRepresentation(), way[i].GetRepresentation()), RGBcolor.Green, 2); } Thread.Sleep(500); Field.SetColorAndWidth(way[i + 1].GetRepresentation(), RGBcolor.Green, 2); } }
private void MarkDefaultEdge(TVertex src, TVertex stc) { if (!Field.SetModelDefaultOptions(ServiceFunctions.EdgeRepresentation(src.GetRepresentation(), stc.GetRepresentation()))) { Field.SetModelDefaultOptions(ServiceFunctions.EdgeRepresentation(stc.GetRepresentation(), src.GetRepresentation())); } }
private void MarkEdge(TVertex src, TVertex stc, RGBcolor color, int width) { if (!Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(src.GetRepresentation(), stc.GetRepresentation()), color, width)) { Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(stc.GetRepresentation(), src.GetRepresentation()), color, width); } }
private void FindPath(Graph <TVertex> G, TVertex source, TVertex stock, SortedDictionary <TVertex, TVertex> parents) { if (stock.Equals(source)) { Field.SetColor(source.GetRepresentation(), RGBcolor.DarkGreen); Thread.Sleep(500); } else if (parents[stock] == null) { //Field.UnmarkGraphModels(); Field.UserInterface.PostMessage($"Путь из {source} в {stock} не существует"); return; } else { FindPath(G, source, parents[stock], parents); Thread.Sleep(500); var tmp = parents[stock]; if (!Field.SetColor(ServiceFunctions.EdgeRepresentation(tmp?.ToString(), stock.ToString()), RGBcolor.DarkGreen) && !G.IsOrgraph) { Field.SetColor(ServiceFunctions.EdgeRepresentation(tmp?.ToString(), stock.ToString()), RGBcolor.DarkGreen); } Thread.Sleep(500); Field.SetColor(stock.GetRepresentation(), RGBcolor.DarkGreen); } }
private int BFS(TVertex s, TVertex t, out SortedDictionary <TVertex, TVertex> p) { int sleep = 400; var color = RGBcolor.DarkGreen; int incrFlow = int.MaxValue; Field.RefreshDefault(false); p = new SortedDictionary <TVertex, TVertex>(); foreach (var v in G.AdjList.Keys) { p[v] = null; } var Q = new Queue <TVertex>(); Q.Enqueue(s); while (Q.Count != 0) { var v = Q.Dequeue(); foreach (var uw in G[v]) { var u = uw.Vertex; if (c[v][u] > 0 && p[u] == null) { Q.Enqueue(u); p[u] = v; if (u == t) { List <TVertex> vs = new List <TVertex>(); var last = t; while (last != null) { vs.Add(last); last = p[last]; } vs.Reverse(); for (int i = 0; i < vs.Count - 1; i++) { if (c[vs[i]][vs[i + 1]] < incrFlow) { incrFlow = c[vs[i]][vs[i + 1]]; } } for (int j = 0; j < vs.Count - 1; j++) { Field.SetColorAndWidth(vs[j].GetRepresentation(), color, 2); Thread.Sleep(sleep); Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(vs[j].GetRepresentation(), vs[j + 1].GetRepresentation()), color, 2); Thread.Sleep(sleep); } Field.SetColorAndWidth(vs[vs.Count - 1].GetRepresentation(), color, 2); Thread.Sleep(sleep); return(incrFlow); } } } } return(0); }
public AEdgeModel(AVertexModel source, AVertexModel stock, string weight) : base(ServiceFunctions.EdgeRepresentation(source.VertexStr, stock.VertexStr)) { StringRepresent = weight; Source = source; Stock = stock; Weight = weight; Weighted = weight != null; }
private void DFS(Graph <TVertex> G, TVertex v, SortedDictionary <TVertex, bool> visited) { Visit(v); visited[v] = true; foreach (var adj in G[v]) { if (!visited[adj.Vertex]) { Thread.Sleep(500); if (!Field.MarkGraphModel(ServiceFunctions.EdgeRepresentation(v.ToString(), adj.Vertex.ToString())) && !G.IsOrgraph) { Field.MarkGraphModel(ServiceFunctions.EdgeRepresentation(adj.Vertex.ToString(), v.ToString())); } DFS(G, adj.Vertex, visited); } } Field.UserInterface.PostStatusMessage("Выполниение обхода в глубину закончен!"); }
private void MarkedWay(List <TVertex> way, int cost) { StringBuilder sb = new StringBuilder(); way.ForEach(v => sb.Append($"{v} -> ")); _args.OutWays.Add(sb.ToString().TrimEnd(' ', '>', '-') + $" = {cost}"); for (int i = 0; i < way.Count - 1; i++) { Field.SetColorAndWidth(way[i].GetRepresentation(), RGBcolor.Green, 2); Thread.Sleep(500); if (!Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(way[i].GetRepresentation(), way[i + 1].GetRepresentation()), RGBcolor.Green, 2) && !Field.IsOrgraph) { Field.SetColorAndWidth(ServiceFunctions.EdgeRepresentation(way[i + 1].GetRepresentation(), way[i].GetRepresentation()), RGBcolor.Green, 2); } Thread.Sleep(500); Field.SetColorAndWidth(way[i + 1].GetRepresentation(), RGBcolor.Green, 2); } }
private int EdmondsKarp(TVertex s, TVertex t) { int sleep = 500; int maxFlow = 0; int incrFlow; Field.RefreshDefault(); while ((incrFlow = BFS(s, t, out var p)) != 0) { var sink = t; while (sink != s) { c[p[sink]][sink] -= incrFlow; c[sink][p[sink]] += incrFlow; Thread.Sleep(sleep); Field.SetWeightMark(ServiceFunctions.EdgeRepresentation(p[sink].GetRepresentation(), sink.GetRepresentation()), $"{c[sink][p[sink]]}/", true); Thread.Sleep(sleep); sink = p[sink]; } maxFlow += incrFlow; Field.RefreshDefault(false); } return(maxFlow); }
public void Execute() { var G = ((ModelsField <TVertex>)Field).Graph; SortedDictionary <TVertex, bool> visited = new SortedDictionary <TVertex, bool>(); G.AdjList.Keys.ToList().ForEach(el => visited[el] = false); List <List <TVertex> > components = new List <List <TVertex> >(); if (!G.IsOrgraph) { int i = 0; G.AdjList.Keys.ToList().ForEach(k => { if (!visited[k]) { components.Add(new List <TVertex>()); var cmp = components.Last(); if (i >= RGBcolor.Collection.Count) { i = 0; } var color = RGBcolor.Collection[i++]; BFS(G, k, visited, cmp, color); cmp.ForEach(el => { G[el].ForEach(edge => { if (cmp.Exists(e => e.Equals(edge.Vertex))) { string key1 = ServiceFunctions.EdgeRepresentation(el.GetRepresentation(), edge.Vertex.GetRepresentation()); Field.SetColor(key1, color, false); } }); }); Field.Refresh(); } }); if (components.Count == 1) { _args.ConnectedTypeOut = "Связынй граф"; } else { _args.ConnectedTypeOut = $"Граф содержит {components.Count} копанента(ов) связности"; } _args.AlgorithmNameOut = "Обход в ширину"; } else { Stack <TVertex> oredrs = new Stack <TVertex>(); var Gt = G.Transpose(); G.AdjList.Keys.ToList().ForEach(k => { if (!visited[k]) { DFS_G(G, k, visited, oredrs); } }); G.AdjList.Keys.ToList().ForEach(el => visited[el] = false); int i = 0; while (oredrs.Count != 0) { var vert = oredrs.Pop(); if (!visited[vert]) { components.Add(new List <TVertex>()); var cmp = components.Last(); if (i >= RGBcolor.Collection.Count) { i = 0; } var color = RGBcolor.Collection[i++]; DFS_Gt(Gt, vert, visited, cmp, color); cmp.ForEach(el => // оптимизировать { Gt[el].ForEach(edge => { if (cmp.Exists(e => e.Equals(edge.Vertex))) { string key = ServiceFunctions.EdgeRepresentation(edge.Vertex.GetRepresentation(), el.GetRepresentation()); Field.SetColor(key, color, false); } }); }); Field.Refresh(); } } components.ForEach(conn => { if (i >= RGBcolor.Collection.Count) { i = 0; } var color = RGBcolor.Collection[i++]; }); _args.AlgorithmNameOut = "Обход в глубину с использованием транспонированного Орграфа"; if (components.Count == 1) { _args.ConnectedTypeOut = "Сильно-связный Орграф"; } else { _args.ConnectedTypeOut = $"Орграф содержит {components.Count} сильно-связных компонента(ов)"; } } _args.ComponentsOut = new List <List <string> >(); components.ForEach(conn => { _args.ComponentsOut.Add(new List <string>()); var current = _args.ComponentsOut.Last(); conn.ForEach(vertex => { current.Add(vertex.GetRepresentation()); }); }); }