【发布时间】:2016-02-15 00:31:35
【问题描述】:
在我学会了如何确定一个有向图是否有 1 个拓扑排序之后,我有点好奇是否有一种方法可以确定是否存在正好 2 的图。首先,确实有 2 的图拓扑排序?
我学会了使用哈密顿路径来确定 DAG 是否具有唯一的拓扑排序。这是否适用于此? 谢谢
【问题讨论】:
标签: algorithm data-structures graph graph-theory topological-sort
在我学会了如何确定一个有向图是否有 1 个拓扑排序之后,我有点好奇是否有一种方法可以确定是否存在正好 2 的图。首先,确实有 2 的图拓扑排序?
我学会了使用哈密顿路径来确定 DAG 是否具有唯一的拓扑排序。这是否适用于此? 谢谢
【问题讨论】:
标签: algorithm data-structures graph graph-theory topological-sort
如果在 (*) 行,您可以从 2 个不同的节点中选择一次 - 有两个拓扑排序
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S (*)
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
更准确地引用 R. Sedgewick 的话:
有向图具有唯一的拓扑排序当且仅当存在 中每对连续顶点之间的有向边 拓扑顺序(即,有向图具有哈密顿路径)。如果 有向图有多个拓扑排序,然后是第二个拓扑 顺序可以通过交换一对连续的顶点来获得。
【讨论】: