Network Delay Time

Network Delay Time Problem

You are given a network of  n directed nodes labeled from 1 to n, along with a list of directed edges, times, where times[i] = (ui, vi, ti):

  • ui represents the source node (1 to n).
  • vi represents the target node (1 to n).
  • ti is the time it takes for a signal to travel from ui to vi (non-negative integer).

An integer k is also provided, representing the starting node for the signal.

Return the minimum time required for all n nodes to receive the signal. If it’s not possible for all nodes to receive the signal, return -1.

Network Delay Time Problem

Examples related to Network Delay Time Problem

Example 1:

Network Delay Time Question

Example 2:

Constraints:

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 1000

Hints to solve Network Delay Time

Recommended Time & Space Complexity

Aim for a solution with O(E log V) time and O(V + E) space, where E is the number of edges and V is the number of nodes.

Hint 1:

  • This problem requires finding the shortest time to reach all nodes from a single source node.

  • Heap-based algorithm like Dijkstra’s Algorithm is ideal for finding the shortest paths from a source to all other nodes.

Hint 2:

  • Using Dijkstra’s Algorithm, calculate the shortest paths from the source to all nodes.

  • To ensure all nodes are reached, the algorithm continues until the heap is empty.

  • This approach systematically updates the shortest distances as new edges are processed.

Hint 3:

  • Implement Dijkstra’s Algorithm with a Min-Heap. Build an adjacency list from the given edge list.

  • Initialize a dist[] array with infinity for all nodes, except the source node (dist[source] = 0).

  • Push the source node into the heap and start processing. After the heap is empty, check if all nodes were visited.

  • If any node remains unreachable, return -1. Otherwise, return the maximum value in dist[], representing the minimum time to reach all nodes.

Methods to Solve Network Delay Time Problem

There are mainly 5 approach to solve Network Delay Time problem:

  1. Depth First Search Method
  2. Floyd Warshall Algorithm
  3. Bellman Ford Algorithm
  4. Shortest Path Faster Algorithm
  5. Dijkstra’s Algorithm

1. Depth First Search Method

Traverse all possible paths from the source node using DFS. At each step, update the time taken to reach a node if a shorter path is found. This approach can be inefficient for large graphs.

  • Time complexity: O(V∗E)
  • Space complexity: O(V+E)

Where V is the number of vertices and E is the number of edges.

Code:

2. Floyd Warshall Algorithm

  • Dynamic programming algorithm that calculates the shortest paths between all pairs of nodes.
  • It iteratively updates the shortest paths using each node as an intermediate, but it has a higher time complexity of O(n^3).
  • Time complexity: O(V^3)
  • Space complexity: O(V^2)

Where V is the number of vertices.

Code:

3. Bellman Ford Algorithm

  • Iteratively relax all edges up to (n-1) times to find the shortest paths from the source to all other nodes.

  • It works well with negative edge weights but is slower, with O(VE) time complexity.

  • Time complexity: O(V x E)
  • Space complexity: O(V)

Where V is the number of vertices and E is the number of edges.

Code:

4. Shortest Path Faster Algorithm

  • An optimized version of the Bellman-Ford algorithm that uses a queue to relax edges more selectively, improving the average runtime in many cases.

  • Time complexity: O(V+E) in average case, O(V∗E) in worst case.
  • Space complexity: O(V + E)

Where V is the number of vertices and E is the number of edges.

Code:

5. Dijkstra’s Algorithm

  • Greedy algorithm that uses a min-heap to find the shortest path from a source node to all other nodes efficiently.

  • It processes nodes in increasing order of distance, with O(E log V) time complexity.

  • Time complexity: O(E log ⁡V)
  • Space complexity: O(V + E)

Where V is the number of vertices and E is the number of edges.

Code:

More Articles