1

I would like to find all the best paths for the problem in this question

The frog's longest Smallest Jump

using non recursive depth first search (dfs). Basically each stone is a vertex of a graph and the goal vertex is reaching the opposite shore.

In pseudo code it would be something like the following (using a recursive implementation of dfs)

dfs( currentLocation, visited):
  if currentLocation is opposite bank:
     return (best jump 0, empty path)
  for each rock R not in visited set: // treat opposite bank as a rock here
     (maxHop, path) = dfs( R, visited + currentLocation )
     hop = distance( currentLocation, R)
     path = [R] + path
     if hop > maxHop then maxHop = hop
     # find best hop, path pair  over all R
     if longest < best_jump: 
                 best_jump = longest 
                 best_path = (i, j, k)
return (best hop, best path)

I am having trouble figuring out how to tweak the following non recursive python implementation of dfs

def dfs_paths(graph, start, goal):
    stack = [(start, [start])]
    while stack:
        (vertex, path) = stack.pop()
        for next in graph[vertex].difference(set(path)):
            if next == goal:
                yield path + [next]
            else:
                stack.append((next, path + [next]))

to take into account the hops lengths. The graph in this implementation is a dictionary like

 graph = {'A': set(['B', 'C']),
          'B': set(['A', 'D', 'E']),
          'C': set(['A', 'F']),
          'D': set(['B']),
          'E': set(['B', 'F']),
          'F': set(['C', 'E'])}

but in the jumps problem I need to replace the characters with a representation of the stones and the far shore. I could then get the paths through

paths=list(dfs_paths(graph, start_goal, end_goal))
Community
  • 1
  • 1
user35202
  • 389
  • 1
  • 10
  • The problem you linked asks for a path that has the shortest longest jump, where you seem to be stating that you want paths with the longest shortest jump. Which one do you want? – user2566092 Apr 13 '15 at 16:17
  • For each possible path there will be one jump that is the longest. I want to find the paths that have the shortest of this longest jump (lets say the shortest longest jump is 4 for a given setup of the 3 stones; in general there will be several paths where the longest jump is 4; I would like to find them all) – user35202 Apr 13 '15 at 16:25

1 Answers1

0

What makes this problem potentially time-consuming to solve is that for example if you have a "bottleneck" jump in your graph that must be used, then if that jump is the longest over all jumps then you need to print out all possible paths in your graph. So, in light of this, the best possible solution is probably to first solve for the length of the shortest possible longest jump, and then take your graph and remove all edges that have length greater than that, and then do DFS keeping track of the current path as you go, to print out all possible paths that use the restricted set of edges. This will give you the set of all optimal paths.

user2566092
  • 4,631
  • 15
  • 20
  • This approach seems sensible to me. I imagine you are removing the edges of length greater than the shortest longest jump to make the search faster? (although for the case of 3 stones probably there is no difference). Thanks for giving me ideas for optimizing the search. – user35202 Apr 13 '15 at 16:42