Skip to content

Commit 4a80c91

Browse files
committed
[Function add]
1.Add BFP for directed graph.
1 parent 93793f9 commit 4a80c91

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ca.mcmaster.chapter.four.graph.directed;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.Stack;
7+
import java.util.concurrent.LinkedBlockingQueue;
8+
9+
import ca.mcmaster.chapter.four.graph.Path;
10+
11+
public class BreadFirstPathDirectedGraph implements Path {
12+
private final Digraph g;
13+
private int s;
14+
private final boolean[] marked;
15+
private final int[] edgeTo;
16+
public BreadFirstPathDirectedGraph(Digraph g, int s) {
17+
this.g = g;
18+
this.s = s;
19+
marked = new boolean[g.V()];
20+
edgeTo = new int[g.V()];
21+
bfs(g, s);
22+
}
23+
@Override
24+
public boolean hasPathTo(int v) {
25+
return marked[v];
26+
}
27+
28+
@Override
29+
public Iterable<Integer> pathTo(int v) {
30+
Stack<Integer> path = new Stack<>();
31+
path.push(v);
32+
while(s != edgeTo[v]){
33+
path.push(edgeTo[v]);
34+
v= edgeTo[v];
35+
}
36+
return path;
37+
}
38+
private void bfs(Digraph g, int s){
39+
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>();
40+
marked[s] = true;
41+
q.offer(s);
42+
while(!q.isEmpty()){
43+
int v = q.poll();
44+
for(int w : g.adj(v)){
45+
if(!marked[w]){
46+
edgeTo[w] = v;
47+
marked[w] = true;
48+
q.offer(w);
49+
}
50+
}
51+
}
52+
}
53+
public static void main(String[] args) throws FileNotFoundException {
54+
DigraphImpl g = new DigraphImpl(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt")));
55+
DepthFirstPathDirectedGraph p = new DepthFirstPathDirectedGraph(g, 6);
56+
Iterable<Integer> pathTo = p.pathTo(4);
57+
System.out.print(6 + " ");
58+
for(Integer w : pathTo) System.out.print(w + " ");
59+
System.out.println();
60+
}
61+
}

Diff for: Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/DepthFirstPathDirectedGraph.java

+9
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ private void dfs(DigraphImpl g, int v){
5050
}
5151
}
5252
}
53+
public static void main(String[] args) throws FileNotFoundException {
54+
DigraphImpl g = new DigraphImpl(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt")));
55+
DepthFirstPathDirectedGraph p = new DepthFirstPathDirectedGraph(g, 6);
56+
Iterable<Integer> pathTo = p.pathTo(0);
57+
System.out.print(6 + " ");
58+
for(Integer w : pathTo) System.out.print(w + " ");
59+
System.out.println();
60+
// System.out.println(p.hasPathTo(10));
61+
}
5362
}

