Implement an efficient algorithm to detect a cycle in a directed graph representing system dependencies.