You are given a directed dependency graph representing services in a system.
n
services, labeled from
0
to
n - 1
.
dependencies
, where each element is a pair
[a, b]
meaning
service a depends on service b
(i.e., there is a directed edge
a -> b
).
Assume:
1 <= n <= 10^5
0 <= a, b < n
Task:
true
if there is a cycle.
false
otherwise.
dependencies1
,
dependencies2
, ...,
dependenciesk
dependenciesi
is a list of
[a, b]
pairs as defined above. Treat the union of all these edges as a single directed graph and determine whether this combined graph contains any cycle.
Describe the approach, data structures, and algorithm you would use, and analyze the time and space complexity.