In a school, I have multiple classes. Each class could be divided into breakout rooms of 3 or 4 students randomly. We want to avoid duplicate breakout rooms at least we want more randomness and connect new students not completely similar to the last 2-3 breakouts. If all combinations of breakout rooms are exhausted, divide them randomly into a given group size. Do you have better way to do this or optimize this algo ?
function calculateDissimilarity(group1, group2) {
let commonStudents = 0;
for (const student of group1) {
if (group2.includes(student)) {
commonStudents++;
}
}
return commonStudents;
}
function generateBreakoutRooms(students, groupSize, previousGroups) {
// Shuffle students array to add randomness
shuffle(students);
let breakoutRooms = [];
let remainingStudents = [...students];
// Continue until all students are assigned
while (remainingStudents.length > 0) {
let room = [];
// Fill the room with either 3 or 4 students
for (let i = 0; i < groupSize && remainingStudents.length > 0; i++) {
room.push(remainingStudents.pop());
}
// Prioritize dissimilarity with previous groups
if (previousGroups.length > 0) {
let maxDissimilarity = -1;
let mostDissimilarGroup = null;
for (const previousGroup of previousGroups) {
const dissimilarity = calculateDissimilarity(room, previousGroup);
if (dissimilarity > maxDissimilarity) {
maxDissimilarity = dissimilarity;
mostDissimilarGroup = previousGroup;
}
}
// If room is too similar to a previous group, shuffle and try again
if (maxDissimilarity <= 1) {
remainingStudents.push(...room);
shuffle(remainingStudents);
continue;
}
}
breakoutRooms.push(room);
}
return breakoutRooms;
}
function shuffle(array) {
// Fisher-Yates shuffle algorithm
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// Example usage
const students = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank", "Grace", "Hannah"];
const previousGroups = [["Alice", "Bob", "Charlie"], ["David", "Eve", "Frank"]];
const groupSize = 3; // Or 4
const breakoutRooms = generateBreakoutRooms(students, groupSize, previousGroups);
console.log(breakoutRooms);