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
.
For example:
times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output: 2
times = [[1,2,1]], n = 2, k = 1
Output: 1
times = [[1,2,1]], n = 2, k = 2
Output: -1
Explain how you would approach this problem. What data structures and algorithms would you use, and what is the time and space complexity of your solution? Consider any edge cases and explain how your code handles them.
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
.
A brute-force solution would involve exploring all possible paths from the source node k
to all other nodes in the graph. We can perform a Depth-First Search (DFS) or Breadth-First Search (BFS) to find paths. However, this approach would not guarantee the minimum time and could be inefficient due to exploring many non-optimal paths.
Complexity Analysis
The optimal solution is to use Dijkstra's algorithm, which is designed to find the shortest paths from a single source node to all other nodes in a weighted graph. Here's how it works:
dist
of size n
, initialized with infinity for all nodes except the source node k
, which is initialized with 0.u
with the smallest distance from the priority queue.v
of u
:
v
through u
.v
:
dist[v]
with the new smaller distance.v
to the priority queue (or update its priority if it's already in the queue).dist
array contains the shortest distances from the source node k
to all other nodes.dist
array. If any value is still infinity, it means that some nodes are unreachable, and return -1. Otherwise, return the maximum distance.Code Implementation (Python)
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, weight in graph[u]:
if dist[v] > dist[u] + weight:
dist[v] = dist[u] + weight
heapq.heappush(pq, (dist[v], v))
max_time = max(dist.values())
return max_time if max_time != float('inf') else -1
Complexity Analysis
k
. In this case, Dijkstra's algorithm will result in some distances remaining infinity, and the function should return -1
.k
is not a valid node (e.g., k
is less than 1 or greater than n
), it may be good to raise an exception, but based on the problem constraints that case is excluded.