Алгоритм Крускала. Работоспособность под вопросом

This commit is contained in:
Морозов Андрей 2021-11-15 21:22:16 +04:00
parent cec41fc63c
commit b19011ed8b
2 changed files with 56 additions and 24 deletions

View File

@ -34,7 +34,7 @@ void main(List<String> arguments) {
print(x2.bfsPath(1, 3));
*/
var x1 = Graphs("Gt", false, true);
/*var x1 = Graphs("Gt", false, true);
x1.addIsolated("T1");
x1.addIsolated("T2");
x1.addIsolated("T3");
@ -42,8 +42,13 @@ void main(List<String> arguments) {
x1.addPath(1, 3, 1);
x1.addPath(3, 2, 5);
x1.addPath(2, 4, 10);
x1.printG();
print(x1.getLongestPath());
x1.printG();*/
var x1 = Graphs.fromFile("star.txt");
for (var i in x1.getSortedPathList()) {
print("${i.l} ${i.d} ${i.p}");
}
x1.kruskal()!.printG();
//print(x1.getSortedPathList());
//print(x1.bfsPath(1, 4));
//print(x1.dfsIterative(1));
//print(x1.dijkstra(2));

View File

@ -432,6 +432,24 @@ class Graphs {
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++) {
@ -744,29 +762,26 @@ class Graphs {
}
}
}
/*Graphs? kruskal() {
int m;
List<pair<int,pair<int,int>>> g (m); // вес - вершина 1 - вершина 2
Graphs? kruskal() {
//List<pair<int,pair<int,int>>> g (m); // вес - вершина 1 - вершина 2
List<LenDotPath> g = getSortedPathList();
// ignore: unused_local_variable
int cost = 0;
List<Dot> res = <Dot>[];
for (int i = 0; i < _amount; i++) {
res.add(Dot(_dots[i].getName()));
}
sort (g.begin(), g.end());
List<int> treeId = List<int>.filled(_amount, 0);
for (int i = 0; i < _amount; ++i) {
treeId[i] = i;
}
for (int i=0; i<_amount; ++i)
{
int a = g[i].second.first, b = g[i].second.second, l = g[i].first;
if (treeId[a] != treeId[b])
{
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.add (make_pair (a, b));
int oldId = treeId[b], newId = treeId[a];
res[i].addPath(a, b);
int oldId = treeId[b - 1], newId = treeId[a];
for (int j = 0; j < _amount; ++j) {
if (treeId[j] == oldId) {
treeId[j] = newId;
@ -774,6 +789,18 @@ class Graphs {
}
}
}
}*/
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;
}
}