Advertisement

Network Delay Time - LeetCode 743 Solution

Network Delay Time - Complete Solution Guide

Network Delay Time is LeetCode problem 743, 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

You are given a network of n nodes, labeled from 1 to n . You are also given times , a list of travel times as directed edges times[i] = (u i , v i , w i ) , where u i is the source node, v i is the target node, and w i is the time it takes for a signal to travel from source to target. We will send a signal from a given node k . Return the minimum time it takes for all the n nodes to receive the signal . If it is impossible for all the n nodes to receive the signal, return -1 . Example 1: Input:

Detailed Explanation

The problem asks us to find the minimum time required for a signal to reach all nodes in a network. The network is represented as a directed graph where nodes are numbered from 1 to n, and edges represent travel times between nodes. We are given a starting node 'k' from which the signal originates. If it's impossible for the signal to reach all nodes, we return -1.

Solution Approach

The provided solutions use Dijkstra's algorithm to find the shortest time it takes for a signal to reach each node from the starting node 'k'. The algorithm maintains a priority queue to explore nodes in order of their shortest known time from the starting node. It also keeps track of the shortest time it takes to reach each node. After running Dijkstra's, the algorithm checks if all nodes have been reached (i.e., their shortest time is known). If so, it returns the maximum of these shortest times. Otherwise, it returns -1.

Step-by-Step Algorithm

  1. Step 1: Build the graph represented as an adjacency list where the key is the source node and the value is a list of its neighbors along with the travel time to each neighbor.
  2. Step 2: Initialize a priority queue (min-heap) to store nodes to visit, prioritized by the time it takes to reach them from the source node 'k'. Initially, the priority queue contains only the starting node 'k' with a time of 0.
  3. Step 3: Initialize a dictionary/hash map 'dist' to store the shortest time it takes to reach each node from the source node. This can be used as a visited set as well.
  4. Step 4: While the priority queue is not empty, extract the node with the smallest time from the priority queue.
  5. Step 5: If the extracted node has already been visited (i.e., its shortest time is already known and stored in 'dist'), skip it.
  6. Step 6: Mark the extracted node as visited and store its shortest time in 'dist'.
  7. Step 7: Iterate through all the neighbors of the extracted node. For each neighbor, calculate the time it takes to reach the neighbor from the source node (which is the time to reach the current node plus the travel time from the current node to the neighbor).
  8. Step 8: If the neighbor has not been visited, add it to the priority queue with its calculated time.
  9. Step 9: After the priority queue is empty, check if all nodes have been visited (i.e., if the size of 'dist' is equal to 'n').
  10. Step 10: If all nodes have been visited, find the maximum time in 'dist'. This is the minimum time it takes for all nodes to receive the signal. Return this maximum time.
  11. Step 11: If not all nodes have been visited, it means that some nodes are not reachable from the starting node. Return -1.

Key Insights

  • Insight 1: The problem can be modeled as a shortest path problem on a directed graph.
  • Insight 2: Dijkstra's algorithm is well-suited for finding the shortest paths from a single source node to all other nodes in a weighted graph.
  • Insight 3: The problem requires us to check if all nodes are reachable from the starting node 'k' and return the maximum of the shortest path times to all reachable nodes.

Complexity Analysis

Time Complexity: O(ElogV)

Space Complexity: O(V+E)

Topics

This problem involves: Depth-First Search, Breadth-First Search, Graph, Heap (Priority Queue), Shortest Path.

Companies

Asked at: Akuna Capital, Netflix.