<p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p>

<ul>
	<li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li>
</ul>

<p>Return <code>true</code> if you can finish all courses. Otherwise, return <code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
<strong>Output:</strong> true
<strong>Explanation:</strong> There are a total of 2 courses to take. 
To take course 1 you should have finished course 0. So it is possible.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> There are a total of 2 courses to take. 
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= numCourses &lt;= 2000</code></li>
	<li><code>0 &lt;= prerequisites.length &lt;= 5000</code></li>
	<li><code>prerequisites[i].length == 2</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; numCourses</code></li>
	<li>All the pairs prerequisites[i] are <strong>unique</strong>.</li>
</ul>

class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        indegrees = [0] * numCourses

        G = [[] for _ in range(numCourses)]

        totalEdges = 0
        # create adj list based on pre reqs
        for (u,v) in prerequisites:
            # this format is tricky. it's saying v -> u
            G[v].append(u)
            indegrees[u] += 1
            totalEdges += 1

        bfs = [n for n in range(numCourses) if indegrees[n] == 0]

        removedEdges = 0
        levels = 0

        while bfs:
            levelSize = len(bfs)
            for l in range(levelSize):
                el = bfs.pop(0)
                for n in G[el]:
                    indegrees[n] -= 1
                    removedEdges += 1

                    if indegrees[n] == 0:
                        bfs.append(n)
            print(levels)
            levels += 1
        
        print(levels)
        return removedEdges == totalEdges


















        # keep track of indegrees
        indegrees = [0] * numCourses

        G = [[] for _ in range(numCourses)]

        totalEdges = 0
        # create adj list based on preq reqs
        for (u,v) in prerequisites:
            G[v].append(u)
            indegrees[u] += 1
            totalEdges += 1

        # find nodes with indegrees of 0, instantiate a queue
        bfs = [n for n in range(numCourses) if indegrees[n] == 0]

        removedEdges = 0

        # process the queue in a bfs fashion
        while bfs:
            el = bfs.pop(0)
            for n in G[el]:
                # decrement neighbors' indegree
                indegrees[n] -=1
                removedEdges +=1
                # if any neighbors have 0 indegree, put them into a queue to process next iteration
                if indegrees[n] == 0:
                    bfs.append(n)

        # if after processing all the prerequisites, we find that the topological order length does not match the num of courses
        return removedEdges == totalEdges
        # then it is false
17900