Friday, June 7, 2019

Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
Example 1:
Input: 2, [[1,0]] 
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished   
             course 0. So the correct course order is [0,1] .
Example 2:
Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both     
             courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. 
             So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .
Note:
  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

Solution:

class GraphNode {
    public:
    int val;
    vector<int> neighbors;
    GraphNode (int val) {
        val = val;
    }
};

class Solution {
private:
    
public:
    void topoSortDFS(int index, unordered_map<int, int>& visited,
                    vector<int>& ans, vector<GraphNode>& graph,
                    bool& cycle) {
        if (visited[index] == 2) {
            return;
        }
        
        if (visited[index] == 1) {
            cycle = true;
            return;
        }

        visited[index] = 1;
        for (int i = 0; i < graph[index].neighbors.size(); i++) {
            topoSortDFS(graph[index].neighbors[i], visited, ans, graph, cycle);
        }
        ans.push_back(index);
        visited[index] = 2;
        return;
    }
    
    void topoSort(vector<GraphNode>& graph,
                  unordered_map<int, int>& visited,
                  vector<int>& ans,
                bool& cycle) {
        for (int i = 0; i < graph.size(); i++) {
            topoSortDFS(i, visited, ans, graph, cycle);
        }
        return;
    }

    void prepareGraph(vector<GraphNode>& graph,
                      vector<vector<int>>& prerequisites) {
        for (int i = 0; i < prerequisites.size(); i++) {
            vector<int> neighbor = prerequisites[i];
            graph[neighbor[1]].neighbors.push_back(neighbor[0]);
        }
    }

    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<int> ans;
        unordered_map<int, int> visited;
        bool cycle = false;
        for (int i = 0; i < numCourses; i++) {
            visited[i] = 0;
        }
        
        // Prepare graph.
        vector<GraphNode> graph;
        for (int i = 0; i < numCourses; i++) {
            GraphNode node = GraphNode(i);
            graph.push_back(node);
        }
        prepareGraph(graph, prerequisites);
        
        // ToPo Sort.
        topoSort(graph, visited, ans, cycle);
        if (cycle) {
            vector<int> empty;
            return empty;
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

=========== Another smaller one ========
class Solution { public: vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) { p_graph = vector<vector<int>>(numCourses); for (const auto& p : prerequisites) { p_graph[p[1]].push_back(p[0]); } vector<int> visit(numCourses, 0); for (int i = 0; i < numCourses; ++i) { if (dfs(i, visit)) return {}; } reverse(p_order.begin(), p_order.end()); return p_order; } private: vector<vector<int>> p_graph; vector<int> p_order; bool dfs(int cur, vector<int>& v) { if (v[cur] == 1) return true; if (v[cur] == 2) return false; v[cur] = 1; for (const auto& t: p_graph[cur]) { if (dfs(t, v)) return true; } v[cur] = 2; p_order.push_back(cur); return false; } };

No comments:

Post a Comment