import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
class DepthFirstPaths {
private boolean[] visited; // visited[v] = true if v connected to s
private int[] edgeTo; // edgeTo[v] = previous vertex on path from s to v
private int s;
public DepthFirstPaths(Digraph G, int s) {
// initialize data structures
this.visited = new boolean[G.V()];
this.edgeTo = new int[G.V()];
this.s = s;
// find vertices connected to s
dfs(G, s);
}
// is there a path from s to v?
public boolean hasPathTo(int v) {
return visited[v];
}
// path from s to v; null if no such path
public Iterable pathTo(int v) {
if (!hasPathTo(v)) {
return null;
}
Stack path = new Stack();
int previousVertices = v;
while(previousVertices != this.s) {
path.push(previousVertices);
previousVertices = edgeTo[previousVertices];
}
path.push(s);
return path;
}
private void dfs(Digraph G, int v) {
// recursive DFS does the work
System.out.print(v + "->");
visited[v] = true;
for (int w : G.adj(v))
if(!visited[w]) {
dfs(G, w);
edgeTo[w] = v;
}
}
private static Digraph prepareGraph() {
int[][] edges = new int[][] {
{0, 5}, {0, 1},
{2, 0}, {2, 3},
{3, 5}, {3, 2},
{4, 3}, {4, 2},
{5, 4},
{6, 9}, {6, 4}, {6, 8}, {6, 0},
{7, 6}, {7, 9},
{8, 6},
{9, 11}, {9, 10},
{10, 12},
{11, 4}, {11, 12},
{12, 9}
};
Digraph G = new Digraph(13);
// add all edges
for(int i = 0; i < edges.length; i++) {
int v = edges[i][0];
int w = edges[i][1];
G.addEdge(v, w);
}
return G;
}
static class Digraph {
private final int V; // number of vertices
private final List[] adj; // adjacency lists... 0 to V-1
/** create an empty digraph with V vertices */
public Digraph(int V) {
this.V = V;
this.adj = new ArrayList[V]; // create empty graph with V vertices
for(int i = 0; i < V; i++) {
adj[i] = new ArrayList<>();
}
}
/** add a directed edge v→w */
public void addEdge(int v, int w) {
// add edge v-w
adj[v].add(w);
}
/** Iterator for vertices pointing from v */
public Iterable adj(int v) {
return adj[v];
}
/** number of vertices */
public int V() {
return V;
}
/** number of edges */
public int E() {
int countEdges = 0;
for(int v = 0; v < V; v++) {
countEdges += adj[v].size();
}
return countEdges;
}
/** reverse of this digraph */
Digraph reverse() {
return null;// TODO
}
/** string representation */
public String toString() {
return "TODO";
}
}
public static void main (String[] args) {
System.out.println("Preparing graph ");
Digraph G = prepareGraph();
System.out.println("V: " + G.V());
System.out.println("E: " + G.E());
int s = 0;
System.out.println("All connected vertices to " + s + " are: ");
DepthFirstPaths depthFirstPaths = new DepthFirstPaths(G, s);
System.out.println("\nPath to 4 is: ");
for(Integer i : depthFirstPaths.pathTo(4)) {
System.out.print(i + "->");
}
s = 7;
System.out.println("\nAll connected vertices to " + s + " are: ");
new DepthFirstPaths(G, s);
s = 9;
System.out.println("\nAll connected vertices to " + s + " are: ");
new DepthFirstPaths(G, s);
}
}
Preparing graph
V: 13
E: 22
All connected vertices to 0 are:
0->5->4->3->2->1->
Path to 4 is:
4->5->0->
All connected vertices to 7 are:
7->6->9->11->4->3->5->2->0->1->12->10->8->
All connected vertices to 9 are:
9->11->4->3->5->2->0->1->12->10->
Comments
Post a Comment