Minimum Hops in a Server Network
As a network administrator at Zoho, understanding the connectivity and communication efficiency within our distributed server infrastructure is paramount. Data packets traversing the network incur latency, which is often directly proportional to the number of 'hops' or direct connections they must pass through. This problem aims to simulate a critical network monitoring task: determining the maximum number of hops a signal would take to reach all reachable servers from a designated starting point.
Your task is to implement a function that calculates the maximum of the minimum hops required for a signal to reach every other server in a network from a given startServer. If any server in the network cannot be reached from the startServer, the function should indicate this by returning -1.
n: An integer representing the total number of servers in the network. Servers are labeled from 1 to n.connections: A 2D array of integers, where each connections[i] = [u, v] denotes a direct, bidirectional network connection between server u and server v.startServer: An integer representing the server from which the signal originates.startServer. If not all servers can be reached, return -1.[u, v] exists, [v, u] is also implied.n=1, the startServer is the only server, and 0 hops are needed.connections is empty and n > 1, only the startServer is reachable from itself, so other servers are unreachable.Examples
Example 1
Input:
n = 4, connections = [[1,2],[1,3],[2,4]], startServer = 1
Output:
2
Note:
From server 1:
Example 2
Input:
n = 3, connections = [[1,2]], startServer = 1
Output:
-1
Note:
From server 1:
Example 3
Input:
n = 1, connections = [], startServer = 1
Output:
0
Note:
There is only one server, which is the start server itself. 0 hops are needed to 'reach' it.
Constraints
1 <= n <= 1000 <= connections.length <= n * (n - 1) / 21 <= u, v, startServer <= nu != v (no self-loops)[u, v] pairs in connections)Loading editor...