-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
''' | ||
# 207 | ||
์ฐธ๊ณ ์์: https://www.youtube.com/watch?v=EgI5nU9etnU | ||
๋ฌธ์ ํ์ด: https://www.algodale.com/problems/course-schedule/ | ||
## ๋ฌธ์ ์ ๋ฆฌ | ||
๐ prerequisites๋? ํ์ ์ ์ ๊ณผ๋ชฉ์ด๋ค. | ||
๐ ๋ฐฉํฅ์ฑ์ด ์๋ ์ฐ๊ฒฐ ๊ด๊ณ์ด๋ฏ๋ก, Directed Graph๋ค. | ||
๐ Cycle ๋ฐ์ ์, ์ฝ์ค๋ฅผ ์ด์ํ ์ ์๋ค.(์๋ก ์์กดํ๋ ์ํ์ด ์์ด์ ๋์์ด ๋๊ฒ ๋๋ ๊ฒฝ์ฐ) | ||
## ํด๊ฒฐ ๋ฐฉ์ ๋๊ฐ์ง | ||
1. BFS, Queue, Topological Sort: ์์ ์ ๋ ฌ | ||
2. DFS, Cycle Detection: ์ํ ํ์ง | ||
### ์์ ์ ๋ ฌ(Topological Sort) - BFS, Queue | ||
- ์ง์ ์ฐจ์(indegree): ๋ ธ๋๋ก ๋ค์ด์ค๋ ํ์ดํ ์ | ||
- ๊ทธ๋ํ๋ก ์ธ์ ๋ฆฌ์คํธ ๊ตฌ์ฑ | ||
- Queue์ ๋ฃ๊ธฐ | ||
- Queue BFS ํ์ | ||
- ๋ชจ๋ ๊ณผ๋ชฉ์ ๋ค์๋์ง ํ์ธ | ||
### ์ํ ํ์ง(Cycle Detection) - DFS | ||
- ๊ทธ๋ํ๋ก ์ธ์ ๋ฆฌ์คํธ ๊ตฌ์ฑ | ||
- ๋ฐฉ๋ฌธ ์ํ ๋ฐฐ์ด ์ด๊ธฐํ | ||
- dfs ํจ์ | ||
- ๋ชจ๋ ๋ ธ๋์ ๋ํด dfs ์คํ | ||
## TC & SC | ||
- ์๊ฐ ๋ณต์ก๋์ ๊ณต๊ฐ ๋ณต์ก๋๋ O(V + E)๋ก ๋์ผํ๋ค. | ||
``` | ||
V: ๋ ธ๋ ์(๊ณผ๋ชฉ ์) | ||
E: ๊ฐ์ ์(์ ์ ๊ณผ๋ชฉ ๊ด๊ณ ์) | ||
``` | ||
### TC is O(V + E) | ||
๋ ๋ฐฉ๋ฒ ๋ชจ๋, ๊ทธ๋ํ์ ๋ชจ๋ ๋ ธ๋์ ๊ฐ์ ์ ํ ๋ฒ์ฉ ํ์ธํจ | ||
- BFS: ๋ชจ๋ V๋ฅผ ์ํํ๋ฉด์, ๊ฐ ๋ ธ๋์์ ๋๊ฐ๋ E๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ์ฐจ์๋ฅผ ์ค์ | ||
- DFS: ๋ชจ๋ V๋ฅผ ์ํํ๋ฉด์, ๊ฐ ๋ ธ๋์์ ์ฐ๊ฒฐ๋ E๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ๊น์ด ํ์ | ||
### SC is O(V + E) | ||
- O(V+E): V + E๋ฅผ ์ ์ฅํ๋ ์ธ์ ๋ฆฌ์คํธ ๊ทธ๋ํ | ||
- O(V)'s: ๋ฐฉ๋ฌธ ์ํ ์ ์ฅ, ์ง์ ์ฐจ์ ๋ฐฐ์ด, BFS ํ, DFS ํธ์ถ ์คํ | ||
## ์์์ ๋ ฌ(BFS) vs ์ํํ์ง(DFS)๐ค | ||
### BFS๋ฅผ ์ฌ์ฉํ์ ๋ | ||
- ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ BFS๊ฐ indegree(์ง์ ์ฐจ์) ๊ฐ๋ ์ด ๋ณด๋ค ์ง๊ด์ ์ด๋ฏ๋ก => "์์๋๋ก ์ฒ๋ฆฌํ ์ ์๋์ง ํ์ธ"ํ ๋ ๋ช ํํ๊ฒ ์ฌ์ฉํ ์ ์๋ค. ์ง์ ์ฐจ์๊ฐ 0์ธ ๋ ธ๋๋ถํฐ ์์ํด์ ์ฒ๋ฆฌ | ||
- ์ ์ ๊ณผ๋ชฉ์ ๋ค ๋ค์ ๊ณผ๋ชฉ์ ์ง์ ์ฐจ์๊ฐ 0์ด ๋๋ฏ๋ก ๋ค์ด๊ฐ ์ ์๋ ๊ณผ๋ชฉ์ด๋ผ๋ ์ ์ด ๋ช ํํจ | ||
``` | ||
ํค์๋: ์ฒ๋ฆฌ ์์๋ฅผ ์ถ๋ ฅ, ์์๋๋ก ์ฒ๋ฆฌํ ์ ์๋์ง | ||
``` | ||
### DFS๋ฅผ ์ฌ์ฉํ์ ๋ | ||
- DFS ์ํ ํ์ง๋ "์ํ ์ฌ๋ถ"๊ฐ ํต์ฌ์ผ ๋ ์์ฐ์ค๋ฝ๋ค. | ||
- ์ํ(Status)๋ฅผ ์ฌ์ฉํด์, ๋ฐฉ๋ฌธ ์ค์ธ ๋ ธ๋ ์ํ๋ฅผ ๋ค์ ๋ฐฉ๋ฌธํ๋ค๋ฉด ์ํ์ด ์์์ ๋ฐ๋ก ์ ์ ์๋ค. | ||
- ์ํ์ด ๋ฐ๊ฒฌ๋๋ฉด ๋ฐ๋ก ์ค๋จํ๋ฏ๋ก, ์ํ ํ์ง์ ์์ฐ์ค๋ฝ๋ค. | ||
``` | ||
ํค์๋: ์ฌ์ดํด์ด ์๋์ง ํ๋จ | ||
``` | ||
### +a) `@cache`๋ฅผ ํ์ฉํด๋ณด์. | ||
- ํ์ด์ 3.9~ ๋ฉ๋ชจ์ด์ ์ด์ ํจ์ | ||
- ์์ ํจ์ + ์ฌ๊ท ์ต์ ํ์ ์ฌ์ฉ (์ธ๋ถ ์์กด์ฑ, ๋ถ์ํจ๊ณผ์ ์ฃผ์ํ ๊ฒ) | ||
''' | ||
from enum import Enum | ||
|
||
class Status(Enum): # use it to dfs | ||
INITIAL = 1 | ||
IN_PROGRESS = 2 | ||
FINISHED = 3 | ||
|
||
class Solution: | ||
''' | ||
1. BFS | ||
์์ ์ ๋ ฌ | ||
''' | ||
def canFinishTopologicalSort(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
indegree = [0] * numCourses | ||
graph = defaultdict(list) | ||
|
||
for dest, src in prerequisites: | ||
graph[src].append(dest) | ||
indegree[dest] += 1 | ||
|
||
queue = deque([i for i in range(numCourses) if indegree[i] == 0]) | ||
processed_count = 0 | ||
|
||
while queue: | ||
node = queue.popleft() | ||
processed_count += 1 | ||
for neighbor in graph[node]: | ||
indegree[neighbor] -= 1 | ||
if indegree[neighbor] == 0: | ||
queue.append(neighbor) | ||
|
||
return processed_count == numCourses | ||
|
||
''' | ||
2. DFS | ||
์ํ ํ์ง | ||
''' | ||
def canFinishCycleDetection(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
graph = defaultdict(list) | ||
|
||
for dest, src in prerequisites: | ||
graph[src].append(dest) | ||
|
||
statuses = {i: Status.INITIAL for i in range(numCourses)} | ||
|
||
def dfs(node): | ||
if statuses[node] == Status.IN_PROGRESS: | ||
return False | ||
if statuses[node] == Status.FINISHED: | ||
return True | ||
|
||
statuses[node] = Status.IN_PROGRESS | ||
for neighbor in graph[node]: | ||
if not dfs(neighbor): | ||
return False | ||
statuses[node] = Status.FINISHED | ||
return True | ||
|
||
return all(dfs(crs) for crs in range(numCourses)) | ||
|
||
''' | ||
3. @cache | ||
ํ์ด์ฌ 3.9 ์ด์์์ ์ฌ์ฉํ๋ ๋ฉ๋ชจ์ด์ ์ด์ ๋ฐ์ฝ๋ ์ดํฐ | ||
- ๋์ผ ์ ๋ ฅ -> ๋์ผ ์ถ๋ ฅ์ ๋ณด์ฅํ๋ค. | ||
- 128๊ฐ ๊น์ง๋ง ์ ์ฅํ๋ @lru_cache๋ ์๋ค. | ||
''' | ||
def canFinishWithCache(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
graph = defaultdict(list) | ||
|
||
for dest, src in prerequisites: | ||
graph[src].append(dest) | ||
|
||
traversing = set() | ||
|
||
@cache | ||
def dfs(node): | ||
if node in traversing: | ||
return False | ||
|
||
traversing.add(node) | ||
result = all(dfs(pre) for pre in graph[node]) | ||
traversing.remove(node) | ||
return result | ||
|
||
return all(dfs(node) for node in range(numCourses)) | ||
|
||
''' | ||
4. visited๊ณผ ํจ๊ป ์ฌ์ฉํ๊ธฐ | ||
@cache ๋ฐ์ฝ๋ ์ดํฐ๋ ๋ฉ๋ชจ์ด์ ์ด์ , ๊ฐ์ ์ ๋ ฅ๊ฐ์ ๋ฐ๋ผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๊ฒํจ | ||
๊ฒฐ๊ณผ๊ฐ ๋ณํ์ง ์์ ๋ ์ ์ฉํจ => dfs(node)๋ ์ธ๋ถ ์ํ ์ํ traversing์ ์์กดํด์ ๋์์ด ๋ฌ๋ผ์ง ์ ์๋ค. | ||
๋ฐ๋ผ์ visited set์ด ๋ ์์ฐ์ค๋ฌ์ธ ์ ์๋ค | ||
''' | ||
def canFinishWithVisited(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
graph = defaultdict(list) | ||
|
||
for dest, src in prerequisites: | ||
graph[src].append(dest) | ||
|
||
traversing = set() | ||
visited = set() | ||
|
||
def dfs(node): | ||
if node in traversing: | ||
return False | ||
if node in visited: | ||
return True | ||
|
||
traversing.add(node) | ||
for pre in graph[node]: | ||
if not dfs(pre): | ||
return False | ||
traversing.remove(node) | ||
|
||
visited.add(node) | ||
return True | ||
|
||
return all(dfs(i) for i in range(numCourses)) |