tideman locks array

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.9 kB
4
Indexable
Never
// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    int i, j, w, l;
    extern int pair_count;
    extern pair pairs[MAX * (MAX - 1) / 2];
    extern bool locked[MAX][MAX];
    int counter = 0;

    for (i = 0; i < pair_count; i++)
    {
        w = pairs[i].winner;    // store winner/loser of each array into var
        l = pairs[i].loser;
        // I had to add this portion for no cycle lock to be satisfied. Not sure why
        // my guess is i am doing something wrong...
        if (pair_count < candidate_count)
        {
            locked[w][l] = true;
        }
        else if (no_wins(l) == true)     // call no_wins(); checks l for locks
        {
            locked[w][l] = true;    // if no locks; lock w to l
            ++counter;
        }
        else if (cycle(w, l) == false)  // makes sure not to lock in a cycle
        {
            locked[w][l] = true;
            ++counter;
        }
    }
    return;
}

// check to see if candidate l has any locks
bool no_wins(int l)
{
    int i;
    extern int candidate_count;
    extern bool locked[MAX][MAX];

    for (i = 0; i < candidate_count; i++)
    {
        if (locked[l][i] == true)
        {
            return false;
        }
    }
    return true;
}

// checks to see if cycles goes from w all the way back to l; reverse
bool cycle(int w, int l)
{
    int i;
    extern int candidate_count;
    extern bool locked[MAX][MAX];

    if (w == l)     // cycle is true if it comes back to l
    {
        return true;
    }
    for (i = 0; i < candidate_count; i++)
    {
        if (locked[i][w])   // if someone locked onto w, start recursion
        {
            if (cycle(i, l))    // testing backwards to l
                ;
            {
                return true;
            }
        }
    }
    return false;   // false means it did not reach back to l, so can lock in
}