From cec41fc63c90cc7e10ec1f8613359e6e276d346a Mon Sep 17 00:00:00 2001 From: lnd212 Date: Fri, 12 Nov 2021 03:28:15 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=B0=D0=BB?= =?UTF-8?q?=D0=B3=D0=BE=D1=80=D0=B8=D1=82=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/bin/graphs0.dart | 16 ++++++-- cmd/bin/src/graph.dart | 93 +++++++++++++++++++++++++++++++++++++++--- cmd/star.txt | 10 +++++ 3 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 cmd/star.txt diff --git a/cmd/bin/graphs0.dart b/cmd/bin/graphs0.dart index e8422c0..9db16f1 100644 --- a/cmd/bin/graphs0.dart +++ b/cmd/bin/graphs0.dart @@ -33,7 +33,7 @@ void main(List arguments) { x2.printG(); print(x2.bfsPath(1, 3)); - +*/ var x1 = Graphs("Gt", false, true); x1.addIsolated("T1"); x1.addIsolated("T2"); @@ -43,10 +43,13 @@ void main(List arguments) { x1.addPath(3, 2, 5); x1.addPath(2, 4, 10); x1.printG(); + print(x1.getLongestPath()); //print(x1.bfsPath(1, 4)); //print(x1.dfsIterative(1)); - print(x1.dijkstra(2)); -*/ + //print(x1.dijkstra(2)); + /* var x1 = Graphs.fromFile("star.txt"); + print(x1.prim()); + int deistvie = 1; List graphs = []; @@ -342,6 +345,11 @@ void main(List arguments) { } break; } + case 10: + { + print(graphs[0].prim()); + break; + } case 0: getStrLine(); exit(0); @@ -349,5 +357,5 @@ void main(List arguments) { print("Действие не распознано"); break; } - } + }*/ } diff --git a/cmd/bin/src/graph.dart b/cmd/bin/src/graph.dart index 9dfe2d4..3abb457 100644 --- a/cmd/bin/src/graph.dart +++ b/cmd/bin/src/graph.dart @@ -404,6 +404,34 @@ class Graphs { return out; } + List? getLongestPath([int start = 1]) { + start--; + if (start < 0 || start >= _amount) { + return null; + } + int max = -1; + int inD = -1; + int out = -1; + List? res = []; + 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 getNoRepeatDots() { List ret = []; for (int i = 0; i < _amount; i++) { @@ -657,11 +685,6 @@ class Graphs { } List dijkstra(int from) { - /* - int n; - ... чтение n ... - //vector>> g(_amount); - */ List d = List.filled(_amount, intMax); List p = List.filled(_amount, -1); @@ -692,5 +715,65 @@ class Graphs { } return d; } + + List? prim() { + // работоспособность? + List used = List.filled(_amount, false); + List minE = List.filled(_amount, intMax); + List selE = List.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>> g (m); // вес - вершина 1 - вершина 2 + + int cost = 0; + List res = []; + for (int i = 0;i<_amount;i++){ + res.add(Dot(_dots[i].getName())); + } + + sort (g.begin(), g.end()); + List treeId = List.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; + } + } + } + } + }*/ //************Алгоритмы************ } diff --git a/cmd/star.txt b/cmd/star.txt new file mode 100644 index 0000000..6e645bb --- /dev/null +++ b/cmd/star.txt @@ -0,0 +1,10 @@ +star +Ориентированный +НеВзвешенный +1: 2|0 +2: 4|0 +3: 5|0 +4: 6|0 +5: 2|0 +6: 3|0 +END \ No newline at end of file