BFS在有向图和无向图上的实现有何不同。
我在网上找到了以下伪代码,对于无向图我没问题,但是不知道如何在有向图上实现。
frontier = new Queue()
mark root visited (set root.distance = 0)
frontier.push(root)
while frontier not empty {
Vertex v = frontier.pop()
for each successor v' of v {
if v' unvisited {
frontier.push(v')
mark v' visited (v'.distance = v.distance + 1)
}
}
}