Use bit masking and dynamic programming to solve the minimum cost task allocation problem Bitmasking & Dynamic Programming

Use bit masking and dynamic programming to solve problems Bitmasking & Dynamic Programming

Basics of Bit Masking

The previous article briefly introduced the assignment operation of the bit mask. Before solving the problem, I will introduce the value operation and check operation of the bit mask.

De-value operation:

b & !(1 << i)

example:

取i = 1, (1 << i) = 00010

!(1 << i) = 11101 (inverted each bit)

b = 01010

b &! (1 << i) = 01010 & 11101 = 01000 (the value in 1 bit has been removed)

Check operation:

b & (1 << i)

example:

取i = 1, (1 << i) = 00010

b = 01010

b & (1 << i) = 01010 & 00010 = 00010 (the result is not 0) then the check bit has a value, that is, the child contains the checked value.

Solve problems with bit masking and dynamic programming

topic:

A total of N tasks need to be assigned to N people, each person completes a task, and an N x N matrix is ​​given, cost[i][j] represents the cost of the i-th person to complete the j-th task. Now we need to find the least costly solution for allocating tasks.

Violent solution:
assign(N, cost)
	for i = 0 to N
		assignment[i] = 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 brute force solution will traverse all the arrangements, and then return the value of the smallest cost. Its time complexity is O(N!).

Next, try to solve the problem with the idea of ​​bit masking and dynamic programming:

The definition function dp=(k, mask)
k indicates that the first k people (task 0~k-1) have been assigned tasks, and mask indicates the current mask word.
When assigning the k-th task, in the process of dynamically searching for dp, the state equation is:
dp(k + 1, mask | (1 << i)) = min(dp(k + 1, mask | (1 << i) )), dp(k, mask) + cost[k][i]) It
can be noticed that k in the above equation is the number of '1' in the mask word (the number of mask words set to one is already The number of assigned tasks has the same meaning as k, so the above equation can be simplified as:
dp(mask | (1 << i)) = min(dp(mask | (1 << i), dp(mask) + cost[x][i] (x is the number of 1s in the mask)

Therefore, the solution is as follows:

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]

The time complexity of the above algorithm is O(2 n n) and the space complexity is O(2 n ).

If you can't understand anything in the algorithm, please ask in the comment area and I will do my best to help you understand!

This article refers to via hackerearth: dynamic-programming/bit-masking

Guess you like

Origin blog.csdn.net/qq_35714301/article/details/113403025