<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 {
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        
        
        // Create the map
        unordered_map<int, vector<int>> graph;
        unordered_map<int, int> incomingEdges;
        // Create the graph        
        for(int i = 0; i < prerequisites.size(); i++) {
            if(graph.count(prerequisites[i][1]) == 0) {
                vector<int> preqs;
                preqs.push_back(prerequisites[i][0]);
                graph[prerequisites[i][1]] = preqs;
            } else {
                graph[prerequisites[i][1]].push_back(prerequisites[i][0]);
            }   
             incomingEdges[prerequisites[i][0]]++;
        }

        // Find initial nodes with nothing pointing to htem  
        vector<int> noIncoming;
        unordered_set<int> visited;
        for(vector<int> keys : prerequisites) {
            
            if(!visited.contains(keys[0]) && incomingEdges[keys[0]] == 0) {
               
                noIncoming.push_back(keys[0]);
                visited.insert(keys[0]);
            }

            if(!visited.contains(keys[1]) && incomingEdges[keys[1]] == 0) {
                
                noIncoming.push_back(keys[1]);
                visited.insert(keys[1]);
            }

        }             

        // for(int val : noIncoming) cout << val << ", ";
        // cout << endl;

        // Checking to see if a cycle exists by trying to perform a topological sort.
        // Essentially, find a node with no incoming edges, delete it, and continue.
        // If we run out of nodes with no incoming edges whilst numCourses != 0, then
        // that means we have a cycle.
       
        while(noIncoming.size() != 0) {
            int remove = noIncoming.back();
            noIncoming.pop_back();
            vector<int> neighbors = graph[remove];
            visited = unordered_set<int>();

            for(int val : neighbors) {
                incomingEdges[val]--;
                if(!visited.contains(val) && incomingEdges[val] == 0) {
                    visited.insert(val);
                    noIncoming.push_back(val);
                }
            }

        }

        for(auto val : incomingEdges){
            if(val.second != 0) {
                return false;
            }
        }

        return true;
    }
};
22400