Skip to content

Commit 4a04d96

Browse files
author
tianqing.liang
committed
桥,割点,最小生成树
1 parent a72a4fb commit 4a04d96

File tree

19 files changed

+812
-0
lines changed

19 files changed

+812
-0
lines changed

29-Brigde-And-Cut-Points/Edge.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Edge {
2+
int? v, w;
3+
Edge(int v, int w){
4+
this.v = v;
5+
this.w = w;
6+
}
7+
8+
@override
9+
String toString(){
10+
return ("$v-$w");
11+
}
12+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import 'Graph.dart';
2+
import 'Edge.dart';
3+
import 'dart:io';
4+
import 'dart:math';
5+
6+
/**
7+
* 查找桥
8+
*/
9+
class FindBridges {
10+
Graph? _G;
11+
12+
List? _visited;
13+
14+
List? _ord;
15+
16+
List? _low;
17+
18+
int cnt = 0;
19+
20+
List? res;
21+
22+
FindBridges(Graph G) {
23+
this._G = G;
24+
_visited = List.filled(G.V()!, false, growable: true);
25+
26+
res = List.filled(0, Edge, growable: true);
27+
_ord = List.filled(G.V()!, 0, growable: true);
28+
_low = List.filled(G.V()!, 0, growable: true);
29+
cnt = 0;
30+
31+
for (int v = 0; v < _G!.V()!; v++) {
32+
if (!_visited![v]) {
33+
_dfs(v, v);
34+
}
35+
}
36+
}
37+
38+
_dfs(int v, int parent) {
39+
_visited![v] = true;
40+
_ord![v] = cnt;
41+
_low![v] = _ord![v];
42+
cnt++;
43+
// print("循环:${_G!.adj(v)}");
44+
for (int w in _G!.adj(v))
45+
if (!_visited![w]) {
46+
_dfs(w, v);
47+
_low![v] = [(_low![v] as int), (_low![w] as int)].reduce(min);
48+
// print(" 最小值:${_low![v]}");
49+
if (_low![w] > _ord![v]) {
50+
res!.add(Edge(v, w));
51+
}
52+
} else if (w != parent) {
53+
_low![v] = [(_low![v] as int), (_low![w] as int)].reduce(min);
54+
}
55+
}
56+
57+
result() {
58+
return res;
59+
}
60+
}
61+
62+
void main() async {
63+
File content = File("g.txt");
64+
List<String> list = await content.readAsLines();
65+
//读取到文件的顶点数量以及
66+
int v = int.parse(list[0].split(" ")[0]);
67+
int e = int.parse(list[0].split(" ")[1]);
68+
Graph graph = Graph(v, e, list);
69+
70+
FindBridges fb = new FindBridges(graph);
71+
print("Bridges in g : " + fb.result().toString());
72+
73+
File content2 = File("g2.txt");
74+
List<String> list2 = await content2.readAsLines();
75+
//读取到文件的顶点数量以及
76+
int v2 = int.parse(list2[0].split(" ")[0]);
77+
int e2 = int.parse(list2[0].split(" ")[1]);
78+
Graph graph2 = Graph(v2, e2, list2);
79+
FindBridges fb2 = new FindBridges(graph2);
80+
print("Bridges in g2 : " + fb2.result().toString());
81+
82+
File content3 = File("g3.txt");
83+
List<String> list3 = await content3.readAsLines();
84+
//读取到文件的顶点数量以及
85+
int v3 = int.parse(list3[0].split(" ")[0]);
86+
int e3 = int.parse(list3[0].split(" ")[1]);
87+
Graph graph3 = Graph(v3, e3, list3);
88+
FindBridges fb3 = new FindBridges(graph3);
89+
print("Bridges in g3 : " + fb3.result().toString());
90+
91+
File content4 = File("g4.txt");
92+
List<String> list4 = await content4.readAsLines();
93+
//读取到文件的顶点数量以及
94+
int v4 = int.parse(list4[0].split(" ")[0]);
95+
int e4 = int.parse(list4[0].split(" ")[1]);
96+
Graph graph4 = Graph(v4, e4, list4);
97+
FindBridges fb4 = new FindBridges(graph4);
98+
print("Bridges in g4 : " + fb4.result().toString());
99+
100+
File content5 = File("tree.txt");
101+
List<String> list5 = await content5.readAsLines();
102+
//读取到文件的顶点数量以及
103+
int v5 = int.parse(list5[0].split(" ")[0]);
104+
int e5 = int.parse(list5[0].split(" ")[1]);
105+
Graph graph5 = Graph(v5, e5, list5);
106+
FindBridges fb_tree = new FindBridges(graph5);
107+
print("Bridges in tree : " + fb_tree.result().toString());
108+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import 'Graph.dart';
2+
import 'dart:collection';
3+
import 'dart:math';
4+
import 'dart:io';
5+
6+
/**
7+
* 查找割点
8+
*/
9+
class FindCutPoints {
10+
Graph? _G;
11+
12+
List? _visited;
13+
14+
List? _ord;
15+
16+
List? _low;
17+
18+
int _cnt = 0;
19+
20+
HashSet<int>? res;
21+
22+
FindCutPoints(Graph G) {
23+
this._G = G;
24+
_visited = List.filled(_G!.V()!, false, growable: true);
25+
26+
res = HashSet();
27+
_ord = List.filled(_G!.V()!, 0, growable: true);
28+
_low = List.filled(_G!.V()!, 0, growable: true);
29+
_cnt = 0;
30+
31+
for (int v = 0; v < _G!.V()!; v++) if (!_visited![v]) _dfs(v, v);
32+
}
33+
34+
_dfs(int v, int parent) {
35+
_visited![v] = true;
36+
_ord![v] = _cnt;
37+
_low![v] = _ord![v];
38+
_cnt++;
39+
40+
int child = 0;
41+
42+
for (int w in _G!.adj(v))
43+
if (!_visited![w]) {
44+
_dfs(w, v);
45+
_low![v] = [(_low![v] as int), (_low![w] as int)].reduce(min);
46+
47+
if (v != parent && _low![w] >= _ord![v]) res!.add(v);
48+
49+
child++;
50+
if (v == parent && child > 1) res!.add(v);
51+
} else if (w != parent)
52+
_low![v] = [(_low![v] as int), (_low![w] as int)].reduce(min);
53+
}
54+
55+
result() {
56+
return res;
57+
}
58+
}
59+
60+
void main() async {
61+
File content = File("g.txt");
62+
List<String> list = await content.readAsLines();
63+
//读取到文件的顶点数量以及
64+
int v = int.parse(list[0].split(" ")[0]);
65+
int e = int.parse(list[0].split(" ")[1]);
66+
Graph graph = Graph(v, e, list);
67+
FindCutPoints fc = new FindCutPoints(graph);
68+
print("Cut Points in g : " + fc.result().toString());
69+
70+
File content2 = File("g2.txt");
71+
List<String> list2 = await content2.readAsLines();
72+
//读取到文件的顶点数量以及
73+
int v2 = int.parse(list2[0].split(" ")[0]);
74+
int e2 = int.parse(list2[0].split(" ")[1]);
75+
Graph graph2 = Graph(v2, e2, list2);
76+
FindCutPoints fc2 = new FindCutPoints(graph2);
77+
print("Cut Points in g2 : " + fc2.result().toString());
78+
79+
File content3 = File("g3.txt");
80+
List<String> list3 = await content3.readAsLines();
81+
//读取到文件的顶点数量以及
82+
int v3 = int.parse(list3[0].split(" ")[0]);
83+
int e3 = int.parse(list3[0].split(" ")[1]);
84+
Graph graph3 = Graph(v3, e3, list3);
85+
FindCutPoints fc3 = new FindCutPoints(graph3);
86+
print("Cut Points in g3 : " + fc3.result().toString());
87+
88+
File content5 = File("tree.txt");
89+
List<String> list5 = await content5.readAsLines();
90+
//读取到文件的顶点数量以及
91+
int v5 = int.parse(list5[0].split(" ")[0]);
92+
int e5 = int.parse(list5[0].split(" ")[1]);
93+
Graph graph5 = Graph(v5, e5, list5);
94+
FindCutPoints fc4 = new FindCutPoints(graph5);
95+
print("Cut Points in tree : " + fc4.result().toString());
96+
}

29-Brigde-And-Cut-Points/Graph.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import 'dart:collection';
2+
import 'dart:io';
3+
4+
/**
5+
* 图
6+
*/
7+
class Graph{
8+
int? _V; //图的顶点数量
9+
int? _E; //图的边的数量
10+
List? _treeSet;
11+
12+
Graph(V, E, List list) {
13+
_V = V;
14+
_E = E;
15+
if (V < 0) throw Exception("V must be non-negative");
16+
if (E < 0) throw Exception("E must be non-negative");
17+
_treeSet = List.generate(V, (_) => SplayTreeSet<int>(), growable: false);
18+
19+
for (int i = 1; i <= E; i++) {
20+
int a = int.parse(list[i].split(" ")[0]);
21+
validateVertex(a);
22+
int b = int.parse(list[i].split(" ")[1]);
23+
validateVertex(b);
24+
(_treeSet![a] as SplayTreeSet).add(b);
25+
(_treeSet![b] as SplayTreeSet).add(a);
26+
}
27+
}
28+
29+
validateVertex(int v) {
30+
if (v < 0 || v >= _V!) throw new Exception("vertex $v is invalid");
31+
}
32+
33+
int? V() {
34+
return _V;
35+
}
36+
37+
bool hasEdge(int v, int w) {
38+
validateVertex(v);
39+
validateVertex(w);
40+
return (_treeSet![v] as SplayTreeSet).contains(w);
41+
}
42+
43+
int _degree(int v) {
44+
return _treeSet![v].length;
45+
}
46+
47+
int? E() {
48+
return _E;
49+
}
50+
51+
adj(int v) {
52+
validateVertex(v);
53+
return _treeSet![v];
54+
}
55+
56+
@override
57+
String toString() {
58+
StringBuffer sb = new StringBuffer();
59+
sb.write("V = $_V, E = $_E\n");
60+
for (int j = 0; j < _V!; j++) {
61+
sb.write("节点:$j ");
62+
sb.write("${_treeSet![j].toString()} ");
63+
sb.write('\n');
64+
}
65+
return sb.toString();
66+
}
67+
}
68+
void main() async {
69+
File content = File("g.txt");
70+
List<String> list = await content.readAsLines();
71+
//读取到文件的顶点数量以及
72+
print(list[0].split(" ")[0]);
73+
print(list[0].split(" ")[1]);
74+
int v = int.parse(list[0].split(" ")[0]) ;
75+
int e = int.parse(list[0].split(" ")[1]) ;
76+
//
77+
Graph adjList= Graph(v, e, list);
78+
print(adjList);
79+
80+
}

29-Brigde-And-Cut-Points/g.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
7 8
2+
0 1
3+
0 2
4+
1 3
5+
2 3
6+
3 5
7+
4 5
8+
4 6
9+
5 6

29-Brigde-And-Cut-Points/g2.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
12 16
2+
0 1
3+
0 2
4+
1 3
5+
2 3
6+
3 5
7+
4 5
8+
4 6
9+
4 7
10+
5 6
11+
6 8
12+
8 9
13+
8 10
14+
8 11
15+
9 10
16+
9 11
17+
10 11

29-Brigde-And-Cut-Points/g3.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
4 5
2+
0 1
3+
0 2
4+
1 2
5+
1 3
6+
2 3

29-Brigde-And-Cut-Points/g4.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
7 8
2+
0 1
3+
0 2
4+
1 3
5+
1 4
6+
2 3
7+
2 6
8+
3 5
9+
5 6

29-Brigde-And-Cut-Points/tree.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
7 6
2+
0 1
3+
0 3
4+
1 6
5+
2 3
6+
2 5
7+
3 4

0 commit comments

Comments
 (0)