Press enter to see results or esc to cancel.

Redundant Connection Leetcode Problem 684 [Python Solution]

In this post, we solve the Redundant Connection Leetcode puzzle. This problem falls under the category of Graphs and is of medium difficulty.

It’s also associated with companies like TikTok.

Problem Overview

In this problem, we are given a graph that initially started as a tree with n nodes labeled from 1 to n.

However, one additional edge was added to the graph, and this edge connects two different vertices chosen from 1 to n.

The graph is represented as an array edges of length n, where edges[i] = [ai, bi] indicates an edge between nodes ai and bi in the graph.

Our task is to find and return an edge that can be removed from the graph so that the resulting graph is a tree with n nodes.

If there are multiple answers, we should return the last such edge in the input.

Example 1:

Input:

edges = [[1,2],[1,3],[2,3]]

Output:

[2,3]

Example 2:

Input:

edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]

Output:

[1,4]

Understanding the Constraints

Before we dive into the Python solution, let’s understand the constraints of this problem:

  • n is the number of nodes in the graph.
  • edges[i] is an array of length 2, representing an edge between nodes ai and bi.
  • The graph is connected, which means there are no disconnected components.

Efficient Python Code Solution

To solve this problem efficiently, we’ll use the Union-Find (Disjoint Set Union) algorithm.

This algorithm allows us to determine whether adding an edge creates a cycle in the graph.

If we find an edge that forms a cycle, it is the redundant connection we’re looking for.

Here’s the Python code solution:

def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
    par = [i for i in range(len(edges) + 1)]
    rank = [1] * (len(edges) + 1)

    def find(n):
        p = par[n]
        while p != par[p]:
            par[p] = par[par[p]]
            p = par[p]
        return p

    def union(n1, n2):
        p1, p2 = find(n1), find(n2)

        if p1 == p2:
            return False
        if rank[p1] > rank[p2]:
            par[p2] = p1
            rank[p1] += rank[p2]
        else:
            par[p1] = p2
            rank[p2] += rank[p1]
        return True

    for n1, n2 in edges:
        if not union(n1, n2):
            return [n1, n2]

This Python solution efficiently uses the Union-Find algorithm to identify the redundant connection in the graph.

The algorithm keeps track of the parent nodes and their ranks to determine whether adding an edge creates a cycle.

The last edge that leads to a cycle is returned as the result.

Time and Space Complexity

  • Time Complexity: O(n) – We iterate through the edges once.
  • Space Complexity: O(n) – We use additional data structures for the parent and rank arrays.

Reasoning Behind Our Approach

The reasoning behind this approach lies in understanding the nature of a tree and the properties of a connected graph.

If we keep adding edges to a graph with n nodes and ensure they don’t create cycles, we’ll eventually end up with a tree.

The Union-Find algorithm efficiently helps us detect when adding an edge would create a cycle.

Related Interview Questions By Company:

Related Interview Questions By Difficulty:

Related Interview Questions By Category:

Conclusion

In this post, we’ve solved the Redundant Connection problem on LeetCode efficiently using the Union-Find algorithm.

We’ve explored the problem statement, the constraints, and provided a Python solution.

Understanding this algorithm and problem-solving approach can be valuable for tackling similar graph-related challenges.

If you found this post helpful, please consider liking and subscribing for more content.

Feel free to comment, ask questions, make suggestions, and share this post with others who might find it useful.

Question Link on LeetCode

Happy coding!

>