Home >Backend Development >PHP Tutorial >. Redundant Connection
684. Redundant Connection
Difficulty: Medium
Topics: Depth-First Search, Breadth-First Search, Union Find, Graph
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] = [ai, bi] indicates that there is an edge between nodes ai and bi in the graph.
Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.
Example 1:
Example 2:
Constraints:
Solution:
The Redundant Connection problem asks us to identify an edge in a graph that can be removed to turn the graph into a valid tree. A tree is an undirected graph that is connected and acyclic. We are given a graph that started as a tree but was modified by adding one extra edge. Our goal is to find and return this extra edge.
The approach involves using Depth-First Search (DFS) to detect cycles:
Adjacency List Representation:
Cycle Detection via DFS:
Returning the Edge:
Let's implement this solution in PHP: 684. Redundant Connection
<?php /** * @param Integer[][] $edges * @return Integer[] */ function findRedundantConnection($edges) { ... ... ... /** * go to ./solution.php */ } /** * Helper function to perform DFS and check connectivity * * @param $src * @param $target * @param $visited * @param $adjList * @return bool */ private function isConnected($src, $target, &$visited, &$adjList) { ... ... ... /** * go to ./solution.php */ } // Example usage: $edges1 = [[1,2],[1,3],[2,3]]; $edges2 = [[1,2],[2,3],[3,4],[1,4],[1,5]]; print_r(findRedundantConnection($edges1)) . "\n"; // Output: [2,3] print_r(findRedundantConnection($edges2)) . "\n"; // Output: [1,4] ?>
DFS Implementation:
Edge Addition:
Redundant Edge:
Input: edges = [[1, 2], [1, 3], [2, 3]]
Steps:
Output: [2, 3]
Input: edges = [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]]
Steps:
Output: [1, 4]
DFS Traversal:
Total Complexity:
Space Complexity:
Input: [[1, 2], [1, 3], [2, 3]]
Output: [2, 3]
Input: [[1, 2], [2, 3], [3, 4], [1, 4], [1, 5]]
Output: [1, 4]
The problem can be efficiently solved using a DFS-based approach to detect cycles. This method dynamically builds the graph and checks for redundant edges at each step. The solution ensures correctness by adhering to the problem constraints and outputs the edge that forms a cycle and occurs last in the input.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of . Redundant Connection. For more information, please follow other related articles on the PHP Chinese website!