Bit Manipulation     Bitmask     C++     Combinatorics     Depth-First Search     Dynamic Programming     Graph     Hard    

Problem Statement:

You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCoursei], representing a prerequisite relationship between course prevCoursei and course nextCoursei: course prevCoursei has to be taken before course nextCoursei. Also, you are given the integer k.

In one semester, you can take at most k courses as long as you have taken all the prerequisites in the previous semesters for the courses you are taking.

Return the minimum number of semesters needed to take all courses. The testcases will be generated such that it is possible to take every course.

 

Example 1:

Input: n = 4, relations = [[2,1],[3,1],[1,4]], k = 2
Output: 3
Explanation: The figure above represents the given graph.
In the first semester, you can take courses 2 and 3.
In the second semester, you can take course 1.
In the third semester, you can take course 4.

Example 2:

Input: n = 5, relations = [[2,1],[3,1],[4,1],[1,5]], k = 2
Output: 4
Explanation: The figure above represents the given graph.
In the first semester, you can only take courses 2 and 3 since you cannot take more than two per semester.
In the second semester, you can take course 4.
In the third semester, you can take course 1.
In the fourth semester, you can take course 5.

 

Constraints:

  • 1 <= n <= 15
  • 1 <= k <= n
  • 0 <= relations.length <= n * (n-1) / 2
  • relations[i].length == 2
  • 1 <= prevCoursei, nextCoursei <= n
  • prevCoursei != nextCoursei
  • All the pairs [prevCoursei, nextCoursei] are unique.
  • The given graph is a directed acyclic graph.

Solution:

This problem contains concepts of DFS + DP + Bitmask + combinatorics. The underlying idea is that for every K-combination of viable courses at a moment, choose the combination that minimizes answer. Bitmask represents courses with 1 for not done and 0 for done. Initially we have all ones (no course is done) and we stop when mask has all zeros (all courses are done).

We need a utility function with signature vector<vector<int>> combinations(vector<int>v, int k) which given an array of numbers v, gives us all possible combinations of size k. Viable courses are stored in array doable.

 
class Solution 
{
    unordered_map<int,int>cache;
public:
    int dfs(int n, vector<vector<int>>&G, vector<int> inDegree, int mask, int k)
    {
        if (mask==0) return 0;
        if (cache.count(mask)) return cache[mask];
        vector<int> doable;
        for(int i=0; i<n; i++) if((mask&(1<<i)) && !inDegree[i]) doable.push_back(i);
        int res = INT_MAX;
        for (auto comb: combinations(doable, k))
        {
            int newMask = mask;
            vector<int> newInDegree = inDegree;
            for (int u: comb)
            {
                newMask ^= (1<<u);
                for(int v: G[u]) newInDegree[v]--;
            }
            res = min(res, 1+dfs(n, G, newInDegree, newMask, k));
        }
        return cache[mask] = res;
    }

    int minNumberOfSemesters(int n, vector<vector<int>>& relations, int k) 
    {
        vector<vector<int>> G(n);
        vector<int> inDegree(n,0);
        for(auto r: relations)
        {
            int u=r[0]-1, v=r[1]-1;
            G[u].push_back(v);
            inDegree[v]++;
        }
        return dfs(n, G, inDegree, (1<<n)-1, k);
    }

    vector<vector<int>> combinations(vector<int>v, int k)
    {
        if (v.size()<=k) return {v};
        vector<int> cur;
        vector<vector<int>> res;
        generateCombinations(v, k, cur, 0, res);
        return res;
    }

    void generateCombinations(vector<int>&v, int k, vector<int>&cur, int index, vector<vector<int>>&res)
    {
        if (k==0){res.push_back(cur); return;}
        if (index==v.size()) return;
        cur.push_back(v[index]);
        generateCombinations(v, k-1, cur, index+1, res);
        cur.pop_back();
        generateCombinations(v, k, cur, index+1, res);
    }
};