find_paths [s, t, d, k]
This question is a little old now ... but I will throw my hat in the ring.
I personally found the form algorithm find_paths[s, t, d, k] useful, where:
- s is the starting node
- t - target node
- d - maximum depth to search
- k - the number of paths to search
Using your form of the infinity programming language for d and k will give you all the ways.
§ Obviously, if you use a directed graph and want all the non-oriented paths between s and t , you will have to run these two methods:
find_paths[s, t, d, k] <join> find_paths[t, s, d, k]
Helper function
I personally like recursion, although it can be complicated several times, in any case, it allows us to define our helper function first:
def find_paths_recursion(graph, current, goal, current_depth, max_depth, num_paths, current_path, paths_found) current_path.append(current) if current_depth > max_depth: return if current == goal: if len(paths_found) <= number_of_paths_to_find: paths_found.append(copy(current_path)) current_path.pop() return else: for successor in graph[current]: self.find_paths_recursion(graph, successor, goal, current_depth + 1, max_depth, num_paths, current_path, paths_found) current_path.pop()
Main function
From this point of view, the main function is trivial:
def find_paths[s, t, d, k]: paths_found = []
First, pay attention to a few things:
- the above pseudocode is a markup of languages, but most closely resembles python (since I just encoded in it). Strict copy-paste will not work.
[] - uninitialized list, replace it with the equivalent for your chosen programming language.paths_found is passed by reference . It is clear that the recursion function does not return anything. Handle it accordingly.- here
graph takes some kind of hashed structure. There are many ways to implement a schedule. In any case, the graph[vertex] gets a list of adjacent vertices in the oriented graph - adjust accordingly. - it is assumed that you have pre-processed to remove self-loops, loops and polyhedra