Skip to content

Commit 22bf4e8

Browse files
Merge pull request #3 from rvguradiya/add-dodgeball_team_partitioning
Add solution for bipartite graph problem: Dodgeball team partitioning
2 parents 8682900 + 3119284 commit 22bf4e8

File tree

5 files changed

+268
-21
lines changed

5 files changed

+268
-21
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
This problem was asked by **Twitter**.
2+
3+
A teacher must divide a class of students into two teams to play dodgeball. Unfortunately, not all the kids get along, and several refuse to be put on the same team as that of their enemies.
4+
5+
Given an adjacency list of students and their enemies, write an algorithm that finds a satisfactory pair of teams, or returns False if none exists.
6+
7+
For example, given the following enemy graph you should return the teams `{0, 1, 4, 5}` and `{2, 3}`.<br>
8+
9+
```javascript
10+
students = {
11+
0: [3],
12+
1: [2],
13+
2: [1, 4],
14+
3: [0, 4, 5],
15+
4: [2, 3],
16+
5: [3],
17+
};
18+
```
19+
20+
On the other hand, given the input below, you should return False.
21+
22+
```javascript
23+
students = {
24+
0: [3],
25+
1: [2],
26+
2: [1, 3, 4],
27+
3: [0, 2, 4, 5],
28+
4: [2, 3],
29+
5: [3],
30+
};
31+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import java.util.*;
2+
3+
public class Approach {
4+
5+
// Function to determine if the graph is bipartite
6+
public static boolean isBipartite(Map<Integer, List<Integer>> students) {
7+
int n = students.size(); // Number of students (vertices)
8+
int[] color = new int[n]; // 0 means uncolored, 1 for team A, -1 for team B
9+
10+
// Iterate over each student
11+
for (int student = 0; student < n; student++) {
12+
if (color[student] == 0) { // If the student is uncolored, start BFS
13+
if (!bfs(student, students, color)) {
14+
return false; // If BFS fails, the graph is not bipartite
15+
}
16+
}
17+
}
18+
19+
// Print the two teams if bipartite
20+
List<Integer> team1 = new ArrayList<>();
21+
List<Integer> team2 = new ArrayList<>();
22+
for (int i = 0; i < n; i++) {
23+
if (color[i] == 1) {
24+
team1.add(i);
25+
} else if (color[i] == -1) {
26+
team2.add(i);
27+
}
28+
}
29+
System.out.println("Team 1: " + team1);
30+
System.out.println("Team 2: " + team2);
31+
return true;
32+
}
33+
34+
// BFS to color the graph
35+
private static boolean bfs(int start, Map<Integer, List<Integer>> students, int[] color) {
36+
Queue<Integer> queue = new LinkedList<>();
37+
queue.add(start);
38+
color[start] = 1; // Start by assigning the student to team A (1)
39+
40+
while (!queue.isEmpty()) {
41+
int currentStudent = queue.poll();
42+
43+
// Traverse all the enemies of the current student
44+
for (int enemy : students.get(currentStudent)) {
45+
if (color[enemy] == 0) { // If the enemy is uncolored
46+
color[enemy] = -color[currentStudent]; // Assign opposite team
47+
queue.add(enemy);
48+
} else if (color[enemy] == color[currentStudent]) {
49+
// If an enemy has the same color, the graph is not bipartite
50+
return false;
51+
}
52+
}
53+
}
54+
return true;
55+
}
56+
57+
public static void main(String[] args) {
58+
// Example 1: Bipartite graph, should return two teams
59+
Map<Integer, List<Integer>> students1 = new HashMap<>();
60+
students1.put(0, Arrays.asList(3));
61+
students1.put(1, Arrays.asList(2));
62+
students1.put(2, Arrays.asList(1, 4));
63+
students1.put(3, Arrays.asList(0, 4, 5));
64+
students1.put(4, Arrays.asList(2, 3));
65+
students1.put(5, Arrays.asList(3));
66+
67+
// Example 2: Not bipartite, should return false
68+
Map<Integer, List<Integer>> students2 = new HashMap<>();
69+
students2.put(0, Arrays.asList(3));
70+
students2.put(1, Arrays.asList(2));
71+
students2.put(2, Arrays.asList(1, 3, 4));
72+
students2.put(3, Arrays.asList(0, 2, 4, 5));
73+
students2.put(4, Arrays.asList(2, 3));
74+
students2.put(5, Arrays.asList(3));
75+
76+
// Test Case 1: Bipartite, should print two teams
77+
System.out.println("Test Case 1 Result:");
78+
if (!isBipartite(students1)) {
79+
System.out.println("Impossible to divide into two teams.");
80+
}
81+
82+
// Test Case 2: Not bipartite, should return false
83+
System.out.println("\nTest Case 2 Result:");
84+
if (!isBipartite(students2)) {
85+
System.out.println("Impossible to divide into two teams.");
86+
}
87+
}
88+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function isBipartite(students) {
2+
const n = Object.keys(students).length; // Number of students (vertices)
3+
const color = Array(n).fill(0); // 0 means uncolored, 1 for team A, -1 for team B
4+
5+
// Helper function to perform BFS
6+
const bfs = (student) => {
7+
const queue = [student];
8+
color[student] = 1; // Start coloring the first student with team A (1)
9+
10+
while (queue.length > 0) {
11+
const currentStudent = queue.shift();
12+
13+
// Traverse all enemies of the current student
14+
for (const enemy of students[currentStudent]) {
15+
if (color[enemy] === 0) {
16+
// If enemy is not yet colored
17+
color[enemy] = -color[currentStudent]; // Assign opposite team
18+
queue.push(enemy);
19+
} else if (color[enemy] === color[currentStudent]) {
20+
// If enemy has the same color as the current student, it's not bipartite
21+
return false;
22+
}
23+
}
24+
}
25+
return true;
26+
};
27+
28+
// Try to color each component of the graph
29+
for (let student = 0; student < n; student++) {
30+
if (color[student] === 0) {
31+
// If student is uncolored
32+
if (!bfs(student)) {
33+
return false; // If one component is not bipartite, return false
34+
}
35+
}
36+
}
37+
38+
// Separate students into two teams based on their color
39+
const team1 = [];
40+
const team2 = [];
41+
for (let i = 0; i < n; i++) {
42+
if (color[i] === 1) {
43+
team1.push(i);
44+
} else if (color[i] === -1) {
45+
team2.push(i);
46+
}
47+
}
48+
49+
return [team1, team2]; // Return the two teams
50+
}
51+
52+
// Example 1: Should return two teams
53+
const students1 = {
54+
0: [3],
55+
1: [2],
56+
2: [1, 4],
57+
3: [0, 4, 5],
58+
4: [2, 3],
59+
5: [3],
60+
};
61+
62+
// Example 2: Should return false
63+
const students2 = {
64+
0: [3],
65+
1: [2],
66+
2: [1, 3, 4],
67+
3: [0, 2, 4, 5],
68+
4: [2, 3],
69+
5: [3],
70+
};
71+
72+
console.log(isBipartite(students1)); // Output: [[0, 1, 4, 5], [2, 3]]
73+
console.log(isBipartite(students2)); // Output: false
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from collections import deque
2+
3+
def is_bipartite(students):
4+
# Number of students is the number of keys in the students dictionary
5+
n = len(students)
6+
7+
# Colors array to store team assignment (0 for uncolored, 1 for team A, -1 for team B)
8+
color = [0] * n
9+
10+
# Perform BFS for each uncolored student
11+
for student in range(n):
12+
if color[student] == 0: # If the student is not yet colored
13+
queue = deque([student])
14+
color[student] = 1 # Assign the student to team 1
15+
16+
# BFS traversal
17+
while queue:
18+
curr_student = queue.popleft()
19+
20+
# Traverse all the enemies of the current student
21+
for enemy in students[curr_student]:
22+
if color[enemy] == 0: # If the enemy is not colored
23+
# Assign the enemy the opposite team
24+
color[enemy] = -color[curr_student]
25+
queue.append(enemy)
26+
elif color[enemy] == color[curr_student]: # Conflict found
27+
return False
28+
29+
# Separate students into two teams based on their color
30+
team1 = [i for i in range(n) if color[i] == 1]
31+
team2 = [i for i in range(n) if color[i] == -1]
32+
33+
return (team1, team2)
34+
35+
# Example usage:
36+
37+
# Example 1: Should return two teams
38+
students1 = {
39+
0: [3],
40+
1: [2],
41+
2: [1, 4],
42+
3: [0, 4, 5],
43+
4: [2, 3],
44+
5: [3]
45+
}
46+
47+
# Example 2: Should return False
48+
students2 = {
49+
0: [3],
50+
1: [2],
51+
2: [1, 3, 4],
52+
3: [0, 2, 4, 5],
53+
4: [2, 3],
54+
5: [3]
55+
}
56+
57+
print(is_bipartite(students1)) # Output: ([0, 1, 4, 5], [2, 3])
58+
print(is_bipartite(students2)) # Output: False
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
2-
import javax.sound.midi.SysexMessage;
3-
4-
51
public class Approach {
62

7-
public static boolean isHaveSum(int []arr,int k,int sum,int i){
8-
if (sum == 0) {
9-
return true;
10-
} else if (i == arr.length || k == 0 || sum < 0) {
11-
return false;
12-
}
13-
14-
if (isHaveSum(arr, k - 1, sum - arr[i], i + 1) == true) {
15-
return true;
16-
} else {
17-
return isHaveSum(arr, k, sum, i + 1);
18-
}
3+
public static boolean isHaveSum(int[] arr, int k, int sum, int i) {
4+
if (sum == 0) {
5+
return true;
6+
} else if (i == arr.length || k == 0 || sum < 0) {
7+
return false;
198
}
20-
public static void main(String[] args) {
21-
int []arr = {20,303,3,4,25};
22-
int k =3;
23-
int sum = 49;
249

25-
System.out.println(isHaveSum(arr, k, sum, 0));
10+
if (isHaveSum(arr, k - 1, sum - arr[i], i + 1) == true) {
11+
return true;
12+
} else {
13+
return isHaveSum(arr, k, sum, i + 1);
2614
}
15+
}
16+
17+
public static void main(String[] args) {
18+
int[] arr = { 20, 303, 3, 4, 25 };
19+
int k = 3;
20+
int sum = 49;
21+
22+
System.out.println(isHaveSum(arr, k, sum, 0));
23+
}
2724
}

0 commit comments

Comments
 (0)