BackmediumGraphsuncategorizedmedium

Shortest Node-Covering Path Solution

Problem Statement

You are provided with an undirected, connected graph consisting of N vertices, indexed from 0 to N-1. The connectivity is defined by an adjacency list adj, where adj[i] contains the indices of all vertices directly connected to vertex i. You are also given two distinct integers, source and destination. Your task is to determine the minimum number of edge traversals required to construct a path that begins at source, terminates at destination, and ensures that every vertex in the graph is visited at least once during the traversal. If no such path exists (though the graph is connected, this condition is theoretically always satisfiable in a connected graph), return -1.

Example 1
Input
N = 4, adj = [[1, 2], [0, 3], [0, 3], [1, 2]], source = 0, destination = 3
Output
3

Explanation: The graph is a cycle 0-1-3-2-0. We must visit all nodes {0, 1, 2, 3}. Starting at 0, we can go 0 -> 1 -> 3 -> 2. This path visits 0, 1, 3, 2. However, it ends at 2, not 3. We need to end at 3. Let's try 0 -> 2 -> 3 -> 1. Ends at 1. Let's try 0 -> 1 -> 3 -> 2 -> 3. This visits 0, 1, 3, 2, 3. All nodes visited. Length is 4. Wait, is there a shorter one? 0 -> 2 -> 3 -> 1 -> 3. Length 4. 0 -> 1 -> 0 -> 2 -> 3. Length 4. Actually, in a cycle of 4, to visit all and end at a specific node, you often have to backtrack or take the long way. Let's re-evaluate. Path 0->1->3->2->3 visits {0,1,3,2}. Length 4. Path 0->2->3->1->3 visits {0,2,3,1}. Length 4. Is there a length 3 path? 0->1->3->2 (ends at 2). 0->2->3->1 (ends at 1). No length 3 path ends at 3 while visiting all. So 4 is the answer? Let's check constraints. N=4. Edges: (0,1), (1,3), (3,2), (2,0). Path 0->1->3->2->3. Nodes: 0,1,3,2,3. All unique nodes {0,1,2,3} covered. Length 4. Let's try another example to be safe.

Example 2
Input
N = 3, adj = [[1, 2], [0, 2], [0, 1]], source = 0, destination = 1
Output
2

Explanation: The graph is a triangle 0-1-2-0. We need to start at 0, end at 1, and visit {0, 1, 2}. Path 0 -> 2 -> 1 visits 0, 2, 1. All nodes covered. Length is 2. Path 0 -> 1 -> 2 -> 1 visits 0, 1, 2, 1. Length 3. Minimum is 2.

Example 3
Input
N = 5, adj = [[1, 4], [0, 2], [1, 3], [2, 4], [0, 3]], source = 0, destination = 4
Output
4

Explanation: Graph is a cycle 0-1-2-3-4-0. Start 0, End 4. Must visit {0,1,2,3,4}. Path 0 -> 1 -> 2 -> 3 -> 4. Visits all. Length 4. Path 0 -> 4 -> 3 -> 2 -> 1 -> 4. Length 5. Minimum is 4.

Constraints

  • 2 <= N <= 100
  • 0 <= source, destination < N
  • source != destination
  • The graph is connected and undirected.
  • No self-loops or multiple edges between the same pair of nodes.
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

Shortest Node-Covering Path — Problem Statement & Solution Guide

GraphsMediumMixed
TimeO((N + E) · 2^N)
|
SpaceO(N · 2^N)

Problem Description

You are provided with an undirected, connected graph consisting of N vertices, indexed from 0 to N-1. The connectivity is defined by an adjacency list adj, where adj[i] contains the indices of all vertices directly connected to vertex i. You are also given two distinct integers, source and destination. Your task is to determine the minimum number of edge traversals required to construct a path that begins at source, terminates at destination, and ensures that every vertex in the graph is visited at least once during the traversal. If no such path exists (though the graph is connected, this condition is theoretically always satisfiable in a connected graph), return -1.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Shortest Node-Covering Path"

medium

WHY DOES IT MATTER?

State‑space search with bitmasking is essential because it transforms an NP‑hard combinatorial problem into a manageable dynamic programming problem for small N, enabling exact optimal solutions rather than heuristics.

OPTIMIZATION CHALLENGE

The key insight is to encode the set of visited vertices as a bitmask, which collapses an exponential number of possible visit orders into a single integer state, allowing BFS/Dijkstra to explore only O(N·2^N) states instead of N! permutations.

REAL-WORLD CONNECTION

Consider a delivery drone that must visit multiple waypoints before returning to base. The drone’s flight plan is a node‑covering path; optimizing it reduces fuel consumption and delivery time.

When implementing, pre‑allocate a 2D array of distances [N][1<<N] and use a queue of tuples. Avoid repeated bit‑mask operations by using bitwise OR and pre‑computed masks for neighbors.

COMPLEXITY AT A GLANCE

⏱ Time:O((N + E) · 2^N)
💾 Space:O(N · 2^N)

Core Theory — Why This Approach?

