Write a function to check if a given directed graph contains a cycle. Explain the algorithm you would use and its time complexity.
Write a function to check if a given directed graph contains a cycle. Explain the algorithm you would use and its time complexity.