Попытки добавить алгоритмы

This commit is contained in:
Морозов Андрей 2021-11-12 03:28:15 +04:00
parent 4953a9ad19
commit cec41fc63c
3 changed files with 110 additions and 9 deletions

View File

@ -33,7 +33,7 @@ void main(List<String> arguments) {
x2.printG(); x2.printG();
print(x2.bfsPath(1, 3)); print(x2.bfsPath(1, 3));
*/
var x1 = Graphs("Gt", false, true); var x1 = Graphs("Gt", false, true);
x1.addIsolated("T1"); x1.addIsolated("T1");
x1.addIsolated("T2"); x1.addIsolated("T2");
@ -43,10 +43,13 @@ void main(List<String> arguments) {
x1.addPath(3, 2, 5); x1.addPath(3, 2, 5);
x1.addPath(2, 4, 10); x1.addPath(2, 4, 10);
x1.printG(); x1.printG();
print(x1.getLongestPath());
//print(x1.bfsPath(1, 4)); //print(x1.bfsPath(1, 4));
//print(x1.dfsIterative(1)); //print(x1.dfsIterative(1));
print(x1.dijkstra(2)); //print(x1.dijkstra(2));
*/ /* var x1 = Graphs.fromFile("star.txt");
print(x1.prim());
int deistvie = 1; int deistvie = 1;
List<Graphs> graphs = <Graphs>[]; List<Graphs> graphs = <Graphs>[];
@ -342,6 +345,11 @@ void main(List<String> arguments) {
} }
break; break;
} }
case 10:
{
print(graphs[0].prim());
break;
}
case 0: case 0:
getStrLine(); getStrLine();
exit(0); exit(0);
@ -349,5 +357,5 @@ void main(List<String> arguments) {
print("Действие не распознано"); print("Действие не распознано");
break; break;
} }
} }*/
} }

View File

@ -404,6 +404,34 @@ class Graphs {
return out; return out;
} }
List<int>? getLongestPath([int start = 1]) {
start--;
if (start < 0 || start >= _amount) {
return null;
}
int max = -1;
int inD = -1;
int out = -1;
List<int>? res = <int>[];
for (int i = start; i < _amount; i++) {
var lens = _dots[i].getL();
for (var d in lens.keys) {
if (lens[d]! > max) {
max = lens[d]!;
inD = i + 1;
out = d;
}
}
}
if (inD == -1) {
return null;
}
res.add(inD);
res.add(out);
res.add(max);
return res;
}
/*List<Dot> getNoRepeatDots() { /*List<Dot> getNoRepeatDots() {
List<Dot> ret = <Dot>[]; List<Dot> ret = <Dot>[];
for (int i = 0; i < _amount; i++) { for (int i = 0; i < _amount; i++) {
@ -657,11 +685,6 @@ class Graphs {
} }
List<int?> dijkstra(int from) { List<int?> dijkstra(int from) {
/*
int n;
... чтение n ...
//vector<vector<pair<int,int>>> g(_amount);
*/
List<int?> d = List<int?>.filled(_amount, intMax); List<int?> d = List<int?>.filled(_amount, intMax);
List<int> p = List<int>.filled(_amount, -1); List<int> p = List<int>.filled(_amount, -1);
@ -692,5 +715,65 @@ class Graphs {
} }
return d; return d;
} }
List<int?>? prim() {
// работоспособность?
List<bool> used = List<bool>.filled(_amount, false);
List<int> minE = List<int>.filled(_amount, intMax);
List<int> selE = List<int>.filled(_amount, -1);
minE[0] = 0;
for (int i = 0; i < _amount; ++i) {
int v = -1;
for (int j = 0; j < _amount; ++j) {
if (!used[j] && (v == -1 || minE[j] < minE[v])) {
v = j;
}
}
if (minE[v] == intMax) {
return null;
}
used[v] = true;
if (selE[v] != -1) {
print("${v + 1} ${selE[v]}");
}
for (int to in _dots[v].getL().keys) {
if (_dots[v].getL()[to]! < minE[to - 1]) {
minE[to] = _dots[v].getL()[to]!;
selE[to] = v;
}
}
}
}
/*Graphs? kruskal() {
int m;
List<pair<int,pair<int,int>>> g (m); // вес - вершина 1 - вершина 2
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])
{
cost += l;
res.add (make_pair (a, b));
int oldId = treeId[b], newId = treeId[a];
for (int j=0; j<_amount; ++j){
if (treeId[j] == oldId){
treeId[j] = newId;
}
}
}
}
}*/
//************Алгоритмы************ //************Алгоритмы************
} }

10
cmd/star.txt Normal file
View File

@ -0,0 +1,10 @@
star
Ориентированный
НеВзвешенный
1: 2|0
2: 4|0
3: 5|0
4: 6|0
5: 2|0
6: 3|0
END