zoukankan      html  css  js  c++  java
  • [LeetCode] Course Schedule I (207) & II (210) 解题思路

    207. Course Schedule

    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, is it possible for you to finish all courses?

    For example:

    2, [[1,0]]

    There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.

    2, [[1,0],[0,1]]

    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.

    问题:已知课程数量,以及各个课程间的依赖关系,求是否可以满足所有依赖关系把课程上完。

    这是典型的拓扑排序应用场景。是否满足所有依赖关系,实际上就是求,在课程为节点、依赖关系为边的图中,是否存在环。若存在还,则不可能满足所有依赖关系把课程上完,反之,则可能。

    使用DFS 方式遍历图中的节点。使用白、灰、黑三种颜色表示 DFS 在图中遍历进度。

    当前节点被访问时的颜色含义:

    • 白色:当前节点首次被访问。说明可以继续深度探索。
    • 灰色:当前节点已在当前的 DFS 路径中。说明存在环。
    • 黑色:当前节点已经从节点出发的后续路径已全部被访问。

    判断1 : 一个节点以及从该节点出发的后续路径是否存在环:

    • 若该节点为白色,则将颜色改为灰色,表示该节点已在搜索路径上。继续深度遍历节点的邻居节点,直达所有邻接节点已经邻居节点的后续节点都已全部被访问,将该节点改为黑色。不存在环。
    • 若该节点为灰色,表示该节点以及在 DFS 搜索路径上,存在环。退出程序。
    • 若该节点为黑色,表示节点已经后续路径已全部被访问,无需在探索。不存在环。

    对所有节点都进行判断1 检查,即可知道图中是否存在环。

     1 const int WHITE = 0;
     2 const int GRAY = 1;
     3 const int BLACK = 2;
     4 
     5 class gnode{
     6 public:
     7     int val;
     8     int col;
     9     vector<gnode*> neighbours;
    10     
    11     gnode(int val){
    12         this->val = val;
    13     }
    14 };
    15 
    16 
    17 
    18     // 当要访问的节点颜色为灰色时,表示该节点已在搜索路径中,则形成了环。有还,无法完成。
    19     bool canFinshNode(gnode* node){
    20     
    21         if (node->col == WHITE) {
    22             node->col = GRAY;
    23         }else if(node->col == BLACK){
    24             return true;
    25         }else{
    26             // be gray
    27             return false;
    28         }
    29         
    30         for (int i = 0 ; i < node->neighbours.size(); i++) {
    31             gnode* tmpn = node->neighbours[i];
    32             bool res = canFinshNode(tmpn);
    33             if (res == false) {
    34                 return false;
    35             }
    36         }
    37         
    38         node->col = BLACK;
    39         
    40         return true;
    41     }
    42 
    43     bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
    44     
    45         map<int, gnode*> val_node;
    46         for (int i = 0 ; i < numCourses; i++) {
    47             gnode* node = new gnode(i);
    48             node->col = WHITE;
    49             val_node[i]    = node;
    50         }
    51         
    52         pair<int, int> pp;
    53         for (int i = 0 ; i < prerequisites.size(); i++) {
    54             pp = prerequisites[i];
    55             val_node[pp.second]->neighbours.push_back(val_node[pp.first]);
    56         }
    57         
    58         map<int, gnode*>::iterator m_iter;
    59         for (m_iter = val_node.begin(); m_iter != val_node.end(); m_iter++) {
    60             if (m_iter->second->col == WHITE) {
    61                 
    62                 bool res = canFinshNode(m_iter->second);
    63                 if (res == false) {
    64                     return false;
    65                 }
    66             }
    67         }
    68         
    69         return true;
    70     }
    View Code

    210. 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.

    For example:

    2, [[1,0]]

    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]

    4, [[1,0],[2,0],[3,1],[3,2]]

    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].

    问题:和上面问题相似,不同的是需要额外求出可行的一个课程安排顺序。

    典型的拓扑排序问题,而且这次需要求确切的拓扑顺序。

    虽然 问题1 中的代码只回答了是否有可能存在拓扑顺序,但是,实际上在求解过程中已经将拓扑顺序求出来了。所有只需要将少量代码,将求出的顺序保存并返回即可。

    当一个节点变为黑色,则表示该节点课程所依赖的后续课程都已排序,新变黑得节点就是下一个被排序的课程。

     1 const int WHITE = 0;
     2 const int GRAY = 1;
     3 const int BLACK = 2;
     4 
     5 class gnode{
     6 public:
     7     int val;
     8     int col;
     9     vector<gnode*> neighbours;
    10     
    11     gnode(int val){
    12         this->val = val;
    13     }
    14 };
    15 
    16     vector<int> resVec;
    17 
    18     /**
    19      * To varify if the path which souce is node has a circle . 
    20      * If has a circle, return false;
    21      * If has no circle, push node into res vector after visit the neighbours of node, and return true;
    22      *
    23      */
    24     bool visit(gnode* node){
    25         
    26         if (node->col == WHITE) {
    27             node->col = GRAY;
    28         }else if (node->col == BLACK){
    29             return true;
    30         }else{
    31             // be Gray, has a circle.
    32             return false;
    33         }
    34         
    35         for (int i = 0 ; i < node->neighbours.size(); i++) {
    36             gnode* tmp = node->neighbours[i];
    37             int res = visit(tmp);
    38             if (res == false) {
    39                 return false;
    40             }
    41         }
    42         
    43         node->col = BLACK;
    44         
    45         resVec.push_back(node->val);
    46         
    47         return true;
    48     }
    49 
    50     vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
    51         
    52         vector<int> emptyV;
    53         
    54         // draw the graph representing the prerequisities relationship
    55         map<int, gnode*> val_node;
    56         for (int i = 0 ; i < numCourses; i++) {
    57             gnode* node = new gnode(i);
    58             node->col = WHITE;
    59             val_node[i] = node;
    60         }
    61         
    62         for (int i = 0 ; i < prerequisites.size(); i++) {
    63             pair<int, int> pp = prerequisites[i];
    64             val_node[pp.second]->neighbours.push_back(val_node[pp.first]);
    65         }
    66         
    67         map<int, gnode*>::iterator m_iter;
    68         for (m_iter = val_node.begin(); m_iter != val_node.end(); m_iter++) {
    69             if (m_iter->second->col == WHITE) {
    70                 bool res = visit(m_iter->second);
    71                 if (res == false) {
    72                     return emptyV;
    73                 }
    74             }
    75         }
    76         
    77         // has a variable order
    78         vector<int> resOdr(resVec.size());
    79         for (int i = 0 ; i < resVec.size(); i++) {
    80             resOdr[i] = resVec[resVec.size() - i - 1];
    81         }
    82         
    83         return resOdr;
    84     }
    View Code

    参考思路:

    《算法导论》22.3 深度优先搜索, P349

    《算法导论》22.4 拓扑排序, P355

  • 相关阅读:
    [USACO18DEC]Fine Dining
    [USACO18DEC]Cowpatibility(容斥 or bitset优化暴力)
    [P2387魔法森林
    P4172 [WC2006]水管局长
    P2486 [SDOI2011]染色
    P3950部落冲突
    P4332三叉神经树
    莫比乌斯反演习题总结
    牛客 斐波那契数列问题的递归和动态规划3
    牛客 统计和生成所有不同的二叉树
  • 原文地址:https://www.cnblogs.com/TonyYPZhang/p/5118760.html
Copyright © 2011-2022 走看看