File tree 11 files changed +264
-29
lines changed
11 files changed +264
-29
lines changed Original file line number Diff line number Diff line change 1
- 7 9
1
+ 7 6
2
2
0 1
3
- 0 3
4
- 1 2
5
- 1 6
3
+ 0 2
4
+ 1 3
5
+ 1 4
6
6
2 3
7
- 2 5
8
- 3 4
9
- 4 5
10
- 5 6
7
+ 2 6
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+
3
+ // Connected Component 联通分量
4
+ public class CC {
5
+ private Graph G ;
6
+ private int [] visited ;
7
+ private int cccount ;
8
+
9
+ public CC (Graph G ){
10
+ this .G = G ;
11
+ visited = new int [G .V ()];
12
+ for (int i =0 ;i <visited .length ;i ++) visited [i ] = -1 ;
13
+
14
+ // 多包一层for,防止有多个联通分量
15
+ for (int v = 0 ;v <G .V ();v ++){
16
+ if (visited [v ] == -1 ){
17
+ dfs (v ,cccount );
18
+ cccount ++;
19
+ }
20
+
21
+ }
22
+ }
23
+
24
+ // 深度优先遍历
25
+ private void dfs (int v ,int ccid ){
26
+ visited [v ] = ccid ;
27
+ // 遍历相邻节点
28
+ for (int w : G .adj (v )){
29
+ if (visited [w ] == -1 )
30
+ dfs (w ,ccid );
31
+ }
32
+ }
33
+
34
+ // 返回几个联通分量
35
+ public int count (){
36
+ for (int e : visited ){
37
+ System .out .print (e + " " );
38
+ }
39
+ System .out .println ();
40
+ return cccount ;
41
+ }
42
+
43
+ // 2个顶点是否相连
44
+ public boolean isConnected (int v ,int w ){
45
+ G .validateVertex (v );
46
+ G .validateVertex (w );
47
+ return visited [v ] == visited [w ];
48
+ }
49
+
50
+ // 返回联通分量数组
51
+ public ArrayList <Integer >[] components (){
52
+ ArrayList <Integer >[] res = new ArrayList [cccount ];
53
+ for (int i =0 ;i <cccount ;i ++){
54
+ res [i ] = new ArrayList <>();
55
+ }
56
+
57
+ // 遍历每一个顶点
58
+ for (int v =0 ;v <G .V ();v ++){
59
+ // 放到各自的联通分量
60
+ res [visited [v ]].add (v );
61
+ }
62
+
63
+ return res ;
64
+ }
65
+
66
+
67
+ public static void main (String [] args ){
68
+
69
+ Graph g = new Graph ("g.txt" );
70
+ CC cc = new CC (g );
71
+ // System.out.println(cc.count());
72
+ // System.out.println(cc.isConnected(0,5));
73
+
74
+ ArrayList <Integer >[] comp = cc .components ();
75
+ for (int ccid =0 ;ccid <comp .length ;ccid ++){
76
+ System .out .print (ccid + " : " );
77
+ for (int w :comp [ccid ]){
78
+ System .out .print (w + " " );
79
+ }
80
+ System .out .println ();
81
+ }
82
+
83
+ }
84
+ }
Original file line number Diff line number Diff line change @@ -44,7 +44,7 @@ public Graph(String filename) {
44
44
}
45
45
46
46
47
- private void validateVertex (int v ){
47
+ public void validateVertex (int v ){
48
48
if (v < 0 || v >= V )
49
49
throw new IllegalArgumentException ("vertex " + v + "is invalid" );
50
50
}
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+
3
+ public class GraphDFS {
4
+
5
+ private Graph G ;
6
+ private boolean [] visited ;
7
+ private ArrayList <Integer > pre = new ArrayList <>(); //先序遍历结果
8
+ private ArrayList <Integer > post = new ArrayList <>();//后序遍历结果
9
+
10
+ public GraphDFS (Graph G ){
11
+ this .G = G ;
12
+ visited = new boolean [G .V ()];
13
+
14
+ // 多包一层for,防止有多个联通分量
15
+ for (int v = 0 ;v <G .V ();v ++){
16
+ if (!visited [v ])
17
+ dfs (v );
18
+ }
19
+ }
20
+
21
+ // 深度优先遍历
22
+ private void dfs (int v ){
23
+ visited [v ] = true ;
24
+ pre .add (v );
25
+
26
+ // 遍历相邻节点
27
+ for (int w : G .adj (v )){
28
+ if (!visited [w ])
29
+ dfs (w );
30
+ }
31
+ post .add (v );
32
+ }
33
+
34
+ public Iterable <Integer > pre (){
35
+ return pre ;
36
+ }
37
+
38
+ public Iterable <Integer > post (){
39
+ return post ;
40
+ }
41
+
42
+ public static void main (String [] args ){
43
+
44
+ Graph g = new Graph ("g.txt" );
45
+ GraphDFS graphDFS = new GraphDFS (g );
46
+ System .out .println (graphDFS .pre ());
47
+ System .out .println (graphDFS .post ());
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .Collection ;
3
+ import java .util .Collections ;
4
+
5
+ // 单源路径
6
+ public class SingleSourcePath {
7
+ private Graph G ;
8
+ private int s ;
9
+ private boolean [] visited ;
10
+ private int [] pre ;
11
+
12
+
13
+ public SingleSourcePath (Graph G , int s ){
14
+ G .validateVertex (s );
15
+ this .G = G ;
16
+ this .s = s ; // 原点
17
+ visited = new boolean [G .V ()];
18
+ pre = new int [G .V ()];
19
+
20
+ for (int i =0 ;i <pre .length ;i ++)
21
+ pre [i ] = -1 ;
22
+
23
+ dfs (s ,s );
24
+
25
+ }
26
+
27
+ // 深度优先遍历
28
+ private void dfs (int v ,int parent ){
29
+ visited [v ] = true ;
30
+ pre [v ] = parent ;
31
+ // 遍历相邻节点
32
+ for (int w : G .adj (v )){
33
+ if (!visited [w ])
34
+ dfs (w ,v );
35
+ }
36
+ }
37
+
38
+ // 从源到目标是否可以连接
39
+ public boolean isConnectedTo (int t ){
40
+ G .validateVertex (t );
41
+ return visited [t ];
42
+ }
43
+
44
+ // 从源到目标的路径
45
+ public Iterable <Integer > path (int t ){
46
+ ArrayList <Integer > res = new ArrayList <>();
47
+ if (!isConnectedTo (t )) return res ;
48
+
49
+ int cur = t ;
50
+ while (cur != s ){
51
+ res .add (cur );
52
+ cur = pre [cur ]; // 上一个节点
53
+ }
54
+ res .add (s );
55
+ Collections .reverse (res ); // 反
56
+ return res ;
57
+ }
58
+
59
+
60
+ public static void main (String [] args ){
61
+
62
+ Graph g = new Graph ("g.txt" );
63
+ SingleSourcePath singleSourcePath = new SingleSourcePath (g ,0 );
64
+ System .out .println (singleSourcePath .path (6 ));
65
+ }
66
+ }
You can’t perform that action at this time.
0 commit comments