LeetCode Problem

How to Solve Network Delay Time

This problem involves sending a signal from a given node to all nodes in a network, and determining the minimum time for the signal to reach every node. You must apply graph traversal techniques like Depth-First Search (DFS) or Breadth-First Search (BFS) to find the solution. If all nodes cannot receive the signal, return -1.

GhostInterview Help

Need help with Network Delay Time without spending extra time grinding it?

GhostInterview can read Network Delay Time from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #743Graph traversal with depth-first searchReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Graph traversal with depth-first search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem involves sending a signal from a given node to all nodes in a network, and determining the minimum time for the signal to reach every node. You must apply graph traversal techniques like Depth-First Search (DFS) or Breadth-First Search (BFS) to find the solution. If all nodes cannot receive the signal, return -1.

Problem Statement

You are given a network of n nodes, each labeled from 1 to n. A list of travel times, represented as directed edges times[i] = (ui, vi, wi), is provided, where ui is the source node, vi is the target node, and wi is the time it takes for a signal to travel from the source to the target. The goal is to send a signal from a specified node k and determine the minimum time it takes for all n nodes to receive the signal.

If it is impossible for all nodes to receive the signal, the function should return -1. Otherwise, the output should be the time it takes for the signal to reach every node. In cases where there are unreachable nodes, the solution must account for those cases by returning -1.

Examples

Example 1

Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2

Output: 2

Example details omitted.

Example 2

Input: times = [[1,2,1]], n = 2, k = 1

Output: 1

Example details omitted.

Example 3

Input: times = [[1,2,1]], n = 2, k = 2

Output: -1

Example details omitted.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • times[i].length == 3
  • 1 <= ui, vi <= n
  • ui != vi
  • 0 <= wi <= 100
  • All the pairs (ui, vi) are unique. (i.e., no multiple edges.)

Solution Approach

Graph Traversal with DFS

Start by representing the network as a graph, using an adjacency list. You can use Depth-First Search (DFS) to explore the graph from the starting node k, tracking the signal's arrival time at each node. Ensure to update the minimum time as you traverse edges to other nodes.

Instead of DFS, a more efficient approach is to use Dijkstra's algorithm with a priority queue (heap). This allows for an optimized search of the shortest path, ensuring the signal reaches nodes in the minimal time, while taking care of possible cycles and unreachable nodes.

BFS for Uniform Weight Graph

If the graph is uniform, with all edge weights equal, a Breadth-First Search (BFS) can be an effective strategy. By traversing level by level, BFS can help determine the shortest time required for the signal to reach all nodes, stopping once all nodes are covered or confirming if some are unreachable.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of the solution depends on the method chosen. Using DFS or BFS, the time complexity is O(V + E), where V is the number of nodes and E is the number of edges. With a priority queue, using Dijkstra's algorithm, the complexity is O((V + E) log V), which is more efficient for graphs with non-uniform weights. The space complexity for DFS and BFS is O(V), while the priority queue-based approach uses O(V + E) space for the graph representation and priority queue management.

What Interviewers Usually Probe

  • The candidate is expected to demonstrate knowledge of graph traversal methods and the ability to optimize with a priority queue or BFS.
  • Be prepared for candidates to compare DFS and BFS as options, with an emphasis on using the most efficient method for large input sizes.
  • A successful answer should include handling edge cases, such as unreachable nodes or cycles, and the reasoning for selecting the approach used.

Common Pitfalls or Variants

Common pitfalls

  • Failing to correctly handle the case where not all nodes can receive the signal, leading to an incorrect return value of -1.
  • Incorrectly assuming all graphs are uniform, which may lead to inefficient solutions or improper use of BFS/Dijkstra.
  • Not accounting for the possibility of cycles in the graph, which could lead to an infinite loop or incorrect time calculations.

Follow-up variants

  • A variation of this problem could involve multiple source nodes for the signal, testing the candidate's ability to modify their approach to handle additional starting points.
  • Another variant could involve a directed graph with negative edge weights, which would require considering algorithms like Bellman-Ford instead of Dijkstra or BFS.
  • The problem could also be modified by adding additional constraints, such as limiting the number of allowed edges, which could test the candidate's ability to optimize graph traversal techniques.

How GhostInterview Helps

  • GhostInterview assists in preparing by providing a structured approach to common graph traversal problems, such as DFS, BFS, and Dijkstra's algorithm.
  • By practicing with multiple variations, GhostInterview helps sharpen the ability to adapt solutions based on specific constraints and graph properties.
  • The platform allows users to review typical pitfalls in problems like this one, ensuring they understand how to efficiently handle edge cases like unreachable nodes and cycles.

Topic Pages

FAQ

How do I approach the 'Network Delay Time' problem?

Start by choosing a graph traversal method like DFS or BFS. For more efficiency with weighted edges, use Dijkstra's algorithm with a priority queue.

What is the time complexity of this problem?

The time complexity depends on the approach: DFS/BFS is O(V + E), and Dijkstra's algorithm with a priority queue is O((V + E) log V).

Can I use BFS for graphs with different edge weights?

BFS works best for graphs with uniform edge weights. For non-uniform weights, Dijkstra's algorithm is more efficient.

How do I handle unreachable nodes in 'Network Delay Time'?

Make sure to check for nodes that remain unvisited after the traversal and return -1 if not all nodes can receive the signal.

What makes DFS unsuitable for large networks in this problem?

DFS can be inefficient for large networks, especially when dealing with cycles or non-uniform edge weights. Dijkstra's algorithm with a priority queue offers better performance for such cases.

GhostInterview Solver

Need direct help with Network Delay Time instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Network Delay Time from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.