Topological Sort as a Relation (Math) / Linear Extension / DFS & BFS
0. VocabularyPermalink
- Topological Sort: refers to the result of ordering, i.e. the order
- Topological Sorting: refers to the precedure/algorithm of sorting
- Topological Space: See Topology / Topological Space
- Simple Graph: refers to graphs with no loops nor parallel edges. See Basic Graph Algorithms
- DAG: Directed Acyclic Graph. See Basic Graph Algorithms
- Strict Partial Order, Strict Weak Order, Strict Total Order: See Relation (Math) and Asymptotic Notations
1.Digression: What does “topological” mean in topological sorting?Permalink
ChatGPT 给我的一个答案是:
It’s an unfortunate naming overlap. The term “topology” in graph theory means “the layout or structure of connections,” which is different from its meaning in topology as a branch of math.
这个 “the layout or structure of connections” 的意思,按某些网友的说法,应该是 Network topology 的研究领域 (参考 Why is “topological sorting” topological?)
但如果我硬要把 topological sorting 和 topological space 扯上关系呢?也不是不行。实际上,从 graph 可以 induce 出多种类型的 topological spaces,比如:
- 这篇 Kilicman, Adem, and Khalid Abdulkalek. “Topological spaces associated with simple graphs.” Journal of Mathematical Analysis 9.4 (2018): 44-52. 讲的就是从 simple graph 的 vertex set
出发构建一个 topological space - DeepSeek 给我的一个例子是:
such that edge , define as a point somewhere between and on the edge, where 就是 approaching 就是 approaching- 然后你任意的 edge 就可以映射到一个独立的
区间上,这些区间不在一个坐标系内,但 endpoint 是可以重叠的 - 这么一来,只要有 edge
,那么在这条 edge 对应的 区间上,就有
构成 topological space 的 set- topology
太复杂这里就不写了
DeepSeek 这个例子有点绕,但你从它这个角度去理解 topological sort,也不是不行……
我这里讨论这个问题,主要是为了引出 “A graph is naturally a 1-dimensional topological space (sort of)” 这个观点,i.e. graph theory 和 topology space 的研究是可以联动的。
2. Is topological sort (on DAG) a relation (math)?Permalink
快速回顾一下 topological sort。它其实只要求:
- Input:
- Output: A sequence of vertices
- 如果存在 edge
,那么 一定要排在 前面,i.e. 必须是 这样的形式
2.1 DAG Reachability is a Strict Partial OrderPermalink
我们先考虑这个和 topological sort 相关的 relation: Reachable.
Definition: If there is an path
Claim: DAG Reachability is a Strict Partial Order. (Just like strict “subset”
Proof:
(✅ Irreflexive) Obviously,
(✅ Asymmetric) Obviously,
(✅ Transitive) Suppose
Claim: DAG Reachability is NOT a Strict Weak Order.
Proof: We reduce the problem to showing that the incomparable relation
Note that
(✅ Reflexive) Obviously,
(✅ Symmetric) Obviously,
(❌ Transitive) Suppose
flowchart LR
v0(($$v_0$$)) --> vi(($$v_i$$))
v0 --> vj(($$v_j$$))
vi -.-> vk(($$v_k$$))
Claim: DAG Reachability is NOT a Strict Total Order.
Proof: We reduce the problem to showing that relation
(❌ Connected)
2.2 Digression: Visualize the reachability relation to recover the DAG (kind of)Permalink
如果
MEG Problem: Given a DiGraph, find a smallest subset of
这个问题我们就不展开了。但
2.3 Any FIXED Topological Sort is a Strict Total OrderPermalink
我们先不考虑 topological sorting 的不确定性 (即同一个 graph 可能有不同的 topological sort 结果).
但如果是一个特定的 topological sort,它本质就是个 sequence,所以一定是 Strict Total Order,类似 relation
证明很直接,我们就略过了。
2.4 Topological Sorting is to construct a Linear Extension on DAG ReachabilityPermalink
我找了两个 Linear Extension 的定义,可以对比一下:
Definition: Given a partial order
是不是有点没看懂?我们看下一个:
Definition: Given a partial order
such that , , either , or , or
换言之,linear extension on
- preserving of the old partial order
- i.e.
, there must be - 我们也称:total order
preserves the partial order of
- i.e.
- augmentation to the new total order
- i.e.
, we add or to - i.e. reduce
to empty set ; make connected - augmentation 存在 arbitrariness,即我在 augmentation 的过程中并不 care 到底是
还是
- i.e.
Any FIXED topological sort
flowchart LR
v0(($$v_0$$)) --> vi(($$v_i$$))
v0 --> vj(($$v_j$$))
对
Counting the number of linear extensions of a partial order is
3. How algorithms of DFS & BFS eliminate the arbitrarinessPermalink
如果我们处理的不是 graph
- topological sorting on
- DFS
- pre-order (yields prefix notation)
- in-order (yields infix notation)
- post-order (yields postfix notation)
- BFS == level-order
- DFS
3.1 Pre-order ( DFS)Permalink
假设我们有这么一个简单的 tree
flowchart TD
r(($$r$$))
l1(($$l_1$$))
l2(($$l_2$$))
r --- l1
r --- l2
pre-order 相当于把
flowchart TD
r(($$r$$))
l1(($$l_1$$))
l2(($$l_2$$))
r --> l1
r --> l2
有
3.2 In-order ( DFS)Permalink
in-order 相当于把
flowchart TD
r(($$r$$))
l1(($$l_1$$))
l2(($$l_2$$))
r ~~~ l1 --> r
l2 ~~~ r --> l2
我们人为选择了从
3.3 Post-order ( DFS)Permalink
post-order 相当于把
flowchart TD
r(($$r$$))
l1(($$l_1$$))
l2(($$l_2$$))
r ~~~ l1 --> r
r ~~~ l2 --> r
l1 ~~~ r
l2 ~~~ r
有
3.4 Level-order (i.e. BFS)Permalink
与 pre-order 类似,但对
flowchart TD
r(($$r$$))
l1(($$l_1$$))
l2(($$l_2$$))
r --> l1
r --> l2
l1 -.-> v(($$v$$))
pre-order 中,我们相当于是人为 augment 了
level-order 中,我们同样人为 augment 了
,按从左至右排成 and , if , then
于是最后的结果是:
留下评论