How can I find a path of a given length between two nodes in a weighted (undirected) graph?
-
Given a graph, how can I find a path of length X between two nodes in the graph. The path should ideally visit an edge no more than once.
-
Answer:
You can use a customized version of http://en.wikipedia.org/wiki/Uniform-cost_search. Use http://en.wikipedia.org/wiki/Uniform-cost_search but do not keep track of visited nodes, and on each node keep track of all of the possible costs to that node. You then have to backtrack from a node, when either your computed cost for the node is more that the given wanted length, or if the computed length is already existing on the node. For a better performance you can use http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm (single-source shortest-path) from the end node so you can change the first backtracking condition to when the cost plus the shortest path to the end node becomes more that given wanted length.
Fardad Jalili at Quora Visit the source
Other answers
Consider any node that is not the root: its possible distances from the root are all possible distances of its neighbors plus the weight of the connecting edges. If you think carefully, it's easy to see that there can be many graphs such that the number of paths to nodes in layers that are further away from the source can explode exponentially, making the problem intractable if the target is far away. A guarantee of acyclicity in the undirected case obviously avoids this problem because there can only be one (simple) source to target path. A DAG is trickier because you can have multiple source to target paths and paths can converge/diverge. The general directed problem (even cyclic) can in fact be approached using maximum flow to generate a set of edge-disjoint paths in the graph, but this is only a subset of all possible paths and the full problem is still, ultimately, intractable. You can probably prove this more formally with a reduction from a known NP complete problem.
Sanjay Paul
It looks like NP problem cause graph could contain loops through which you can travel as long as needed to achive desired path length. In case of loops http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm does not fit for this task. Maybe you can use http://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm first to find min distances between all nodes in a graph. So having such information you can try to find desired path using brute force search. But again this approach does not take loops into account.
Vjacheslav Murashkin
Related Q & A:
- How can I find a Yahoo user ID if I only have the name?Best solution by Yahoo! Answers
- How can I find a better job while I am currently working?
- How can I find a job in a bar/cafe/restaurant in London?Best solution by Yahoo! Answers
- How can I find a person in Prague if I know only his name, citizenship (USA) and business?Best solution by Yahoo! Answers
- How can i find a job that i want in the future?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.