Skip to content

Commit 1e821e5

Browse files
committed
[Function add]
1.Add method for finding path from source vertex to v.
1 parent fae5f3d commit 1e821e5

File tree

9 files changed

+308
-4
lines changed

9 files changed

+308
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
public abstract class AbstractPath implements Path {
4+
protected final Graph g;
5+
protected final int s;
6+
public AbstractPath(Graph g, int s) {
7+
super();
8+
this.g = g;
9+
this.s = s;
10+
};
11+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
7+
import ca.mcmaster.chapter.one.stack.ListStack;
8+
import ca.mcmaster.chapter.one.stack.MyStack;
9+
10+
public class DepthFirstPath extends AbstractPath {
11+
private boolean[] marked; //If current vertex has been accessed.
12+
private int[] edgeTo; //Record the path from that point to s.
13+
public DepthFirstPath(Graph g, int s) {
14+
super(g, s);
15+
marked = new boolean[g.V()];
16+
edgeTo = new int[g.V()];
17+
dfs(g, s);
18+
}
19+
@Override
20+
public boolean hasPathTo(int v) {
21+
return marked[v];
22+
}
23+
@Override
24+
public Iterable<Integer> pathTo(int v) {
25+
if(true == hasPathTo(v)){
26+
MyStack<Integer> path = new ListStack<>();
27+
do {
28+
path.push(v);
29+
v = edgeTo[v];
30+
} while (v != s);
31+
return path;
32+
}
33+
return null;
34+
}
35+
36+
private void dfs(Graph g, int v){
37+
marked[v] = true;
38+
for(int w : g.adj(v)){ //All vertex connected to v
39+
if(!marked[w]){
40+
edgeTo[w] = v;
41+
dfs(g, w);
42+
}
43+
}
44+
}
45+
46+
public static void main(String[] args) throws FileNotFoundException {
47+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyCG.txt")));
48+
Path p = new DepthFirstPath(g, 0);
49+
Iterable<Integer> path = p.pathTo(1);
50+
StringBuilder sb = new StringBuilder("0");
51+
for(Integer pnode : path){
52+
sb.append("->" + pnode);
53+
}
54+
System.out.println(sb.toString());
55+
}
56+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
public interface Path {
4+
/**
5+
* @Description: If there is a path from s to v
6+
* @param v
7+
* @return
8+
*/
9+
public boolean hasPathTo(int v);
10+
/**
11+
* @Description: Return the path from s to v if it exists and return null if not existing.
12+
* @param v
13+
* @return
14+
*/
15+
Iterable<Integer> pathTo(int v);
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
6
2+
8
3+
0 5
4+
2 4
5+
2 3
6+
1 2
7+
0 1
8+
3 4
9+
3 5
10+
0 2

DataStructrue/Graph/AbstractPath.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
public abstract class AbstractPath implements Path {
4+
protected final Graph g;
5+
protected final int s;
6+
public AbstractPath(Graph g, int s) {
7+
super();
8+
this.g = g;
9+
this.s = s;
10+
};
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
7+
public class DeepFirstSearch extends AbstractSearch {
8+
private boolean[] marked;
9+
private int count;
10+
public DeepFirstSearch(Graph g, int s) {
11+
super(g, s);
12+
marked = new boolean[g.V()];
13+
dfs(g, s);
14+
}
15+
private void dfs(Graph g, int v){
16+
marked[v] = true; //It means current vertex has been accessed.
17+
count++; //update the number of vertex connected to s.
18+
for(int w : g.adj(v))
19+
if(!marked[w]) dfs(g, w); //Check all point connected to v, if not accessed, access recursively.
20+
}
21+
@Override
22+
public boolean mark(int v) {
23+
return marked[v];
24+
}
25+
@Override
26+
public int count() {
27+
return this.count;
28+
}
29+
30+
public static void main(String[] args) throws FileNotFoundException {
31+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyG.txt")));
32+
Search search = new DeepFirstSearch(g, 12);
33+
System.out.println(search.mark(9));
34+
System.out.println(search.count());
35+
g.display();
36+
}
37+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
7+
import ca.mcmaster.chapter.one.stack.ListStack;
8+
import ca.mcmaster.chapter.one.stack.MyStack;
9+
10+
public class DepthFirstPath extends AbstractPath {
11+
private boolean[] marked; //If current vertex has been accessed.
12+
private int[] edgeTo; //Record the path from that point to s.
13+
public DepthFirstPath(Graph g, int s) {
14+
super(g, s);
15+
marked = new boolean[g.V()];
16+
edgeTo = new int[g.V()];
17+
dfs(g, s);
18+
}
19+
@Override
20+
public boolean hasPathTo(int v) {
21+
return marked[v];
22+
}
23+
@Override
24+
public Iterable<Integer> pathTo(int v) {
25+
if(true == hasPathTo(v)){
26+
MyStack<Integer> path = new ListStack<>();
27+
do {
28+
path.push(v);
29+
v = edgeTo[v];
30+
} while (v != s);
31+
return path;
32+
}
33+
return null;
34+
}
35+
36+
private void dfs(Graph g, int v){
37+
marked[v] = true;
38+
for(int w : g.adj(v)){ //All vertex connected to v
39+
if(!marked[w]){
40+
edgeTo[w] = v;
41+
dfs(g, w);
42+
}
43+
}
44+
}
45+
46+
public static void main(String[] args) throws FileNotFoundException {
47+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyCG.txt")));
48+
Path p = new DepthFirstPath(g, 0);
49+
Iterable<Integer> path = p.pathTo(1);
50+
StringBuilder sb = new StringBuilder("0");
51+
for(Integer pnode : path){
52+
sb.append("->" + pnode);
53+
}
54+
System.out.println(sb.toString());
55+
}
56+
}

DataStructrue/Graph/Path.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
public interface Path {
4+
/**
5+
* @Description: If there is a path from s to v
6+
* @param v
7+
* @return
8+
*/
9+
public boolean hasPathTo(int v);
10+
/**
11+
* @Description: Return the path from s to v if it exists and return null if not existing.
12+
* @param v
13+
* @return
14+
*/
15+
Iterable<Integer> pathTo(int v);
16+
}

DataStructrue/Graph/UndirectedGraph.md

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public abstract class AbstractSearch implements Search {
168168
}
169169
```
170170

171-
#### 通过加权并查集实现的Seach类: UFSearch
171+
### 通过加权并查集实现的Seach类: UFSearch
172172
```Java
173173
public class UFSearch extends AbstractSearch {
174174
private final UF uf;
@@ -249,7 +249,7 @@ public class UFSearch extends AbstractSearch {
249249
>11 -> 9 12
250250
>12 -> 11 9
251251

252-
#### 深度优先查找DFSearch
252+
### 深度优先查找DFSearch
253253
```Java
254254
public class DeepFirstSearch extends AbstractSearch {
255255
private boolean[] marked; //A array used to mark if current node is connected to s
@@ -283,6 +283,97 @@ public class DeepFirstSearch extends AbstractSearch {
283283
Search search = new DeepFirstSearch(g, 12);
284284
System.out.println(search.mark(9));
285285
System.out.println(search.count());
286-
g.display();
286+
//g.display();
287+
}
288+
```
289+
* 结果
290+
>true
291+
4
292+
293+
### 寻找路径
294+
* 定义路径的接口函数
295+
```Java
296+
public interface Path {
297+
/**
298+
* @Description: If there is a path from s to v
299+
* @param v
300+
* @return
301+
*/
302+
public boolean hasPathTo(int v);
303+
/**
304+
* @Description: Return the path from s to v if it exists and return null if not existing.
305+
* @param v
306+
* @return
307+
*/
308+
Iterable<Integer> pathTo(int v);
309+
}
310+
```
311+
312+
* 抽象类
313+
>用于定义构造函数。
314+
```Java
315+
public abstract class AbstractPath implements Path {
316+
protected final Graph g;
317+
protected final int s;
318+
public AbstractPath(Graph g, int s) {
319+
super();
320+
this.g = g;
321+
this.s = s;
322+
};
323+
}
324+
```
325+
326+
* 深度优先查找路径
327+
```Java
328+
public class DepthFirstPath extends AbstractPath {
329+
private boolean[] marked; //If current vertex has been accessed.
330+
private int[] edgeTo; //Record the path from that point to s.
331+
public DepthFirstPath(Graph g, int s) {
332+
super(g, s);
333+
marked = new boolean[g.V()];
334+
edgeTo = new int[g.V()];
335+
dfs(g, s);
336+
}
337+
@Override
338+
public boolean hasPathTo(int v) {
339+
return marked[v];
340+
}
341+
@Override
342+
public Iterable<Integer> pathTo(int v) {
343+
if(true == hasPathTo(v)){
344+
//存入的时候是逆序,读取的时候需要顺序,所以LIFO的队列最为合适,使用栈
345+
MyStack<Integer> path = new ListStack<>();
346+
do {
347+
path.push(v);
348+
v = edgeTo[v];
349+
} while (v != s);
350+
return path;
351+
}
352+
return null;
287353
}
288-
```
354+
private void dfs(Graph g, int v){
355+
marked[v] = true;
356+
for(int w : g.adj(v)){ //All vertex connected to v
357+
if(!marked[w]){ //If current vertex has not been accessed, we mark it and save the previous vertex to current vertex.
358+
edgeTo[w] = v;
359+
dfs(g, w);
360+
}
361+
}
362+
}
363+
}
364+
```
365+
366+
* 测试
367+
```Java
368+
public static void main(String[] args) throws FileNotFoundException {
369+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyCG.txt")));
370+
Path p = new DepthFirstPath(g, 0);
371+
Iterable<Integer> path = p.pathTo(1);
372+
StringBuilder sb = new StringBuilder("0");
373+
for(Integer pnode : path){
374+
sb.append("->" + pnode);
375+
}
376+
System.out.println(sb.toString());
377+
}
378+
```
379+
>0->2->1

0 commit comments

Comments
 (0)