From b19011ed8b2c6470863823ea354d48b9ed8df94b Mon Sep 17 00:00:00 2001 From: lnd212 Date: Mon, 15 Nov 2021 21:22:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82?= =?UTF-8?q?=D0=BC=20=D0=9A=D1=80=D1=83=D1=81=D0=BA=D0=B0=D0=BB=D0=B0.=20?= =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=BE=D1=81=D0=BF=D0=BE=D1=81?= =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=20=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/bin/graphs0.dart | 11 +++++-- cmd/bin/src/graph.dart | 69 +++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 24 deletions(-) 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; + } +}