Uncategorized
A holding area for graph problems that don't yet belong to a named pattern section. As patterns emerge, these get promoted into their own sections.
1791. Find Center of Star Graph
Easy·
3 Approachesclick to switch
1
Degree Counting
O(V + E)
O(V)
2
First Repeat Wins
O(1)
O(1)
3
Read Two Edges
O(1)
O(1)
Explanation
In a star graph, one center node is connected to every other node, and there are no other edges. The general approach: count how many edges touch each node, then return the node whose degree equals len(counter) - 1 (it is connected to all other nodes). Correct for any graph shape, but it does full O(E) work and builds a degree map.
Analysis
- Time
- O(V + E)
- Every edge is scanned once to build
counter-O(E). - The final pass over
counter.items()checks every node once -O(V). - Space
- O(V)
counterstores one degree entry per node -O(V).