There is a network of n servers, labeled from 0 to n - 1. You are given a 2D integer array edges, where edges[i] = [ui, vi] indicates there is a message channel between servers ui and vi, and they can pass any number of messages to each other directly in one second. You are also given a 0-indexed integer array patience of length n.
All servers are connected, i.e., a message can be passed from one server to any other server(s) directly or indirectly through the message channels.
The server labeled 0 is the master server. The rest are data servers. Each data server needs to send its message to the master server for processing and wait for a reply. Messages move between servers optimally, so every message takes the least amount of time to arrive at the master server. The master server will process all newly arrived messages instantly and send a reply to the originating server via the reversed path the message had gone through.
At the beginning of second 0, each data server sends its message to be processed. Starting from second 1, at the beginning of every second, each data server will check if it has received a reply to the message it sent (including any newly arrived replies) from the master server:
i will resend the message every patience[i] second(s), i.e., the data server i will resend the message if patience[i] second(s) have elapsed since the last time the message was sent from this server.The network becomes idle when there are no messages passing between servers or arriving at servers.
Return the earliest second starting from which the network becomes idle.
Example 1:
Input: edges = [[0,1],[1,2]], patience = [0,2,1] Output: 8 Explanation: At (the beginning of) second 0, - Data server 1 sends its message (denoted 1A) to the master server. - Data server 2 sends its message (denoted 2A) to the master server. At second 1, - Message 1A arrives at the master server. Master server processes message 1A instantly and sends a reply 1A back. - Server 1 has not received any reply. 1 second (1 < patience[1] = 2) elapsed since this server has sent the message, therefore it does not resend the message. - Server 2 has not received any reply. 1 second (1 == patience[2] = 1) elapsed since this server has sent the message, therefore it resends the message (denoted 2B). At second 2, - The reply 1A arrives at server 1. No more resending will occur from server 1. - Message 2A arrives at the master server. Master server processes message 2A instantly and sends a reply 2A back. - Server 2 resends the message (denoted 2C). ... At second 4, - The reply 2A arrives at server 2. No more resending will occur from server 2. ... At second 7, reply 2D arrives at server 2. Starting from the beginning of the second 8, there are no messages passing between servers or arriving at servers. This is the time when the network becomes idle.
Example 2:
Input: edges = [[0,1],[0,2],[1,2]], patience = [0,10,10] Output: 3 Explanation: Data servers 1 and 2 receive a reply back at the beginning of second 2. From the beginning of the second 3, the network becomes idle.
Constraints:
n == patience.length2 <= n <= 105patience[0] == 01 <= patience[i] <= 105 for 1 <= i < n1 <= edges.length <= min(105, n * (n - 1) / 2)edges[i].length == 20 <= ui, vi < nui != viProblem Overview: You are given an undirected network of servers where server 0 is the main server. Every other server repeatedly sends messages to server 0 until it receives a reply. The task is to determine the earliest second when no messages are being transmitted anywhere in the network.
Approach 1: BFS to Find Shortest Path and Calculate Idle Time (O(n + m) time, O(n + m) space)
This approach models the system as a graph and computes the shortest distance from the main server to every other server using Breadth-First Search. Since each edge takes one second to traverse, the shortest path gives the minimum round-trip time for messages. For each server i, calculate the round-trip time 2 * dist[i]. If the server’s patience[i] is smaller than this round-trip time, the server resends messages periodically before receiving the reply. The last message sent is determined using ((roundTrip - 1) // patience[i]) * patience[i]. Add the round-trip time to that send moment to get the final reply arrival time. Track the maximum across all servers and add one second to determine when the network becomes idle. BFS works well here because the graph is unweighted and guarantees shortest paths efficiently.
Approach 2: DFS Mimicking BFS for Recursive Traversal (O(n + m) time, O(n + m) space)
This method performs a depth-first traversal while explicitly tracking distance levels from the root to mimic BFS behavior. The graph is stored using adjacency lists built from the edges array. During recursion, maintain the current distance from server 0 and propagate it through neighbors that have not been visited. Once distances are determined, apply the same resend timing formula used in the BFS solution to compute when the last reply arrives. Although DFS can compute distances, it requires careful handling to avoid revisiting nodes and does not naturally guarantee shortest paths unless the traversal is controlled level by level. In practice, BFS is cleaner for shortest-path problems in unweighted graph structures.
Recommended for interviews: The BFS shortest-path solution is the expected approach. Interviewers want to see that you immediately recognize the problem as an unweighted graph shortest-path scenario. Computing resend timing afterward shows understanding of message scheduling and arithmetic edge cases. A DFS-based implementation can work but is less intuitive; BFS demonstrates stronger mastery of graph traversal patterns commonly used in systems and networking problems.
Approach: Use Breadth-First Search (BFS) to find the shortest path from each server to the master server (server 0). Each server needs to send a message to and receive a reply back from the master server. For this, calculate the round-trip time for each server. If the patience time of a server is less than twice its one-way distance, the server will resend messages until it receives a reply. Determine the time when the network becomes idle by considering all these factors.
This Python code utilizes a queue to perform BFS starting from the master server. The level of each node in the BFS corresponds to the shortest distance. For each server, calculate the time taken for a message roundtrip and determine if additional messages are sent based on the patience time of the server. Finally, determine the maximum time required for the network to become idle and add 1 to account for zero-based indexing.
Time Complexity: O(E + V), where V is the number of servers and E is the number of edges. BFS traverses each node and edge once.
Space Complexity: O(V + E) to store the graph and additional arrays for distance and queue operations.
Approach: Use Depth-First Search (DFS) to simulate a BFS-like level exploration without maintaining an explicit queue. By recursively visiting each node and its neighbors, similar to BFS, compute the shortest paths. Despite DFS being inherently stack-based, we can emulate BFS by careful control of recursion depth. This alternative can offer a recursive way of computation, matching typical BFS outcomes on tree-like graphs.
In C++, we take an alternative route with DFS to mimic the shortest path evaluation usually done by BFS. By tracking current depth in recursion and updating path lengths accordingly, this solution efficiently determines the idle time, just as the BFS would.
C++
Time Complexity: O(V + E) as we visit each edge and node similarly to BFS, ensuring linear traversal in interconnected graphs.
Space Complexity: O(V + E), including the internal stack usage depth required for recursion.
First, we construct an undirected graph g based on the 2D array edges, where g[u] represents all neighboring nodes of node u.
Then, we can use breadth-first search (BFS) to find the shortest distance d_i from each node i to the main server. The earliest time that node i can receive a reply after sending a message is 2 times d_i. Since each data server i resends a message every patience[i] seconds, the last time that each data server sends a message is (2 times d_i - 1) / patience[i] times patience[i]. Therefore, the latest time that the network becomes idle is (2 times d_i - 1) / patience[i] times patience[i] + 2 times d_i, plus 1 second for processing time. We find the latest of these times, which is the earliest time that the computer network becomes idle.
The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| BFS to Find Shortest Path and Calculate Idle Time | Time Complexity: O(E + V), where V is the number of servers and E is the number of edges. BFS traverses each node and edge once. Space Complexity: O(V + E) to store the graph and additional arrays for distance and queue operations. |
| DFS Mimicking BFS for Recursive Traversal | Time Complexity: O(V + E) as we visit each edge and node similarly to BFS, ensuring linear traversal in interconnected graphs. Space Complexity: O(V + E), including the internal stack usage depth required for recursion. |
| BFS | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| BFS Shortest Path + Idle Time Calculation | O(n + m) | O(n + m) | Best general solution for unweighted graphs where shortest path from a source is required |
| DFS Mimicking BFS Distance Tracking | O(n + m) | O(n + m) | Useful if implementing recursive traversal, though less natural than BFS for shortest paths |
2039. The Time When the Network Becomes Idle (Leetcode Medium) • Programming Live with Larry • 1,977 views views
Watch 2 more video solutions →Practice The Time When the Network Becomes Idle with our built-in code editor and test cases.
Practice on FleetCode