LeetCode 210. 课程表 II(BFS/拓扑排序)

文章目录

  • 拓扑排序
  • 检测循环依赖
    • 题目描述
    • 题目分析
  • LeetCode 210 课程表II
    • 题目描述
    • 解法:BFS
  • Reference


拓扑排序

给定一个包含 n n n 个节点的有向图 G G G,我们给出它的节点编号的一种排列,如果满足:

对于图 G G G 中的任意一条有向边 ( u , v ) (u, v) (u,v) u u u在排列中都出现在 v v v的前面。

那么称该排列是图 G G G的「拓扑排序」。根据上述的定义,可以得出两个结论:

  • 如果图 G G G 中存在环(即图 G G G 不是「有向无环图」),那么图 G G G 不存在拓扑排序。

    • 这是因为假设图中存在环 x 1 , x 2 , ⋯   , x n , x 1 x_1, x_2, \cdots, x_n, x_1 x1,x2,,xn,x1,那么 x 1 x_1 x1 在排列中必须出现在 x n x_n xn 的前面,但 x n x_n xn 同时也必须出现在 x 1 x_1 x1 的前面,因此不存在一个满足要求的排列,也就不存在拓扑排序;
  • 如果图 G G G是有向无环图,那么它的拓扑排序可能不止一种.

    • 最极端的例子,如果图 G G G 值包含 n n n个节点却没有任何边,那么任意一种编号的排列都可以作为拓扑排序

「拓扑排序」的作用

  • 得到一个「拓扑序」,「拓扑序」不唯一
  • 检测「有向图」是否有环。
    • 如果有向图中,存在环,拓扑排序不能继续得到入度值为 0 的节点,退出循环,此时图中存在没有遍历到的节点,说明图中存在环。
  • 补充:「无向图」中检测是否有环,使用的数据结构是「并查集」

检测循环依赖

题目描述

LeetCode 210. 课程表 II(BFS/拓扑排序)_第1张图片

题目分析

LeetCode 210. 课程表 II(BFS/拓扑排序)_第2张图片

拓扑排序的算法过程:

  1. 选择图中一个入度为 0 0 0的点,记录下来
  2. 在图中删除该点和所有以它为起点的边
  3. 重复1和2,直到图为空或没有入度为 0 0 0的点

LeetCode 210. 课程表 II(BFS/拓扑排序)_第3张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第4张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第5张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第6张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第7张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第8张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第9张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第10张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第11张图片

借助BFS来实现拓扑排序,队列中存储入度为 0 0 0的点

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class CyclicDependency {
    List<Integer> haveCircularDependency(int n, int[][] prerequisites) {
        // 邻接表,存储图结构
        List<List<Integer>> g = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            g.add(new ArrayList<>());
        }
        // 每个点的入度
        int[] indeg = new int[n];
        // 结果序列
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < prerequisites.length; i++) {
            int a = prerequisites[i][0];
            int b = prerequisites[i][1]; // b 依赖于 a    a -> b
            g.get(a).add(b);
            indeg[b]++;
        }
        Deque<Integer> queue = new LinkedList<>();
        // 将入度为0的点全部入队
        for (int i = 0; i < n; i++) {
            if (indeg[i] == 0) {
                queue.addLast(i);
            }
        }
        while (!queue.isEmpty()) {
            int t = queue.removeFirst();
            res.add(t);
            // 删除边时,将终点的入度减一,若入度为0,则入队
            for (int i = 0; i < g.get(t).size(); i++) {
                int j = g.get(t).get(i);
                indeg[j]--;
                if (indeg[j] == 0) {
                    queue.addLast(j);
                }
            }
        }
        if (res.size() == n) {
            return res;
        }
        return new ArrayList<Integer>();
    }

    public static void main(String[] args) {
        CyclicDependency cyclicDependency = new CyclicDependency();
        int[][] pre1 = {{0,2}, {1,2}, {2,3}, {2,4}};
        int n = 5;
        List<Integer> list = cyclicDependency.haveCircularDependency(n, pre1);
        System.out.println(list);

        int[][] pre2 = {{0,1}, {1,2}, {2,1}};
        int m = 3;
        List<Integer> list1 = cyclicDependency.haveCircularDependency(m, pre2);
        System.out.println(list1);
    }
}

LeetCode 210 课程表II

题目描述

  • 210. 课程表 II
    LeetCode 210. 课程表 II(BFS/拓扑排序)_第12张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第13张图片

LeetCode 210. 课程表 II(BFS/拓扑排序)_第14张图片

解法:BFS

可以将本题建模成一个求拓扑排序的问题:

  • 将每一门课看成一个节点;

  • 如果想要学习课程 A 之前必须完成课程 B,那么从 B 到 A 连接一条有向边。这样,在拓扑排序中,B 一定出现在 A 的前面。

求出该图的拓扑排序,就可以得到一种符合要求的课程学习顺序

class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        List> g = new ArrayList<>();   // 邻接表 
        for (int i = 0; i < numCourses; i++) {
            g.add(new ArrayList<>());
        }
        int[] indeg = new int[numCourses];           // 入度
        List res = new ArrayList<>();

        for (int i = 0; i < prerequisites.length; i++) {
            int a = prerequisites[i][0];
            int b = prerequisites[i][1];   // a 依赖于 b   b -> a
            g.get(b).add(a);
            indeg[a]++;
        }

        Deque queue = new LinkedList<>();
        // 所有入度为0的点入队
        for (int i = 0; i < numCourses; i++) {
            if (indeg[i] == 0) {
                queue.addLast(i);
            }
        }

        while (!queue.isEmpty()) {
            int t = queue.removeFirst();
            res.add(t);
            for (int i = 0; i < g.get(t).size(); i++) {
                int j = g.get(t).get(i);
                indeg[j]--;
                if (indeg[j] == 0) {
                    queue.addLast(j);
                }
            }
        }
        if (res.size() == numCourses) {
            int[] ans = new int[numCourses];
            int idx = 0;
            for (int i = 0; i < numCourses; i++) {
                ans[idx++] = res.get(i);
            }
            return ans;
        } 
        return new int[0];
    }
}
  • 时间复杂度: O ( n + m ) O(n+m) O(n+m),其中 n n n 为课程数, m m m 为先修课程的要求数。这其实就是对图进行广度优先搜索的时间复杂度。
  • 空间复杂度: O ( n + m ) O(n+m) O(n+m)。题目中是以列表形式给出的先修课程关系,为了对图进行广度优先搜索,需要存储成邻接表的形式,空间复杂度为 O ( n + m ) O(n+m) O(n+m)。在广度优先搜索的过程中,需要最多 O ( n ) O(n) O(n) 的队列空间(迭代)进行广度优先搜索,并且还需要若干个 O ( n ) O(n) O(n)的空间存储节点入度、最终答案等

Reference

  • 补充题:检测循环依赖

  • 拓扑排序(广度优先遍历) + 深度优先遍历(Java、Python)

  • 课程表 II

你可能感兴趣的:(LeetCode,java,leetcode,算法,bfs)