简体   繁体   中英

How do you find the path that traverses the largest number of nodes in an undirected graph?

Given a an undirected graph and two arbitrary nodes (A and B) in the graph, how do I find the path that passes through the most number of unique nodes in order to navigate between nodes A and B?

I know that you can just depth search it and compare all the lengths, but is there a better way?

That's an NP complete problem . All you can really do is try every possibility.

This problem makes only sense if we are talking about acyclic graphs, so I assume you you mean that.

You will have to brute-force-try all possible paths.

To see why, imagine a graph in which you know the longest path of the two node and you add one node. You now have to test every path that contains the new node, including the ones that you already tested, if the node somehow connect to them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM