Skip to content

Commit 1048576

Browse files
committed
feat: Add AssignmentUsingBitmask new algorithm with Junit tests
1 parent 213fd5a commit 1048576

File tree

2 files changed

+142
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)