public void BronKerbosh_MIS(List <VertexGraph> R, List <VertexGraph> P, List <VertexGraph> X, Graph graph, List <List <VertexGraph> > MaxIS) { if (!P.Any() && !X.Any()) { MaxIS.Add(R); return; } else { while (P.Any()) { VertexGraph v = P.First(); List <VertexGraph> P1 = new List <VertexGraph>(P); List <VertexGraph> X1 = new List <VertexGraph>(X); List <VertexGraph> R1 = new List <VertexGraph>(R); R1.Add(v); foreach (var u in graph.GetVertexLists(v)) { P1.Remove(u); X1.Remove(u); } P1.Remove(v); BronKerbosh_MIS(R1, P1, X1, graph, MaxIS); P.Remove(v); X.Add(v); } } }
public int EdmondsKarp(VertexGraph I, VertexGraph S, Graph graph, int[,] capacity, out int[,] legalFlows) { int flow = 0; legalFlows = new int[graph.VertexCount, graph.VertexCount]; while (true) { int[] parent; int new_flow = bfs(I, S, out parent, graph, capacity, legalFlows); if (new_flow == 0) { break; } flow += new_flow; int v = S.Number; while (v != I.Number) { int prev = parent[v]; legalFlows[prev, v] += new_flow; legalFlows[v, prev] -= new_flow; v = prev; } } return(flow); }
public List <VertexGraph> Dfs(VertexGraph start, VertexGraph finish, List <VertexGraph> result, Graph graph) { result.Add(start); foreach (var v in graph.GetVertexLists(start)) { if (!result.Contains(v) && !result.Contains(finish)) { Dfs(v, finish, result, graph); } } return(result); }
public List <VertexGraph> GetVertexLists(VertexGraph vertex) { var result = new List <VertexGraph>(); foreach (var edge in Edges) { if (edge.From == vertex) { result.Add(edge.To); } } return(result); }
public void Path(VertexGraph u, VertexGraph v, int[,] next, List <VertexGraph> path) { if (next[u.Number, v.Number] == 0) { return; } path.Add(u); while (u != v) { VertexGraph n1 = new VertexGraph(next[u.Number, v.Number]); u = n1; path.Add(u); } }
public void BronKerbosh(List <VertexGraph> R, List <VertexGraph> P, List <VertexGraph> X, Graph graph, List <List <VertexGraph> > MaxCliques) { if (!P.Any() && !X.Any()) { //if (R.Count >= 3) MaxCliques.Add(R); return; } else { while (P.Any()) { VertexGraph v = P.First(); List <VertexGraph> intersectionP = new List <VertexGraph>(); List <VertexGraph> intersectionX = new List <VertexGraph>(); // Пересечение с P foreach (var u in graph.GetVertexLists(v)) { for (int j = 0; j < P.Count; j++) { VertexGraph w = P[j]; if (u == w) { intersectionP.Add(u); } } // Пересечение с Х for (int k = 0; k < X.Count; k++) { VertexGraph l = X[k]; if (u == l) { intersectionX.Add(u); } } } List <VertexGraph> R_new = new List <VertexGraph>(R); R_new.Add(v); BronKerbosh(R_new, intersectionP, intersectionX, graph, MaxCliques); P.Remove(v); X.Add(v); } } }
public List <VertexGraph> Wave(VertexGraph start, VertexGraph finish, Graph graph) { var list = new List <VertexGraph>(); list.Add(start); for (int i = 0; i < list.Count; i++) { var vertex = list[i]; foreach (var v in graph.GetVertexLists(vertex)) { if (!list.Contains(v) && !list.Contains(finish)) { list.Add(v); } } } return(list); }
public Graph(string InputText) { Vertices = new List <VertexGraph>(); Edges = new List <EdgeGraph>(); if (InputText != String.Empty) { InputText = InputText.Replace(" ", ""); InputText = InputText.Replace("\r", ""); int k = 0; string[] col; string[] text = InputText.Split('\n'); foreach (var row in text) { col = row.Split(','); VertexGraph from = new VertexGraph(k); Vertices.Add(from); for (int i = 0; i < col.Length; i++) { if (col[i] != "0" && col[i] != "") { VertexGraph to = new VertexGraph(i); EdgeGraph edge; if (col[i] != "1") { int weight = int.Parse(col[i]); edge = new EdgeGraph(from, to, weight); } else { edge = new EdgeGraph(from, to); } Edges.Add(edge); } } k++; } } }
int bfs(VertexGraph I, VertexGraph S, out int[] parent, Graph graph, int[,] capacity, int[,] legalFlows) { parent = new int [graph.VertexCount]; for (int i = 0; i < graph.VertexCount; i++) { parent[i] = -1; } parent[I.Number] = -2; int[] m = new int[graph.VertexCount]; m[I.Number] = int.MaxValue; Queue <VertexGraph> q = new Queue <VertexGraph>(); q.Enqueue(I); while (q.Any()) { VertexGraph cur = q.Dequeue(); foreach (var next in graph.GetVertexLists(cur)) { if (capacity[cur.Number, next.Number] - legalFlows[cur.Number, next.Number] > 0 && parent[next.Number] == -1) { parent[next.Number] = cur.Number; m[next.Number] = Math.Min(m[cur.Number], capacity[cur.Number, next.Number] - legalFlows[cur.Number, next.Number]); if (next != S) { q.Enqueue(next); } else { return(m[S.Number]); } } } } return(0); }
public EdgeGraph(VertexGraph from, VertexGraph to, int weight = 1) { From = from; To = to; Weight = weight; }
public void AddEdge(VertexGraph from, VertexGraph to, int weight) { var edge = new EdgeGraph(from, to, weight); Edges.Add(edge); }
public void AddEdge(VertexGraph from, VertexGraph to) { var edge = new EdgeGraph(from, to); Edges.Add(edge); }
public void AddVertex(VertexGraph vertex) { Vertices.Add(vertex); }
private void Run1_Click_1(object sender, EventArgs e) { int index = Tasks.SelectedIndex; Algorithms algorithms = new Algorithms(); switch (index) { case 0: { output.Clear(); List <int> res = new List <int>(); bool answer = algorithms.FindEulerPath(graph, out res); if (answer) { output.AppendText("Нет эйлерова пути!\n"); } else { if (res.First() == res.Last()) { output.AppendText("Граф НЕ полуэйлеровый, а эйлеровый!"); } else { output.AppendText("Граф полуэйлеровый!\nЭйлеров путь:\n"); foreach (var v in res) { output.AppendText(v.ToString() + " "); } } } break; } case 1: { output.Clear(); List <VertexGraph> combsub = new List <VertexGraph>(); List <VertexGraph> not = new List <VertexGraph>(); List <List <VertexGraph> > MaxCliques = new List <List <VertexGraph> >(); List <VertexGraph> candidates = graph.GetVertices(); List <VertexGraph> max = new List <VertexGraph>(); algorithms.BronKerbosh(combsub, candidates, not, graph, MaxCliques); if (MaxCliques.Count > 1) { output.AppendText("Наибольшая клика:\n"); max = MaxCliques[0]; for (int i = 1; i < MaxCliques.Count; i++) { if (MaxCliques[i].Count > max.Count) { max = MaxCliques[i]; } } foreach (var item in max) { output.AppendText(item.ToString() + " "); } } else { output.AppendText("Наибольших клик нет!\n"); } break; } case 2: { output.Clear(); VertexGraph S = new VertexGraph(graph.VertexCount - 1); VertexGraph I = new VertexGraph(0); int [,] capacity = graph.GetMatrix(); int[,] legalFlows = new int[graph.VertexCount, graph.VertexCount]; int max_flow = algorithms.EdmondsKarp(I, S, graph, capacity, out legalFlows); output.AppendText($"Максимальный поток = {max_flow}\n"); int rows = legalFlows.GetUpperBound(0) + 1; for (int i = 0; i < rows; i++) { for (int j = 0; j < rows; j++) { output.AppendText(legalFlows[i, j].ToString() + " "); } output.AppendText("\n"); } break; } case 3: { output.Clear(); List <VertexGraph> combsub = new List <VertexGraph>(); List <VertexGraph> not = new List <VertexGraph>(); List <List <VertexGraph> > MaxIS = new List <List <VertexGraph> >(); List <VertexGraph> candidates = graph.GetVertices(); List <VertexGraph> max = new List <VertexGraph>(); algorithms.BronKerbosh_MIS(combsub, candidates, not, graph, MaxIS); if (MaxIS.Count > 1) { output.AppendText("Наибольшее независимое множество:\n"); max = MaxIS[0]; for (int i = 1; i < MaxIS.Count; i++) { if (MaxIS[i].Count > max.Count) { max = MaxIS[i]; } } foreach (var item in max) { output.AppendText(item.ToString() + " "); } } else { output.AppendText("Наибольшее независимого множества нет!\n"); } break; } case 4: { output.Clear(); int[,] next = new int[graph.VertexCount, graph.VertexCount]; int[,] distanse = algorithms.Floyd_Warshall(graph, next); VertexGraph start = new VertexGraph(0); VertexGraph finish = new VertexGraph(graph.VertexCount - 1); List <VertexGraph> path = new List <VertexGraph>(); algorithms.Path(start, finish, next, path); output.AppendText("Матрица кратчайших расстояний:\n"); int rows = distanse.GetUpperBound(0) + 1; for (int i = 0; i < rows; i++) { for (int j = 0; j < rows; j++) { output.AppendText(distanse[i, j].ToString() + " "); } output.AppendText("\n"); } output.AppendText($"\nКратчайший путь от вершины " + start.ToString() + " до " + finish.ToString() + ":\n"); foreach (var item in path) { output.AppendText(item.ToString() + " "); } break; } } }