Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/topological-sort-problems
Topological Sort Problems
https://github.com/emahtab/topological-sort-problems
kahns-alogrithm leetcode topological-sort
Last synced: 15 days ago
JSON representation
Topological Sort Problems
- Host: GitHub
- URL: https://github.com/emahtab/topological-sort-problems
- Owner: eMahtab
- Created: 2024-10-28T02:25:11.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-10-28T02:41:32.000Z (about 2 months ago)
- Last Synced: 2024-10-28T06:15:11.009Z (about 2 months ago)
- Topics: kahns-alogrithm, leetcode, topological-sort
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Topological Sort Problems : O(V+E)
Topological sort of a directed graph is a linear ordering of its vertices such that for every directed edge (u,v) from vertex u to vertex v, u comes before v in the ordering.
When topological sort is implemented using **Kahn's Algorithm (BFS with a queue)**, the time complexity and space complexity are as follows:
### Time Complexity : O(V+E)
##### Explanation:
1. Counting in-degrees takes 𝑂(𝐸) time because we iterate over each edge once.
2. Enqueueing and dequeueing each vertex takes 𝑂(𝑉) time.
3. Processing each neighbor of each vertex (decreasing in-degrees and enqueuing zero in-degree nodes) takes 𝑂(𝐸) time as each edge is visited once.### Space Complexity : : O(V+E)
##### Explanation:
The space complexity is also 𝑂(𝑉+𝐸) because:
1. We need space to store the graph (using an adjacency list) with 𝑉 vertices and 𝐸 edges, which takes 𝑂(𝑉+𝐸).
2. We maintain an in-degree array of size 𝑉, taking 𝑂(𝑉).
3. The queue holds vertices with zero in-degrees, and in the worst case, all vertices could be added to the queue, so it also takes 𝑂(𝑉) space.## Problems :
1. https://leetcode.com/problems/course-schedule
2. https://leetcode.com/problems/course-schedule-ii
3. https://leetcode.com/problems/parallel-courses
4. https://leetcode.com/problems/find-all-possible-recipes-from-given-supplies
5. https://leetcode.com/problems/alien-dictionary