Problem Description
Given n nodes labeled from 0 to n - 1 and an edge list representing an undirected graph, determine if these edges form a valid tree. A valid tree satisfies both connectivity (all nodes are connected) and acyclicity (no cycles exist). Additionally, when there are n nodes, a tree must have exactly n - 1 edges.
Key Insights
- A tree must be acyclic and connected.
- For n nodes, a valid tree must have exactly n - 1 edges.
- A DFS/BFS traversal can check connectivity and simultaneously detect cycles.
- The Union-Find (disjoint set) approach provides an alternative for cycle detection and connectivity verification.
Space and Time Complexity
Time Complexity: O(n + e), where e is the number of edges
Space Complexity: O(n + e) for storing the graph and traversal/union-find structures
Solution
We can solve the problem using either a DFS/BFS approach or a Union-Find algorithm. For the DFS/BFS method, we first check if the number of edges is exactly n - 1. If not, it cannot be a tree. Then, we build an adjacency list for the graph and perform a DFS starting from node 0 while keeping track of visited nodes and parent information to avoid false-positive cycle detections. If during DFS we encounter a node that has been visited (and is not the parent), then a cycle exists. Finally, if all nodes are reached from node 0 and no cycle is found, the graph is a valid tree.
Using Union-Find, we iterate over each edge and try to union the two nodes. If they already share the same parent (i.e., are in the same set), a cycle exists. After processing all edges, we confirm that exactly one connected component exists.