2020年12月22日星期二

Directed cyclic graph traversal to find a path of certain length

I have a directed graph that looks sort of like this. All edges are unidirectional, cycles exist, and some nodes have no children.

I want a traversal algorithm where the goal is to find a path of length n nodes anywhere in the graph. The algorithm should do the following:

  • Starting node is randomly chosen, it traverses its children, and visited nodes is kept somewhere to return a path at the end
  • Same nodes can be visited again
  • If it reaches a node with no children, it traverses the most recent node with unexplored children. If all possible paths from the starting node are traversed, try starting from other nodes. (I think this method ensures all possible paths are explored)
  • Traversal stops when number of nodes visited reaches n and path is returned
  • If no path of length n is found, it returns "No valid path"

I'm not sure if an algorithm for this already exists. Most search algorithms seem to deal with finding shortest path, MSTs, and can't visit same nodes. Pathfinding algorithms like A* and Dijkstra's seem overcomplicated for my needs. I might need a modified version of one of these, but not sure exactly which one to use.

https://stackoverflow.com/questions/65417748/directed-cyclic-graph-traversal-to-find-a-path-of-certain-length December 23, 2020 at 08:52AM

没有评论:

发表评论