Search⌘ K
AI Features

Longest Cycle in a Graph

Explore how to determine the longest cycle length in a directed graph with nodes having at most one outgoing edge. Understand cycle detection principles, analyze graph constraints, and implement a solution in C# that returns the longest cycle length or -1 if none exists. This lesson reinforces graph theory concepts and practical problem-solving skills.

Statement

You are given a directed graph with n nodes, labeled from 0 to n - 1. Each node in the graph has at most one outgoing edge.

The graph is described using a 0-indexed integer array edges of length n, where:

  • edges[i] represents a directed edge from node i to node edges[i].

  • If node i has no outgoing edge, then edges[i] == -1.

Your task is to find the longest cycle length in the graph. If no cycle exists, return -1.

Note: A cycle is defined as a path that starts and ends at the same node, following the direction of the edges.

Constraints:

  • n ...