I have a graph which consists of nodes and I need a fast algorithm that generates a random path between two nodes. I designed several algorithms from scratch for this but can’t seem to get it right.
Either the algorithm gets stuck in loops, or when I keep record of the visited nodes it sometimes gets stuck between visited nodes. Another problem I encountered is that my algorithm was too unstable in performance.
So my question is; does anyone know a fast and stable algorithm for a random path between two reachable nodes in an undirected graph?
Let your graph be
G=(V,E). Create a subsetUofVsuch thatU = { u | there is a path from u to the target }.Uis easy – and can be done in lineartime using DFS or BFS on the reversed edges from the
target.
Using this subset, create a graph
G'=(U,E')whereUis defined above andE' = E [intersection] UxU[The same edges, but applied only on vertices inU].Run randomized (choosing which vertex to explore next on random) DFS on
G'until you reach the target.visitedset.