๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/LeetCode

๐Ÿ’ฏ Daily LeetCode Challenge Day_18 - Course Schedule 2

by HaningYa 2020. 7. 24.
728x90

 

Account Login - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com


๋ฌธ์ œ

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.

Example 1:
Input: 2, [[1,0]]
Output: [0,1]
Explanation: 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] .

Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: 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:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

์„  ์ด์ˆ˜ ๊ณผ๋ชฉ์ด ์ •ํ•ด ์ ธ ์žˆ์„๋•Œ ์ปค๋ฆฌํ˜๋Ÿผ์„ ๋งˆ์น  ์ˆ˜ ์žˆ๋Š” ์ˆœ์„œ๋ฅผ ์ •ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ธ๊ฑด ์•Œ๊ฒ ๋‹ค.

unweighted, directed, Acyclic ์˜ ํ‚ค์›Œ๋“œ๋กœ ์ฐพ์•„๋ณด๋‹ˆ Topological sort (์œ„์ƒ์ •๋ ฌ) ๋ฌธ์ œ ์˜€๋‹ค.

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์œ„์ƒ ์ •๋ ฌ(Topological Sort)์ด๋ž€ - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

๊ณ„ํš์€ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ณ  dfs ๋กœ ํƒ์ƒ‰ํ•˜๋ฉฐ ์œ„์ƒ์ •๋ ฌํ•œ ๋’ค ๋‹ต์„ ๋ฐ˜ํ™˜ํ•  ๊ฒƒ์ด๋‹ค.

๋จผ์ € ๊ทธ๋ž˜ํ”„์— ๋Œ€ํ•œ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

var graph : [[Int]] = Array(repeating: [Int]() , count: numCourses)
        for i in prerequisites {
            let from = i[1]
            let to = i[0]
            graph[from].append(to)
        }

๊ทธ ๋‹ค์Œ ์œ„์ƒ ์ •๋ ฌ์„ ํ• ๊ฑด๋ฐ ํŠน์ดํ•œ๊ฒŒ cycle์ด ์žˆ์œผ๋ฉด ์•ˆ๋˜๋ฏ€๋กœ visited ์˜ ์ƒํƒœ๊ฐ€ 3๊ฐ€์ง€๊ฐ€ ๋œ๋‹ค.

  1. unvisited : 0 (์ดˆ๊ธฐ์ƒํƒœ)
  2. in stack   : 1 (์‚ฌ์ดํด ํ™•์ธ์šฉ)
  3. visited    : 2 (๋‹ค์‹œ๋Š” ์•ˆ๋ณผ๊ฒƒ)

 

*cycle detection ์ฐธ๊ณ ์ž๋ฃŒ

 

์ˆœ์„œ๋ฅผ ๋ณด๊ด€ํ•  stack ๊ณผ vertex ์ƒํƒœ๋ฅผ ๋‹ด์„ visited ๋ฅผ ์„ ์–ธํ•œ๋‹ค.

   var stack : [Int] = []
   // 0: unvisited 1: in stack 2: visited
   var visited : [Int] = Array(repeating: 0, count: numCourses)

๋ชจ๋“  unvisited ํ•œ vertex ๋ฅผ ๋Œ๋ฉฐ dfs ๋ฐฉ์‹์œผ๋กœ iterate ํ•œ๋‹ค.

        for i in 0..<numCourses {
            if visited[i] == 0 && dfs(i, graph, &stack, &visited) {
                return []
            }
        }

์—ฌ๊ธฐ์„œ dfs ์˜ ๋ฆฌํ„ด์€ Bool ์ธ๋ฐ cycle ์ด ์žˆ์„๊ฒฝ์šฐ true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๋นˆ ๋ฐฐ์—ด์„ ๋‹ต์œผ๋กœ ์ œ์ถœํ•œ๋‹ค.

dfs ํ•จ์ˆ˜๋Š” u๋ผ๋Š” vertex ์— ๋Œ€ํ•œ ์ธ์ ‘ vertex ๋ฅผ ๋Œ๋ฉฐ visited ๊ฐ€ 1์ผ ๊ฒฝ์šฐ (stack ์— ์žˆ์„ ๊ฒฝ์šฐ) cycle ๋กœ ํŒ๋‹จํ•˜๊ณ  0 ์ผ ๊ฒฝ์šฐ backtracking ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น u ๋ผ๋Š” vertex๋ฅผ 2๋กœ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  stack ์— ์ถ”๊ฐ€ํ•ด ์ค€ ๋‹ค์Œ false ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    func dfs(_ u : Int, _ graph : [[Int]], _ stack:inout[Int],_ visited:inout[Int]) -> Bool{
        visited[u] = 1
        for v in graph[u] {
            if visited[v] == 1 {return true} //cycle detected
            if visited[v] == 0 && dfs(v, graph, &stack, &visited) {return true}
        }
        visited[u] = 2
        stack.append(u)
        return false //no cycle
    }

์ „์ฒด์ฝ”๋“œ

class Solution {
    func findOrder(_ numCourses: Int, _ prerequisites: [[Int]]) -> [Int] {
        var answer : [Int] = []
        
        var graph : [[Int]] = Array(repeating: [Int]() , count: numCourses)
        for i in prerequisites {
            let from = i[1]
            let to = i[0]
            graph[from].append(to)
        }
        var stack : [Int] = []
        // 0: unvisited 1: visited, but not all visited
        var visited : [Int] = Array(repeating: 0, count: numCourses)
        for i in 0..<numCourses {
            if visited[i] == 0 && dfs(i, graph, &stack, &visited) {
                return []
            }
        }
        return stack.reversed()
    }
    func dfs(_ u : Int, _ graph : [[Int]], _ stack:inout[Int],_ visited:inout[Int]) -> Bool{
        visited[u] = 1
        for v in graph[u] {
            if visited[v] == 1 {return true} //cycle detected
            if visited[v] == 0 && dfs(v, graph, &stack, &visited) {return true}
        }
        visited[u] = 2
        stack.append(u)
        return false //no cycle
        
    }
}

 

[ํ•„์š”ํ–ˆ๋˜ ์ง€์‹]

  • Topological sorting
  • Depth First Search (DFS)
  • Detecting Cycle in Directed Graph
  • Making Adjacency List of Directed Graph

 

 

728x90

๋Œ“๊ธ€