给定一个图形 G(V,E)和一个源顶点 s ,有多少个BFS排序,以及算法如何找到它们?
只有一个顶点 A 的图只有 s = A 的一个BFS排序,即[ A ]。
具有两个连接顶点 A 和 B 的图形具有 s = A 的一个BFS排序,即[ A ,乙
树顶点 A , B 和 C 彼此连接的图形有两个BFS排序 s = A ,即[ A , B , C ]和[ A , C < / EM>,乙
答案 0 :(得分:1)
您需要增加BFS算法以回溯并查找所有有效排序。您需要在通道中进行搜索,每层一次通过(距起始节点的最短距离)。
"done" <- [start node]
"open" <- [all other nodes]
"pending" <- empty list
while "open" is not empty
for each node in "done",
for each adjacent node
if the node is not in "done",
add it to "pending".
for each node in "pending"
add node to "done", recording the added step in a solution
recur, using new path as set of start nodes, other nodes as "pending"
BUT ... if no nodes are pending, record this path as a solution.
这是否足以让你感动?
请注意,您可以通过动态编程大大缩短这一点:为给定的path
集和最新节点记忆子解决方案。对于大图,这可以避免重复ABCD和ACBD的延续。这两种情况将具有相同的延续。
答案 1 :(得分:0)
我认为,如果我们绘制bfs树,我们可以假设 级别从0到n,N(级别no)给出该级别中的节点数 然后 对于(i = 0; i
答案 2 :(得分:0)
对于有限图,计算 BFS 排序等效于计算 DFS 前或后排序,这等效于计算拓扑排序。这是在尖锐的#P 完全复杂性类中,被认为是一个比 NP 更难的问题。