First thing to make sure before using bitmasks for solving a problem is that it must be having small constraints, as solutions which use bitmasking generally take up exponential time and memory.
Let's first try to understand what Bitmask means. Mask in Bitmask means hiding something. Bitmask is nothing but a binary number that represents something. Let's take an example. Consider the set . We can represent any subset of using a bitmask of length , with an assumption that if bit is set then it means element is present in subset. So the bitmask represents the subset
Now the benefit of using bitmask. We can set the bit, unset the bit, check if bit is set in just one step each. Let's say the bitmask, .
Set the bit: . Let , so,
Unset the bit: . Let , so,
Check if bit is set: , doing this operation, if bit is set, we get a non zero integer otherwise, we get zero. Let
Let's take a problem, given a set, count how many subsets have sum of elements greater than or equal to a given value.
Algorithm is simple:
solve(set, set_size, val)
count = 0
for x = 0 to power(2, set_size)
sum = 0
for k = 0 to set_size
if kth bit is set in x
sum = sum + set[k]
if sum >= val
count = count + 1
return count
To iterate over all the subsets we are going to each number from 0 to 2set_size-1
The above problem simply uses bitmask and complexity is O(2nn).
The above problem simply uses bitmask and complexity is O(2nn).
Now, let's take another problem that uses dynamic programming along with bitmasks.
Assignment Problem:
There are persons and tasks, each task is to be alloted to a single person. We are also given a matrix of size , where denotes, how much person is going to charge for task . Now we need to assign each task to a person in such a way that the total cost is minimum. Note that each task is to be alloted to a single person, and each person will be alloted only one task.
There are persons and tasks, each task is to be alloted to a single person. We are also given a matrix of size , where denotes, how much person is going to charge for task . Now we need to assign each task to a person in such a way that the total cost is minimum. Note that each task is to be alloted to a single person, and each person will be alloted only one task.
The brute force approach here is to try every possible assignment. Algorithm is given below:
assign(N, cost)
for i = 0 to N
assignment[i] = i //assigning task i to person i
res = INFINITY
for j = 0 to factorial(N)
total_cost = 0
for i = 0 to N
total_cost = total_cost + cost[i][assignment[i]]
res = min(res, total_cost)
generate_next_greater_permutation(assignment)
return res
The complexity of above algorithm is , well that's clearly not good.
Let's try to improve it using dynamic programming. Suppose the state of is , where represents that person to have been assigned a task, and is a binary number, whose bit represents if the task has been assigned or not.
Now, suppose, we have , we can assign a task to person , iff task is not yet assigned to any peron i.e. then, will be given as:
Now, suppose, we have , we can assign a task to person , iff task is not yet assigned to any peron i.e. then, will be given as:
One thing to note here is is always equal to the number set bits in , so we can remove that. So the dp state now is just , and if we have , then
Complete algorithm is given below:
assign(N, cost)
for i = 0 to power(2,N)
dp[i] = INFINITY
dp[0] = 0
for mask = 0 to power(2, N)
x = count_set_bits(mask)
for j = 0 to N
if jth bit is not set in i
dp[mask|(1<<j)] = min(dp[mask|(1<<j)], dp[mask]+cost[x][j])
return dp[power(2,N)-1]
Time complexity of above algorithm is and space complexity is .This is just one problem that can be solved using DP+bitmasking. There's a whole lot.
Let's go to another problem, suppose we are given a graph and we want to find out if it contains a Hamiltonian Path. This problem can also be solved using DP+bitmasking in time complexity and space complexity. Here's a link to it's solution.
Không có nhận xét nào:
Đăng nhận xét