graph

Finding all nodes reachable from a particular node.

def explore(G,v)
"""Input:G=(V,E) is a graph;v belongs to V"""
"""Output: visited(u) is set to true for all nodes u reachable from v"""

visited(v) = true
previsited(v)
for each edge (v,u) in E:
if not visited(u): explore(u)
postvisited(v)

 

DFS:

def dfs(G):
for v in V:
visited(v) = false

for v in V:
if not visited(v): explore(v)

Time: O(|V|+|E|)

 

Connectivity:

  • property: for any nodes u and v, the two intervals [pre[u],post[u]] and [pre[v],post[v]] are either disjoint or one is contained within the other.

cause [pre[u],post[u]] is essentially the time during which the vertex u was on the stack.

The last-in, first-out behaviour of a stack explains it.

def previsited(v):
pre[v]=clock
clock=clock+1


def postvisited(v):
post[v]=clock
clock=clock+1


types of edges

forward edges (or "discovery edges"), which point from a node of the tree to one of its descendants,(not previsited)

back edges, which point from a node to one of its ancestors,(previsited but not postvisited)

and cross edges, which do neither.(that is, already postvisited)

pre/post ordering for (u,v) Edge type
[u[v]v]u forward
[v[u]u]v back
[u]u,[v]v cross

property:

  • a directed graph has a cycle if and only if its depth-first search reveals a back edge
  • In a dag, every edge leads to a vertex with a lower post number
  Since a dag is linearized by decreasing post numbers, the vertex with the smallest post number comes
last in this linearization, and it must be a sink--no outgoing edges. Symmetrically, the one with the highest post
is a source, a node with no incoming edges.
  • Every dag has at least one source and at least one sink

The decomposition of a directed graph into its strongly connected components is very informative and useful.

It turns out, fortunately, that it can be found in linear time by making further use of depth-first search.

property:

  • If the explore subroutine is started at node u, then it will terminate precisely when all nodes reachable from u have been visited.

if we call explore on a node that lies somewhere in a sink strongly connected component, then we will retrieve exactly that component.

So, the solution is clear,

Property:

  • The node that receives the highest post number in a depth-first search must lie in a source strongly connected component.
  • C AND CT have the same connectivity.

 



posted on 2012-02-27 15:46  grep  阅读(364)  评论(0编辑  收藏  举报