Graphs_dart/tex/dart/graph/kruscal.dart

28 lines
811 B
Dart
Raw Permalink Normal View History

2021-11-24 20:35:55 +03:00
Graphs? kruskal() {
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);
}