BackhardGraphs

Identify Cyclic Patterns in Network Structures Solution

Problem Statement

Given a network represented as an adjacency list, determine the minimum cycle length present in the graph, if one exists.

Example 1
Input
[[1,2],[2,3],[3,4],[4,1],[5,6]]
Output
4

Explanation: The minimum cycle length is 4, formed by the nodes 1, 2, 3, and 4.

Example 2
Input
[[1,2],[2,3],[3,1]]
Output
3

Explanation: The minimum cycle length is 3, formed by the nodes 1, 2, and 3.

Example 3
Input
[[1,2],[2,3],[3,4],[4,5]]
Output
-1

Explanation: No cycle exists in the given graph.

Constraints

  • The network is represented as an adjacency list.
  • The graph can contain self-loops and parallel edges.
  • If no cycle exists, return -1.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free