title: Volleyball Player Combination
body: <p>Some Background:<br>
  In volleyball, players play in pools to determine rankings. Teams are pairs of players. Matches are a pair of players vs another pair of players. For this example, lets assume there is only one court the match can be played on and when a player isn't playing, they are sitting/waiting. The number of players in a pool will be between 4 and 7. If there are 8 players in a pool, they would just break it into 2 pools of 4 instead. </p>
  
  <p><strong>I want to calculate the least number of matches in order for each player to play with every other player.</strong> </p>
  
  <p>For instance, a 4 player pool will have the following teams:</p>
  
  <pre><code>import itertools
  players = [1,2,3,4]
  teams = [t for t in itertools.combinations(players,2)]
  print 'teams:'
  for t in teams:
      print t
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>teams:
  (1, 2)
  (1, 3)
  (1, 4)
  (2, 3)
  (2, 4)
  (3, 4)
  </code></pre>
  
  <p>and the number of matches:</p>
  
  <pre><code>matches = []
  for match in itertools.combinations(teams,2):
      # A player cannot be on both teams at the same time
      if set(match[0]) &amp; set(match[1]) == set():
          matches.append(match)
  
  for match in matches:
      print match
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>((1, 2), (3, 4))
  ((1, 3), (2, 4))
  ((1, 4), (2, 3))
  </code></pre>
  
  <p>Which is correct but this algorithm breaks when I add a 5th player to the pool:</p>
  
  <pre><code>((1, 2), (3, 4))
  ((1, 2), (3, 5))
  ((1, 2), (4, 5))
  ((1, 3), (2, 4))
  ((1, 3), (2, 5))
  ((1, 3), (4, 5))
  ((1, 4), (2, 3))
  ((1, 4), (2, 5))
  ((1, 4), (3, 5))
  ((1, 5), (2, 3))
  ((1, 5), (2, 4))
  ((1, 5), (3, 4))
  ((2, 3), (4, 5))
  ((2, 4), (3, 5))
  ((2, 5), (3, 4))
  </code></pre>
  
  <p>The teams are duplicated a number of times. </p>
  
  <p>I tried to keep a list of teams that play but that algorithm turns out to be greedy. By this I mean when it gets to the (1,5) team, all the other teams [(2,3),(2,4),(3,4)] have already played and (1,5) never gets to play. </p>
  
  <p>What I'm looking for:</p>
  
  <pre><code>((1,2), (3,4)) (player 5 waits)
  ((1,3), (2,5)) (player 4 waits)
  ((1,4), (3,5)) (player 2 waits)
  ((1,5), (4,2)) (player 3 waits)
  ((2,3), (4,5)) (player 1 waits)
  </code></pre>
  
  <p>Would it be easier to just calculate this by hand for each pool size or can this be done easily in python?  </p>
  
  <p>Thanks for the help! </p>
  
  <p><hr>
  Edit:
  Removed Python tag. Any language will suffice and I can convert it to Python. </p>