DFS(to visit a vertex v)
{
Mark v as visited.
Recursively visit all unmarked vertices w pointing from v.
}
public class DirectedDFS
{
private boolean[] marked;
public DirectedDFS(Digraph G, int s)
{
marked = new boolean[G.V()];
dfs(G, s);
}
private void dfs(Digraph G, int v)
{
marked[v] = true;
for(int w: G.adj(v))
{
if(!marked[w])
dfs(G, w);
}
}
}
Basis:
Find all vertices reachable from s along a directed path.
Application:
public boolean visited(int v)
{
return marked[v];
}
NONE…
public class DepthFirstOrder
{
private boolean[] marked;
private Stack<Integer> reversePost;
public DepthFirstOrder(Digraph G)
{
reversePost = new Stack<Integer>();
marked = new boolean[G.V()];
for(int v = 0; v < G.V(); v++)
{
if (!marked[v])
dfs(G, v);
}
}
private void dfs(Digraph G, int v)
{
marked[v] = true;
for(int w: G.adj(v))
{
if(!marked[w])
dfs(G, w);
}
reversePost.push(v);
}
public Iterable<Integer> reversePost()
{
return reversePost;
}
}
A digraph has a topological order iff no directed cycle.
application:
Def: Vertices v and w are strongly connected if there is both a directed path from v to w and a directed path from w to v. A strong component is a maximal subset of strongly-connected vertices.
Application:
public class KosarajuSharirSCC
{
private boolean marked[];
private int id[];
private int count;
public KosarajuSharirSCC(Digraph G)
{
marked = new boolean[G.V()];
id = new int[G.V()];
DepthFirstOrder dfs = new DepthFirstOrder(G.reverse());
for (int v: dfs.reversePost())
{
if(!marked[v])
{
dfs(G, v);
count++;
}
}
}
private void dfs(Digraph G, int v)
{
marked[v] = true;
id[v] = count;
for(int w: G.adj(v))
{
if(!marked[w])
{
dfs(G, w);
}
}
}
public boolean stronglyConnected(int v, int w)
{
return id[v] == id[w];
}
}