I have created an undirected graph with 6 vertices, visually represented like this: https://i.stack.imgur.com/EtIbO.png
I would like to write a script that can find all paths from a starting point without revisiting the same node, with no given end point.
Every example of the BFS, DFS, A* algorithm I have looked at require an end destination node. However, on a larger graph it may be NP-hard to find all possible pathways from point A to point Z. For this reason, I want to find all paths to all destinations that are achievable within a set number of moves (on this graph for example -- 3 moves == 4 max vertices in path)
I coded the graph using PHP arrays with each key being a vertice and its array containing the adjacent points:
<?php
$graph[1] = array(2,6);
$graph[2] = array(1,4);
$graph[3] = array(4,5);
$graph[4] = array(2,3,6);
$graph[5] = array(6,3);
$graph[6] = array(1,5,4);
I don't know of an algorithm though, that performs a path search in this manner. My desired output would be something like this:
Path 1: 1,2
Path 2: 1,2,4
Path 3: 1,2,4,3
Path 4: 1,6
Path 5: 1,6,4,3
Path 6: 1,6,5
Path 7: 1,6,5,3
I have no problem writing the required code, but the necessary steps for the algorithm/function (assuming tree traversal recursion?) are difficult to understand.
Question: What approach/algorithm should be used to do this, and do you have an example (or at least pseudocode) that shows how it works given the graph input array?