How can I rank teams based off of head to head wins/losses

Posted by TMP on Programmers See other posts from Programmers or by TMP
Published on 2014-06-12T03:16:04Z Indexed on 2014/06/12 3:47 UTC
Read the original article Hit count: 348

Filed under:
|
|

I'm trying to write an algorithm (specifically in Ruby) that will rank teams based on their record against each other. If a team A and team B have won the same amount of games against each other, then it goes down to point differentials.

Here's an example:

A beat B two times
B beats C one time
A beats D three times
C bests D two times
D beats C one time
B beats A one time

Which sort of reduces to

A[B] = 2
B[C] = 1
A[D] = 3
C[D] = 2
D[C] = 1
B[A] = 1

Which sort of reduces to

A[B] = 1
B[C] = 1
A[D] = 3
C[D] = 1
D[C] = -1
B[A] = -1

Which is about how far I've got

I think the results of this specific algorithm would be:

A, B, C, D

But I'm stuck on how to transition from my nested hash-like structure to the results.

My psuedo-code is as follows (I can post my ruby code too if someone wants):

For each game(g):
  hash[g.winner][g.loser] += 1

That leaves hash as the first reduction above

hash2 = clone of hash
For each key(winner), value(losers hash) in hash:
  For each key(loser), value(losses against winner):
    hash2[loser][winner] -= losses

Which leaves hash2 as the second reduction

Feel free to as me question or edit this to be more clear, I'm not sure of how to put it in a very eloquent way. Thanks!

© Programmers or respective owner

Related posts about algorithms

Related posts about ruby