Skip to content

Commit 2d3e9f6

Browse files
committed
dfs 2
1 parent efafb75 commit 2d3e9f6

File tree

8 files changed

+203
-25
lines changed

8 files changed

+203
-25
lines changed

.idea/workspace.xml

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

g2.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
4 6
2+
0 1
3+
0 2
4+
0 3
5+
1 2
6+
1 3
7+
2 3
1.63 KB
Binary file not shown.
1.49 KB
Binary file not shown.

out/production/Graph/Path.class

2.21 KB
Binary file not shown.

src/BipartitionDetection.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
//二分图检测
3+
public class BipartitionDetection {
4+
private Graph G;
5+
private boolean[] visited;
6+
private int[] colors;
7+
private boolean isBipartite = true;
8+
9+
public BipartitionDetection(Graph G){
10+
this.G = G;
11+
visited = new boolean[G.V()];
12+
colors = new int[G.V()];
13+
14+
for(int i=0;i<G.V();i++) colors[i] = -1;
15+
16+
// 多包一层for,防止有多个联通分量
17+
for(int v = 0 ;v<G.V();v++){
18+
if(!visited[v])
19+
if(!dfs(v,0)) {
20+
isBipartite = false;break;
21+
}
22+
}
23+
}
24+
25+
// 深度优先遍历
26+
private boolean dfs(int v,int color){
27+
visited[v] = true;
28+
colors[v] = color;
29+
// 遍历相邻节点
30+
for(int w : G.adj(v)){
31+
if(!visited[w]){
32+
if(!dfs(w,1-color)) return false; // 遍历下一个点,染不同颜色
33+
}
34+
else if(colors[w] == colors[v]) return false; // 如果相邻被访问过且相等,则是同一颜色,不是二分图。二分图边的两点隶属于不同的2部分
35+
}
36+
return true;
37+
}
38+
39+
public boolean isBipartite(){
40+
return isBipartite;
41+
}
42+
43+
44+
public static void main(String[] args){
45+
46+
Graph g = new Graph("g2.txt");
47+
BipartitionDetection bipartitionDetection = new BipartitionDetection(g);
48+
System.out.println(bipartitionDetection.isBipartite());
49+
}
50+
}

src/CycleDetection.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 CycleDetection {
4+
5+
private Graph G;
6+
private boolean[] visited;
7+
private boolean hasCycle = false;
8+
9+
public CycleDetection(Graph G){
10+
this.G = G;
11+
visited = new boolean[G.V()];
12+
13+
// 多包一层for,防止有多个联通分量
14+
for(int v = 0 ;v<G.V();v++){
15+
if(!visited[v])
16+
if(dfs(v,v)) {
17+
hasCycle = true;break;
18+
}
19+
20+
}
21+
}
22+
23+
// 从顶点v开始,判断图中是否有环
24+
private boolean dfs(int v,int parent){
25+
visited[v] = true;
26+
// 遍历相邻节点
27+
for(int w : G.adj(v)){
28+
if(!visited[w])
29+
if(dfs(w,v)) return true; //
30+
else if(w != parent) // 判断是否有环:一个相邻节点w被访问过并且 该相邻节点w不是当前节点v的上个节点,则说明有环
31+
return true;
32+
}
33+
return false;
34+
}
35+
36+
// 是否有环
37+
public boolean hasCycle(){
38+
return hasCycle;
39+
}
40+
41+
42+
public static void main(String[] args){
43+
44+
Graph g = new Graph("g.txt");
45+
CycleDetection cycleDetection = new CycleDetection(g);
46+
System.out.println(cycleDetection.hasCycle());
47+
48+
}
49+
}

src/Path.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.util.ArrayList;
2+
import java.util.Collection;
3+
import java.util.Collections;
4+
5+
//
6+
public class Path {
7+
private Graph G;
8+
private int s;
9+
private int t;
10+
private boolean[] visited;
11+
private int[] pre;
12+
13+
14+
public Path(Graph G , int s, int t){
15+
G.validateVertex(s);
16+
G.validateVertex(s);
17+
18+
this.G = G;
19+
this.s = s; // 原点
20+
this.t = t;
21+
22+
visited = new boolean[G.V()];
23+
pre = new int[G.V()];
24+
25+
for(int i=0;i<pre.length;i++)
26+
pre[i] = -1;
27+
28+
dfs(s,s);
29+
30+
}
31+
32+
// 深度优先遍历
33+
private boolean dfs(int v,int parent){
34+
visited[v] = true;
35+
pre[v] = parent;
36+
37+
if(v == t) return true;
38+
39+
// 遍历相邻节点
40+
for(int w : G.adj(v)){
41+
if(!visited[w])
42+
if(dfs(w,v) ) return true;
43+
}
44+
45+
return false;
46+
}
47+
48+
// 从源到目标是否可以连接
49+
public boolean isConnectedTo(){
50+
return visited[t];
51+
}
52+
53+
// 从源到目标的路径
54+
public Iterable<Integer> path(){
55+
ArrayList<Integer> res = new ArrayList<>();
56+
if(!isConnectedTo()) return res;
57+
58+
int cur = t;
59+
while(cur != s){
60+
res.add(cur);
61+
cur = pre[cur]; // 上一个节点
62+
}
63+
res.add(s);
64+
Collections.reverse(res); // 反
65+
return res;
66+
}
67+
68+
69+
public static void main(String[] args){
70+
71+
Graph g = new Graph("g.txt");
72+
Path path = new Path(g,0,6);
73+
System.out.println(path.path());
74+
}
75+
}

0 commit comments

Comments
 (0)