Finding all possible paths starting between a specific start and end

主宰稳场 提交于 2020-01-17 03:40:52

问题


I want to find all possible paths starting from a specific node and ending at specific node. I tried depth first search in java but it did not work on my case. Because my paths will be on the same direction. I do not want to traverse all other nodes around selected ones.

I couldn't upload the image that shows what I want. Anyway, I'll try to explain. Among nodes

0 1 2 3 4 5 6 7 8 9

The paths I want to find are, for example, will start from 2 to 9. The possible paths produced by the algorithm should be

2-7-9

2-4-6-8-9

2-4-6-9

For node-1 my next possibility would be node-2 only, so I will not try node 0 and and node -3. Because of some special rules I set, only node-2 fits for node-1. Next nodes for node-2, node-4 and node-7 are selected. For node-4, only node-6 is suitable. For node-6, node 8 and node 9 are suitable. On the other hand, for node-7, the next node would be 9 only.

All these paths are created and saved in a hashmap or list structure.

DFS finds paths, for instance, between 0-1 and 1-3 which are unacceptable for me. Since the nature of the algorithm, it finds the shortest path. I want all possiblities according to the rule not the shortest one only. The rule is not the problem, so I do not want make you confused and bored. The general way to solve this problem is important for me.

Thanks in advance


回答1:


You will want to use breadth-first search using a Queue.

Edit: after re-reading your question I think the issue lies in how you're representing a directional graph, rather than in what algorithm you choose. Either DFS or BFS would work for your case, but you need to correctly implement the graph so that the algorithm knows not to try a path in the wrong direction.

DFS finds paths, for instance, between 0-1 and 1-3 which are unacceptable for me. Since the nature of the algorithm, it finds the shortest path. I want all possiblities according to the rule not the shortest one only.

Actually, BFS is typically used to find a shortest path, while both can be used to find all possible paths. I would stick to DFS since it is easier to implement (using recursion rather than a Queue, etc.)




回答2:


If you represent your nodes & rules properly, the algorithm would not be finding paths that aren't legit. Eg, if you want to start with node 2, make that the starting node.

Simplest is to define a class to represent a node. In the class have an array list of all the "child" nodes of this "parent". Also in the node have an integer that is used as a "cursor" for your depth-first walk. (Doesn't hurt to also have another integer/char string that is the node # or other identifier.)

Set the integer "cursor" in all nodes to zero. Pick your start node -- make it the "current node". Call a method on that node to walk it. It picks up the cursor and extracts the corresponding array list element. Then it calls the "walk" method for the node in that array list element. On return the "cursor" is incremented.

The "walk" method returns when it reaches the target node, or when the "cursor" is incremented beyond the size of the child array.

Along the way you keep whatever record of the path you want. One way is to pass an array list of the nodes visited on each "walk" call, adding the current node to the list before passing it along. When the walk gets to the terminal node the array list is copied as one of the answers. On return from "walk" the added element is removed.



来源:https://stackoverflow.com/questions/6889918/finding-all-possible-paths-starting-between-a-specific-start-and-end

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!