Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def bruteForceSearch(digraph, start, end, maxTotalDist, maxDistOutdoors,dist = 0,visited = []):
- """
- Finds the shortest path from start to end using brute-force approach.
- The total distance travelled on the path must not exceed maxTotalDist, and
- the distance spent outdoor on this path must not exceed maxDisOutdoors.
- Parameters:
- digraph: instance of class Digraph or its subclass
- start, end: start & end building numbers (strings)
- maxTotalDist : maximum total distance on a path (integer)
- maxDistOutdoors: maximum distance spent outdoors on a path (integer)
- Assumes:
- start and end are numbers for existing buildings in graph
- Returns:
- The shortest-path from start to end, represented by
- a list of building numbers (in strings), [n_1, n_2, ..., n_k],
- where there exists an edge from n_i to n_(i+1) in digraph,
- for all 1 <= i < k.
- If there exists no path that satisfies maxTotalDist and
- maxDistOutdoors constraints, then raises a ValueError.
- """
- path = [str(start)]
- distance = [str(dist)]
- if MITNode(start) == MITNode(end):
- return path[:], distance[:]
- shortest = None
- newDistance = None
- for edge in digraph.childrenOf(MITNode(start)):
- if str(edge.getDestination()) not in visited:
- visited = visited + [str(edge.getDestination())]
- ## print visited
- newPath, possDist = bruteForceSearch(digraph,edge.getDestination(),end,maxTotalDist,maxDistOutdoors,str(edge.getDistance()),visited[:])
- if newPath == None:
- continue
- if shortest == None or len(newPath) < len(shortest):
- shortest = newPath
- newDistance = possDist
- if shortest != None:
- path = path + shortest
- distance = distance + newDistance
- else:
- path = None
- distance = None
- return path,distance
- graph = load_map(mapFileName)
- for edge in graph.childrenOf(MITNode('2')):
- print str(edge.getDestination())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement