Redundant Connection

Redundant Connection – Medium Level Problem

You are given a connected, undirected graph with n nodes labeled from 1 to n. Initially, the graph had no cycles and contained exactly n-1 edges.

Now, one extra edge has been added between two different nodes (from 1 to n) that wasn’t part of the original graph.

The graph is provided as an array edges of length n, where edges[i] = [ai, bi] represents an edge between nodes ai and bi.

Redundant Connection Problem

Examples related to Redundant Connection Problem

Example 1:

Redundant Connection Question 1

Example 2:

Redundant Connection Question 2

Constraints:

  • n == edges.length
  • 3 <= n <= 100
  • 1 <= edges[i][0] < edges[i][1] <= edges.length
  • There are no repeated edges and self loops in the input.

Hints to solve Redundant Connection Problem

Recommended Time & Space Complexity

Aim for a solution with a time complexity of O(V + E) and a space complexity of O(V + E), where V is the number of vertices and E is the number of edges.

Hint 1:

  • There is only one edge that creates a cycle because the graph starts as acyclic, and the cycle forms after adding one extra edge.
  • Can you think of an algorithm to detect if the current edge creates a cycle? Consider using a component-based approach.

Hint 2:

  • The Union-Find (Disjoint Set Union) algorithm can be used to process the edges.

  • While connecting edges, if an edge cannot be connected (indicating it forms a cycle), that edge is the redundant one, and we return it.

  • How would you design this?

Hint 3:

  • Initialize a DSU object and iterate through the edges. For each edge, use the union function to connect the nodes.
  • If the union function fails (returns false), it means the current edge forms a cycle, and that edge is the answer.

Methods to Solve Redundant Connection Problem

There are mainly 4 approaches to solve Redundant Connection Problem:

  1. Cycle Detection (DFS)
  2. Depth First Search (Optimal)
  3. Topological Sort (Kahn’s Algorithm)
  4. Disjoint Set Union

1. Cycle Detection (DFS) Method

Use Depth First Search to explore the graph and check for cycles. If adding an edge forms a cycle, that edge is redundant and should be removed.

  • Time complexity: O(E∗(V+E))
  • Space complexity: O(V+E)

Where V is the number of vertices and E is the number of edges.

Code:

2. Depth First Search (Optimal)

Traverse the graph using DFS while keeping track of visited nodes. If revisiting a node through an already visited path, the edge causing the revisit is the redundant one.

  • Time complexity: O(V + E)
  • Space complexity: O(V + E)

Where V is the number of vertices and E is the number of edges.

Code:

3. Topological Sort (Kahn’s Algorithm)

Leverage Kahn’s Algorithm to maintain a topological order of the graph. The edge causing a cycle during this process is identified as redundant.

  • Time complexity: O(V+E)
  • Space complexity: O(V+E)

Where V is the number of vertices and E is the number of edges.

Code:

4. Disjoint Set Union

Use DSU to manage connected components. When an edge connects nodes already in the same component, it indicates a cycle, and the edge is redundant.

  • Time complexity: O(V+(E∗α(V)))
  • Space complexity: O(V)

Where V is the number of vertices and E is the number of edges.

Code:

More Articles