6 分钟阅读

0. VocabularyPermalink

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 V 出发构建一个 topological space
  • DeepSeek 给我的一个例子是:
    • vi,vjV such that edge e=(vivj)E, define (e,t) as a point somewhere between vi and vj on the edge, where t(0,1)
      • t0 就是 approaching vi
      • t1 就是 approaching vj
      • 然后你任意的 edge 就可以映射到一个独立的 [0,1] 区间上,这些区间不在一个坐标系内,但 endpoint 是可以重叠的
      • 这么一来,只要有 edge e=(vivj),那么在这条 edge 对应的 [0,1] 区间上,就有 vi<vj
    • X=V{(e,t)eE,t(0,1)} 构成 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。它其实只要求:

  1. Input: G=(V,E)
  2. Output: A sequence of vertices s=v(1)v(n)
  3. 如果存在 edge vivj,那么 vi 一定要排在 vj 前面,i.e. 必须是 s=vivj 这样的形式

2.1 DAG Reachability is a Strict Partial OrderPermalink

我们先考虑这个和 topological sort 相关的 relation: Reachable.

Definition: If there is an path vivj, we say vj is reachable from vi, denoted by vivj

Claim: DAG Reachability is a Strict Partial Order. (Just like strict “subset” relation)

Proof:

(✅ Irreflexive) Obviously, vV, v⇝̸v. Otherwise there would be a self loop vv, which contradicts the fact of DAG.

(✅ Asymmetric) Obviously, vi,vjV, vivjvj⇝̸vi. Otherwise there would be loop vivj, which contradicts the fact of DAG.

(✅ Transitive) Suppose vivj and vjvk, transitivity holds obviously.

Claim: DAG Reachability is NOT a Strict Weak Order.

Proof: We reduce the problem to showing that the incomparable relation is NOT an equivalence relation.

Note that vivjvi⇝̸vjvj⇝̸vi, i.e. the two vertices are not connected by any path. E.g.

(✅ Reflexive) Obviously, vV, vv (any vertex is not reachable from itself)

(✅ Symmetric) Obviously, vi,vjV, vivjvjvi (by definition of )

(❌ Transitive) Suppose vivj and vjvk. It’s still possible that there is a vivk or vkvi path.

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 is NOT connected.

(❌ Connected) vi,vjV, it’s possible that vi⇝̸vj and vj⇝̸vi, so is NOT connected.

2.2 Digression: Visualize the reachability relation to recover the DAG (kind of)Permalink

如果 vivj, 我们都画一条 edge vivj,我们最终能得到一个新的 graph G=(V,E),它不一定就是原来的 G=(V,E),因为它可能有冗余的 edges。这里又引入了一个新的课题:Minimum Equivalent Graph (MEG).

MEG Problem: Given a DiGraph, find a smallest subset of E that maintains all reachability relations between vertices.

这个问题我们就不展开了。但 GG 肯定是能 reduce 到同一个 MEG.

2.3 Any FIXED Topological Sort is a Strict Total OrderPermalink

我们先不考虑 topological sorting 的不确定性 (即同一个 graph 可能有不同的 topological sort 结果).

但如果是一个特定的 topological sort,它本质就是个 sequence,所以一定是 Strict Total Order,类似 relation < 和 sequence 1<2<3<n.

证明很直接,我们就略过了。

2.4 Topological Sorting is to construct a Linear Extension on DAG ReachabilityPermalink

我找了两个 Linear Extension 的定义,可以对比一下:

Definition: Given a partial order on a set X, a linear extension is total order (a.k.a. linear order) on the same set X such that identity function I:⊆ is order preserving.

是不是有点没看懂?我们看下一个:

Definition: Given a partial order on a set X, a linear extension is total order (a.k.a. linear order) on the same set X such that:

  1. a,bX such that ab, ab
  2. a,bX, either ab, or ba, or a=b

换言之,linear extension on 做了两件事:

  1. preserving of the old partial order
    • i.e. (a,b)∈⊆, there must be (a,b)∈≤
    • 我们也称:total order preserves the partial order of
  2. augmentation to the new total order
    • i.e. (a,b), we add (a,b) or (b,a) to
    • i.e. reduce to empty set ; make connected
    • augmentation 存在 arbitrariness,即我在 augmentation 的过程中并不 care 到底是 ab 还是 ba

Any FIXED topological sort is a linear extension on DAG Reachability . Augmentation 的 arbitrariness 也正是 topological sorting 结果的不确定性的原因。E.g.

flowchart LR
    v0(($$v_0$$)) --> vi(($$v_i$$))
    v0 --> vj(($$v_j$$))

vivj,我们可以 augment 成 vivj (i.e. s=v0vivj),也可以 augment 成 vjvi (i.e. s=v0vjvi)

Counting the number of linear extensions of a partial order is #P-complete (and well studied).

3. How algorithms of DFS & BFS eliminate the arbitrarinessPermalink

如果我们处理的不是 graph G 而是一棵 tree T,那么 DFS & BFS 本质上都是 T 上的 topological sorting。进一步细分:

  • topological sorting on T
    • DFS
      • pre-order (yields prefix notation)
      • in-order (yields infix notation)
      • post-order (yields postfix notation)
    • BFS == level-order

3.1 Pre-order ( DFS)Permalink

假设我们有这么一个简单的 tree T:

flowchart TD
    r(($$r$$))
    l1(($$l_1$$))
    l2(($$l_2$$))

    r --- l1
    r --- l2

pre-order 相当于把 T 改造成了 DAG G1:

flowchart TD
    r(($$r$$))
    l1(($$l_1$$))
    l2(($$l_2$$))

    r --> l1
    r --> l2

rl1rl2;对于 l1v2,我们人为选择 augment 成 l1l2;最终有 rl1l2.

3.2 In-order ( DFS)Permalink

in-order 相当于把 T 改造成了 DAG G:

flowchart TD
    r(($$r$$))
    l1(($$l_1$$))
    l2(($$l_2$$))

    r ~~~ l1 --> r
    l2 ~~~ r --> l2

我们人为选择了从 l1 开始而不是从 l2 开始。这已经是一个 linear order 了,所以 l1rl2.

3.3 Post-order ( DFS)Permalink

post-order 相当于把 T 改造成了 DAG G3:

flowchart TD
    r(($$r$$))
    l1(($$l_1$$))
    l2(($$l_2$$))

    r ~~~ l1 --> r
    r ~~~ l2 --> r
    l1 ~~~ r
    l2 ~~~ r

l1rl2r;对于 l1v2,我们人为选择 augment 成 l1l2;最终有 l1l2r.

3.4 Level-order (i.e. BFS)Permalink

与 pre-order 类似,但对 l1-subtree 和 l2-subtree 的处理有不同。

flowchart TD
    r(($$r$$))
    l1(($$l_1$$))
    l2(($$l_2$$))

    r --> l1
    r --> l2

    l1 -.-> v(($$v$$))

pre-order 中,我们相当于是人为 augment 了 vl1-subtree,vl2;同时又因为 l1v (by definition of pre-order) 且 l1l2 (人为限定),所以相当于是有:

pre-order:=rl1pre-order of l1-subtree except l1l2pre-order of l2-subtree except l2

level-order 中,我们同样人为 augment 了 l1l2;但 more generally,我们限定了:

  1. vk(1),vk(2k1)vertices on level k,按从左至右排成 vk(1)vk(2k1)
  2. vk on level k and vx on level x, if k<x, then vkvx

于是最后的结果是:

level-order:=rl1l2level-order of level 2level-order of level 3level-order of level 4

分类:

更新时间:

留下评论