Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Update 2: The first half of this post was found to be wrong, and lived inside <strike> tags for a long time. Now it's in HTML comments..
Update 2: Wouldn't you know it, in combinatorics class today we actually discussed this exact problem. It has an analogous problem in graph theory dealing with edge-colorings of complete graphs. In any case, the solution is certainly not NP-complete, and the algorithm is very simple with a visual explanation..

Make a circle out of the competitors, except put one player in the middle. For each round, match up the middle guy with someone, then match up the other ones in a symmetric. It's easier to understand what I mean by "symmetric" with a picture, so here's an example with n=8:

Round 1: match 8 with 1, then pair up the rest symmetric to that line.. 1 | 7---|----2 8 6----------3 5----4 Round 2: match 8 with 2, then pair up the rest symmetric to the line.. 1 7 \ 2 \ \^ \ 8^ \ 6 \ 3 \ \ \ \ 5 4 Round X: ... keep rotating around the circle, etc
You obviously never get duplicates with the person you put in the middle. But you will also never get duplicates with the other people in the circle, because there are an odd number of them to pair up. You won't get duplicates until you've rotated all the way around.

Doing this in code is pretty easy:

my @players = qw/Frodo Sam Merry Pippin Strider Gandalf Legolas Gimli/ +; ######## my $num = @players; die "must have even number of players" if $num % 2; for my $partner (0 .. $num-2) { print "Round #" . ($partner + 1) . ":\n"; print " $players[-1] vs $players[$partner]\n"; for my $pair (1 .. ($num-2)/2) { my $p1 = ($partner - $pair) % ($num - 1); my $p2 = ($partner + $pair) % ($num - 1); print " $players[$p1] vs $players[$p2]\n"; } }
Yay combinatorics class!


In reply to Re: Round robin tournament problem by blokhead
in thread Round robin tournament problem by CiceroLove

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (9)
    As of 2021-04-15 12:09 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found