Graph Valid Tree

Graph Valid Tree – Medium Level Problem

You are given n nodes labeled from 0 to n – 1 and a list of undirected edges. Write a function to determine if these edges form a valid tree.

To determine if the given edges form a valid tree, the graph must satisfy two conditions:

  • Connected: All nodes must be reachable from any starting node, ensuring there is no isolated component.
  • Acyclic: The graph must not contain any cycles, as a tree cannot have loops.
    An integer k is also provided, representing the starting node for the signal.
Graph Valid Tree Problem

Examples related to Graph Valid Tree Problem

Example 1:

Example 2:

Constraints:

  • 1 <= n <= 100
  • 0 <= edges.length <= n * (n – 1) / 2

Hints to solve Graph Valid Tree Problem

Recommended Time & Space Complexity

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

Hint 1:

  • A tree is defined as an undirected graph that is fully connected (one component), contains no cycles, and has exactly V – 1 edges.

  • To validate the input graph as a tree, we need to ensure these properties.

  • Can you think of a recursive algorithm to check for cycles while ensuring connectivity?

Hint 2:

  • Use Depth First Search (DFS) to detect cycles in the graph. Start DFS from any node (e.g., node 0) and recursively visit its neighbors.

  • If a visited node is encountered and it is not the parent of the current node, it indicates a cycle.

  • If no cycles are found and all nodes are visited, the graph is a tree.

  • How would you implement this?

Hint 3:

  • Begin DFS from node 0, treating its parent as -1. Use a set to track visited nodes.

  • For each node, if it’s already in the set, return false to indicate a cycle. Otherwise, mark it as visited and continue DFS for its neighbors, skipping the parent.

  • After DFS completes, check if all nodes are visited to ensure the graph is connected. If any node is unvisited, the graph is not a tree.

Methods to Solve Graph Valid Tree Problem

There are mainly 3 approaches to solve Graph Valid Tree problem:

  1. Cycle Detection (DFS)
  2. Breadth First Search
  3. Disjoint Set Union

1. Cycle Detection (DFS) Method

Use Depth First Search to check if the graph has any cycles. Start from any node, and as you traverse, track visited nodes and their parents. If you revisit a node that’s not the parent, it indicates a cycle. Ensure all nodes are connected by checking if all nodes are visited.

  • 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:

2. Breadth First Search

Use Breadth First Search to traverse the graph and check for cycles. Maintain a queue and track visited nodes. If you revisit a node that’s not the parent, it’s a cycle. After traversal, verify all nodes are connected for a valid tree.

  • 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. Disjoint Set Union

Use Union-Find to check for cycles and connectivity. For every edge, check if the two nodes belong to the same set (indicating a cycle). If not, union them. After processing all edges, ensure exactly one connected component for a valid tree.

  • 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