Advertisement
loubot

1 millionth attempt

May 2nd, 2013
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. def bruteForceSearch(digraph, start, end, maxTotalDist, maxDistOutdoors,dist = 0,visited = []):
  2.     """
  3.    Finds the shortest path from start to end using brute-force approach.
  4.    The total distance travelled on the path must not exceed maxTotalDist, and
  5.    the distance spent outdoor on this path must not exceed maxDisOutdoors.
  6.  
  7.    Parameters:
  8.        digraph: instance of class Digraph or its subclass
  9.        start, end: start & end building numbers (strings)
  10.        maxTotalDist : maximum total distance on a path (integer)
  11.        maxDistOutdoors: maximum distance spent outdoors on a path (integer)
  12.  
  13.    Assumes:
  14.        start and end are numbers for existing buildings in graph
  15.  
  16.    Returns:
  17.        The shortest-path from start to end, represented by
  18.        a list of building numbers (in strings), [n_1, n_2, ..., n_k],
  19.        where there exists an edge from n_i to n_(i+1) in digraph,
  20.        for all 1 <= i < k.
  21.  
  22.        If there exists no path that satisfies maxTotalDist and
  23.        maxDistOutdoors constraints, then raises a ValueError.
  24.    """
  25.     path = [str(start)]
  26.     distance = [str(dist)]
  27.     if MITNode(start) == MITNode(end):
  28.         return path[:], distance[:]
  29.     shortest = None
  30.     newDistance = None
  31.     for edge in digraph.childrenOf(MITNode(start)):
  32.         if str(edge.getDestination()) not in visited:
  33.             visited = visited + [str(edge.getDestination())]
  34. ##            print visited
  35.             newPath, possDist = bruteForceSearch(digraph,edge.getDestination(),end,maxTotalDist,maxDistOutdoors,str(edge.getDistance()),visited[:])
  36.             if newPath == None:
  37.                 continue
  38.             if shortest == None or len(newPath) < len(shortest):
  39.                 shortest = newPath
  40.                 newDistance = possDist
  41.  
  42.     if shortest != None:
  43.         path = path + shortest
  44.         distance = distance + newDistance
  45.     else:
  46.         path = None
  47.         distance = None
  48.     return path,distance
  49.  
  50.  
  51. graph = load_map(mapFileName)
  52. for edge in graph.childrenOf(MITNode('2')):
  53.     print str(edge.getDestination())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement