Skip to content

Commit efafb75

Browse files
committed
dfs 1
1 parent 3d85570 commit efafb75

11 files changed

+264
-29
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 53 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

g.txt

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

out/production/Graph/CC.class

2.73 KB
Binary file not shown.

out/production/Graph/Graph.class

0 Bytes
Binary file not shown.

out/production/Graph/GraphDFS.class

1.86 KB
Binary file not shown.
2.21 KB
Binary file not shown.

src/CC.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

src/Graph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Graph(String filename) {
4444
}
4545

4646

47-
private void validateVertex(int v){
47+
public void validateVertex(int v){
4848
if(v < 0 || v >= V)
4949
throw new IllegalArgumentException("vertex " + v + "is invalid");
5050
}

src/GraphDFS.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

src/SingleSourcePath.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

0 commit comments

Comments
 (0)