TIC
The Interns Company

Graph Valid Tree

MediumAcceptance: 49.2%

Problem Statement

You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that there is an undirected edge between nodes ai and bi in the graph. Return true if the graph is a valid tree, or false otherwise.

Constraints:

  • 1 <= n <= 2000
  • 0 <= edges.length <= 5000
  • edges[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • There are no self-loops or repeated edges

Input Format:

  • An integer n representing the number of nodes
  • A 2D array edges where each edge is [ai, bi]

Output Format:

  • Return true if the graph is a valid tree, or false otherwise

Examples:

Example 1:

Input:

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

Output:

true

Explanation:

The graph is a tree with 5 nodes and 4 edges, which satisfies the condition for a tree.

Example 2:

Input:

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

Output:

false

Explanation:

The graph has a cycle between nodes 1-2-3-1, so it's not a tree.

Example 3:

Input:

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

Output:

true

Explanation:

The graph is a valid tree with 3 nodes and 2 edges.

Solutions

DFS Approach

Time: O(n + e) where n is the number of nodes and e is the number of edges
Space: O(n + e)

Use depth-first search to check if the graph has cycles and if all nodes are reachable.

Union-Find Approach

Time: O(n + e * α(n)) where α(n) is the inverse Ackermann function
Space: O(n)

Use Union-Find to detect cycles in the graph.

BFS Approach

Time: O(n + e)
Space: O(n)

Use breadth-first search to check for connectivity and cycles.

Algorithm Walkthrough

Example input:

Step-by-step execution:

  1. Input: n = 5, edges = [[0,1],[0,2],[0,3],[1,4]]
  2. Check condition 1: edges.length (4) === n-1 (5-1=4)? true
  3. Build adjacency list:
  4. Node 0: [1, 2, 3]
  5. Node 1: [0, 4]
  6. Node 2: [0]
  7. Node 3: [0]
  8. Node 4: [1]
  9. Start DFS from node 0:
  10. Mark node 0 as visited
  11. Visit neighbor 1:
  12. Mark node 1 as visited
  13. Visit neighbor 0: Already visited but it's the parent, skip
  14. Visit neighbor 4:
  15. Mark node 4 as visited
  16. Visit neighbor 1: Already visited but it's the parent, skip
  17. Visit neighbor 2:
  18. Mark node 2 as visited
  19. Visit neighbor 0: Already visited but it's the parent, skip
  20. Visit neighbor 3:
  21. Mark node 3 as visited
  22. Visit neighbor 0: Already visited but it's the parent, skip
  23. Check if all nodes visited: Visited nodes = [0, 1, 4, 2, 3], size = 5, n = 5
  24. Return true (the graph is a valid tree)

Hints

Hint 1
For a graph to be a valid tree, it must have exactly n-1 edges and be fully connected.
Hint 2
You can use BFS or DFS to check if the graph has cycles and if all nodes are reachable.
Hint 3
Union-Find is another approach to detect cycles in an undirected graph.

Video Tutorial

Video tutorials can be a great way to understand algorithms visually

Visualization

Visualize the graph structure and the DFS/BFS traversal process to check if it is a valid tree.

Key visualization elements:

  • current node
  • visited nodes
  • cycle detection

Implementation Notes

This problem demonstrates important graph theory concepts. A graph is a valid tree if it has exactly n-1 edges, has no cycles, and all nodes are connected. The Union-Find solution is particularly elegant for this problem.