简体   繁体   中英

All permutations using a recursive function with 2 lists

I'm currently working on a problem to get all permutations given 2 lists.

The problem: We have 3 football matches, and 3 pitches, using a recusive function, find all possible permutations in which the pitches can be allocated to the matches.

Matches {"a", "b", "c"}, pitches {1, 2, 3}.

The result would be a set of HashMaps with the match=>pitch allocation.

{{"a"=>1, "b"=>2, "c"=>3}, {"a"=>1, "c"=>2, "b"=>3}, {"b"=>1, "a"=>2, "c"=>3}, {"b"=>1, "c"=>2, "a"=>3}, {"c"=>1, "a"=>2, "b"=>3}, {"c"=>1, "b"=>2, "a"=>3}}

Any links to similar problems, advice or pseudocode would be helpful.

I have this done in a different way in which I'm getting all the different sequences possible of the pitches, and then iterating over these results and assigning matches a,b,c. Code is below. But I feel there could be a better solution to this.

public static void allocate(ArrayList<Match> matches, ArrayList<Pitch> pitches){

    Set<Map<Match, Pitch>> set = new HashSet<Map<Match, Pitch>>();

    // list is the return object
    ArrayList<ArrayList<Pitch>> list = new ArrayList<ArrayList<Pitch>>();

    // call the recursive function to populate the list object
    list = (ArrayList<ArrayList<Pitch>>) pitchPermutations(new ArrayList<Pitch>(), pitches, list, matches.size());

    // iterate through all the possible combinations of pitches add the matches
    for(ArrayList<Pitch> vs : list){
        if(vs.size() != matches.size()) continue;

        HashMap<Match,Pitch> m = new HashMap<Match,Pitch>();

        for(int i = 0; i < vs.size(); i++){
            Match e = matches.get(i);
            Pitch v = vs.get(i);
            m.put(e, v);
        }
        set.add(m);
    }

    // print the information
    int count = 1;
    for (Map<Match, Pitch> m : set ){
        System.out.println("----- "+count+" ------");
        for (Map.Entry<Match, Pitch> entry : m.entrySet()) {
            System.out.println("Match :  " + entry.getKey().getName());
            System.out.println("Pitch :  " + entry.getValue().getName());
        }
        count++;
    }

    // return set;

}

private static ArrayList<ArrayList<Pitch>> pitchPermutations(ArrayList<Pitch> pitches, ArrayList<Pitch> pitchesList, ArrayList<ArrayList<Pitch>> returnList, int length){
    // variable to hold the amount of pitches available
    int n = pitchesList.size();

    // if the pitches equals the number of pitches needed, add this list to the returnList variable
    if(pitches.size() == length){
        returnList.add(pitches);
        return returnList;
    }else{

        // 
        for( int i = 0 ; i < pitchesList.size(); i++){
            // remove the item which is added to the pitches to be passed recursively
            ArrayList <Pitch> p = new ArrayList<Pitch>(pitchesList.subList(0,i));
            p.addAll(new ArrayList<Pitch>(pitchesList.subList(i+1, n)));

            ArrayList<Pitch> pitches1 = (ArrayList<Pitch>) pitches.clone();
            pitches1.add(pitchesList.get(i));

            returnList = pitchPermutations(pitches1, p, returnList, length );
        }

    }
    return returnList;
}

You're making this a little too complicated.

  1. Leave the pitches as a constant outside the permutation process. Just pass in the list of matches and receive a list of permutations. Then simply iterate through that list, assigning pitches in order.
  2. The recursive permutation process is well-known:
    1. for each element N in the list
      1. recur on the remainder of the list;
      2. take the returned list of permutations; append N to each element of that list.
      3. append these new permutations (with N added) to this call's result list.
    2. return the result list to the calling instance.

Does that get you moving? If you search on line, you can likely find Java permutation code ready to adapt to your purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM