本文整理汇总了Python中networkx.classes.digraph.DiGraph.has_node方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.has_node方法的具体用法?Python DiGraph.has_node怎么用?Python DiGraph.has_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.has_node方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BoardGraph
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import has_node [as 别名]
class BoardGraph(object):
def walk(self, board, size_limit=maxint):
pending_nodes = []
self.graph = DiGraph()
self.start = board.display(cropped=True)
self.graph.add_node(self.start)
pending_nodes.append(self.start)
self.min_domino_count = len(board.dominoes)
while pending_nodes:
if len(self.graph) >= size_limit:
raise GraphLimitExceeded(size_limit)
state = pending_nodes.pop()
board = Board.create(state, border=1)
dominoes = set(board.dominoes)
self.min_domino_count = min(self.min_domino_count, len(dominoes))
for domino in dominoes:
dx, dy = domino.direction
self.try_move(state, domino, dx, dy, pending_nodes)
self.try_move(state, domino, -dx, -dy, pending_nodes)
self.last = state
return set(self.graph.nodes())
def try_move(self, old_state, domino, dx, dy, pending_states):
try:
new_state = self.move(domino, dx, dy)
move = domino.describe_move(dx, dy)
if not self.graph.has_node(new_state):
# new node
self.graph.add_node(new_state)
pending_states.append(new_state)
self.graph.add_edge(old_state, new_state, move=move)
except BoardError:
pass
def move(self, domino, dx, dy):
""" Move a domino and calculate the new board state.
Afterward, put the board back in its original state.
@return: the new board state
@raise BoardError: if the move is illegal
"""
domino.move(dx, dy)
try:
board = domino.head.board
if not board.isConnected():
raise BoardError('Board is not connected.')
if board.hasLoner():
raise BoardError('Board has a lonely domino.')
return board.display(cropped=True)
finally:
domino.move(-dx, -dy)