BackmediumArraysarraysmedium

Minimum Node Activation Set Solution

Problem Statement

Consider a distributed network consisting of N distinct processing units, indexed from 0 to N-1. You are provided with a collection of activation signals, represented as a list of lists named signals. Each element signals[i] is a list of unique integer indices, indicating the specific processing units that will be powered on if signal i is transmitted. A processing unit is considered active if it is included in at least one of the selected signals.

Your objective is to determine the minimum number of signals that must be transmitted to ensure every processing unit in the network becomes active. If it is impossible to activate all N units using the available signals, return -1.

Input: An integer N representing the total count of processing units, and a list of lists signals where each inner list contains the indices of units activated by that specific signal. Output: An integer representing the minimum count of signals required to cover all units, or -1 if full coverage is unattainable.

Example 1
Input
N = 5, signals = [[0, 1], [2, 3], [4]]
Output
3

Explanation: Signal 0 activates units 0 and 1. Signal 1 activates units 2 and 3. Signal 2 activates unit 4. To cover all units {0, 1, 2, 3, 4}, we must select all three signals. No single signal covers more than two units, and no combination of two signals covers all five units. Thus, the minimum count is 3.

Example 2
Input
N = 4, signals = [[0, 1, 2], [2, 3], [1, 3]]
Output
2

Explanation: We need to cover units {0, 1, 2, 3}. Selecting signal 0 covers {0, 1, 2}. The remaining uncovered unit is 3. Signal 1 covers {2, 3}, which includes 3. Selecting signals 0 and 1 covers all units. Alternatively, signal 0 and signal 2 also work. The minimum number of signals required is 2.

Example 3
Input
N = 3, signals = [[0], [1]]
Output
-1

Explanation: The available signals can only activate units 0 and 1. Unit 2 is not present in any signal. Therefore, it is impossible to activate all 3 units. Return -1.

Example 4
Input
N = 6, signals = [[0, 1, 2], [3, 4, 5], [0, 3], [1, 4], [2, 5]]
Output
2

Explanation: Signal 0 covers {0, 1, 2}. Signal 1 covers {3, 4, 5}. Together, they cover all units {0, 1, 2, 3, 4, 5}. Other combinations like signal 2 and signal 3 only cover {0, 1, 3, 4}, missing 2 and 5. The optimal solution uses signals 0 and 1, resulting in a count of 2.

Constraints

  • 1 <= N <= 10^5
  • 1 <= signals.length <= 10^5
  • 1 <= signals[i].length <= N
  • 0 <= signals[i][j] < N
  • All elements within a single signal list are distinct.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Minimum Node Activation Set — Problem Statement & Solution Guide

ArraysMediumMixed
TimeO(2^N * M)
|
SpaceO(2^N)

Problem Description

Consider a distributed network consisting of N distinct processing units, indexed from 0 to N-1. You are provided with a collection of activation signals, represented as a list of lists named signals. Each element signals[i] is a list of unique integer indices, indicating the specific processing units that will be powered on if signal i is transmitted. A processing unit is considered active if it is included in at least one of the selected signals.

Your objective is to determine the minimum number of signals that must be transmitted to ensure every processing unit in the network becomes active. If it is impossible to activate all N units using the available signals, return -1.

Input: An integer N representing the total count of processing units, and a list of lists signals where each inner list contains the indices of units activated by that specific signal.

Output: An integer representing the minimum count of signals required to cover all units, or -1 if full coverage is unattainable.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Minimum Node Activation Set"

medium

WHY DOES IT MATTER?

Set‑cover style DP is a cornerstone for problems where the universe size is modest but the collection of subsets is large; mastering it lets you solve many combinatorial optimization tasks that appear in resource allocation, feature selection, and test case minimization.

OPTIMIZATION CHALLENGE

The key insight is to shift the exponential dimension from the number of signals (M) to the number of nodes (N) by representing node coverage as bitmasks, enabling DP over 2^N states instead of 2^M subsets.

REAL-WORLD CONNECTION

Think of a distributed system where each deployment script (signal) provisions a specific set of services (nodes). Minimizing the number of scripts reduces rollout time and risk, mirroring the activation set problem.

When coding the DP, pre‑compute each signal's bitmask and use an integer array for dp; also prune signals that are supersets of others, as they never improve the optimal count.

COMPLEXITY AT A GLANCE

⏱ Time:O(2^N * M)
đź’ľ Space:O(2^N)

Core Theory — Why This Approach?

