I have a set of paths that join nodes and need to be able to enter two nodes to see if there are any viable paths (and count, if more than one). On top of this, I need to track the route. A simplified example can be found in this image below.
The image is essentially this array:
mappings = [
[1,3],
[3,6],
[6,11],
[11,12],
[3,7],
[7,10],
[2,4],
[4,5],
[4,8],
[5,9],
[9,10]
];
I currently have a demo where I can enter 2 and 10, and it will tell me the correct path (2 > 4 > 5 > 9 > 10). However, entering 1 and 10 doesn't produce a path, because my rudimentary algorithm isn't smart enough. Any tips on modifying mine, or an existing algorithm I can use to tackle this problem?
Ideally, I would have the following input/output:
Input: 1,10
Output: 1 > 3 > 7 > 10
Input: 1,11
Output: 1 > 3 > 6 > 11
Basically you can use BFS, setting one of the input nodes as the root of the algorithm and observe if the second one was visited after the execution. Implementation is quite straightforward.
On the other hand, finding all possible paths in graph which may contain cycles is quite hard (NP-hard). The number of possible paths is exponential. Even worse, if graph is not Directed Acyclic graph (and contain cycles) there may be infinite number of paths between two nodes.