LeetCode 2204: Distance to a Cycle in Undirected Graph Solution

Master LeetCode problem 2204 (Distance to a Cycle in Undirected Graph), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2204. Distance to a Cycle in Undirected Graph

Problem Explanation

Explanation

To find the distance to a cycle in an undirected graph, we can perform a breadth-first search (BFS) starting from each node in the graph. For each node, we calculate the shortest distance to any cycle in the graph. To do this, we keep track of the distance from each node to its parent during the BFS traversal. If we encounter a node that is already visited and is not the parent of the current node, we have found a cycle. We then calculate the distance from the current node to the cycle by adding the distances from the current node to the common ancestor of the two nodes forming the cycle.

  • Time complexity: O(V + E) where V is the number of vertices and E is the number of edges in the graph
  • Space complexity: O(V) for storing visited nodes and distances

Solution Code

import java.util.*;

class Solution {
    public int[] distanceToCycle(int[][] graph) {
        int n = graph.length;
        int[] result = new int[n];
        
        Arrays.fill(result, -1);

        for (int i = 0; i < n; i++) {
            result[i] = bfs(graph, i);
        }

        return result;
    }

    private int bfs(int[][] graph, int start) {
        Queue<Integer> queue = new LinkedList<>();
        int[] visited = new int[graph.length];
        int[] dist = new int[graph.length];

        Arrays.fill(visited, -1);
        Arrays.fill(dist, -1);

        queue.offer(start);
        visited[start] = start;
        dist[start] = 0;

        while (!queue.isEmpty()) {
            int curr = queue.poll();

            for (int neighbor : graph[curr]) {
                if (visited[neighbor] == -1) {
                    visited[neighbor] = curr;
                    dist[neighbor] = dist[curr] + 1;
                    queue.offer(neighbor);
                } else if (visited[neighbor] != curr) {
                    return dist[curr] + dist[neighbor] + 1;
                }
            }
        }

        return -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2204 (Distance to a Cycle in Undirected Graph)?

This page provides optimized solutions for LeetCode problem 2204 (Distance to a Cycle in Undirected Graph) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2204 (Distance to a Cycle in Undirected Graph)?

The time complexity for LeetCode 2204 (Distance to a Cycle in Undirected Graph) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2204 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2204 in Java, C++, or Python.

Back to LeetCode Solutions