Алгоритм Дейкстры
This commit is contained in:
parent
9024c24c4c
commit
7945800116
|
@ -89,6 +89,7 @@ class Dot {
|
|||
}
|
||||
|
||||
class Graphs {
|
||||
static const int intMax = 0x7fffffffffffffff;
|
||||
//Data
|
||||
String _name = "Undefined"; //Имя
|
||||
int _amount = 0; //Количество вершин
|
||||
|
@ -151,6 +152,9 @@ class Graphs {
|
|||
if (from <= 0 || from > _amount || to <= 0 && to > _amount) {
|
||||
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);
|
||||
if (!_oriented) {
|
||||
_dots[to - 1].delPath(from);
|
||||
|
@ -652,28 +656,41 @@ class Graphs {
|
|||
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{
|
||||
dist[v] ← INFINITY ;
|
||||
prev[v] ← UNDEFINED ;
|
||||
add v to Q;}
|
||||
dist[source] ← 0;
|
||||
|
||||
while Q is not empty{
|
||||
u ← vertex in Q with min dist[u]
|
||||
|
||||
remove u from Q
|
||||
|
||||
for each neighbor v of u still in Q{
|
||||
alt ← dist[u] + length(u, v);
|
||||
if alt < dist[v]: {
|
||||
dist[v] ← alt;
|
||||
prev[v] ← u;}
|
||||
}}
|
||||
return dist[], prev[]*/
|
||||
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) {
|
||||
// int t;
|
||||
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;
|
||||
}
|
||||
//************Алгоритмы************
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue