本文整理汇总了Python中Graph.out_vertices方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.out_vertices方法的具体用法?Python Graph.out_vertices怎么用?Python Graph.out_vertices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.out_vertices方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BestPath
# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import out_vertices [as 别名]
class BestPath(object):
"""Generates the best Hamiltonian Path froma connected graph.
This class generates a best Hamiltonian path for a Travelling Sales Person
Problem form a connected graph for a given set of vertices that the traveller
wants to visit only.
Attributes:
currentTour: A stack that records the current tour
bestTour: A stack taht records the best tour
currentCost: Cost of the current tour
bestCost: Cost of the best tour. Inititally set to a very large number.
interestCities: A list of citites that the traveller is interested to visit
_map: A map of the city containing all the vertices and edges
_visitedPoints: this contains all the points visited so far
_visitedCitites: this contians all the interest cities visited so far
"""
def __init__(self, cities=[]):
self.currentTour = Stack()
self.bestTour = Stack()
self.currentCost = 0
self.bestCost = 10000000000000000000000000000000
self.interestCities = [Vertex(city) for city in cities]
self._map = Graph()
self._visitedPoints = []
self._visitedCities = []
def set_map(self, pmap):
"""Sets the passed map to the instance variable _map
"""
self._map = pmap
def mark_visit(self, city):
"""Marks the passed city visited by putting it in the _visitedCities
and/or _visitedPoints list. City should be a vertex objcet.
"""
if city in self.interestCities: # if the city is an interest city
self._visitedCities.append(city) # add city to visitesCitites
self._visitedPoints.append(city) # add city to visitedPoints
else:
self._visitedPoints.append(city) # only add to visitedPoints
def mark_unvisit(self, city):
"""
Marks the passed city unvisited by removing it from the _visitedCities
and/or _visitedPoints list. City should be a vertex objcet.
"""
if city in self.interestCities: # if the city is an interest city
self._visitedCities.remove(city)
self._visitedPoints.remove(city)
else:
self._visitedPoints.remove(city)
def all_visited(self):
"""Returns a True bool type if all the interest cities have been visited
"""
# if the length of interest cities list and visited cities lists are equal
if len(self.interestCities) == len(self._visitedCities):
return True
else:
return False
def next_cities(self):
"""
It generates a list of points that have not been visited that we may
potentially want to visit
"""
# sets the current city as the top item on currentTour stack
currentCity = self.currentTour.top()
# calls Graph Class's out vertices method to get all the vertices out of currentCity in the map
possibleMoves = self._map.out_vertices(currentCity)
# The following line returns a list of points that we can visit next
# that has not already been visited yet in the currentTour
return [move for move in possibleMoves if move not in self._visitedPoints]
def move_branch(self, currentCity):
"""This method generates a bestTour by starting at currentCity and explores
all the possible vertices until it reaches a dead_end or all the interestCities
have been visited.
It does that by recursively exploring all possibilities but has a bounding algorithm
that causes it to prune.
"""
# base case
if self.dead_end() or self.all_visited():
if self.all_visited():
if self.currentCost < self.bestCost:
self.bestCost = self.currentCost
self.bestTour = Stack() # reset bestTour stack
for i in self.currentTour.items[::-1]: # add all the items in currentTour to bestTour
self.bestTour.push(i)
return
# recursive call
else:
#.........这里部分代码省略.........