Intro

Intro

Kahn's Algorithm

Use Case

  • 这个问题应该被什么顺序觉解

  • 这个问题让你判断能不能被解决,给你的条件有没有矛盾==》 判断有向图中有没有环

Topological Sort最重要的点

  • 定义dependecy, a---> b (b depends a)

重要的概念:inDegree

  • indegree[]: how many edges are pointing to itself

topological sort和有无环的关系

  • 有topological sort那就一定是有向无环图

  • 没有topological sort有可能是无向图,不一定是有环(有向就没问题,无向不可以)

怎么判断有无环

  • 对于任意一个有方向,定义好dependency:

    • run topological sort algorithm

    • 如果排好序的点= 总点数 ok,无环

BFS Queue装的点的物理意义?

  • 当前优先级相同的点,所有入度为0的点。

  • 实际情况

    • 先generate一堆点:所有入度为0的node

    • 每次从入度为0的点出发:

      • expand

      • exapnd一个入度为0点:加入拓扑排序

        • update所有我expand的这个点的dependency:

        • 我的neighbor node:indegree[nei of curNode]--;

      • genereate:如果你发现update完了以后这个neighbors node indegree变成0了,就把它generate出来

基本Implementation

  • 图representation: Map<Node, List<Node>/ Set<Node>> graph

  • inDegree:

    • int[] inDegree (if Node is represnted by integer id)

    • Map<Node, Integer> indegreeMap

  • 最简单化,Set<Node> allNodes 记录graph里所有的Node

  • Queue: Deque<Node> queue = new ArrayDeque<>();

  • Topological Result: List<Node> result

Step 1:先generate所有入度为0的点

Step 2:Do BFS

Step 3:check环(nodes.size == result.size)

Question: 不联通的图,可不可以有拓扑排序?

  • example A, B-> C, E-> F-> G

  • yes, one of the valid topological sort: A, B, E, C, F, G

Last updated