[关闭]
@Yano 2019-09-20T02:56:18.000000Z 字数 5128 阅读 7498

LeetCode之Graph题目汇总

LeetCode

公众号

coding 笔记、点滴记录,以后的文章也会同步到公众号(Coding Insight)中,希望大家关注^_^

https://github.com/LjyYano/Thinking_in_Java_MindMapping


LeetCode 题目汇总

LeetCode之Array题目汇总
LeetCode之Hash Table题目汇总
LeetCode之Linked List题目汇总
LeetCode之Math题目汇总
LeetCode之String题目汇总
LeetCode之Binary Search题目汇总
LeetCode之Divide and Conquer题目汇总
LeetCode之Dynamic Programming题目汇总
LeetCode之Backtracing题目汇总
LeetCode之Stack题目汇总
LeetCode之Sort题目汇总
LeetCode之Bit Manipulation题目汇总
LeetCode之Tree题目汇总
LeetCode之Depth-first Search题目汇总
LeetCode之Breadth-first Search题目汇总
LeetCode之Graph题目汇总
LeetCode之Trie题目汇总
LeetCode之Design题目汇总


文章目录:


Course Schedule

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

2, [[1,0],[0,1]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

Note: 
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.


题目等价为:检测图中是否有环

参考网址:LeetCode – Course Schedule (Java)

BFS:

  1. // BFS
  2. public static boolean canFinish(int numCourses, int[][] prerequisites) {
  3. // 参数检查
  4. if (prerequisites == null) {
  5. return false;
  6. }
  7. int len = prerequisites.length;
  8. if (numCourses <= 0 || len == 0) {
  9. return true;
  10. }
  11. // 记录每个course的prerequisites的数量
  12. int[] pCounter = new int[numCourses];
  13. for (int i = 0; i < len; i++) {
  14. pCounter[prerequisites[i][0]]++;
  15. }
  16. // 用队列记录可以直接访问的course
  17. LinkedList<Integer> queue = new LinkedList<Integer>();
  18. for (int i = 0; i < numCourses; i++) {
  19. if (pCounter[i] == 0) {
  20. queue.add(i);
  21. }
  22. }
  23. // 取出队列的course,判断
  24. int numNoPre = queue.size();
  25. while (!queue.isEmpty()) {
  26. int top = queue.remove();
  27. for (int i = 0; i < len; i++) {
  28. // 该course是某个course的prerequisites
  29. if (prerequisites[i][1] == top) {
  30. pCounter[prerequisites[i][0]]--;
  31. if (pCounter[prerequisites[i][0]] == 0) {
  32. numNoPre++;
  33. queue.add(prerequisites[i][0]);
  34. }
  35. }
  36. }
  37. }
  38. return numNoPre == numCourses;
  39. }

DFS:

  1. // DFS
  2. public static boolean canFinish2(int numCourses, int[][] prerequisites) {
  3. // 参数检查
  4. if (prerequisites == null) {
  5. return false;
  6. }
  7. int len = prerequisites.length;
  8. if (numCourses <= 0 || len == 0) {
  9. return true;
  10. }
  11. int[] visit = new int[numCourses];
  12. // key:course;value:以该course为prerequisites的course
  13. HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>();
  14. // 初始化map
  15. for (int[] p : prerequisites) {
  16. if (map.containsKey(p[1])) {
  17. map.get(p[1]).add(p[0]);
  18. } else {
  19. ArrayList<Integer> l = new ArrayList<Integer>();
  20. l.add(p[0]);
  21. map.put(p[1], l);
  22. }
  23. }
  24. // dfs
  25. for (int i = 0; i < numCourses; i++) {
  26. if (!canFinishDFS(map, visit, i)) {
  27. return false;
  28. }
  29. }
  30. return true;
  31. }
  32. private static boolean canFinishDFS(
  33. HashMap<Integer, ArrayList<Integer>> map, int[] visit, int i) {
  34. if (visit[i] == -1) {
  35. return false;
  36. }
  37. if (visit[i] == 1) {
  38. return true;
  39. }
  40. visit[i] = -1;
  41. // course i是某些course的prerequisites
  42. if (map.containsKey(i)) {
  43. for (int j : map.get(i)) {
  44. if (!canFinishDFS(map, visit, j)) {
  45. return false;
  46. }
  47. }
  48. }
  49. visit[i] = 1;
  50. return true;
  51. }

Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n - 1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

Note: 
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

click to show more hints.

Hints:

  1. This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
  2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
  3. Topological sort could also be done via BFS.

参考:LeetCode 207 Course Schedule

只是加了一句话而已,用于保存结果。注意prerequisites为空的时候,任意输出一组结果即可。

  1. public int[] findOrder(int numCourses, int[][] prerequisites) {
  2. // 参数检查
  3. if (prerequisites == null) {
  4. throw new IllegalArgumentException();
  5. }
  6. int len = prerequisites.length;
  7. if (len == 0) {
  8. int[] seq = new int[numCourses];
  9. for (int i = 0; i < seq.length; i++) {
  10. seq[i] = i;
  11. }
  12. return seq;
  13. }
  14. int[] seq = new int[numCourses];
  15. int c = 0;
  16. // 记录每个course的prerequisites的数量
  17. int[] pCounter = new int[numCourses];
  18. for (int i = 0; i < len; i++) {
  19. pCounter[prerequisites[i][0]]++;
  20. }
  21. // 用队列记录可以直接访问的course
  22. LinkedList<Integer> queue = new LinkedList<Integer>();
  23. for (int i = 0; i < numCourses; i++) {
  24. if (pCounter[i] == 0) {
  25. queue.add(i);
  26. }
  27. }
  28. // 取出队列的course,判断
  29. int numNoPre = queue.size();
  30. while (!queue.isEmpty()) {
  31. int top = queue.remove();
  32. // 保存结果 +_+
  33. seq[c++] = top;
  34. for (int i = 0; i < len; i++) {
  35. // 该course是某个course的prerequisites
  36. if (prerequisites[i][1] == top) {
  37. pCounter[prerequisites[i][0]]--;
  38. if (pCounter[prerequisites[i][0]] == 0) {
  39. numNoPre++;
  40. queue.add(prerequisites[i][0]);
  41. }
  42. }
  43. }
  44. }
  45. if (numNoPre != numCourses) {
  46. return new int[] {};
  47. }
  48. return seq;
  49. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注