2

I have created a function in Java to find all possible combination with lists. I created this code only for 2 skills, but since the number of skills may change dynamically, I need to change my code to support a dynamic number of nested loops to find the combination of experts for skills.

public List<ProjectAndTeam> teamCombinations(List<ProjectAndTeam> projectAndTeams) {
    List<ProjectAndTeam> allTeamCombinations = new ArrayList<>();
    for (ProjectAndTeam currentProjectTeam : projectAndTeams) {
        ProjectAndTeam projectAndTeam = new ProjectAndTeam();
        projectAndTeam.project = currentProjectTeam.project;
        for (int i = 0; i < currentProjectTeam.expertForSkill.get(0).expertList.size(); i++) {
            for (int j = 0; j < currentProjectTeam.expertForSkill.get(1).expertList.size(); j++) {
                ExpertForSkill expertForSkill = new ExpertForSkill();
                expertForSkill.skill = currentProjectTeam.expertForSkill.get(0).skill;
                expertForSkill.expertList.add(currentProjectTeam.expertForSkill.get(0).expertList.get(i));
                ExpertForSkill expertForSkillSecond = new ExpertForSkill();
                expertForSkillSecond.skill = currentProjectTeam.expertForSkill.get(1).skill;
                expertForSkill.expertList.add(currentProjectTeam.expertForSkill.get(1).expertList.get(j));
                projectAndTeam.expertForSkill.add(expertForSkill);
                projectAndTeam.expertForSkill.add(expertForSkillSecond);
            }
        }
        allTeamCombinations.add(projectAndTeam);
    }
    return allTeamCombinations;
}

Here are my ProjectAndTeam, ExprtForSkill and Expert classes

public class ProjectAndTeam {
int id;
Project project;
List<ExpertForSkill> expertForSkill = new ArrayList<>();
double totalSalary;
double totalProductivity;
}

public class ExpertForSkill {
String skill;
List<Expert> expertList = new ArrayList<>();
}

public class Expert {
int id;
List<String> skills = new ArrayList<>();
int capacity;
double productivity;
double salary;
}

How can I get all combinations with a varying number of nested loops?

I believe that I have to write a recursion function to handle it but I'm confused.

1
  • I would highly recommend reducing your code to a minimal example. What you have here is a bit hard to follow, and really unnecessary in terms of demonstrating your problem. Commented Nov 4, 2018 at 5:15

1 Answer 1

1

Rather than using recursion, I would implement your *dynamic nested loops" via a single loop and an array of counters representing i, j, k, etc. You would always increment the last possible counter, representing the innermost loop, until it reached the end, in which case you would reset it, increment the loop outside it by one, and continue.

Let's say you have n different skills. The bookkeeping could look something like this:

int[] counters = new int[n]; // i, j, k, ...

while(counters[0] < currentProjectTeam.expertForSkill.get(0).expertList.size()) {
    // Compute projectAndTeam, using loop over counters
    allTeamCombinations.add(projectAndTeam);

    for(int currentDepth = n - 1; ++counters[currentDepth] == currentProjectTeam.expertForSkill.get(currentDepth).expertList.size() && currentDepth > 0; currentDepth--) {
        counters[currentDepth] = 0;
    }
}

You should probably have an array for the skill values corresponding to each item in counters, and update it in that final loop. That might be easier than recomputing everything from scratch in the loop that computes projectAndTeam.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.