开发者

Design an algorithm to detect cycle in graph G

What would the following algorithm look like:

a linear-time algorithm which, given an undirected graph G, and a particular edge e in it, determines whether G has a cycle containing e

I have following Idea:

for each v that belongs to V, if v is a descendant of e and (e,v) has not been traversed then check following:

if we visited e before v and left v before 开发者_如何学Cwe left e then the graph contains cycle


I am not sure if this is your homework so I'll just give a little hint - use the properties of breadth-first search tree (with root in any of the two vertices of the edge e), its subtrees which are determined by neighbors of the root and the edges between those subtrees.


Per comingstorm's hint, an undirected edge is itself a cycle. A<->B back and forth as many times as you like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