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<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>)
, where u<sub>i</sub>
is the source node, v<sub>i</sub>
is the target node, and w<sub>i</sub>
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:
times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output: 2
Example 2:
times = [[1,2,1]], n = 2, k = 1
Output: 1
Example 3:
times = [[1,2,1]], n = 2, k = 2
Output: -1
Explain how you would solve this problem efficiently. Consider edge cases and discuss the time and space complexity of your approach.
This problem asks us to find the minimum time it takes for a signal sent from node k
to reach all other nodes in a network. If it's impossible for all nodes to receive the signal, return -1.
A brute-force approach would be to explore all possible paths from the starting node k
and keep track of the minimum time it takes to reach each node. We can use a recursive Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the graph. However, this approach can be inefficient, especially if there are cycles in the graph, as we might end up revisiting nodes multiple times.
dist
array where dist[i]
represents the shortest time to reach node i
from k
. Initialize all values to infinity except dist[k]
which will be zero.dist
whenever a shorter path to a node is found.dist
is still infinity, return -1.dist
array.Big O Complexity
A more efficient approach is to use Dijkstra's algorithm, which is designed to find the shortest path from a source node to all other nodes in a weighted graph. Dijkstra's algorithm uses a priority queue to efficiently explore the nodes with the smallest known distance first.
dist
array to store the shortest distance from the source node k
to each node, setting all distances to infinity initially.k
to itself to 0.u
with the smallest distance from the priority queue.v
of u
:
v
through u
.v
:
v
in the dist
array.v
to the priority queue with the new distance.dist
array, which represents the minimum time it takes for all nodes to receive the signal.Edge Cases
k
. In this case, the algorithm should return -1.times
is empty and n
is greater than 1, then it is impossible to reach all nodes, so return -1. If n
is 1, return 0.Big O Complexity
dist
array.Python Code
import heapq
def networkDelayTime(times, n, k):
graph = {i: [] for i in range(1, n + 1)}
for u, v, w in times:
graph[u].append((v, w))
dist = {i: float('inf') for i in range(1, n + 1)}
dist[k] = 0
pq = [(0, k)] # (distance, node)
while pq:
d, u = heapq.heappop(pq)
if d > dist[u]:
continue
for v, w in graph[u]:
if dist[v] > dist[u] + w:
dist[v] = dist[u] + w
heapq.heappush(pq, (dist[v], v))
max_time = max(dist.values())
return max_time if max_time != float('inf') else -1