-
Notifications
You must be signed in to change notification settings - Fork 2
feat(algorithms graphs): course schedule #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c270e15
refactor(algorithms, graphs, course-schedule): additional alternative…
BrianLusina 617ab62
updating DIRECTORY.md
396709a
ci(github): update pre-commit workflow versions
BrianLusina 8641f40
docs(readme): add codacy badge
BrianLusina 0ef2896
chore(lint): format fixes
BrianLusina 5a5d7a1
chore(lint): minor format fixes
BrianLusina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,41 +1,127 @@ | ||
| from collections import defaultdict | ||
| from typing import List | ||
| from collections import defaultdict, deque | ||
| from typing import List, Dict, Deque | ||
|
|
||
| WHITE = 1 | ||
| GRAY = 2 | ||
| BLACK = 3 | ||
|
|
||
|
|
||
| def find_order(num_courses: int, prerequisites: List[List[int]]) -> List[int]: | ||
| """ | ||
| This function finds the topological sorted order of the courses given the prerequisites. | ||
|
|
||
| Args: | ||
| num_courses (int): The total number of courses. | ||
| prerequisites (List[List[int]]): A list of tuples where each tuple contains the source and destination of the prerequisite. | ||
|
|
||
| Returns: | ||
| List[int]: A list of the courses in the topological sorted order. If there is no valid order, return an empty list. | ||
|
|
||
| """ | ||
| adjacency_list = defaultdict(list) | ||
|
|
||
| # Populate the adjacency list with the prerequisites | ||
| for destination, source in prerequisites: | ||
| adjacency_list[source].append(destination) | ||
|
|
||
| topological_sorted_order = [] | ||
| is_possible = True | ||
|
|
||
| # Use a dictionary to keep track of the color of each node | ||
| color = {k: WHITE for k in range(num_courses)} | ||
|
|
||
| def dfs(node): | ||
| """ | ||
| This function performs a depth-first search on the graph. | ||
|
|
||
| Args: | ||
| node (int): The current node being visited. | ||
|
|
||
| Returns: | ||
| None | ||
|
|
||
| """ | ||
| nonlocal is_possible | ||
|
|
||
| # If there is no valid order, return immediately | ||
| if not is_possible: | ||
| return | ||
|
|
||
| # Mark the current node as gray | ||
| color[node] = GRAY | ||
|
|
||
| # If the current node has any neighbours, visit them | ||
| if node in adjacency_list: | ||
| for neighbour in adjacency_list[node]: | ||
| # If the neighbour is white, visit it | ||
| if color[neighbour] == WHITE: | ||
| dfs(neighbour) | ||
| # If the neighbour is gray, there is no valid order | ||
| elif color[neighbour] == GRAY: | ||
| is_possible = False | ||
|
|
||
| # Mark the current node as black | ||
| color[node] = BLACK | ||
| topological_sorted_order.append(node) | ||
|
|
||
| # Visit all the nodes | ||
| for vertex in range(num_courses): | ||
| if color[vertex] == WHITE: | ||
| dfs(vertex) | ||
|
|
||
| # If there is no valid order, return an empty list | ||
| return topological_sorted_order[::-1] if is_possible else [] | ||
|
|
||
|
|
||
| def can_finish( | ||
| num_courses: int, | ||
| prerequisites: List[List[int]] | ||
| ) -> bool: | ||
| """ | ||
| Determines if there is a valid order of courses such that | ||
| all prerequisites are satisfied. | ||
|
|
||
| Args: | ||
| num_courses (int): The total number of courses. | ||
| prerequisites (List[List[int]]): A list of tuples where each tuple contains the source and destination of the prerequisite. | ||
|
|
||
| Returns: | ||
| bool: True if there is a valid order, False otherwise. | ||
| """ | ||
| counter: int = 0 | ||
| if num_courses <= 0: | ||
| return True | ||
|
|
||
| # Initialize the in-degree of all nodes to 0 | ||
| in_degree: Dict[int, int] = {i: 0 for i in range(num_courses)} | ||
| # Initialize an adjacency list to store the graph | ||
| graph: Dict[int, List[int]] = {i: [] for i in range(num_courses)} | ||
|
|
||
| # Populate the adjacency list and the in-degree of all nodes | ||
| for child, parent in prerequisites: | ||
| if parent in graph: | ||
| graph[parent].append(child) | ||
| else: | ||
| graph[parent] = [child] | ||
| if child in in_degree: | ||
| in_degree[child] += 1 | ||
| else: | ||
| in_degree[child] = 1 | ||
|
|
||
| # Initialize a queue to store all nodes with an in-degree of 0 | ||
| sources: Deque[int] = deque() | ||
| for key in in_degree: | ||
| if in_degree[key] == 0: | ||
| sources.append(key) | ||
|
|
||
| # Perform a BFS traversal of the graph | ||
| while sources: | ||
| course: int = sources.popleft() | ||
| counter += 1 | ||
| for child in graph[course]: | ||
| in_degree[child] -= 1 | ||
| if in_degree[child] == 0: | ||
| sources.append(child) | ||
|
|
||
| # If all nodes have been visited, return True | ||
| return counter == num_courses |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,89 @@ | ||
| import unittest | ||
| from . import find_order, can_finish | ||
|
|
||
|
|
||
| class FindOrderTestCase(unittest.TestCase): | ||
| def test_1(self): | ||
| num_courses = 2 | ||
| prerequisites = [[1,0]] | ||
| expected = [0, 1] | ||
| actual = find_order(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_2(self): | ||
| num_courses = 4 | ||
| prerequisites = [[1,0],[2,0],[3,1],[3,2]] | ||
| expected = [0,2,1,3] | ||
| actual = find_order(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| def test_3(self): | ||
| num_courses = 1 | ||
| prerequisites = [] | ||
| expected = [0] | ||
| actual = find_order(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_4(self): | ||
| num_courses = 3 | ||
| prerequisites = [[1,0],[2,1]] | ||
| expected = [0,1,2] | ||
| actual = find_order(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_5(self): | ||
| num_courses = 3 | ||
| prerequisites = [[1,0],[2,1],[1,2]] | ||
| expected = [] | ||
| actual = find_order(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
| def test_6(self): | ||
| num_courses = 3 | ||
| prerequisites = [[1,0],[2,1],[4,3]] | ||
| expected = [0,1,2] # or [0,1,2,3,4] | ||
| actual = find_order(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertEqual(expected, actual) | ||
|
|
||
|
|
||
| class CanFinishTestCases(unittest.TestCase): | ||
| def test_1(self): | ||
| num_courses = 2 | ||
| prerequisites = [[1,0]] | ||
| actual = can_finish(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertTrue(actual) | ||
|
|
||
| def test_2(self): | ||
| num_courses = 4 | ||
| prerequisites = [[1,0],[2,0],[3,1],[3,2]] | ||
| actual = can_finish(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertTrue(actual) | ||
|
|
||
| def test_3(self): | ||
| num_courses = 1 | ||
| prerequisites = [] | ||
| actual = can_finish(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertTrue(actual) | ||
|
|
||
| def test_4(self): | ||
| num_courses = 3 | ||
| prerequisites = [[1,0],[2,1]] | ||
| actual = can_finish(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertTrue(actual) | ||
|
|
||
| def test_5(self): | ||
| num_courses = 3 | ||
| prerequisites = [[1,0],[2,1],[1,2]] | ||
| actual = can_finish(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertFalse(actual) | ||
|
|
||
| def test_6(self): | ||
| num_courses = 3 | ||
| prerequisites = [[1,0],[2,1],[4,3]] | ||
| actual = can_finish(num_courses=num_courses, prerequisites=prerequisites) | ||
| self.assertTrue(actual) | ||
BrianLusina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.