The Time When the Network Becomes Idle - Problem

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:

  • If it has not, it will resend the message periodically. The data 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.
  • Otherwise, no more resending will occur 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.

Input & Output

Example 1 — Basic Network
$ Input: edges = [[0,1],[1,2]], patience = [0,2,1]
Output: 8
💡 Note: Server 1 (distance 1): round trip = 2, patience = 2, no resending needed, idle at time 2+1 = 3. Server 2 (distance 2): round trip = 4, patience = 1, resends at times 0,1,2,3, last send at 3, idle at time 3+4 = 7+1 = 8.
Example 2 — No Resending
$ Input: edges = [[0,1],[0,2],[1,2]], patience = [0,10,10]
Output: 3
💡 Note: Both servers 1 and 2 have distance 1, round trip = 2. Since patience ≥ round trip, no resending occurs. Network idle at time 2+1 = 3.
Example 3 — Multiple Resends
$ Input: edges = [[0,1],[1,2],[2,3]], patience = [0,1,1,1]
Output: 12
💡 Note: Server 1: distance 1, round trip = 2, patience = 1. Resends at time 1, idle at time 3. Server 2: distance 2, round trip = 4, patience = 1. Resends at times 1,2,3, idle at time 7. Server 3: distance 3, round trip = 6, patience = 1. Resends at times 1,2,3,4,5, last send at time 5, idle at time 11. Network idle at time 12.

Constraints

  • n == patience.length
  • 2 ≤ n ≤ 105
  • patience[0] == 0
  • 1 ≤ patience[i] ≤ 105 for 1 ≤ i < n
  • 1 ≤ edges.length ≤ min(105, n × (n - 1) / 2)
  • edges[i].length == 2
  • 0 ≤ ui, vi < n
  • ui != vi
  • There are no duplicate edges
  • Each server can directly or indirectly reach every other server

Visualization

Tap to expand
Network Becomes Idle - BFS Approach INPUT Network Graph 0 Master 1 Data 2 Data edges: [0,1] [1,2] patience: 0 idx 0 2 idx 1 1 idx 2 Server 1: dist=1, p=2 Server 2: dist=2, p=1 Round trip = 2 * dist ALGORITHM STEPS 1 BFS from Master Find shortest dist to each server dist[0]=0, dist[1]=1, dist[2]=2 2 Calc Round Trip roundTrip = 2 * dist[i] S1: 2*1=2, S2: 2*2=4 3 Find Last Resend lastSend = ((rt-1)/p) * p S1: ((2-1)/2)*2 = 0 S2: ((4-1)/1)*1 = 3 4 Calc Idle Time idle = lastSend + roundTrip + 1 S1: 0 + 2 + 1 = 3 S2: 3 + 4 + 1 = 8 FINAL RESULT Message Timeline Server 1 (p=2): t=0 t=2 reply Server 2 (p=1): t=0 t=1 t=2 t=3 t=4 reply Last msg sent at t=3 arrives at t=7 Network idle at t=8 Output: 8 max(3, 8) = 8 Network becomes idle Key Insight: Use BFS to find shortest distance from master (server 0) to all data servers. For each server, calculate when its last resent message returns. The last message sent before receiving reply is at time: ((roundTrip - 1) / patience) * patience. Network is idle 1 second after all replies arrive. TutorialsPoint - The Time When the Network Becomes Idle | BFS Approach
Asked in
Google 15 Microsoft 12
18.0K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen