1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Perth Western Australia
    Rep Power

    Algorithm required

    I am in the process of developing a website where users can roster their associations sporting schedules or fixtures.

    I have been trying to develop a simple algorithm that assists with this process and not being strongly math orientated, I can tell you my head hurts!

    My problem is best simplified as follows:

    Given say, 4 entities, I need to be able to calculate each entity playing the remaining three others once without any duplications. This needs to repeat until all possible pairings have been exhausted.

    E.g. Entities A, B, C, D

    First run - AB, CD
    Second run - AC, BD
    Third run - AD, BC

    Of course the amount of entities needs to be variable.

    Any help with this would be greatly appreciated.


  2. #2
  3. T-Shirt Tragic
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2001
    Melbourne, Australia
    Rep Power
    not sure if this what you're after but ...
    this is an example in javascript that will take an array of elements (such as the alphabet) and sort it into an array of pairs without any duplicates...
    <script language="javascript" type="text/javascript">
    // give an array of elems [a...z] sort into pairs without any duplicates. [[a,b],[a,c]...[y,z]]
    function pairs(elements) {
        pairs = new Array();
        var pointer = 1;
        for (k=0;k<elements.length;k++) {
    	for (j=pointer;j<elements.length;j++) pairs[pairs.length] = [elements[k],elements[j]];
        for (x=0;x<pairs.length;x++) document.writeln(pairs[x][0]+"-"+pairs[x][1]+"<br>");
    testArray = ['a','b','c','d','e','f','g','h','i','j','k','l','m',
    The trick is to maintain a "pointer" variable and increment it after each pass through the nested loop.

    Hope I got it right for you.

IMN logo majestic logo threadwatch logo seochat tools logo