Advertisement

Reachable Nodes With Restrictions - LeetCode 2368 Solution

Reachable Nodes With Restrictions - Complete Solution Guide

Reachable Nodes With Restrictions is LeetCode problem 2368, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

There is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are given a 2D integer array edges of length n - 1 where edges[i] = [a i , b i ] indicates that there is an edge between nodes a i and b i in the tree. You are also given an integer array restricted which represents restricted nodes. Return the maximum number of nodes you can reach from node 0 without visiting a restricted node. Note that node 0 will not be a restricted node. Example 1: Input: n = 7, edges = [[

Detailed Explanation

The problem presents an undirected tree represented by a list of edges. We are given a set of 'restricted' nodes that cannot be visited. The goal is to find the number of nodes reachable from node 0 without traversing any restricted nodes. Node 0 is guaranteed not to be restricted. The input consists of the number of nodes 'n', a list of edges 'edges', and a list of restricted nodes 'restricted'. The output is the count of reachable nodes from 0, excluding the restricted ones.

Solution Approach

The solution uses a BFS algorithm to traverse the tree starting from node 0. An adjacency list is used to represent the graph. A set of restricted nodes is created for fast lookups. A queue is used to implement BFS, and a set 'visited' tracks visited nodes to avoid cycles. The algorithm explores the graph level by level, only adding unvisited and non-restricted neighbors to the queue. The count of visited nodes represents the number of reachable nodes from node 0.

Step-by-Step Algorithm

  1. Step 1: Create an adjacency list from the given 'edges' list. Each node in the graph will be a key in the dictionary, and the value will be a list of its adjacent nodes.
  2. Step 2: Create a set 'restricted_set' from the given 'restricted' list for efficient membership checking (O(1) lookup).
  3. Step 3: Initialize a queue 'q' with node 0 (if 0 is not in restricted_set).
  4. Step 4: Initialize a set 'visited' with node 0 to keep track of visited nodes and prevent cycles.
  5. Step 5: Initialize a 'count' variable to 0. This will store the number of reachable nodes.
  6. Step 6: While the queue 'q' is not empty:
  7. Step 7: Dequeue a node 'node' from the queue and increment 'count'.
  8. Step 8: Iterate through the neighbors of the 'node' from the adjacency list.
  9. Step 9: For each neighbor, check if it is not in the 'visited' set and not in the 'restricted_set'.
  10. Step 10: If the neighbor satisfies both conditions, add it to the 'visited' set and enqueue it to the queue.
  11. Step 11: After the queue is empty, return the 'count'.

Key Insights

  • Insight 1: Represent the graph using an adjacency list for efficient neighbor lookups.
  • Insight 2: Use Breadth-First Search (BFS) to explore the graph level by level, starting from node 0.
  • Insight 3: Maintain a 'visited' set to prevent cycles and a 'restricted' set for quick lookups to avoid restricted nodes.

Complexity Analysis

Time Complexity: O(n + E)

Space Complexity: O(n + E)

Topics

This problem involves: Array, Hash Table, Tree, Depth-First Search, Breadth-First Search, Union Find, Graph.

Companies

Asked at: MakeMyTrip.