Skip to content

Commit 93793f9

Browse files
committed
[Function add]
1.Add DFP
1 parent c1758bc commit 93793f9

19 files changed

+192
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
7+
import ca.mcmaster.chapter.four.graph.Path;
8+
import ca.mcmaster.chapter.one.bag.Bag;
9+
import ca.mcmaster.chapter.one.bag.ListBag;
10+
11+
public class DepthFirstPathDirectedGraph implements Path {
12+
private final DigraphImpl g;
13+
private int s;
14+
private final boolean[] marked; //Used to mark if current vertex has been accessed.
15+
private final int[] edgeTo; //Used to save the vertex ahead of current vertex.
16+
public DepthFirstPathDirectedGraph(DigraphImpl g, int s){
17+
this.s = s;
18+
this.g = (DigraphImpl) g;
19+
marked = new boolean[g.V()];
20+
edgeTo = new int[g.V()];
21+
dfs(g, s);
22+
}
23+
public DepthFirstPathDirectedGraph(String file, int s) throws FileNotFoundException{
24+
g = new DigraphImpl(new FileInputStream(new File(file)));
25+
this.s = s;
26+
marked = new boolean[g.V()];
27+
edgeTo = new int[g.V()];
28+
dfs(g, s);
29+
}
30+
@Override
31+
public boolean hasPathTo(int v) {
32+
return edgeTo[v] != 0;
33+
}
34+
@Override
35+
public Iterable<Integer> pathTo(int v) {
36+
Bag<Integer> path = new ListBag<Integer>();
37+
path.add(v);
38+
while(edgeTo[v] != s){
39+
path.add(edgeTo[v]);
40+
v = edgeTo[v];
41+
}
42+
return path;
43+
}
44+
private void dfs(DigraphImpl g, int v){
45+
marked[v] = true;
46+
for(int w : g.adj(v)){
47+
if(!marked[w]){
48+
edgeTo[w] = v;
49+
dfs(g, w);
50+
}
51+
}
52+
}
53+
}

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/Digraph.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/Digraph.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.directed;
22

