Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

algorithm - Finding the longest cycle in a directed graph using DFS

I need to find the longest cycle in a directed graph using DFS.

I once saw this Wikipedia article describing the way of doing this, and I think it approached the problem something like marking the node with one of three states: Node not yet visited, Finished searching the node, and Node visited, but not yet finished visiting.

I would be grateful if anyone could share the link with me. By the way, it isn't Tarjan's Algorithm.

The problem below is what I'm trying to solve, in case you'd like to know.

The two digits given in the first line is N and M, each representing the number of nodes and the number of directed edges.

From the second line is given M sets of two digits A and B, which means that node A and B are connected but you can only traverse the node from A to B.

input.txt:

7 9  
1 2  
2 3  
3 1  
3 4  
4 5  
5 1  
5 6  
6 7  
7 2  

The answer in this case is 6, since 2>3>4>5>6>7>2.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think that longest elementary cycle (or circuit) is better terminology than longest cycle.

Anyway, this pdf may be helpful: Finding All the Elementary Circuits of a Directed Graph

This one year old stackoverflow question has also many links to related problems and algorithms: Finding all cycles in a directed graph


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...