def findShortestRoute(self, startNode, destinationName): # get routes to process by getting all out going links of the starting node candidateRoutes = Routes( [Route([link]) for link in startNode.getLinks()]) minRoute = None # candiate routes are the routes to process. # they can be empty when no more routes can have distances less than the current minimum distance # or cannot find any more routes while candidateRoutes.len() > 0: # finished routes store the routes connecting start node and end node finishedRoutes = candidateRoutes.getRoutesEndedWithNode( destinationName) _minRoute = finishedRoutes.getMinDistance() minRoute = _minRoute if minRoute is None or ( _minRoute is not None and minRoute.getDistance() > _minRoute.getDistance()) else minRoute # filter out routes having bigger distance than min route candidateRoutes = Routes([ route for route in candidateRoutes.getRoutes() if minRoute is None or route.getDistance() < minRoute.getDistance() ]) # all candidate routes go one step further. Store the new generated routes candidateRoutes = Routes([Route([]).copyFrom(route).addNextLink(link) \ for route in candidateRoutes.getRoutes() for link in self.findByName(route.getLastLink().child).getLinks() \ if not route.hasNode(link.child) or (link.child == destinationName and startNode.name == destinationName)]) return minRoute
def findRoutesWithMaxDistance(self, startNode, destinationName, distance): # get routes to process by getting all out going links of the starting node candidateRoutes = Routes( [Route([link]) for link in startNode.getLinks()]) finishedRoutes = None # define filter function with partial function lessThanFilterFunc = partial(Dag.distanceLessThanFilter, distance=distance) while candidateRoutes.len() > 0: # finished routes store the routes connecting start node and end node finishedRoutes = candidateRoutes.getRoutesEndedWithNode( destinationName).filter(lessThanFilterFunc).merge( finishedRoutes) # all candidate routes go one step further. Store the new generated routes candidateRoutes = Routes([Route([]).copyFrom(route).addNextLink(link) \ for route in candidateRoutes.getRoutes() for link in self.findByName(route.getLastLink().child).getLinks()]) # filter out routes having more distance than the max distance candidateRoutes = Routes([ route for route in candidateRoutes.getRoutes() if route.getDistance() < distance ]) return finishedRoutes
def findRoutesWithMaxRound(self, startNode, destinationName, maxRound): # get routes to process by getting all out going links of the starting node candidateRoutes = Routes( [Route([link]) for link in startNode.getLinks()]) count = 0 finishedRoutes = None while candidateRoutes.len( ) > 0 and maxRound is not None and count < maxRound: # finished routes store the routes connecting start node and end node finishedRoutes = candidateRoutes.getRoutesEndedWithNode( destinationName).merge(finishedRoutes) # all candidate routes go one step further. Store the new generated routes candidateRoutes = Routes([Route([]).copyFrom(route).addNextLink(link) \ for route in candidateRoutes.getRoutes() for link in self.findByName(route.getLastLink().child).getLinks()]) count = count + 1 return finishedRoutes