Skip to content

Commit 303d669

Browse files
committed
[Function add]
1.Add Breadth First Path. 2. Add Iterable interface to Stack class.
1 parent 1e821e5 commit 303d669

File tree

6 files changed

+205
-3
lines changed

6 files changed

+205
-3
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.concurrent.LinkedBlockingQueue;
7+
8+
import ca.mcmaster.chapter.one.stack.ListStack;
9+
import ca.mcmaster.chapter.one.stack.MyStack;
10+
11+
public class BreadthFirstPath extends AbstractPath{
12+
private final boolean[] marked;
13+
private final int[] edgeTo;
14+
public BreadthFirstPath(Graph g, int s) {
15+
super(g, s);
16+
marked = new boolean[g.V()];
17+
edgeTo = new int[g.V()];
18+
bfs(g, s);
19+
}
20+
21+
@Override
22+
public boolean hasPathTo(int v) {
23+
return marked[v];
24+
}
25+
26+
@Override
27+
public Iterable<Integer> pathTo(int v) {
28+
MyStack<Integer> stack = new ListStack<>();
29+
do {
30+
stack.push(v);
31+
v = edgeTo[v];
32+
} while (v != s);
33+
return stack;
34+
}
35+
36+
private void bfs(Graph g, int s){
37+
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<Integer>();
38+
marked[s] = true;
39+
q.add(s);
40+
while(!q.isEmpty()){
41+
Integer v = q.poll();
42+
for(int w : g.adj(v)){
43+
if(!marked[w]){ //Current vertex is not accessed.
44+
q.add(w);
45+
edgeTo[w] = v;
46+
marked[w] = true;
47+
}
48+
}
49+
}
50+
}
51+
public static void main(String[] args) throws FileNotFoundException {
52+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyCG.txt")));
53+
Path p = new BreadthFirstPath(g, 0);
54+
Iterable<Integer> path = p.pathTo(4);
55+
StringBuilder sb = new StringBuilder("0");
56+
for(Integer pnode : path){
57+
sb.append("->" + pnode);
58+
}
59+
System.out.println(sb.toString());
60+
}
61+
}

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/stack/ListStack.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ca.mcmaster.chapter.one.stack;
22

3+
import java.util.Iterator;
4+
35
public class ListStack<T> implements MyStack<T>{
46
private Node first;
57
private Integer size = 0;
@@ -35,4 +37,21 @@ public static void main(String[] args){
3537
}
3638
System.out.println(stack.isEmpty());
3739
}
40+
@Override
41+
public Iterator<T> iterator() {
42+
return new Iterator<T>() {
43+
private Node current = first;
44+
@Override
45+
public boolean hasNext() { return current != null; }
46+
@Override
47+
public T next() {
48+
if(!hasNext()){ return null; }
49+
else{
50+
Node res = current;
51+
current = current.next;
52+
return res.t;
53+
}
54+
}
55+
};
56+
}
3857
}

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/one/stack/MyStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ca.mcmaster.chapter.one.stack;
22

3-
public interface MyStack<T>{
3+
public interface MyStack<T> extends Iterable<T>{
44
void push(T t);
55
T pop();
66
Boolean isEmpty();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.util.concurrent.LinkedBlockingQueue;
7+
8+
import ca.mcmaster.chapter.one.stack.ListStack;
9+
import ca.mcmaster.chapter.one.stack.MyStack;
10+
11+
public class BreadthFirstPath extends AbstractPath{
12+
private final boolean[] marked;
13+
private final int[] edgeTo;
14+
public BreadthFirstPath(Graph g, int s) {
15+
super(g, s);
16+
marked = new boolean[g.V()];
17+
edgeTo = new int[g.V()];
18+
bfs(g, s);
19+
}
20+
21+
@Override
22+
public boolean hasPathTo(int v) {
23+
return marked[v];
24+
}
25+
26+
@Override
27+
public Iterable<Integer> pathTo(int v) {
28+
MyStack<Integer> stack = new ListStack<>();
29+
do {
30+
stack.push(v);
31+
v = edgeTo[v];
32+
} while (v != s);
33+
return stack;
34+
}
35+
36+
private void bfs(Graph g, int s){
37+
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<Integer>();
38+
marked[s] = true;
39+
q.add(s);
40+
while(!q.isEmpty()){
41+
Integer v = q.poll();
42+
for(int w : g.adj(v)){
43+
if(!marked[w]){ //Current vertex is not accessed.
44+
q.add(w);
45+
edgeTo[w] = v;
46+
marked[w] = true;
47+
}
48+
}
49+
}
50+
}
51+
public static void main(String[] args) throws FileNotFoundException {
52+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyCG.txt")));
53+
Path p = new BreadthFirstPath(g, 0);
54+
Iterable<Integer> path = p.pathTo(4);
55+
StringBuilder sb = new StringBuilder("0");
56+
for(Integer pnode : path){
57+
sb.append("->" + pnode);
58+
}
59+
System.out.println(sb.toString());
60+
}
61+
}

DataStructrue/Graph/UndirectedGraph.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public class UFSearch extends AbstractSearch {
249249
>11 -> 9 12
250250
>12 -> 11 9
251251

252-
### 深度优先查找DFSearch
252+
### 深度优先查找DFSearch, DFPath
253253
```Java
254254
public class DeepFirstSearch extends AbstractSearch {
255255
private boolean[] marked; //A array used to mark if current node is connected to s
@@ -377,3 +377,64 @@ public class DepthFirstPath extends AbstractPath {
377377
}
378378
```
379379
>0->2->1
380+
381+
### 广度优先搜索
382+
* 广度优先用于寻找最短路径。
383+
* 深度优先(DFP)所寻找到的路径是通过递归调用寻找到路径,递归调用的路径返回是根据adjacency table中的链表的显示顺序。所以返回的值不一定是最短的。
384+
* 广度优先查找定义了相邻的所有顶点,再找到相邻两个,以此类推。
385+
```Java
386+
public class BreadthFirstPath extends AbstractPath{
387+
private final boolean[] marked;
388+
private final int[] edgeTo;
389+
public BreadthFirstPath(Graph g, int s) {
390+
super(g, s);
391+
marked = new boolean[g.V()];
392+
edgeTo = new int[g.V()];
393+
bfs(g, s);
394+
}
395+
396+
@Override
397+
public boolean hasPathTo(int v) {
398+
return marked[v];
399+
}
400+
401+
@Override
402+
public Iterable<Integer> pathTo(int v) {
403+
MyStack<Integer> stack = new ListStack<>();
404+
do {
405+
stack.push(v);
406+
v = edgeTo[v];
407+
} while (v != s);
408+
return stack;
409+
}
410+
private void bfs(Graph g, int s){
411+
LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<Integer>();
412+
marked[s] = true;
413+
q.add(s);
414+
while(!q.isEmpty()){
415+
Integer v = q.poll(); //从队列中读取下一个顶点并对其遍历相邻顶点。
416+
for(int w : g.adj(v)){
417+
if(!marked[w]){ //Current vertex is not accessed.
418+
q.add(w); //如果相邻的顶点没有被访问过,就将它加入队列中。
419+
edgeTo[w] = v;
420+
marked[w] = true;
421+
}
422+
}
423+
}
424+
}
425+
}
426+
```
427+
428+
* 测试
429+
```Java
430+
public static void main(String[] args) throws FileNotFoundException {
431+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyCG.txt")));
432+
Path p = new BreadthFirstPath(g, 0);
433+
Iterable<Integer> path = p.pathTo(4);
434+
StringBuilder sb = new StringBuilder("0");
435+
for(Integer pnode : path){
436+
sb.append("->" + pnode);
437+
}
438+
System.out.println(sb.toString());
439+
}
440+
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ I started learning the data struture systematically, I will list my notes in the
1515

1616
### [Queue](https://github.com/Seanforfun/Algorithm/tree/master/DataStructrue/Queue)
1717
* Blocking Queue
18-
1. [Priority Blocking Queue](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Queue/PriorityBlockingQueue.md)
18+
1. [Array Blocking Queue](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Queue/ArrayBlockingQueue.md)
1919
2. [Linked Blocking Queue](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Queue/LinkedBlockingQueue.md)
2020
3. [Priority Blocking Queue](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Queue/PriorityBlockingQueue.md)
2121
4. [Delay Queue](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Queue/DelayQueue.md)

0 commit comments

Comments
 (0)