Minimum Cost to Connect Two Groups of Points Problem
Description
LeetCode Problem 1595.
You are given two groups of points where the first group has size_1 points, the second group has size_2 points, and size_1 >= size_2.
The cost of the connection between any two points are given in an size_1 x size_2 matrix where cost[i][j] is the cost of connecting point i of the first group and point j of the second group. The groups are connected if each point in both groups is connected to one or more points in the opposite group. In other words, each point in the first group must be connected to at least one point in the second group, and each point in the second group must be connected to at least one point in the first group.
Return the minimum cost it takes to connect the two groups.
Example 1:

Input: cost = [[15, 96], [36, 2]]
Output: 17
Explanation: The optimal way of connecting the groups is:
1--A
2--B
This results in a total cost of 17.
Example 2:

Input: cost = [[1, 3, 5], [4, 1, 1], [1, 5, 3]]
Output: 4
Explanation: The optimal way of connecting the groups is:
1--A
2--B
2--C
3--A
This results in a total cost of 4.
Note that there are multiple points connected to point 2 in the first group and point A in the second group. This does not matter as there is no limit to the number of points that can be connected. We only care about the minimum total cost.
Example 3:
Input: cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]]
Output: 10
Constraints:
size_1 == cost.lengthsize_2 == cost[i].length1 <= size_1, size_2 <= 12size_1 >= size_20 <= cost[i][j] <= 100
Solution
Approach: Bitmask Dynamic Programming
The second group has at most 12 points, so the set of its points that are already connected fits in a bitmask. Handle the points of the first group one at a time.
Let f[mask] be the minimum cost after the first i points of group 1 are each connected to something, where mask is exactly the set of group-2 points that have an edge. Start with f[0] = 0 and everything else infinite.
For point i, compute a new array g. Point i must take at least one edge, so g[0] stays infinite. For a mask and a point k in it, consider the edge i -- k with cost cost[i][k]; the state before adding it can be:
f[mask]:kwas already connected before pointi, and the edge is an extra one.f[mask without k]:kgets its first edge fromi, which isi's first edge too.g[mask without k]:ialready has another edge in this round, and now also connects tokfor the first time.
g[mask] is the smallest of these plus cost[i][k], over all k in mask. Masks are processed in increasing order, so g[mask without k] is ready when it is needed. After all points, the answer is f[full mask].
Note that i never needs more than one edge to an already-connected point: one edge covers i, and any further edges are only worth adding for uncovered points.
Complexity: O(m · n · 2n) time and O(2n) extra space.
class Solution:
def connectTwoGroups(self, cost: List[List[int]]) -> int:
m, n = len(cost), len(cost[0])
INF = float("inf")
full = 1 << n
# f[mask]: min cost so that the points of group 1 handled so far are all connected
# and exactly the points of group 2 in mask are connected
f = [0] + [INF] * (full - 1)
for i in range(m):
g = [INF] * full # point i must get at least one edge, so g[0] stays INF
for mask in range(1, full):
for k in range(n):
bit = 1 << k
if mask & bit:
# edge i--k: k is new (from mask without k, this row or the last),
# or k was already connected before point i
best = min(g[mask ^ bit], f[mask ^ bit], f[mask])
g[mask] = min(g[mask], best + cost[i][k])
f = g
return f[full - 1]