public void Merger(Graph gr1, Graph gr2) { var NodeList1 = gr1.GetNodes(); var Nodelist2 = gr2.GetNodes(); foreach (var node in NodeList1) { object temp = null; foreach (var node2 in Nodelist2) { if (Equals(node.Key, node2.Key)) { temp = node2.Key; break; } } if (temp == null) { gr2.AddVertex(node.Key); Nodelist2 = gr2.GetNodes(); temp = node.Key; } foreach (var joint in node.Value) { bool allGood = false; foreach (var joint2 in Nodelist2[temp]) { if (Equals(joint.Key, joint2.Key)) { allGood = true; break; } } if (!allGood) { gr2.AddJoint(temp, joint.Key, joint.Value); Nodelist2 = gr2.GetNodes(); } } } gr2.PrintToFile("output2.txt"); }
public Graph BuildSpanningTree() { SortedList <object, int> subTrees = new SortedList <object, int>(); var temp = GetNodeNames(); //построение дерева,где каждая компонент связонности находится в разных поддеревьях(0-6) for (int i = 0; i < temp.Count; i++) { subTrees.Add(temp[i], i); } Graph SpanningTree = new Graph(); if (!IsDirected) { SpanningTree.ConvertToDirected(); } //пока while (!CheckNumber(subTrees)) { foreach (var node in Graf) { KeyValuePair <object, int> joint; try { joint = node.Value.ToArray().OrderBy(x => x.Value).First(); } catch { continue; } try { SpanningTree.AddVertex(node.Key); } catch { } try { SpanningTree.AddVertex(joint.Key); } catch { } try { SpanningTree.AddJoint(node.Key, joint.Key, joint.Value); } catch { } DeleteVertex(node.Key, joint.Key); if (subTrees[joint.Key] > subTrees[node.Key]) { int t = subTrees[joint.Key]; foreach (object n in GetNodeNames()) { if (subTrees[n] == t) { subTrees[n] = subTrees[node.Key]; } } } else { int t = subTrees[node.Key]; foreach (object n in GetNodeNames()) { if (subTrees[n] == t) { subTrees[n] = subTrees[joint.Key]; } } } } } return(SpanningTree); }
/*ниже методы для курсовой мне нужны*/ public Graph Hungarian(out int cost) { SortedList <int, object> references; int[,] a = GetMas(out references); int n = Graf.Count; int[] u = new int[n + 1]; int[] v = new int[n + 1]; int[] p = new int[n + 1]; int[] way = new int[n + 1]; for (int i = 1; i <= n; i++) { p[0] = i; int j0 = 0; bool[] used = new bool[n + 1]; int[] minv = new int[n + 1]; for (int j = 0; j <= n; j++) { minv[j] = int.MaxValue; used[j] = false; } do { used[j0] = true; int i0 = p[j0]; int delta = int.MaxValue; int j1 = 0; for (int j = 1; j <= n; j++) { if (!used[j]) { int cur = a[i0, j] - u[i0] - v[j]; if (cur < minv[j]) { minv[j] = cur; way[j] = j0; } if (minv[j] < delta) { delta = minv[j]; j1 = j; } } } for (int j = 0; j <= n; j++) { if (used[j]) { u[p[j]] += delta; v[j] -= delta; } else { minv[j] -= delta; } } j0 = j1; }while (p[j0] != 0); do { int j1 = way[j0]; p[j0] = p[j1]; j0 = j1; }while (j0 != 0); } int[] ans = new int[n + 1]; ans[0] = 0; for (int j = 1; j <= n; j++) { ans[p[j]] = j; } cost = -v[0]; Graph ret = new Graph(); foreach (var node in references.Values) { ret.AddVertex(node); } for (int i = 1; i <= n; i++) { try { ret.AddJoint(references[i - 1], references[ans[i] - 1], 1); } catch { } } if (!IsDirected) { ret.ConvertToUndirected(); } return(ret); }