Cycle in directed graph

46 Views Asked by At

I am following the cs50 course, and in the problem set 3, I chose the tideman, but I struggle checking a cycle in the directed graph. I wrote some code but don't know why it doesn't work. It seems to me logically that it should work, please can you help me without writing any code because that would violate the academic honesty, and I don't want that. here is what I wrote:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

// Max number of candidates
#define MAX 9

// preferences[i][j] is number of voters who prefer i over j
int preferences[MAX][MAX];

// locked[i][j] means i is locked in over j
bool locked[MAX][MAX];

// Each pair has a winner, loser
typedef struct
{
    int winner;
    int loser;
}
pair;

// Array of candidates
string candidates[MAX];
pair pairs[MAX * (MAX - 1) / 2];

int pair_count;
int candidate_count;

// Function prototypes
bool vote(int rank, string name, int ranks[]);
void record_preferences(int ranks[]);
void add_pairs(void);
void sort_pairs(void);
void lock_pairs(void);
void print_winner(void);

bool cycle(int winner, int loser);

int main(int argc, string argv[])
{
    // Check for invalid usage
    if (argc < 2)
    {
        printf("Usage: tideman [candidate ...]\n");
        return 1;
    }

    // Populate array of candidates
    candidate_count = argc - 1;
    if (candidate_count > MAX)
    {
        printf("Maximum number of candidates is %i\n", MAX);
        return 2;
    }
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i] = argv[i + 1];
    }

    // Clear graph of locked in pairs
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            locked[i][j] = false;
        }
    }

    pair_count = 0;
    int voter_count = get_int("Number of voters: ");

    // Query for votes
    for (int i = 0; i < voter_count; i++)
    {
        // ranks[i] is voter's ith preference
        int ranks[candidate_count];

        // Query for each rank
        for (int j = 0; j < candidate_count; j++)
        {
            string name = get_string("Rank %i: ", j + 1);

            if (!vote(j, name, ranks))
            {
                printf("Invalid vote.\n");
                return 3;
            }
        }

        record_preferences(ranks);

        printf("\n");
    }

    add_pairs();
    sort_pairs();
    lock_pairs();
    print_winner();
    return 0;
}

// Update ranks given a new vote
bool vote(int rank, string name, int ranks[])
{

    for (int j = 0; j < candidate_count; j++)
    {
        if (strcmp(name, candidates[j]) == 0)
        {
            ranks[rank] = j;
            printf("%i\n", ranks[rank]);
            return true;
        }
    }


    return false;
}

// Update preferences given one voter's ranks
void record_preferences(int ranks[])
{
    // TODO
    // for (int i = 0; i < candidate_count; i++)
    // {
    //     for (int j = i + 1; j < candidate_count; j++)
    //     {
    //         preferences[ranks[i]][ranks[j]] = 0;
    //     }
    // }
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            preferences[ranks[i]][ranks[j]] += 1;
            printf("%i\n", preferences[ranks[i]][ranks[j]]);
        }
    }
    return;
}

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    // TODO
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = i + 1; j < candidate_count; j++)
        {
            if (preferences[i][j] > preferences[j][i])
            {
                pairs[pair_count].winner = i;
                pairs[pair_count].loser = j;
                pair_count++;
            }
            else if (preferences[i][j] < preferences[j][i])
            {
                pairs[pair_count].winner = j;
                pairs[pair_count].loser = i;
                pair_count++;
            }
        }
    }
    return;
}

// Sort pairs in decreasing order by strength of victory
void sort_pairs(void)
{
    // TODO
    int max_strenght = 0;

    for (int i = 0; i < pair_count; i++)
    {
        int temp_winner = pairs[i].winner;
        int temp_loser = pairs[i].loser;
        int index = i;
        int max_strength = preferences[pairs[i].winner][pairs[i].loser] - preferences[pairs[i].loser][pairs[i].winner];
        for (int j = i; j < pair_count; j++)
        {
            int strength = preferences[pairs[j].winner][pairs[j].loser] - preferences[pairs[j].loser][pairs[j].winner];
            if (strength > max_strength)
            {
                index = j;
                pairs[i].winner = pairs[j].winner;
                pairs[i].loser = pairs[j].loser;
            }

        }
        pairs[index].winner = temp_winner;
        pairs[index].loser = temp_loser;
    }
    return;
}

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

    for (int i = 0; i < pair_count; i++)
    {
        if (cycle(pairs[i].winner, pairs[i].loser))
        {
            continue;
        }
        else
        {
            locked[pairs[i].winner][pairs[i].loser] = true;
            locked[pairs[i].loser][pairs[i].winner] = false;
        }


    }
    return;
}

// Print the winner of the election
void print_winner(void)
{
    // TODO
    bool is_source;
    for (int i = 0; i < candidate_count; i++)
    {
        is_source = true;
        for (int j = 0; j < candidate_count; j++)
        {
            if (j != i)
            {
                if (locked[j][i])
                {
                    is_source = false;
                    break;
                }
            }

        }
        if (is_source == true)
        {
            printf("%s", candidates[i]);
        }
    }
    return;
}
bool cycle(int winner, int loser)
{
    for (int i = 0; i < pair_count; i++)
    {
        if (pairs[i].winner == loser)
        {
            return true;
        }
        else if (pairs[i].loser == winner)
        {
            cycle(pairs[i].winner, loser);
        }
    }
    return false;
}

Simple hints without direct answers.

0

There are 0 best solutions below