Find Center of Star Graph

Easy

Topics
Graph

You are given an undirected star graph of n nodes labeled 1..n. A star graph has one center node connected to every other node, and no other edges. Given its edges, return the label of the center node.

Example 1

123
centre
In a star every edge touches the centre, so the centre is the node that appears in both of the first two edges — no degree counting and no traversal needed. Here that is 2.
Nodes are drawn on a circle; an edge list says nothing about layout, only about connections.
Input:  n = 3, edges = [[1,2],[2,3]]
Output: 2

Example 2

1234
centre
Input:  n = 4, edges = [[4,1],[4,2],[4,3]]
Output: 4

Constraints

  • 3 <= n <= 10^5
  • The given graph is a valid star graph.
Editorial — intuition, approach, complexity and pitfalls

Intuition

In a star graph one node touches every edge and the other n-1 nodes touch exactly one. The obvious approach is to count degrees across all edges and return the node with degree n-1. That is O(n) and perfectly correct.

But the structure gives something much stronger. The centre appears in every edge, so it appears in the first two. The leaves appear in exactly one edge each, so no leaf can be in both. That means the node common to just the first two edges is the centre — and the remaining edges cannot change the answer.

So the first two edges are sufficient. Compare the endpoints of edge 0 against edge 1: whichever endpoint of edge 0 also shows up in edge 1 is the centre. No counting, no array, no full scan.

This only holds because the problem guarantees the input is a valid star graph. If that were not promised, you would have to verify the shape, and verification requires looking at every edge.

Approach

  1. Take (a, b) = edges[0] and (c, d) = edges[1].
  2. Return a if a == c or a == d, otherwise b.

Dry Run

edges = [[1,2],[2,3]]
  edge0 = (1,2),  edge1 = (2,3)
  is 1 in (2,3)?  no
  so the answer is the other endpoint of edge0: 2        -> 2

edges = [[3,1],[3,5],[3,2],[3,4]]
  edge0 = (3,1),  edge1 = (3,5)
  is 3 in (3,5)?  yes                                   -> 3

  the remaining two edges are never examined, and cannot
  disagree: in a valid star every edge contains the centre

Complexity

  • Time — O(1): two edges inspected regardless of n, which reaches 10^5. The degree-counting alternative is O(n) and allocates a counter array, so this is a genuine asymptotic improvement rather than a micro-optimisation.
  • Space — O(1): four variables.

Common Pitfalls

  • Counting degrees over all edges. Correct, but it does O(n) work and allocates, for information the first two edges already settle.
  • Returning the first endpoint of edge 0 unconditionally. The centre may be listed second — [[1,2],[2,3]] has centre 2, not 1.
  • Comparing only a == c. Edge endpoints are unordered, so the centre may appear as either endpoint of edge 1; both comparisons are needed.
  • Relying on this shortcut when the star property is not guaranteed. Then it is unsound, and detecting a non-star needs every edge.
  • Assuming nodes are 0-indexed. They are labelled 1..n here, which matters if you size a counter array in the alternative approach.

Full Solution — all languages

class Solution:
    def findCenter(self, n, edges):
        a, b = edges[0]
        c, d = edges[1]
        return a if (a == c or a == d) else b
SensAI
Beta
Tap the mic to stop voice mode