The Time When the Network Becomes Idle - Problem
Network Message Processing Simulation

Imagine you're managing a distributed server network where server 0 is the master server and servers 1 to n-1 are data servers. The network is connected through message channels defined by edges, where edges[i] = [ui, vi] means servers ui and vi can communicate directly.

Here's how the system works:
• At second 0, all data servers send their first message to the master server
• Messages travel through the shortest path to reach the master server
• The master server processes messages instantly and sends replies back through the same path
• If a data server doesn't receive a reply within its patience[i] seconds, it resends the message
• Once a server receives its reply, it stops sending messages

Goal: Find the earliest second when the network becomes completely idle (no messages in transit).

Input & Output

example_1.py — Basic Network
$ Input: edges = [[0,1],[1,2]], patience = [0,2,1]
Output: 8
💡 Note: Server 1 (distance=1): round_trip=2, patience=2, sends at 0,2,4,6. Last reply at 8. Server 2 (distance=2): round_trip=4, patience=1, sends at 0,1,2,3. Last reply at 7. Network idle at max(8,7)+1 = 8.
example_2.py — Star Network
$ Input: edges = [[0,1],[0,2],[1,3]], patience = [0,10,10,10]
Output: 3
💡 Note: All servers have high patience (10), so they only send one message each. Server 3 has the longest path (distance=2), so it takes 4 seconds for round trip. Network idle at 4+1 = 5. Wait, let me recalculate: distance to 3 is 2, round trip is 4, only one message sent, completes at 4, idle at 5.
example_3.py — Minimum Case
$ Input: edges = [[0,1]], patience = [0,1]
Output: 3
💡 Note: Only one data server at distance 1. Round trip = 2, patience = 1. Messages sent at times 0 and 1. Last message (sent at time 1) completes at time 3. Network idle at 4.

Constraints

  • n == patience.length
  • 2 ≤ n ≤ 105
  • patience[0] == 0
  • 1 ≤ patience[i] ≤ 105 for i > 0
  • 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 computer is directly or indirectly connected to every other server

Visualization

Tap to expand
Network Message Processing0Master Server1Distance: 1Patience: 22Distance: 2Patience: 13Distance: 1Patience: 3MessagesMessagesCalculation Process:1. Server 1: Round trip = 2×1 = 2, sends at times 0,2,4,6, last completes at 82. Server 2: Round trip = 2×2 = 4, sends at times 0,1,2,3, last completes at 73. Server 3: Round trip = 2×1 = 2, patience = 3, sends only at 0, completes at 2Network becomes idle at: max(8,7,2) + 1 = 9 seconds
Understanding the Visualization
1
Build Network Graph
Create connections between servers based on edges
2
Find Shortest Paths
Use BFS to find shortest distance from each server to master
3
Calculate Message Patterns
Determine when each server sends its messages based on patience
4
Find Network Idle Time
Calculate when the last message completes its journey
Key Takeaway
🎯 Key Insight: Instead of simulating every second, calculate mathematically when each server's final message completes its round trip. The network becomes idle one second after the maximum completion time.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
28.4K Views
Medium Frequency
~25 min Avg. Time
1.3K 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