From 6668b0eeae166c48180a98b3c56c4c06b6e9577f Mon Sep 17 00:00:00 2001 From: lnd212 Date: Mon, 15 Nov 2021 22:04:08 +0400 Subject: [PATCH] Kruskal fix+implement --- cmd/bin/src/graph.dart | 19 +++-- flutter/afterKruskal.txt | 11 +++ flutter/lib/pages/drawing_page.dart | 8 ++ flutter/lib/src/graph.dart | 123 ++++++++++++++++++++++++++-- flutter/wikiKruskal.txt | 11 +++ 5 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 flutter/afterKruskal.txt create mode 100644 flutter/wikiKruskal.txt diff --git a/cmd/bin/src/graph.dart b/cmd/bin/src/graph.dart index f4089be..4607fe9 100644 --- a/cmd/bin/src/graph.dart +++ b/cmd/bin/src/graph.dart @@ -504,7 +504,7 @@ class Graphs { mode: FileMode.append); } for (int i = 0; i < _amount; i++) { - file.writeAsStringSync((i + 1).toString() + Separators.dotToConnections, + file.writeAsStringSync(_dots[i].getName() + Separators.dotToConnections, mode: FileMode.append); var d = _dots[i].getL(); for (var j in d.keys) { @@ -766,22 +766,22 @@ class Graphs { Graphs? kruskal() { //List>> g (m); // вес - вершина 1 - вершина 2 List g = getSortedPathList(); - // ignore: unused_local_variable - int cost = 0; + //int cost = 0; List res = []; for (int i = 0; i < _amount; i++) { - res.add(Dot(_dots[i].getName())); + res.add(Dot(_dots[i].getName(), _dots[i].num)); } List treeId = List.filled(_amount, 0); for (int i = 0; i < _amount; ++i) { treeId[i] = i; } for (int i = 0; i < g.length; ++i) { - int a = g[i].d, b = g[i].p, l = g[i].l; - if (treeId[a - 1] != treeId[b - 1]) { - cost += l; - res[i].addPath(a, b); - int oldId = treeId[b - 1], newId = treeId[a]; + int a = g[i].d - 1, b = g[i].p - 1; + int l = g[i].l; + if (treeId[a] != treeId[b]) { + //cost += l; + res[a].addPath(b + 1, l); + int oldId = treeId[b], newId = treeId[a]; for (int j = 0; j < _amount; ++j) { if (treeId[j] == oldId) { treeId[j] = newId; @@ -789,6 +789,7 @@ class Graphs { } } } + //_dots = res; return Graphs.fromList(_name, res, _useLength, _oriented); } //************Алгоритмы************ diff --git a/flutter/afterKruskal.txt b/flutter/afterKruskal.txt new file mode 100644 index 0000000..67aed4e --- /dev/null +++ b/flutter/afterKruskal.txt @@ -0,0 +1,11 @@ +1 +НеОриентированный +Взвешенный +A: 4|5 2|7 +B: 5|7 1|7 +C: 5|5 +D: 6|6 1|5 +E: 7|9 2|7 3|5 +F: 4|6 +G: 5|9 +END \ No newline at end of file diff --git a/flutter/lib/pages/drawing_page.dart b/flutter/lib/pages/drawing_page.dart index b9042c6..60a7dd8 100644 --- a/flutter/lib/pages/drawing_page.dart +++ b/flutter/lib/pages/drawing_page.dart @@ -493,6 +493,14 @@ class _DrawingPageState extends State { createButton("Bfs", bfsPushed), createButton("Dfs", dfsPushed), createButton("Dijkstra", dijkstraPushed), + createButton("Kruskal", () { + clearDropDownVals(); + setState(() { + currOp = "OP: Kruscal algo"; + graphData.kruskal(); + }); + clearInputData(); + }), createButton("Clear OP", () { clearDropDownVals(); clearInputData(); diff --git a/flutter/lib/src/graph.dart b/flutter/lib/src/graph.dart index 9dfe2d4..e58b7c7 100644 --- a/flutter/lib/src/graph.dart +++ b/flutter/lib/src/graph.dart @@ -404,6 +404,52 @@ class Graphs { return out; } + List? getLongestPath([int start = 1]) { + start--; + if (start < 0 || start >= _amount) { + return null; + } + int max = -1; + int inD = -1; + int out = -1; + List? res = []; + for (int i = start; i < _amount; i++) { + var lens = _dots[i].getL(); + for (var d in lens.keys) { + if (lens[d]! > max) { + max = lens[d]!; + inD = i + 1; + out = d; + } + } + } + if (inD == -1) { + return null; + } + res.add(inD); + res.add(out); + res.add(max); + return res; + } + + List getSortedPathList() { + int max = -1; + int inD = -1; + int out = -1; + List result = []; + for (int i = 0; i < _amount; i++) { + var lens = _dots[i].getL(); + for (var d in lens.keys) { + max = lens[d]!; + inD = i + 1; + out = d; + result.add(LenDotPath(max, inD, out)); + } + } + result.sort((a, b) => a.l.compareTo(b.l)); + return result; + } + /*List getNoRepeatDots() { List ret = []; for (int i = 0; i < _amount; i++) { @@ -458,7 +504,7 @@ class Graphs { mode: FileMode.append); } for (int i = 0; i < _amount; i++) { - file.writeAsStringSync((i + 1).toString() + Separators.dotToConnections, + file.writeAsStringSync(_dots[i].getName() + Separators.dotToConnections, mode: FileMode.append); var d = _dots[i].getL(); for (var j in d.keys) { @@ -657,11 +703,6 @@ class Graphs { } List dijkstra(int from) { - /* - int n; - ... чтение n ... - //vector>> g(_amount); - */ List d = List.filled(_amount, intMax); List p = List.filled(_amount, -1); @@ -692,5 +733,75 @@ class Graphs { } return d; } + + List? prim() { + // работоспособность? + List used = List.filled(_amount, false); + List minE = List.filled(_amount, intMax); + List selE = List.filled(_amount, -1); + minE[0] = 0; + for (int i = 0; i < _amount; ++i) { + int v = -1; + for (int j = 0; j < _amount; ++j) { + if (!used[j] && (v == -1 || minE[j] < minE[v])) { + v = j; + } + } + if (minE[v] == intMax) { + return null; + } + used[v] = true; + if (selE[v] != -1) { + print("${v + 1} ${selE[v]}"); + } + for (int to in _dots[v].getL().keys) { + if (_dots[v].getL()[to]! < minE[to - 1]) { + minE[to] = _dots[v].getL()[to]!; + selE[to] = v; + } + } + } + } + + Graphs? kruskal() { + //List>> g (m); // вес - вершина 1 - вершина 2 + List g = getSortedPathList(); + //int cost = 0; + List res = []; + for (int i = 0; i < _amount; i++) { + res.add(Dot(_dots[i].getName(), _dots[i].num)); + } + List treeId = List.filled(_amount, 0); + for (int i = 0; i < _amount; ++i) { + treeId[i] = i; + } + for (int i = 0; i < g.length; ++i) { + int a = g[i].d - 1, b = g[i].p - 1; + int l = g[i].l; + if (treeId[a] != treeId[b]) { + //cost += l; + res[a].addPath(b + 1, l); + int oldId = treeId[b], newId = treeId[a]; + for (int j = 0; j < _amount; ++j) { + if (treeId[j] == oldId) { + treeId[j] = newId; + } + } + } + } + _dots = res; + return Graphs.fromList(_name, res, _useLength, _oriented); + } //************Алгоритмы************ } + +class LenDotPath { + late int l; + late int d; + late int p; + LenDotPath(int len, dot, path) { + l = len; + d = dot; + p = path; + } +} diff --git a/flutter/wikiKruskal.txt b/flutter/wikiKruskal.txt new file mode 100644 index 0000000..c740669 --- /dev/null +++ b/flutter/wikiKruskal.txt @@ -0,0 +1,11 @@ +1 +НеОриентированный +Взвешенный +A: 2|7 4|5 +B: 1|7 4|9 3|8 5|7 +C: 2|8 5|5 +D: 1|5 2|9 5|15 6|6 +E: 3|5 2|7 4|15 6|8 7|9 +F: 4|6 5|8 7|11 +G: 6|11 5|9 +END \ No newline at end of file