Kruskal fix+implement

This commit is contained in:
Морозов Андрей 2021-11-15 22:04:08 +04:00
parent b19011ed8b
commit 6668b0eeae
5 changed files with 157 additions and 15 deletions

View File

@ -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<pair<int,pair<int,int>>> g (m); // вес - вершина 1 - вершина 2
List<LenDotPath> g = getSortedPathList();
// ignore: unused_local_variable
int cost = 0;
//int cost = 0;
List<Dot> res = <Dot>[];
for (int i = 0; i < _amount; i++) {
res.add(Dot(_dots[i].getName()));
res.add(Dot(_dots[i].getName(), _dots[i].num));
}
List<int> treeId = List<int>.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);
}
//************Алгоритмы************

11
flutter/afterKruskal.txt Normal file
View File

@ -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

View File

@ -493,6 +493,14 @@ class _DrawingPageState extends State<DrawingPage> {
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();

View File

@ -404,6 +404,52 @@ class Graphs {
return out;
}
List<int>? getLongestPath([int start = 1]) {
start--;
if (start < 0 || start >= _amount) {
return null;
}
int max = -1;
int inD = -1;
int out = -1;
List<int>? res = <int>[];
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<LenDotPath> getSortedPathList() {
int max = -1;
int inD = -1;
int out = -1;
List<LenDotPath> result = <LenDotPath>[];
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<Dot> getNoRepeatDots() {
List<Dot> ret = <Dot>[];
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<int?> dijkstra(int from) {
/*
int n;
... чтение n ...
//vector<vector<pair<int,int>>> g(_amount);
*/
List<int?> d = List<int?>.filled(_amount, intMax);
List<int> p = List<int>.filled(_amount, -1);
@ -692,5 +733,75 @@ class Graphs {
}
return d;
}
List<int?>? prim() {
// работоспособность?
List<bool> used = List<bool>.filled(_amount, false);
List<int> minE = List<int>.filled(_amount, intMax);
List<int> selE = List<int>.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<pair<int,pair<int,int>>> g (m); // вес - вершина 1 - вершина 2
List<LenDotPath> g = getSortedPathList();
//int cost = 0;
List<Dot> res = <Dot>[];
for (int i = 0; i < _amount; i++) {
res.add(Dot(_dots[i].getName(), _dots[i].num));
}
List<int> treeId = List<int>.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;
}
}

11
flutter/wikiKruskal.txt Normal file
View File

@ -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