Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. def findAllValidPaths(startState, destination, targetWeight):
  2.     stack = []
  3.     for e in startState.outgoingEdges:
  4.         newPath = [e]
  5.         stack.append(newPath)
  6.     while len(stack) > 0:
  7.         path = stack[-1]
  8.         stack = stack[:-1]
  9.         lastState = path[-1].targetState
  10.         weight = 0
  11.         for e in path:
  12.             weight += e.weight
  13.         if lastState == destination and weight == targetWeight:
  14.             print(path)
  15.             continue
  16.         if lastState == destination or weight > targetWeight: # omit the "lastState == destination" part if its ok to find a paths which visit "destination" multiple times as long as they satisfy the target conditions in the end
  17.             continue
  18.         for e in lastState.outgoingEdges:
  19.             newPath = []
  20.             newPath.extend(path)
  21.             newPath.append(e)
  22.             stack.append(newPath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement