Find the Hub Node in a Star-Shaped Network
Trees & GraphsAuto-graded · 17 test casesLast updated
Tests recognition of star-graph topology and the insight that the central node is uniquely identified by its degree.
Problem Statement
You are managing a fleet of n machines, each labeled from 0 to n-1. The machines are wired together in a star topology: one special machine — the hub — has a direct cable to every other machine. The remaining machines are leaf nodes and are only ever cabled to the hub; no two leaf nodes are directly connected to each other.
You receive the wiring plan as a list of undirected connections. Each entry connections[i] = [a, b] means machine a and machine b share a cable.
Given the number of machines n and the list connections, return the label of the hub machine.
Input
n— the total number of machines (integer).connections— a list of pairs[a, b], each representing one undirected cable between machineaand machineb.
Output
Return the integer label of the hub machine.
Constraints
3 <= n <= 10^5connections.length == n - 1- All machine labels are distinct integers in the range
[0, n-1]. - The input always describes a valid star topology, so exactly one hub exists.
Examples
Example 1
Input: find_hub(4, [[0, 1], [0, 2], [0, 3]])
Output: 0
Example 2
Input: find_hub(3, [[1, 2], [2, 0]])
Output: 2
Hints
Hint 1
Count how many cables are attached to each machine. What degree must the hub have, and what degree must every leaf have?
Hint 2
In a star with n nodes there are exactly n-1 edges, so the hub touches n-1 cables. Since n >= 3, no leaf can share that count — leaves always have degree 1.
Hint 3
If you trust the input to be a valid star, you only need the first two connections: the hub is the single machine label that appears in both entries. This is an O(1) shortcut worth knowing, but degree-counting is safer when you also want to validate the input.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Insight
A star graph has exactly two kinds of nodes:
- The hub, which has a cable to every other machine — its degree is
n - 1. - Each leaf, which has a cable only to the hub — its degree is
1.
When n >= 3, these two values are always distinct (n - 1 >= 2 > 1), so the hub is the unique node whose degree equals n - 1.
Algorithm
- Allocate a degree array of length
n, initialised to zero. - Walk through every connection
[a, b]and incrementdegree[a]anddegree[b]. - Scan the degree array and return the first index whose value equals
n - 1. - Return
-1if no such index exists (defensive guard; the problem guarantees valid input).
Why It Is Correct
Every connection in the list touches the hub exactly once (because the hub is an endpoint of every edge) and touches no leaf more than once (because leaves only connect to the hub). After the full scan, the hub's degree equals the number of leaves, which is n - 1. Every leaf's degree is exactly 1. The two values cannot coincide when n >= 3, so the hub is uniquely identified.
Returning the first matching index is safe because there is exactly one match in valid input. The order of iteration does not affect correctness.
O(1) Alternative
Because every edge in a valid star contains the hub, the hub is the node shared by any two edges. Checking just the first two connections is enough:
a, b = connections[0]
return a if a in connections[1] else b
This runs in O(1) time and space. The trade-off is that it performs no validation — it silently returns a node even on malformed input. Present it as an optimization for contexts where the input is guaranteed valid.
Complexity
- Time: O(n) — one pass over
n - 1connections and one pass overndegree entries. - Space: O(n) — for the degree array.
Edge Cases
- Minimum graph (
n = 3): the hub has degree 2 and each leaf has degree 1; still distinct and handled correctly. - Hub is not node 0: the degree scan covers all labels, so the hub's position in the label space does not matter.
- Hub label is
n - 1: the loop runs to the end and still finds it. - Connections listed in any order: the graph is undirected;
[a, b]and[b, a]both increment the same two degree counters.
Full Solution
from typing import List
def find_hub(n: int, connections: List[List[int]]) -> int:
# Tally the number of cables attached to each machine.
degree = [0] * n
for a, b in connections:
degree[a] += 1
degree[b] += 1
# The hub is the only machine connected to all n-1 others.
for machine, d in enumerate(degree):
if d == n - 1:
return machine
# Unreachable on valid input; returned as a defensive sentinel.
return -1