Order of teams in a tournament such that every team has won against its consecutive team

Given N teams and the results of round-robin tournament in which no match resulted is draw or tie. The task is to find the order of the teams such that every team has won against its consecutive team.

Examples:

Input: N = 4
results[] = {{1, 4}, {4, 3}, {2, 3}, {1, 2}, {2, 1}, {3, 1}, {2, 4}}
Output: 2 4 3 1
Explanation:
Team-2 has won against Team-4, in the last match.
Team-3 has lost to Team-4 in the 2nd match and
Team-3 won against Team-1 in the second last match.
Therefore, every team has won against the team next to itself.

Input: N = 5
results[] = {{1, 4}, {4, 3}, {2, 3}, {1, 2}, {2, 1},
{3, 1}, {4, 2}, {2, 5}, {5, 3}, {4, 5}, {5, 1}}
Output: 4 2 5 3 1

Approach: The idea is to maintain a hash-map for every team which stores the teams against which the team has won in the round-robin tournament. This can be done by iterating over the elements of the results and for each winner append it to the list of the hash-map object of that team.



The hashtable for an example is as follows:

Key          Value

Team-1 => [Team-4, Team-2]
Team-2 => [Team-3, Team-1, Team-4]
Team-3 => [Team-1]
Team-4 => [Team-3]

Finally, compute the order of teams using recursively. Below is the illustration of the recursive function:

  • Base Case: When the current length of the teams is 1, return the team.
  • Recursive Case: Compute the order of the N – 1 team and after the computation of the teams, Iterate over the order of the teams and find a position where the current team has lost against its previous team. If there is no such position append the team at the last position.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the
// order of the teams in a round
// robin tournament such that every
// team has won against to its next team
  
import java.util.*;
import java.lang.*;
  
// A class to represent result
// of a match of a tournament.
class Result {
    int winner;
    int loser;
  
    Result(int winner, int loser)
    {
        this.winner = winner;
        this.loser = loser;
    }
}
  
public class TeamOrdering {
  
    // Function to arrange the teams of
    // the round-robin tournament
    static void arrangeTeams(int[] teams,
                             Result[] results)
    {
        HashMap<Integer,
                List<Integer> >
            map = new HashMap<>();
        int winner = 0;
  
        // Creating a hashmap of teams
        // and the opponents against
        // which they have won, using
        // the results of the tournament
        for (int i = 0; i < results.length; i++) {
            winner = results[i].winner;
            if (map.containsKey(winner)) {
                map.get(winner).add(
                    results[i].loser);
            }
            else {
                List<Integer> list
                    = new ArrayList<Integer>();
                list.add(results[i].loser);
                map.put(winner, list);
            }
        }
        List<Integer> output = new ArrayList<>();
  
        // Arrange the teams in required order
        setInOrder(teams, map, teams.length, output);
        Iterator<Integer> it = output.iterator();
  
        // Displaying the final output
        while (it.hasNext()) {
            System.out.print(it.next());
            System.out.print(" ");
        }
    }
  
    // Function to determine
    // the order of teams
    static void setInOrder(
        int[] teams,
        HashMap<Integer, List<Integer> > map,
        int n, List<Integer> output)
    {
        // Base Cases
        if (n < 1) {
            return;
        }
        // If there is only 1 team,
        // add it to the output
        else if (n == 1) {
            output.add(teams[n - 1]);
            return;
        }
  
        // Recursive call to generate
        // output for N-1 teams
        setInOrder(teams, map, n - 1, output);
        int key = teams[n - 1];
        int i;
  
        // Finding the position for the
        // current team in the output list.
        for (i = 0; i < output.size(); i++) {
  
            // Obtain the team at current
            // index in the list
            int team = output.get(i);
  
            // Check if it has lost against
            // current team
            List<Integer> losers = map.get(key);
            if (losers.indexOf(team) != -1)
                break;
        }
        // Add the current team
        // to its final position
        output.add(i, key);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[] teams = { 1, 2, 3, 4 };
        Result[] results = {
            new Result(1, 4),
            new Result(4, 3),
            new Result(2, 3),
            new Result(1, 2),
            new Result(2, 1),
            new Result(3, 1),
            new Result(2, 4)
        };
  
        // Function Call
        arrangeTeams(teams, results);
    }
}

chevron_right


Output:

2 4 3 1

Performance Analysis:

  • Time Complexity: O(N2)
  • Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet