Graphs_dart/tex/dart/graph/dijkstra.dart

29 lines
808 B
Dart
Raw Permalink Normal View History

2021-11-24 20:35:55 +03:00
List<int?> dijkstra(int from) {
List<int?> d = List<int?>.filled(_amount, intMax);
List<int> p = List<int>.filled(_amount, -1);
d[from - 1] = 0;
List<bool> u = List<bool>.filled(_amount, false);
for (int i = 0; i < _amount; ++i) {
int v = -1;
for (int j = 0; j < _amount; ++j) {
if (!u[j] && (v == -1 || d[j]! < d[v]!)) {
v = j;
}
}
if (d[v] == intMax) break;
u[v] = true;
for (int to in _dots[v].getL().keys) {
int len = _dots[v].getL()[to]!;
if (!_useLength && len == 0) len = 1;
if (d[v]! + len < d[to - 1]!) {
d[to - 1] = d[v]! + len;
p[to - 1] = v;
}
}
}
for (int i = 0; i < d.length; i++) {
if (d[i] == intMax) d[i] = null;
}
return d;
}