Diff for: DataStructrue/Graph/BreadFirstPathDirectedGraph.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ca.mcmaster.chapter.four.graph.directed;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.Stack;
7+
import java.util.concurrent.LinkedBlockingQueue;
8+
9+
import ca.mcmaster.chapter.four.graph.Path;
10+
11+
public class BreadFirstPathDirectedGraph implements Path {
12+
private final Digraph g;
13+
private int s;
14+
private final boolean[] marked;
15+
private final int[] edgeTo;
16+
public BreadFirstPathDirectedGraph(Digraph g, int s) {
17+
this.g = g;
18+
this.s = s;
19+
marked = new boolean[g.V()];
20+
edgeTo = new int[g.V()];
21+
bfs(g, s);
22+
}
23+
@Override
24+
public boolean hasPathTo(int v) {
25+
return marked[v];
26+
}
27+
28+
@Override
29+
public Iterable<Integer> pathTo(int v) {
30+
Stack<Integer> path = new Stack<>();
31+
path.push(v);
32+
while(s != edgeTo[v]){
33+
path.push(edgeTo[v]);
34+
v= edgeTo[v];
35+
}
36+
return path;
37+
}
38+
private void bfs(Digraph g, int s){
39+
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>();
40+
marked[s] = true;
41+
q.offer(s);
42+
while(!q.isEmpty()){
43+
int v = q.poll();
44+
for(int w : g.adj(v)){
45+
if(!marked[w]){
46+
edgeTo[w] = v;
47+
marked[w] = true;
48+
q.offer(w);
49+
}
50+
}
51+
}
52+
}
53+
public static void main(String[] args) throws FileNotFoundException {
54+
DigraphImpl g = new DigraphImpl(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt")));
55+
DepthFirstPathDirectedGraph p = new DepthFirstPathDirectedGraph(g, 6);
56+
Iterable<Integer> pathTo = p.pathTo(4);
57+
System.out.print(6 + " ");
58+
for(Integer w : pathTo) System.out.print(w + " ");
59+
System.out.println();
60+
}
61+
}

Diff for: DataStructrue/Graph/DepthFirstPathDirectedGraph.java

+9
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ private void dfs(DigraphImpl g, int v){
5050
}
5151
}
5252
}
53+
public static void main(String[] args) throws FileNotFoundException {
54+
DigraphImpl g = new DigraphImpl(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt")));
55+
DepthFirstPathDirectedGraph p = new DepthFirstPathDirectedGraph(g, 6);
56+
Iterable<Integer> pathTo = p.pathTo(0);
57+
System.out.print(6 + " ");
58+
for(Integer w : pathTo) System.out.print(w + " ");
59+
System.out.println();
60+
// System.out.println(p.hasPathTo(10));
61+
}
5362
}

Diff for: DataStructrue/Graph/DirectedGraph.md

+63
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,66 @@ public class DepthFirstPathDirectedGraph implements Path {
224224
}
225225
}
226226
```
227+
228+
* 测试
229+
```Java
230+
public static void main(String[] args) throws FileNotFoundException {
231+
DigraphImpl g = new DigraphImpl(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyDG.txt")));
232+
DepthFirstPathDirectedGraph p = new DepthFirstPathDirectedGraph(g, 6);
233+
Iterable<Integer> pathTo = p.pathTo(0);
234+
System.out.print(6 + " ");
235+
for(Integer w : pathTo) System.out.print(w + " ");
236+
System.out.println();
237+
// System.out.println(p.hasPathTo(10));
238+
}
239+
```
240+
>6 9 11 4 3 2 0
241+
242+
>通过BFPath实现
243+
```Java
244+
public class BreadFirstPathDirectedGraph implements Path {
245+
private final Digraph g;
246+
private int s;
247+
private final boolean[] marked;
248+
private final int[] edgeTo;
249+
public BreadFirstPathDirectedGraph(Digraph g, int s) {
250+
this.g = g;
251+
this.s = s;
252+
marked = new boolean[g.V()];
253+
edgeTo = new int[g.V()];
254+
bfs(g, s);
255+
}
256+
@Override
257+
public boolean hasPathTo(int v) {
258+
return marked[v];
259+
}
260+
261+
@Override
262+
public Iterable<Integer> pathTo(int v) {
263+
Stack<Integer> path = new Stack<>();
264+
path.push(v);
265+
while(s != edgeTo[v]){
266+
path.push(edgeTo[v]);
267+
v= edgeTo[v];
268+
}
269+
return path;
270+
}
271+
private void bfs(Digraph g, int s){
272+
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>();
273+
marked[s] = true;
274+
q.offer(s);
275+
while(!q.isEmpty()){
276+
int v = q.poll();
277+
for(int w : g.adj(v)){
278+
if(!marked[w]){
279+
edgeTo[w] = v;
280+
marked[w] = true;
281+
q.offer(w);
282+
}
283+
}
284+
}
285+
}
286+
}
287+
```
288+
289+
* 测试和DFS完全一致。

0 commit comments

Comments
 (0)