diff --git a/cmd/bin/graphs0.dart b/cmd/bin/graphs0.dart index 9db16f1..a536bf9 100644 --- a/cmd/bin/graphs0.dart +++ b/cmd/bin/graphs0.dart @@ -34,7 +34,7 @@ void main(List arguments) { print(x2.bfsPath(1, 3)); */ - var x1 = Graphs("Gt", false, true); + /*var x1 = Graphs("Gt", false, true); x1.addIsolated("T1"); x1.addIsolated("T2"); x1.addIsolated("T3"); @@ -42,8 +42,13 @@ void main(List arguments) { x1.addPath(1, 3, 1); x1.addPath(3, 2, 5); x1.addPath(2, 4, 10); - x1.printG(); - print(x1.getLongestPath()); + x1.printG();*/ + var x1 = Graphs.fromFile("star.txt"); + for (var i in x1.getSortedPathList()) { + print("${i.l} ${i.d} ${i.p}"); + } + x1.kruskal()!.printG(); + //print(x1.getSortedPathList()); //print(x1.bfsPath(1, 4)); //print(x1.dfsIterative(1)); //print(x1.dijkstra(2)); diff --git a/cmd/bin/src/graph.dart b/cmd/bin/src/graph.dart index 3abb457..f4089be 100644 --- a/cmd/bin/src/graph.dart +++ b/cmd/bin/src/graph.dart @@ -432,6 +432,24 @@ class Graphs { return res; } + List getSortedPathList() { + int max = -1; + int inD = -1; + int out = -1; + List result = []; + for (int i = 0; i < _amount; i++) { + var lens = _dots[i].getL(); + for (var d in lens.keys) { + max = lens[d]!; + inD = i + 1; + out = d; + result.add(LenDotPath(max, inD, out)); + } + } + result.sort((a, b) => a.l.compareTo(b.l)); + return result; + } + /*List getNoRepeatDots() { List ret = []; for (int i = 0; i < _amount; i++) { @@ -744,36 +762,45 @@ class Graphs { } } } - /*Graphs? kruskal() { - int m; - List>> g (m); // вес - вершина 1 - вершина 2 + Graphs? kruskal() { + //List>> g (m); // вес - вершина 1 - вершина 2 + List g = getSortedPathList(); + // ignore: unused_local_variable int cost = 0; List res = []; - for (int i = 0;i<_amount;i++){ + 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) { + 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; + for (int i = 0; i < g.length; ++i) { + int a = g[i].d, b = g[i].p, l = g[i].l; + if (treeId[a - 1] != treeId[b - 1]) { + cost += l; + res[i].addPath(a, b); + int oldId = treeId[b - 1], newId = treeId[a]; + for (int j = 0; j < _amount; ++j) { + if (treeId[j] == oldId) { + treeId[j] = newId; } } - } + } } - }*/ + return Graphs.fromList(_name, res, _useLength, _oriented); + } //************Алгоритмы************ } + +class LenDotPath { + late int l; + late int d; + late int p; + LenDotPath(int len, dot, path) { + l = len; + d = dot; + p = path; + } +}