33
public interface Digraph {
44
/**

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/DigraphImpl.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/DigraphImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.directed;
22

33
import java.io.File;
44
import java.io.FileInputStream;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/DirectedDFS.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/DirectedDFS.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.directed;
22

33
import java.io.File;
44
import java.io.FileInputStream;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/AbstractCC.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/AbstractCC.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
2+
3+
import ca.mcmaster.chapter.four.graph.Graph;
24

35
public abstract class AbstractCC implements ConnectionComponent {
46
protected final Graph g;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/AbstractPath.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/AbstractPath.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
2+
3+
import ca.mcmaster.chapter.four.graph.Graph;
4+
import ca.mcmaster.chapter.four.graph.Path;
25

36
public abstract class AbstractPath implements Path {
47
protected final Graph g;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/AbstractSearch.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/AbstractSearch.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
2+
3+
import ca.mcmaster.chapter.four.graph.Graph;
24

35
public abstract class AbstractSearch implements Search {
46
protected final Graph g;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/BreadthFirstPath.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/BreadthFirstPath.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileNotFoundException;
66
import java.util.concurrent.LinkedBlockingQueue;
77

8+
import ca.mcmaster.chapter.four.graph.Graph;
9+
import ca.mcmaster.chapter.four.graph.Path;
810
import ca.mcmaster.chapter.one.stack.ListStack;
911
import ca.mcmaster.chapter.one.stack.MyStack;
1012

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/ConnectionComponent.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/ConnectionComponent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
public interface ConnectionComponent {
44
/**

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/DFSCC.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/DFSCC.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileNotFoundException;
66

7+
import ca.mcmaster.chapter.four.graph.Graph;
78
import ca.mcmaster.chapter.one.bag.Bag;
89
import ca.mcmaster.chapter.one.bag.ListBag;
910

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/DeepFirstSearch.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/DeepFirstSearch.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileNotFoundException;
66

7+
import ca.mcmaster.chapter.four.graph.Graph;
8+
79
public class DeepFirstSearch extends AbstractSearch {
810
private boolean[] marked;
911
private int count;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/DepthFirstPath.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/DepthFirstPath.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileNotFoundException;
66

7+
import ca.mcmaster.chapter.four.graph.Graph;
8+
import ca.mcmaster.chapter.four.graph.Path;
79
import ca.mcmaster.chapter.one.stack.ListStack;
810
import ca.mcmaster.chapter.one.stack.MyStack;
911

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/Search.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/Search.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
public interface Search {
44
/**

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/SymbolGraph.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/SymbolGraph.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
2+
3+
import ca.mcmaster.chapter.four.graph.Graph;
24

35
public interface SymbolGraph {
46
/**

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/SymbolGraphImpl.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/SymbolGraphImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.File;
44
import java.io.FileNotFoundException;
55
import java.util.HashMap;
66
import java.util.Map;
77
import java.util.Scanner;
88

9+
import ca.mcmaster.chapter.four.graph.Graph;
10+
911
public class SymbolGraphImpl implements SymbolGraph {
1012
private final Map<String, Integer> st;
1113
private String[] keys;

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/UFSearch.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/UFSearch.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.File;
44
import java.io.FileInputStream;
55
import java.io.FileNotFoundException;
66

7+
import ca.mcmaster.chapter.four.graph.Graph;
8+
79
public class UFSearch extends AbstractSearch {
810
private final UF uf;
911
public UFSearch(Graph g, int s) {

Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/UndirectedGraph.java Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/UndirectedGraph.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package ca.mcmaster.chapter.four.graph;
1+
package ca.mcmaster.chapter.four.graph.undirected;
22

33
import java.io.FileInputStream;
44
import java.util.Scanner;
55

6+
import ca.mcmaster.chapter.four.graph.Graph;
67
import ca.mcmaster.chapter.one.bag.Bag;
78
import ca.mcmaster.chapter.one.bag.ListBag;
89

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
7+
import ca.mcmaster.chapter.four.graph.Path;
8+
import ca.mcmaster.chapter.one.bag.Bag;
9+
import ca.mcmaster.chapter.one.bag.ListBag;
10+
11+
public class DepthFirstPathDirectedGraph implements Path {
12+
private final DigraphImpl g;
13+
private int s;
14+
private final boolean[] marked; //Used to mark if current vertex has been accessed.
15+
private final int[] edgeTo; //Used to save the vertex ahead of current vertex.
16+
public DepthFirstPathDirectedGraph(DigraphImpl g, int s){
17+
this.s = s;
18+
this.g = (DigraphImpl) g;
19+
marked = new boolean[g.V()];
20+
edgeTo = new int[g.V()];
21+
dfs(g, s);
22+
}
23+
public DepthFirstPathDirectedGraph(String file, int s) throws FileNotFoundException{
24+
g = new DigraphImpl(new FileInputStream(new File(file)));
25+
this.s = s;
26+
marked = new boolean[g.V()];
27+
edgeTo = new int[g.V()];
28+
dfs(g, s);
29+
}
30+
@Override
31+
public boolean hasPathTo(int v) {
32+
return edgeTo[v] != 0;
33+
}
34+
@Override
35+
public Iterable<Integer> pathTo(int v) {
36+
Bag<Integer> path = new ListBag<Integer>();
37+
path.add(v);
38+
while(edgeTo[v] != s){
39+
path.add(edgeTo[v]);
40+
v = edgeTo[v];
41+
}
42+
return path;
43+
}
44+
private void dfs(DigraphImpl g, int v){
45+
marked[v] = true;
46+
for(int w : g.adj(v)){
47+
if(!marked[w]){
48+
edgeTo[w] = v;
49+
dfs(g, w);
50+
}
51+
}
52+
}
53+
}

DataStructrue/Graph/DirectedGraph.md

+49
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public class DigraphImpl implements Digraph {
136136
>12: 9|
137137
138138
### 有向图的可达性
139+
>可达性的重要实际应用是在内存管理系统中,一个顶点表示一个对象,一条边表示一个对象的引用。
139140
```Java
140141
public class DirectedDFS {
141142
private final boolean[] marked;
@@ -175,3 +176,51 @@ public static void main(String[] args) throws FileNotFoundException {
175176
}
176177
```
177178
> 0 1 2 3 4 5 6 8 9 10 11 12
179+
180+
### 单点有向路径
181+
>通过DFPath实现
182+
```Java
183+
public class DepthFirstPathDirectedGraph implements Path {
184+
private final DigraphImpl g;
185+
private int s;
186+
private final boolean[] marked; //Used to mark if current vertex has been accessed.
187+
private final int[] edgeTo; //Used to save the vertex ahead of current vertex.
188+
public DepthFirstPathDirectedGraph(DigraphImpl g, int s){
189+
this.s = s;
190+
this.g = (DigraphImpl) g;
191+
marked = new boolean[g.V()];
192+
edgeTo = new int[g.V()];
193+
dfs(g, s);
194+
}
195+
public DepthFirstPathDirectedGraph(String file, int s) throws FileNotFoundException{
196+
g = new DigraphImpl(new FileInputStream(new File(file)));
197+
this.s = s;
198+
marked = new boolean[g.V()];
199+
edgeTo = new int[g.V()];
200+
dfs(g, s);
201+
}
202+
@Override
203+
public boolean hasPathTo(int v) {
204+
return edgeTo[v] != 0;
205+
}
206+
@Override
207+
public Iterable<Integer> pathTo(int v) {
208+
Bag<Integer> path = new ListBag<Integer>();
209+
path.add(v);
210+
while(edgeTo[v] != s){
211+
path.add(edgeTo[v]);
212+
v = edgeTo[v];
213+
}
214+
return path;
215+
}
216+
private void dfs(DigraphImpl g, int v){
217+
marked[v] = true;
218+
for(int w : g.adj(v)){
219+
if(!marked[w]){
220+
edgeTo[w] = v;
221+
dfs(g, w);
222+
}
223+
}
224+
}
225+
}
226+
```

0 commit comments

Comments
 (0)