1

I'm currently working on Pset3 Tideman and am stuck on the recursion part of lock_pairs function. I tried implementing an additional function to check for a cycle, and obviously, it's not working. Here is my code:

// Lock pairs into the candidate graph in order, without creating cycles
void lock_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        if(!isCycle(pairs[i].winner, pairs[i].loser))
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
        }
    }

    return;
}

//check for cycle
bool isCycle(int a, int z)
{
    for (int i = 0; i < pair_count; i++)
    {
        if (locked[a][i] == true)
        {
            isCycle(i, z);
        }
    }
    return false;
}

When I was studying loops (like for loop), it was easy to check what it does by substituting i=0 then i=1 and so on. This method doesn't seem to be working with recursion. If anyone could explain to me why the code above doesn't work, that would be great. Thank you so much!

1
  • Your recursive function as written never returns true. I'd recommend you do some more research on recursion, in order for recursion to work you need to change some of the variables when you call it inside itself otherwise you're calling the exact same function with the same variables each time. Cant' really offer more insight than that though because I haven't been able to successfully id a created cycle yet either – Sean Apr 10 '20 at 1:45

You must log in to answer this question.

Browse other questions tagged .