Advertisement

Redundant Connection - LeetCode 684 Solution

Redundant Connection - Complete Solution Guide

Redundant Connection is LeetCode problem 684, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

In this problem, a tree is an undirected graph that is connected and has no cycles. You are given a graph that started as a tree with n nodes labeled from 1 to n , with one additional edge added. The added edge has two different vertices chosen from 1 to n , and was not an edge that already existed. The graph is represented as an array edges of length n where edges[i] = [a i , b i ] indicates that there is an edge between nodes a i and b i in the graph. Return an edge that can be removed so that

Detailed Explanation

The problem asks us to find the redundant edge in a graph that was initially a tree (a connected graph with no cycles) to which one extra edge has been added. This extra edge creates a cycle, and we need to identify which edge, if removed, would restore the tree structure. If multiple edges could be removed to form a tree, the problem specifies that we should return the edge that appears latest in the input list of edges. The input is given as a list of edges, where each edge is a pair of node numbers [a, b], indicating an undirected connection between nodes 'a' and 'b'. The node numbers range from 1 to n, where n is the number of nodes and also the number of edges in the input. The graph is guaranteed to be connected before the extra edge is added.

Solution Approach

The solution uses the Union Find algorithm (also known as the Disjoint Set Union algorithm) to detect cycles in the graph. The core idea is to represent each node as a set. When an edge (u, v) is encountered, we check if nodes u and v belong to the same set. If they do, it means adding this edge would create a cycle because they are already connected. If they don't, we merge the sets containing u and v to indicate they are now connected. The first edge that causes a cycle is the redundant edge.

Step-by-Step Algorithm

  1. Step 1: Initialize a `parent` array where `parent[i] = i` for all nodes from 1 to n. This means each node initially forms its own disjoint set.
  2. Step 2: Initialize a `rank` array with all elements set to 0. This array is used for optimizing the Union Find operations by attaching smaller trees to the root of larger trees.
  3. Step 3: Iterate through the given list of edges.
  4. Step 4: For each edge (u, v), find the root (representative) of the sets containing u and v using the `find` operation with path compression. Path compression optimizes the find operation by flattening the tree structure.
  5. Step 5: Use `union` operation to merge the sets of u and v. This function returns `true` if the sets were successfully merged (no cycle formed) and `false` if u and v were already in the same set (cycle formed).
  6. Step 6: If the `union` operation returns `false`, it means adding the current edge (u, v) would create a cycle. Return this edge as the redundant edge.
  7. Step 7: If the loop finishes without finding a redundant edge, it indicates an error (as the problem statement guarantees one redundant edge exists). In the code, in this scenario an empty list/array is returned but for the given problem description, this code should not be reached.

Key Insights

  • Insight 1: The redundant edge is the one that introduces a cycle in the graph. Detecting cycles is crucial.
  • Insight 2: Union Find (Disjoint Set) data structure is an efficient way to detect cycles in a graph. It maintains connected components and can quickly determine if adding an edge would connect two already connected components, thus forming a cycle.
  • Insight 3: The problem specifies that if there are multiple possible edges, return the one that appears latest. The provided solution naturally handles this by iterating through the edges in the given order and returning the first edge that causes a cycle.

Complexity Analysis

Time Complexity: O(n α(n))

Space Complexity: O(n)

Topics

This problem involves: Depth-First Search, Breadth-First Search, Union Find, Graph.

Companies

Asked at: Box, InMobi.