Алгоритм Дейкстры

This commit is contained in:
Морозов Андрей 2021-11-11 08:09:49 +00:00
parent 9024c24c4c
commit 7945800116
1 changed files with 37 additions and 20 deletions

View File

@ -89,6 +89,7 @@ class Dot {
} }
class Graphs { class Graphs {
static const int intMax = 0x7fffffffffffffff;
//Data //Data
String _name = "Undefined"; //Имя String _name = "Undefined"; //Имя
int _amount = 0; //Количество вершин int _amount = 0; //Количество вершин
@ -151,6 +152,9 @@ class Graphs {
if (from <= 0 || from > _amount || to <= 0 && to > _amount) { if (from <= 0 || from > _amount || to <= 0 && to > _amount) {
return "Can't find specified path"; return "Can't find specified path";
} }
if (!_dots[from - 1].hasConnection(to)) {
return "Already no connection between $from and $to";
}
_dots[from - 1].delPath(to); _dots[from - 1].delPath(to);
if (!_oriented) { if (!_oriented) {
_dots[to - 1].delPath(from); _dots[to - 1].delPath(from);
@ -652,28 +656,41 @@ class Graphs {
return label; return label;
} }
void dijkstra(int source) { List<int?> dijkstra(int from) {
/* /*
create vertex set Q; 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);
for each vertex v in Graph{ d[from - 1] = 0;
dist[v] INFINITY ; List<bool> u = List<bool>.filled(_amount, false);
prev[v] UNDEFINED ; for (int i = 0; i < _amount; ++i) {
add v to Q;} int v = -1;
dist[source] 0; for (int j = 0; j < _amount; ++j) {
// int t;
while Q is not empty{ if (!u[j] && (v == -1 || d[j]! < d[v]!)) {
u vertex in Q with min dist[u] v = j;
}
remove u from Q }
if (d[v] == intMax) break;
for each neighbor v of u still in Q{ u[v] = true;
alt dist[u] + length(u, v); for (int to in _dots[v].getL().keys) {
if alt < dist[v]: { int len = _dots[v].getL()[to]!;
dist[v] alt; if (!_useLength && len == 0) len = 1;
prev[v] u;} if (d[v]! + len < d[to - 1]!) {
}} d[to - 1] = d[v]! + len;
return dist[], prev[]*/ p[to - 1] = v;
}
}
}
for (int i = 0; i < d.length; i++) {
// подумать как убрать эту часть
if (d[i] == intMax) d[i] = null;
}
return d;
} }
//************Алгоритмы************ //************Алгоритмы************
} }