Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lock pairs into the candidate graph in order, without creating cycles
- void lock_pairs(void)
- {
- int W, L;
- for (int i = 0; i < pair_count; i++)
- {
- W = pairs[i].winner;
- L = pairs[i].loser;
- //check the cycle
- if (check_cycle(L, W, i)) //not a cycle
- {
- locked[W][L] = true;
- }
- }
- return;
- }
- bool check_cycle(int l, int w, int counter)//return the end of the cycle
- {
- //if there is a cycle than the counter will exceed the number of possible connections wich is pair count
- if (l == w)
- {
- return false;
- }
- if (counter == 0)
- {
- return true;
- }
- //check if there is a path from l to w so check if there is a edge going from locked[i][l]
- for (int i = 0; i < pair_count; i++)
- {
- if (locked[l][i])
- {
- if (!(check_cycle(i, w, counter--)))
- {
- return false;
- }
- }
- }
- return true;
- }
- // Print the winner of the election
- void print_winner(void)
- {
- //find the source of the graph aka the candidate where no one is pointing to
- int winner;
- for (int i = 0; i < candidate_count; i++)
- {
- for (int j = 0; j < candidate_count; j++)
- {
- if (locked[j][i])
- {
- break;
- }
- winner = i;
- }
- }
- printf("%s\n", candidates[winner]);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement