File: find-center-of-star-graph/solution.py

Date: 2026-06-06

Time: 16:35

Find Center of Star Graph — solution.py

Purpose

This file solves LeetCode 1791: Find Center of Star Graph. It identifies the center node of a star graph — the single node that every edge connects to. The file owns both the solution and its test suite, following the repo's per-problem directory convention.

Key Components

Solution.find_center(edges: List[List[int]]) -> int — The core algorithm. Exploits the structural property of a star graph: the center node must appear in every edge. Therefore, it only needs to inspect the first two edges. If edges[0][0] appears in edges[1], it's the center; otherwise edges[0][1] must be.

TestSolution — Five test cases covering:

Patterns

O(1) solution via structural insight. Instead of counting node degrees (O(n)), the solution recognizes that in a star graph, the center appears in *all* edges. Checking just two edges is sufficient — the center must be in both, and each edge has only two nodes, so comparing edges[0][0] against edges[1] (using Python's in on a two-element list) identifies it immediately.

in operator on a small list. edges[0][0] in edges[1] does a linear scan of a two-element list, which is effectively O(1). This is idiomatic Python for small membership checks where importing or constructing a set would be overkill.

Dependencies

Imports: List from typing (type annotation), unittest (test framework). No project-internal dependencies.

Imported by: The test_solution.py in this same directory, plus the "Imported By" list in the prompt shows hundreds of other test files — this is likely an artifact of the repo's import graph tooling rather than actual runtime imports.

Flow

1. Take the first edge edges[0], which connects two nodes: [u, v].

2. Check if u (edges[0][0]) appears in the second edge edges[1].

3. If yes, u is the center. If no, v (edges[0][1]) must be.

4. Return the center node.

No iteration, no data structures, no preprocessing. The entire function is two comparisons and a return.

Invariants

Error Handling

None. Invalid inputs (empty list, single edge, non-star graphs) will either raise IndexError or return an incorrect result silently. This is standard for LeetCode solutions where inputs are guaranteed valid by the problem constraints.

Topics to Explore

Beliefs