views:

143

answers:

1

I'm putting together a tool for a colleague which helps to create a nice fixture list. I got about 2/3 through the tool, collecting various data ... and then I hit a brick wall. It's less of a JavaScript problem and more of a maths/processing brainblock.

Lets say I have 4 teams, and they all need to play each other at home and away. Using this tool - http://www.fixturelist.com/ - I can see that a home and away fixture with 4 teams would take 6 weeks/rounds/whatever. For the life of me, though, I can't work out how that was programmatically worked out.

Can someone explain the logic to process this?

For info, I would use this existing tool, but there are other factors/features that I need to work in, hence doing a custom job. If only I could understand how to represent that logic!

A: 

In your example of 4 teams, call them a, b, c and d:

  • a has to play b, c, d
  • b has to play c, d (game against a already included in a's games)
  • c has to play d (game against a already included in a's games, against b already included in b's games)

If they need to play at home and away, that's 12 games. You can play at most 4/2 = 2 games a week, so that's 6 weeks.

With n teams you need x games, where:

x = ((n-1 + n-2 + n-3 ...) * 2)

This takes y weeks, where:

y = x/(n/2) = 2x/n

This can be simplified with an arithmetic series fairly easily, or calculated with a for loop if you want.

Dominic Rodger
Thanks for this. The formula makes sense and I've got it working on the page accordingly - writes out the correct number of weeks and the correct number of total games, depending on whether I choose home and away of single mathces. But now I've hit another hurdle in working out the logic for actually plotting week by week who plays who so that every permutation is covered. I will try to get my head around this but may end up asking for help again!Thanks very much for this. Much appreciated
Lloydi
@Lloydi - yeah I was going to mention that might be a headache! I think `y` is a lower bound on the number of weeks taken, provided an optimal ordering of games. I'll watch out for any more questions from you - I quite enjoyed this one!
Dominic Rodger
I'm going to ponder this ... see if the approach magically pops into my head while walking the dog. If only that example site had not used PHP to generate fixures. Server-side code, eh? Pfff ;-)I think I'll upload the file anyway for info. Got to say, first time using Stack Overflow, and I *really* like the way that this works :-) A really good developer's resource.
Lloydi
@Lloydi - glad you like it - it's been massively useful to me (not just as a resource for getting my questions answered, but as a place to cut my teeth helping other people out).
Dominic Rodger