๋ฌธ์
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:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
์ ์ด์ ๊ณผ๋ชฉ์ด ์ ํด ์ ธ ์์๋ ์ปค๋ฆฌํ๋ผ์ ๋ง์น ์ ์๋ ์์๋ฅผ ์ ํ๋ ๋ฌธ์ ์ด๋ค.
๊ทธ๋ํ ๋ฌธ์ ์ธ๊ฑด ์๊ฒ ๋ค.
unweighted, directed, Acyclic ์ ํค์๋๋ก ์ฐพ์๋ณด๋ Topological sort (์์์ ๋ ฌ) ๋ฌธ์ ์๋ค.
๊ณํ์ ๊ทธ๋ํ๋ฅผ ์ธ์ ๋ฆฌ์คํธ๋ก ๋ง๋ค๊ณ 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๊ฐ์ง๊ฐ ๋๋ค.
- unvisited : 0 (์ด๊ธฐ์ํ)
- in stack : 1 (์ฌ์ดํด ํ์ธ์ฉ)
- 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
'Algorithm > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฏ Daily LeetCode Challenge Day_20 - Remove Linked List Elements (2) | 2020.07.29 |
---|---|
๐ฏ Daily LeetCode Challenge Day_19 - Add Binary (0) | 2020.07.29 |
๐ฏ Daily LeetCode Challenge Day_17 - Top K Frequent Elements (0) | 2020.07.18 |
๐ฏ Daily LeetCode Challenge Day_16 - Pow(x, n) (0) | 2020.07.18 |
๐ฏ Daily LeetCode Challenge Day_15 - Reverse Words in a String (0) | 2020.07.16 |
๋๊ธ