Find the Shortest Walk Visiting Every Vertex

Quick Overview

Find the Shortest Walk Visiting Every Vertex. Given a connected undirected adjacency-list graph, return the minimum number of edges in a walk visiting every vertex.

Find the Shortest Walk Visiting Every Vertex

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Given a connected undirected adjacency-list graph, return the minimum number of edges in a walk visiting every vertex. You may start and end anywhere, revisit vertices, and reuse edges. ```cpp class Solution { public: int shortestPathLength(vector<vector<int>>& graph); }; ``` ### Examples **Example 1:** ```text graph = [[]] Output: 0 Explanation: A single vertex is visited without traversing an edge. ``` **Example 2:** ```text graph = [[1],[0,2],[1,3],[2]] Output: 3 Explanation: Walk0->1->2->3; visiting four vertices requires at least three edges. ``` ### Constraints - Vertex count V lies in [1,12]. - Neighbor labels are valid, with no self-loop and fewer than V entries per list. - Adjacency is symmetric and the graph is connected. ### Contract considerations Keep edge-count length and allow revisits. ```hint Read the boundary condition What must be remembered in addition to the current vertex? ``` ### Boundary reminders Keep edge-count length and allow revisits.

Overview: Find the Shortest Walk Visiting Every Vertex. Given a connected undirected adjacency-list graph, return the minimum number of edges in a walk visiting every vertex.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Given a connected undirected adjacency-list graph, return the minimum number of edges in a walk visiting every vertex. You may start and end anywhere, revisit vertices, and reuse edges.

class Solution {
public:
    int shortestPathLength(vector<vector<int>>& graph);
};

Examples

Example 1:

graph = [[]]
Output: 0
Explanation: A single vertex is visited without traversing an edge.

Example 2:

graph = [[1],[0,2],[1,3],[2]]
Output: 3
Explanation: Walk0->1->2->3; visiting four vertices requires at least three edges.

Constraints

  • Vertex count V lies in [1,12].
  • Neighbor labels are valid, with no self-loop and fewer than V entries per list.
  • Adjacency is symmetric and the graph is connected.

Contract considerations

Keep edge-count length and allow revisits.

Boundary reminders

Keep edge-count length and allow revisits.