Network Unification Timestamp
Trees & GraphsAuto-graded · 18 test casesLast updated
Tests Union-Find data structure application to track dynamic graph connectivity and determine when all nodes merge into one component.
You have n nodes numbered from 0 to n-1. At the beginning, every node is isolated. You receive a list of connections edges, where each entry edges[i] = [u, v, t] means nodes u and v get linked at timestamp t.
Every timestamp in the input is distinct. The graph becomes "unified" when you can reach any node from any other node through the available connections. Return the smallest timestamp at which unification occurs. If the nodes never form a single connected component, return -1.
Constraints
2 <= n <= 10^51 <= len(edges) <= 10^50 <= u, v < nuandvmay be equal (self-loop)- All timestamp values
tare unique positive integers - The same pair of nodes may appear in multiple edges with different timestamps
Examples
Example 1
Input: network_unification_time(4, [[0, 1, 10], [1, 2, 15], [2, 3, 20]])
Output: 20
Example 2
Input: network_unification_time(3, [[0, 1, 5], [0, 2, 10]])
Output: 10
Hints
Hint 1
Consider processing connections in chronological order and tracking how many separate components remain after each connection is added.
Hint 2
Union-Find (Disjoint Set Union) provides efficient operations to merge components and check if two nodes belong to the same component.
Hint 3
The graph is unified when the number of distinct components drops to exactly one.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
We process connections chronologically and maintain a count of how many disconnected components exist. Initially, each node forms its own component, giving us n components total. When a connection links two nodes from different components, those components merge and our count decreases by one. The moment the count reaches one, every node can reach every other node, so we return that timestamp.
Union-Find is ideal for this scenario. It maintains a forest where each tree represents a component:
find(x)identifies which component contains nodexby returning the tree root- Merging two components happens by making one root point to the other
- We track component sizes to attach smaller trees under larger ones, keeping trees shallow
The algorithm:
- Sort all connections by their timestamps
- Initialize Union-Find with
nsingleton components - Process each connection in chronological order:
- Find the roots of both nodes
- If they share the same root, the connection is redundant (skip it)
- Otherwise, merge the components, decrement the counter, and check if we've reached unity
- If we exhaust all connections without reaching one component, return
-1
Why This Works
Union-Find correctly maintains component membership through the transitive property of connectivity. When we merge two components, all nodes in both groups become mutually reachable. Our component counter accurately reflects reality because:
- Each merge of distinct components reduces the count by exactly one
- Connections within a single component (including duplicates and self-loops) don't change the count
- Processing in time order ensures we find the earliest unification moment
Full connectivity requires at least n-1 connections between distinct components. With fewer merging connections, some nodes remain isolated.
Complexity
Time: O(E log E + E · α(n)) where E is the number of edges. Sorting dominates at O(E log E). Each Union-Find operation costs O(α(n)) amortized (inverse Ackermann function, effectively constant) thanks to path halving and union by size.
Space: O(n) for the parent and size arrays, plus O(E) for sorting (depending on the sort implementation).
Solution Code
from typing import List
def network_unification_time(n: int, edges: List[List[int]]) -> int:
parent = list(range(n))
size = [1] * n
components = n
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for u, v, t in sorted(edges, key=lambda edge: edge[2]):
ru = find(u)
rv = find(v)
if ru == rv:
continue
if size[ru] < size[rv]:
ru, rv = rv, ru
parent[rv] = ru
size[ru] += size[rv]
components -= 1
if components == 1:
return t
return -1