Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d1f4047

Browse files
Course Schedule
1 parent 480cdbc commit d1f4047

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

‎Graphs/207-Course-Schedule.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'''
2+
Leetcode - https://leetcode.com/problems/course-schedule/
3+
'''
4+
'''
5+
207. Course Schedule
6+
7+
There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
8+
9+
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
10+
Return true if you can finish all courses. Otherwise, return false.
11+
12+
13+
Example 1:
14+
15+
Input: numCourses = 2, prerequisites = [[1,0]]
16+
Output: true
17+
Explanation: There are a total of 2 courses to take.
18+
To take course 1 you should have finished course 0. So it is possible.
19+
'''
20+
21+
class Solution:
22+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
23+
def is_cycle(node):
24+
if visited[node] == 1:
25+
return True
26+
if visited[node] == 2:
27+
return False
28+
29+
visited[node] = 1
30+
31+
if node in graph:
32+
for neigh in graph[node]:
33+
if is_cycle(neigh):
34+
return True
35+
36+
visited[node] = 2
37+
return False
38+
39+
graph = {}
40+
for course, prereq in prerequisites:
41+
if prereq not in graph:
42+
graph[prereq] = []
43+
graph[prereq].append(course)
44+
45+
visited = [0]*numCourses
46+
47+
for course in range(numCourses):
48+
if is_cycle(course):
49+
return False
50+
51+
return True
52+
53+
# T:O(V + E)
54+
55+
'''
56+
Explaination -
57+
58+
for example---
59+
60+
numCourses = 4
61+
prerequisites = [[1, 0], [2, 1], [3, 2], [0, 3]]
62+
63+
Step 1: Initialize Variables and Graph
64+
65+
-- numCourses is 4, meaning there are 4 courses labeled from 0 to 3.
66+
-- prerequisites is a list of prerequisite pairs: [1, 0], [2, 1], [3, 2], and [0, 3].
67+
68+
Step 2: Build the Graph
69+
We create an adjacency list to represent the directed graph based on the prerequisite pairs:
70+
71+
Graph:
72+
0 -> [3]
73+
1 -> [0]
74+
2 -> [1]
75+
3 -> [2]
76+
77+
Step 3: Perform DFS to Detect Cycles
78+
We'll perform Depth-First Search (DFS) to traverse the graph and detect cycles. The DFS function will mark nodes as visiting and visited while exploring the graph.
79+
80+
Starting DFS from each node:
81+
82+
- Node 0:
83+
- Marked as visiting (visited[0] = 1)
84+
- Visit its neighbor 3
85+
- Node 3:
86+
- Marked as visiting (visited[3] = 1)
87+
- Visit its neighbor 2
88+
- Node 2:
89+
- Marked as visiting (visited[2] = 1)
90+
- Visit its neighbor 1
91+
- Node 1:
92+
- Marked as visiting (visited[1] = 1)
93+
- Visit its neighbor 0
94+
- Node 0:
95+
- Already visiting (visited[0] = 1)
96+
- Cycle detected, return True
97+
98+
Result: A cycle was detected during the DFS traversal, indicating that it's not possible to finish all courses due to the cyclic dependency.
99+
100+
Final Output: False
101+
102+
In this example, the code correctly identified that there is a cycle in the graph, which means
103+
it's not possible to finish all courses with the given prerequisites. The function returns False.
104+
The cycle [0, 1, 2, 3, 0] was detected during the traversal.
105+
106+
'''

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ Check the notes for the explaination - [Notes](https://stingy-shallot-4ea.notion
4242
- [x] [Graphs](Graphs)
4343
- [x] [Number of Islands](Graphs/200-Number-of-Islands.py)
4444
- [x] [Clone Graph](Graphs/133-Clone-Graph.py)
45+
- [x] [Course Schedule](Graphs/207-Course-Schedule.py)
4546

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /