数据结构–判断一个图中是否有环
bool VCycle(v)
{
bool flag;
visited(v)=-1;//标记起始点
p=adjacent(v);//即p与v相邻
WHILE (p !=NULL)
DO
{
if( visited(vertex(p))==-1) //又回到了起点,所以,存在环
{
flag=ture;
return flag;
}
if(visited(vertex(p))==0 )//没被访问过
{
flag=VCycle(vertex(p));//判断该点是否有回路
if(flag==ture)
return flag;
}
p=link(p);
}
visited(v)=1;//被访问过
flag=false;//该点不成环
return flag;
}
bool Cycle(G)//判断整个图是否有回路
{
Let v be the first vertex in G;//此处用英文理解吧
WHILE v is existed DO (
IF visited(v) = 1
THEN CONTINUE
;
VCycle(v. flag) ;
IF flag=TRUE
THEN RETURN
;
Reset the status of visited vertices
Let v be the next vertex;)
}