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
Lines changed: 53 additions & 0 deletions
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/Digraph.java

Lines changed: 1 addition & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/DigraphImpl.java

Lines changed: 1 addition & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/directed/DirectedDFS.java

Lines changed: 1 addition & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/AbstractCC.java

Lines changed: 3 additions & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/AbstractPath.java

Lines changed: 4 additions & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/AbstractSearch.java

Lines changed: 3 additions & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/BreadthFirstPath.java

Lines changed: 3 additions & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/ConnectionComponent.java

Lines changed: 1 addition & 1 deletion
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 renamed to Algorithm(4th_Edition)/src/ca/mcmaster/chapter/four/graph/undirected/DFSCC.java

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)