The Longest Cycle problem is a graph-based problem where each node has at most one outgoing edge. This special structure makes the graph behave like a functional graph, allowing us to use optimized traversal techniques for cycle detection.

Problem Summary

You are given a directed graph with the following properties:

Your task is to:

Key Observation

Since each node has only one outgoing edge, the graph behaves like:

Example:

0 → 5 → 6 → 3 → 1 → 0

This forms a cycle.

Core Idea

We use a DFS-like traversal combined with visitation tracking.

We maintain:

How Cycle Detection Works

While traversing the graph:

Cycle Length is calculated as:

currentStep - time[node]

Approach

Java Code (Optimized Solution)

import java.util.*;

class Solution {
    public int longestCycle(int V, int[][] edges) {
        int[] out = new int[V];
        Arrays.fill(out, -1);
        
        // Build graph (each node has at most 1 outgoing edge)
        for (int[] e : edges) {
            out[e[0]] = e[1];
        }
        
        boolean[] visited = new boolean[V];
        int[] time = new int[V];
        
        int maxCycle = -1;
        int timer = 1;
        
        for (int i = 0; i < V; i++) {
            if (visited[i]) continue;
            
            int node = i;
            Map<Integer, Integer> map = new HashMap<>();
            
            // Traverse current path
            while (node != -1 && !visited[node]) {
                visited[node] = true;
                map.put(node, timer++);
                node = out[node];
            }
            
            // Check for cycle
            if (node != -1 && map.containsKey(node)) {
                int cycleLength = timer - map.get(node);
                maxCycle = Math.max(maxCycle, cycleLength);
            }
        }
        
        return maxCycle;
    }
}

Dry Run

Input:

V = 7
edges = [[0,5],[1,0],[2,4],[3,1],[4,6],[5,6],[6,3]]

Graph representation:

0 → 5 → 6 → 3 → 1 → 0

Cycle length = 5

Output:

5

Edge Cases

Complexity Analysis

Key Takeaways

Conclusion

This problem highlights an efficient way to detect cycles in a directed graph with constraints. By leveraging the properties of a functional graph and using a timestamp-based approach, we can compute the longest cycle in linear time. This technique is highly useful in graph traversal problems commonly asked in technical interviews.