I want to develop a function that will detect the loop in a graph.
The function will take two parameters: the number of courses, and the prerequisite array.
The prerequisites courses is an array as follows: [[b,a], [c,b], [f,e]]
This means, that course “a” is a prerequisite for course “b”, course “b” is a prerequisite for course “c”, and course “e” is a prerequisite for course “f”.
So if the number of courses is 3, and the prerequisite array is correct, then the output is
a > b > e
e > f
The above is 5 courses, and the output of the function is TRUE because we can take 3 courses by following the prerequisites.
However, if the prerequisits is [[b,a], [c,b], [a,c]]
then the output is FALSE because there is a loop:
a > b > c
c > a
Which is a circular reference. So you cannot take 3 curses because course “a” needs course “c” which is not possible.
So basically, we need to write a function to detect a loop in the graph.
I appreciate your help.