How to find a path in graph with maximum edges?

In a weighted undirected graph, how do I find a fixed length path between two nodes?

  • I would like an algorithm or pointers to further research on how to find a fixed length path between two nodes in a weighted undirected graph.

  • Answer:

    You can not get optimum solutionIn in polynomial time. In general this problem is NP-hard. If you can find a path in fixed length n, by setting n to a large number, it's reducible to finding the longest path, which is a well known NP problem. But if the graph is directed acyclic, you can solve it in linear time.

Yunfei Hou at Quora Visit the source

Was this solution helpful to you?

Other answers

You can always go with a Breadth First Search: http://en.m.wikipedia.org/wiki/Breadth-first%5Fsearch Where it's normally used to find the shortest path, you should be able to adapt it to find a path of desired length n. In doing so, you might even be able to achieve a couple of optimizations by 'short-circuiting' esp. when dealing with weighted edges or nodes—just don't visit any further paths where adding the weight of the edge or node would exceed your target.

Alistair Israel

If your desired length is small, then the color-coding method is pretty reasonable. See http://en.wikipedia.org/wiki/Color-coding. If k is the length of your desired path, the runtime of the algorithm is [math]O(k^k n)[/math]. The algorithm consists of many iterations where you randomly color all of the vertices with [math]k[/math] colors independently with colors 1, 2, ..., k. In the resulting colored graph, you then look for a path of length [math]k[/math] from your starting vertex [math]u[/math] to your terminal vertex [math]v[/math] that contains vertices colored with 1, 2, 3, ..., k. This can be found using a BFS that, when at a vertex with color i, only proceeds to vertices with color i + 1. With probability 1/k^k, a path of length k, if it exists, will be colored with colors 1, 2, ..., k in order. Therefore, you need around k^k iterations to discover the path with high probability. If your graph has many paths of this length between u and v, it will need fewer iterations to find the path.

Aaron Schild

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.