0

Help, i'm losing my mind. I'm not sure where i'm going wrong in this function, it gives me an error on every line of check50 after sort pairs. Can anybody shed any light? Thank you in advance! J

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{

    int cycle = 0;

    for (int i = 0; i < pair_count; i++)
    {
        if (i == 0)
        {
            // if pair 0, lock the pair as it is strongest margin

            locked[pairs[i].winner][pairs[i].loser] = true;
        }

        else if (locked[pairs[i].winner][pairs[i].loser] == false)
        {
            //(if pair is unlocked) check for cycle

            for (int j = 0; j < pair_count; j++)
            {
                //check if the winner is locked[j][winner]

                if (locked[j][pairs[i].winner] == true)
                {
                    printf("cycle detected, did not lock\n\n");
                    cycle = 1;
                }
            }
        }

        if (cycle == 0)
        {
            printf("no cycle detected\n\n");

            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }
    return;
}
0

It looks like you're defining a cycle as "someone beats the winner of this pair", but that's not a cycle. That's more of a chain (A -> B, B -> C). A cycle is when a chain loops back around to the person it started with, like A -> B, B -> C, C -> A.

Also worth pointing out, you should move cycle = 0; into your outer loop. If cycle gets set to 1, you'll never set it back to 0, so all the following pairs will be marked as cycles and not locked, even if they don't cause a cycle.

1
  • ok, thanks for that, i'll have another look and see if that points me in a better direction. Jun 12 '20 at 21:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .