Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. // Lock pairs into the candidate graph in order, without creating cycles
  2. void lock_pairs(void)
  3. {
  4. int W, L;
  5.  
  6. for (int i = 0; i < pair_count; i++)
  7. {
  8. W = pairs[i].winner;
  9. L = pairs[i].loser;
  10. //check the cycle
  11. if (check_cycle(L, W, i)) //not a cycle
  12. {
  13. locked[W][L] = true;
  14. }
  15.  
  16. }
  17. return;
  18. }
  19.  
  20. bool check_cycle(int l, int w, int counter)//return the end of the cycle
  21. {
  22. //if there is a cycle than the counter will exceed the number of possible connections wich is pair count
  23. if (l == w)
  24. {
  25. return false;
  26. }
  27. if (counter == 0)
  28. {
  29. return true;
  30. }
  31. //check if there is a path from l to w so check if there is a edge going from locked[i][l]
  32. for (int i = 0; i < pair_count; i++)
  33. {
  34. if (locked[l][i])
  35. {
  36. if (!(check_cycle(i, w, counter--)))
  37. {
  38. return false;
  39. }
  40. }
  41. }
  42. return true;
  43. }
  44. // Print the winner of the election
  45. void print_winner(void)
  46. {
  47. //find the source of the graph aka the candidate where no one is pointing to
  48. int winner;
  49. for (int i = 0; i < candidate_count; i++)
  50. {
  51. for (int j = 0; j < candidate_count; j++)
  52. {
  53. if (locked[j][i])
  54. {
  55. break;
  56. }
  57. winner = i;
  58. }
  59. }
  60. printf("%s\n", candidates[winner]);
  61. return;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement