Maximum Star Sum of a Graph - Problem

Imagine you're analyzing a social network where each person has a value (representing their influence or importance), and you want to find the most valuable "star-shaped" community.

You're given an undirected graph with n nodes numbered from 0 to n-1, where each node i has a value vals[i]. The graph is defined by edges connecting these nodes.

A star graph is a special subgraph where:

  • There's one center node
  • All other nodes in the star are directly connected to this center
  • No edges exist between the non-center nodes

The star sum is the total value of all nodes in the star (center + its neighbors). Your goal is to find the maximum possible star sum when the star contains at most k edges.

Key insight: Since we want to maximize the sum, for each potential center node, we should greedily select its k highest-valued neighbors (if they have positive values).

Input & Output

example_1.py โ€” Basic Star Graph
$ Input: vals = [1,2,3,4,10,-10,-20], edges = [[0,1],[1,2],[1,3],[3,4],[3,5],[3,6]], k = 2
โ€บ Output: 16
๐Ÿ’ก Note: The optimal star has center at node 3 (val=4). Its neighbors are [1,4,5,6] with values [2,10,-10,-20]. We greedily select the top 2 positive neighbors: 4 (val=10) and 1 (val=2). Star sum = 4 + 10 + 2 = 16.
example_2.py โ€” Single Node Best
$ Input: vals = [-5], edges = [], k = 0
โ€บ Output: -5
๐Ÿ’ก Note: Only one node exists with value -5. Since k=0, we can't include any edges, so the star is just the single node with sum -5.
example_3.py โ€” All Negative Neighbors
$ Input: vals = [5,-1,-2,-3,-4], edges = [[0,1],[0,2],[0,3],[0,4]], k = 2
โ€บ Output: 5
๐Ÿ’ก Note: Node 0 (val=5) has neighbors with all negative values. Since adding negative neighbors decreases the sum, the optimal star contains only the center node 0, giving sum = 5.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค edges.length โ‰ค min(2 ร— 105, n ร— (n - 1) / 2)
  • edges[i].length == 2
  • 0 โ‰ค ai, bi โ‰ค n - 1
  • ai โ‰  bi
  • There are no repeated edges
  • -104 โ‰ค vals[i] โ‰ค 104
  • 0 โ‰ค k โ‰ค n - 1

Visualization

Tap to expand
Maximum Star Sum VisualizationCenterNode 3 (val=4)N1val=10N2val=8N3val=2N4val=-3โœ“ Selected (k=2)โœ— RejectedGreedy ProcessStep 1: Sort neighbors by valueN1: 10, N2: 8, N3: 2, N4: -3Step 2: Select top k=2 positiveโœ“ N1 (val=10) - Best choiceโœ“ N2 (val=8) - Second bestโœ— N3 (val=2) - Exceeds k limitโœ— N4 (val=-3) - Negative valueStep 3: Calculate sumCenter + Selected = 4 + 10 + 8Total Star Sum = 22๐ŸŽฏ Key InsightGreedy selection works because we always want the highest-valued neighbors first.There's no benefit to choosing lower-valued neighbors when higher-valued ones are available!
Understanding the Visualization
1
Choose Center
Pick any node as the center of your star network
2
Rank Connections
Sort all neighbors by their value/influence in descending order
3
Greedy Selection
Select up to k neighbors, but only those with positive values
4
Calculate Value
Sum the center's value plus all selected neighbors' values
5
Try All Centers
Repeat for every possible center and keep the maximum sum
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works perfectly here because we're trying to maximize a sum, and there's never a reason to pick a lower-valued neighbor when a higher-valued one is available. This transforms a potentially complex optimization problem into a simple sorting and selection task!
Asked in
Meta 42 Amazon 35 Google 28 Microsoft 22
67.4K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen