Skip to content

Commit 4a1ecfe

Browse files
committed
[Function add]
1.Add DFS for graph search.
1 parent 9de9298 commit 4a1ecfe

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed
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+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ default void display(){
8686
for(int v = 0; v < vertexNum; v++){
8787
StringBuilder sb = new StringBuilder(v + " -> ");
8888
for(int w : adj(v))
89-
sb.append(w + "");
89+
sb.append(w + " ");
9090
System.out.println(sb.toString());
9191
}
9292
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public int count() {
2828
return uf.size[super.s];
2929
}
3030
private final class UF{
31+
@SuppressWarnings("unused")
3132
private final int N;
3233
private final int[] a;
3334
private final int[] size;
@@ -61,7 +62,8 @@ public boolean connected(int p, int q){
6162

6263
public static void main(String[] args) throws FileNotFoundException {
6364
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyG.txt")));
64-
Search search = new UFSearch(g, 9);
65-
System.out.println(search.mark(4));
65+
Search search = new UFSearch(g, 3);
66+
System.out.println(search.mark(7));
67+
g.display();
6668
}
6769
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class UndirectedGraph implements Graph {
1010
private final int V; //vertex
11+
@SuppressWarnings("unused")
1112
private int E; //edge
1213
private Bag<Integer>[] adj; //adjacency table.
1314
@SuppressWarnings("unchecked")

Diff for: DataStructrue/Graph/UndirectedGraph.md

+61
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,65 @@ public class UFSearch extends AbstractSearch {
224224
}
225225

226226
}
227+
```
228+
229+
#### UFSearch测试
230+
```Java
231+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyG.txt")));
232+
Search search = new UFSearch(g, 3);
233+
System.out.println(search.mark(7));
234+
g.display();
235+
```
236+
* 结果
237+
>false
238+
>0 -> 6 2 1 5
239+
>1 -> 0
240+
>2 -> 0
241+
>3 -> 5 4
242+
>4 -> 5 6 3
243+
>5 -> 3 4 0
244+
>6 -> 0 4
245+
>7 -> 8
246+
>8 -> 7
247+
>9 -> 11 10 12
248+
>10 -> 9
249+
>11 -> 9 12
250+
>12 -> 11 9
251+
252+
#### 深度优先查找DFSearch
253+
```Java
254+
public class DeepFirstSearch extends AbstractSearch {
255+
private boolean[] marked; //A array used to mark if current node is connected to s
256+
private int count; //number of vertex connected to s
257+
public DeepFirstSearch(Graph g, int s) {
258+
super(g, s);
259+
marked = new boolean[g.V()];
260+
dfs(g, s);
261+
}
262+
private void dfs(Graph g, int v){
263+
marked[v] = true; //It means current vertex has been accessed.
264+
count++; //update the number of vertex connected to s.
265+
for(int w : g.adj(v))
266+
if(!marked[w]) dfs(g, w); //Check all point connected to v, if not accessed, access recursively.
267+
}
268+
@Override
269+
public boolean mark(int v) {
270+
return marked[v];
271+
}
272+
@Override
273+
public int count() {
274+
return this.count;
275+
}
276+
}
277+
```
278+
279+
#### 测试
280+
```Java
281+
public static void main(String[] args) throws FileNotFoundException {
282+
Graph g = new UndirectedGraph(new FileInputStream(new File("src/ca/mcmaster/chapter/four/graph/tinyG.txt")));
283+
Search search = new DeepFirstSearch(g, 12);
284+
System.out.println(search.mark(9));
285+
System.out.println(search.count());
286+
g.display();
287+
}
227288
```

0 commit comments

Comments
 (0)