Skip to content

Commit

Permalink
Increase efficiency of cycle finding algo
Browse files Browse the repository at this point in the history
  • Loading branch information
akuazzam authored Oct 29, 2020
1 parent ea14b56 commit eb6188d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tideman.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bool locked[MAX][MAX];

// checks if there is a cylce in locked graph
bool visited [MAX];
bool stack [MAX];

// Each pair has a winner, loser
typedef struct
Expand Down Expand Up @@ -210,21 +211,27 @@ void swap (int i, int j) {
}

bool iscyclic () {
//clear visited and stack arrays
for (int i = 0; i < candidate_count; i ++) {
// clear visited array
for (int k = 0; k < candidate_count; k ++) visited[k] = false;
visited[i] = false;
stack[i] = false;
}
for (int i = 0; i < candidate_count; i ++) {
if (visited[i]) continue;
if (iscyclic_recur (i)) return true;
}

return false;
}

bool iscyclic_recur (int src) {
if (visited[src]) return true;
if (stack[src]) return true;
visited[src] = true;
stack[src] = true;
for (int i = 0; i < candidate_count; i ++) {
if (locked[src][i] && iscyclic_recur(i)) return true;
}
stack[src] = false;

return false;
}
Expand Down

0 comments on commit eb6188d

Please sign in to comment.