The Minimum Node Activation Set problem is a classic instance of the Set Cover problem: we have a universe of N processing units and a collection of subsets (signals) each covering some of those units. The goal is to select the smallest number of subsets whose union equals the entire universe. Naïve enumeration of all possible signal combinations leads to O(2^M) time, which quickly becomes infeasible as the number of signals grows. Moreover, a simple greedy heuristic that repeatedly picks the signal covering the most uncovered nodes does not guarantee optimality, though it offers a logarithmic approximation. The optimal paradigm for moderate N (typically N ≤ 20‑22) leverages bitmask dynamic programming: each state represents a subset of activated nodes, and transitions add a signal’s coverage, updating the minimal count needed to reach that state. This DP runs in O(2^N * M) time, exploiting the fact that the universe size (N) is often smaller than the number of signals, turning an exponential‑in‑M problem into exponential‑in‑N, which is tractable for typical interview constraints.

Interview Questions on This Problem

Q1How would you model the Minimum Node Activation Set problem using graph theory, and why does this lead to a Set Cover formulation?

Treat each processing unit as a vertex in a universe set and each signal as a hyper‑edge connecting all vertices it can activate. Selecting signals corresponds to picking hyper‑edges whose union covers every vertex, which is exactly the Set Cover definition.

Q2Explain why a greedy algorithm that always picks the signal covering the most currently inactive nodes may fail to produce the optimal solution.

Greedy choice is locally optimal but not globally; a signal covering many nodes might overlap heavily with previously chosen signals, leaving a few rare nodes that require additional signals. Counter‑examples exist where the optimal solution uses several smaller, non‑overlapping signals instead of one large overlapping one.

Q3When N is up to 20, describe an exact algorithm that runs in O(2^N * M) time and outline its state transition.

Use a DP array dp[mask] where mask is a bitmask of activated nodes; dp[mask] stores the minimal number of signals needed. Initialize dp[0]=0. For each mask, iterate over all signals, compute newMask = mask | signalMask, and relax dp[newMask] = min(dp[newMask], dp[mask] + 1). The answer is dp[(1<<N)-1].

Examples

Example 1

Input

N = 5, signals = [[0, 1], [2, 3], [4]]

Output

3

Explanation: Signal 0 activates units 0 and 1. Signal 1 activates units 2 and 3. Signal 2 activates unit 4. To cover all units {0, 1, 2, 3, 4}, we must select all three signals. No single signal covers more than two units, and no combination of two signals covers all five units. Thus, the minimum count is 3.

Example 2

Input

N = 4, signals = [[0, 1, 2], [2, 3], [1, 3]]

Output

2

Explanation: We need to cover units {0, 1, 2, 3}. Selecting signal 0 covers {0, 1, 2}. The remaining uncovered unit is 3. Signal 1 covers {2, 3}, which includes 3. Selecting signals 0 and 1 covers all units. Alternatively, signal 0 and signal 2 also work. The minimum number of signals required is 2.

Example 3

Input

N = 3, signals = [[0], [1]]

Output

-1

Explanation: The available signals can only activate units 0 and 1. Unit 2 is not present in any signal. Therefore, it is impossible to activate all 3 units. Return -1.

Example 4

Input

N = 6, signals = [[0, 1, 2], [3, 4, 5], [0, 3], [1, 4], [2, 5]]

Output

2

Explanation: Signal 0 covers {0, 1, 2}. Signal 1 covers {3, 4, 5}. Together, they cover all units {0, 1, 2, 3, 4, 5}. Other combinations like signal 2 and signal 3 only cover {0, 1, 3, 4}, missing 2 and 5. The optimal solution uses signals 0 and 1, resulting in a count of 2.

Constraints

  • 1 <= N <= 10^5
  • 1 <= signals.length <= 10^5
  • 1 <= signals[i].length <= N
  • 0 <= signals[i][j] < N
  • All elements within a single signal list are distinct.

Optimal Approach & Strategy

Use bitmask DP over node subsets: dp[mask] = min signals to reach mask, transition with each signal's coverage; runs in O(2^N * M).

Brute Force Approach

Enumerate every subset of signals, check if their union covers all nodes, and keep the smallest size; this is O(2^M * N).

Verified Code Solutions

JavaScript Solution
Time: O(2^N * M)
function solution(messages) {
   const N = new Set(messages.flat()).size;
   const selectedMessages = [];
   const activatedNodes = new Set();
   messages.sort((a, b) => a.length - b.length);
   for (const message of messages) {
       const newNodes = new Set(message.filter(node => !activatedNodes.has(node)));
       if (newNodes.size > 0) {
           selectedMessages.push(message);
           activatedNodes = new Set([...activatedNodes, ...newNodes]);
       }
       if (activatedNodes.size === N) break;
   }
   return selectedMessages.length;
}

Asked in Top Tech Interviews

arraysmediumnone

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.