python深度优先遍历怎么写_在Python中基于策略实现深度优先遍历

再见。在

我在实现基于策略的深度优先搜索时遇到了一个问题,该策略在战略.py班级。还有一个图和一个遍历类。遍历类负责遍历图。在

策略类如下:class Strategy:

init_priority = 0

def __init__(self, init_pri = 0):

self.init_priority = init_pri

def init(self, graph, node):

"""Called at beginning of traversal process. Expected that

this will carry out any necessary initialisation for the

specific traversal process

"""

pass

def visit(self, node, pri):

"""Called whenever NODE is visited by a traversal process.

PRI is the priority associated with the node in the priority

queue used by the traversal process.

"""

pass

def complete(self, node):

"""Called at the end of all the processing performed in visiting NODE.

"""

pass

def discover(self, nbr, node, weight, pri):

"""Return the priority that should be associated with NBR when it is

added to the priority queue.

Called whenever NBR is discovered for the first time. NODE

is the node from which the neighbour was discovered, and

WEIGHT is the value on the edge from NODE to NBR. PRI is the

value associated with NODE in the priority queue, at the time

of discovering NBR.

"""

def rediscover(self, nbr, node, weight, pri):

"""Return the priority that should be associated with NBR when it is

added to the priority queue.

Called whenever NBR is rediscovered. NODE is the node from

which the neighbour is rediscovered, and WEIGHT is the value

associated with the edge from NODE to NBR. PRI is the

priority of NODE in the priority queue. It is provided in

case it is relevant to the traversal strategy (e.g. for Dijkstra's)

"""

pass

def getResult(self):

"""Called at the end of the traversal process. It should

return whatever is relevant or appropriate for the type of

traversal implemented by this strategy.

"""

pass

我成功地实现了广度优先搜索,如下所示:

^{pr2}$

不过,深度优先给了我一段时间的麻烦。以下是我目前所掌握的情况:class DepthFirst(Strategy):

forward = None # the forward sequence in which nodes are visted

back = None # the backward sequence in which nodes are visited

treeEdges = None # the edges used to visit the nodes traversed

cross = None

root = -1 # the origin of the traversal

last_pri = -1 # the most recent priority used

def __init__(self):

"""The DepthFirst strategy uses an initial priority of 0"""

Strategy(0)

def init(self, graph, node):

"""Called at beginning of traversal process. Expected that

this will carry out any necessary initialisation for the

specific traversal process

"""

self.last_pri = self.init_priority

self.treeEdges = []

self.forward = []

self.back = []

self.cross = []

def visit(self, node, src, pri):

"""Called whenever NODE is visited by a traversal process.

PRI is the priority associated with the node in the priority

queue used by the traversal process.

"""

self.forward.append(node)

if src == -1:

self.root = node

else:

self.treeEdges.append((src, node))

def complete(self, node):

"""Called at the end of all the processing performed in visiting NODE.

"""

if node not in self.forward:

self.cross.append(node)

def discover(self, nbr, node, pri):

"""Return the priority that should be associated with NBR when it is

added to the priority queue.

Called whenever NBR is discovered for the first time. NODE

is the node from which the neighbour was discovered, and

WEIGHT is the value on the edge from NODE to NBR. PRI is the

value associated with NODE in the priority queue, at the time

of discovering NBR.

"""

self.forward.append((node, nbr))

self.last_pri -= 1

return self.last_pri

def rediscover(self, nbr, node, pri):

"""Return the priority that should be associated with NBR when it is

added to the priority queue.

Called whenever NBR is rediscovered. NODE is the node from

which the neighbour is rediscovered, and WEIGHT is the value

associated with the edge from NODE to NBR. PRI is the

priority of NODE in the priority queue. It is provided in

case it is relevant to the traversal strategy (e.g. for Dijkstra's)

"""

self.back.append((nbr, node))

self.last_pri -= 1

return self.last_pri

def getResult(self):

"""Called at the end of the traversal process. It should

return whatever is relevant or appropriate for the type of

traversal implemented by this strategy.

"""

return {"tree":self.treeEdges,

"forward":self.forward,

"back":self.back,

"cross":self.cross}

有什么提示吗?他们会很感激的。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值