|
| 1 | +package com.thealgorithms.dynamicprogramming; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +/** |
| 8 | + * The AssignmentUsingBitmask class is used to calculate the total number of ways |
| 9 | + * tasks can be distributed among people, given specific constraints on who can perform which tasks. |
| 10 | + * The approach uses bitmasking and dynamic programming to efficiently solve the problem. |
| 11 | + * |
| 12 | + * @author Hardvan |
| 13 | + */ |
| 14 | +public final class AssignmentUsingBitmask { |
| 15 | + |
| 16 | + private final int totalTasks; |
| 17 | + private final int[][] dp; |
| 18 | + private final List<List<Integer>> task; |
| 19 | + private final int finalMask; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructor for the AssignmentUsingBitmask class. |
| 23 | + * |
| 24 | + * @param taskPerformed a list of lists, where each inner list contains the tasks that a person can perform. |
| 25 | + * @param total the total number of tasks. |
| 26 | + */ |
| 27 | + public AssignmentUsingBitmask(List<List<Integer>> taskPerformed, int total) { |
| 28 | + this.totalTasks = total; |
| 29 | + this.dp = new int[1 << taskPerformed.size()][total + 1]; |
| 30 | + for (int[] row : dp) { |
| 31 | + Arrays.fill(row, -1); |
| 32 | + } |
| 33 | + |
| 34 | + this.task = new ArrayList<>(totalTasks + 1); |
| 35 | + for (int i = 0; i <= totalTasks; i++) { |
| 36 | + this.task.add(new ArrayList<>()); |
| 37 | + } |
| 38 | + |
| 39 | + // Final mask to check if all persons are included |
| 40 | + this.finalMask = (1 << taskPerformed.size()) - 1; |
| 41 | + |
| 42 | + // Fill the task list |
| 43 | + for (int i = 0; i < taskPerformed.size(); i++) { |
| 44 | + for (int j : taskPerformed.get(i)) { |
| 45 | + this.task.get(j).add(i); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Counts the ways to assign tasks until the given task number with the specified mask. |
| 52 | + * |
| 53 | + * @param mask the bitmask representing the current state of assignments. |
| 54 | + * @param taskNo the current task number being processed. |
| 55 | + * @return the number of ways to assign tasks. |
| 56 | + */ |
| 57 | + private int countWaysUntil(int mask, int taskNo) { |
| 58 | + if (mask == finalMask) { |
| 59 | + return 1; |
| 60 | + } |
| 61 | + if (taskNo > totalTasks) { |
| 62 | + return 0; |
| 63 | + } |
| 64 | + if (dp[mask][taskNo] != -1) { |
| 65 | + return dp[mask][taskNo]; |
| 66 | + } |
| 67 | + |
| 68 | + int totalWays = countWaysUntil(mask, taskNo + 1); |
| 69 | + |
| 70 | + // Assign tasks to all possible persons |
| 71 | + for (int p : task.get(taskNo)) { |
| 72 | + // If the person is already assigned a task |
| 73 | + if ((mask & (1 << p)) != 0) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + totalWays += countWaysUntil(mask | (1 << p), taskNo + 1); |
| 77 | + } |
| 78 | + |
| 79 | + dp[mask][taskNo] = totalWays; |
| 80 | + return dp[mask][taskNo]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Counts the total number of ways to distribute tasks among persons. |
| 85 | + * |
| 86 | + * @return the total number of ways to distribute tasks. |
| 87 | + */ |
| 88 | + public int countNoOfWays() { |
| 89 | + return countWaysUntil(0, 1); |
| 90 | + } |
| 91 | +} |
0 commit comments