The problem reduces to finding the shortest walk that starts at a given source, ends at a destination, and visits every vertex at least once. A naive approach would enumerate all permutations of vertices and compute the path length for each, which is factorial time and infeasible for moderate N. The optimal paradigm treats the problem as a state‑space search where each state is a pair (currentVertex, visitedMask). Using breadth‑first search (BFS) or Dijkstra’s algorithm on this expanded graph guarantees the minimal number of edge traversals because all edges have equal weight. The visitedMask is a bitmask of length N, so the number of states is O(N·2^N). This dynamic‑programming style search is the classic solution for the “Shortest Hamiltonian Path” variant when edge weights are uniform, and it scales to graphs with up to about 15–20 vertices in practice.

Interview Questions on This Problem

Q1How would you modify the algorithm if the graph had weighted edges instead of unit weights?

Replace BFS with Dijkstra’s algorithm on the state space (vertex, visitedMask). Each transition adds the edge weight to the current cost, and the priority queue ensures we always expand the state with the smallest cumulative weight.

Q2What is the time complexity of the optimal solution and why is it acceptable for N ≤ 15?

The time complexity is O((N + E) · 2^N) because each of the O(N·2^N) states can generate up to O(deg(v)) transitions. For N ≤ 15, 2^N ≈ 32,768, so the algorithm runs in a few million operations, which is fine for interview constraints.

Q3Can you explain a real‑world scenario where a node‑covering path is required?

In a warehouse robot that must visit every storage location to pick items, the robot starts at a charging station (source) and must finish at a packing area (destination). The shortest walk that visits all locations corresponds to the minimal battery usage and time.

Examples

Example 1

Input

N = 4, adj = [[1, 2], [0, 3], [0, 3], [1, 2]], source = 0, destination = 3

Output

3

Explanation: The graph is a cycle 0-1-3-2-0. We must visit all nodes {0, 1, 2, 3}. Starting at 0, we can go 0 -> 1 -> 3 -> 2. This path visits 0, 1, 3, 2. However, it ends at 2, not 3. We need to end at 3. Let's try 0 -> 2 -> 3 -> 1. Ends at 1. Let's try 0 -> 1 -> 3 -> 2 -> 3. This visits 0, 1, 3, 2, 3. All nodes visited. Length is 4. Wait, is there a shorter one? 0 -> 2 -> 3 -> 1 -> 3. Length 4. 0 -> 1 -> 0 -> 2 -> 3. Length 4. Actually, in a cycle of 4, to visit all and end at a specific node, you often have to backtrack or take the long way. Let's re-evaluate. Path 0->1->3->2->3 visits {0,1,3,2}. Length 4. Path 0->2->3->1->3 visits {0,2,3,1}. Length 4. Is there a length 3 path? 0->1->3->2 (ends at 2). 0->2->3->1 (ends at 1). No length 3 path ends at 3 while visiting all. So 4 is the answer? Let's check constraints. N=4. Edges: (0,1), (1,3), (3,2), (2,0). Path 0->1->3->2->3. Nodes: 0,1,3,2,3. All unique nodes {0,1,2,3} covered. Length 4. Let's try another example to be safe.

Example 2

Input

N = 3, adj = [[1, 2], [0, 2], [0, 1]], source = 0, destination = 1

Output

2

Explanation: The graph is a triangle 0-1-2-0. We need to start at 0, end at 1, and visit {0, 1, 2}. Path 0 -> 2 -> 1 visits 0, 2, 1. All nodes covered. Length is 2. Path 0 -> 1 -> 2 -> 1 visits 0, 1, 2, 1. Length 3. Minimum is 2.

Example 3

Input

N = 5, adj = [[1, 4], [0, 2], [1, 3], [2, 4], [0, 3]], source = 0, destination = 4

Output

4

Explanation: Graph is a cycle 0-1-2-3-4-0. Start 0, End 4. Must visit {0,1,2,3,4}. Path 0 -> 1 -> 2 -> 3 -> 4. Visits all. Length 4. Path 0 -> 4 -> 3 -> 2 -> 1 -> 4. Length 5. Minimum is 4.

Constraints

  • 2 <= N <= 100
  • 0 <= source, destination < N
  • source != destination
  • The graph is connected and undirected.
  • No self-loops or multiple edges between the same pair of nodes.

Optimal Approach & Strategy

Use BFS (or Dijkstra for weighted edges) on the expanded state space where each state is (currentVertex, visitedMask). Each transition adds an edge and updates the mask; the first time we reach (destination, allVisitedMask) gives the minimal number of traversals.

Brute Force Approach

Enumerate all permutations of the vertices, compute the path length for each permutation that starts at source and ends at destination, and return the minimum. This requires O(N!) time and is impractical for N > 10.

Verified Code Solutions

JavaScript Solution
Time: O((N + E) · 2^N)
function shortestNodeCoveringPath(graph, start, target) {
   const visited = new Set();
   const queue = [[start, 0]];
   let minPathLength = Infinity;
   while (queue.length > 0) {
       const [node, pathLength] = queue.shift();
       if (node === target) {
           minPathLength = Math.min(minPathLength, pathLength);
       }
       visited.add(node);
       for (const neighbor of graph[node]) {
           if (!visited.has(neighbor)) {
               queue.push([neighbor, pathLength + 1]);
           }
       }
   }
   return minPathLength === Infinity ? -1 : minPathLength - 1;
}

Asked in Top Tech Interviews

uncategorizedmediumnone

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.