Some facts about topological sort

本文探讨了有向无环图(DAG)的拓扑排序定义及其相关推论,包括如何确保排序的有效性以及通过一系列步骤调整顶点顺序来维持拓扑排序的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j.

Then we have following corollaries:

  • A sort is a topological sort of a DAG G iff for all i, j such that there is a path from i to j, i precedes j in the sort.
  • A sort is a topological sort of a DAG G iff for all i, j, if i precedes j, then there is no path from j to i.

A Lemma:

  • If in a DAG G, for i, j of G, there is no path from i to j and from j to i, then there must be a topological sort of G with i preceding j.

Proof:

Suppose there is a toplogical sort with j preceding i, then with the following procedure:

For every k between j and i, with the one nearest to j processed first:

   If there is a path from k to i, then move k right before j

Move i right before j

We can derive a topological order with i preceding j without breaking the validity of the order. The reason is that, since k is between j and i, then there is no path from k to j and no path from i to k. If there is a path from k to i, then there must be no path from j to k (because otherwise there would be a path from j to i). Then k can be moved before j without breaking the validity of the toplogical order. If there has been other vertices between j and i which are moved before j, then moving k right before j would keep the orginal preceding relationship between them. If there are other vertices before k that are not moved, for any such k', since there is no path from k' to i, then there is no path from k' to k (because otherwise there will be a path from k' to i), then k can also be moved in front of k' without breaking the validity of the toplogical sort. Since each step of the above procedure keeps the validity of the toplogical sort, we have derived a valid topological sort with this procedure.

转载于:https://www.cnblogs.com/waytofall/p/6414727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值