Watch 3 video solutions for Maximum Sum of Edge Values in a Graph, a hard level problem involving Math, Greedy, Graph. This walkthrough by ExpertFunda has 509 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an undirected connected graph of n nodes, numbered from 0 to n - 1. Each node is connected to at most 2 other nodes.
The graph consists of m edges, represented by a 2D array edges, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi.
You have to assign a unique value from 1 to n to each node. The value of an edge will be the product of the values assigned to the two nodes it connects.
Your score is the sum of the values of all edges in the graph.
Return the maximum score you can achieve.
Example 1:
Input: n = 4, edges = [[0,1],[1,2],[2,3]]
Output: 23
Explanation:
The diagram above illustrates an optimal assignment of values to nodes. The sum of the values of the edges is: (1 * 3) + (3 * 4) + (4 * 2) = 23.
Example 2:
Input: n = 6, edges = [[0,3],[4,5],[2,0],[1,3],[2,4],[1,5]]
Output: 82
Explanation:
The diagram above illustrates an optimal assignment of values to nodes. The sum of the values of the edges is: (1 * 2) + (2 * 4) + (4 * 6) + (6 * 5) + (5 * 3) + (3 * 1) = 82.
Constraints:
1 <= n <= 5 * 104m == edges.length1 <= m <= nedges[i].length == 20 <= ai, bi < nai != biProblem Overview: You are given a graph where each edge contributes a value to the total score. The goal is to choose a set of edges that maximizes the total sum while respecting the constraints defined by the graph structure and allowed operations.
Approach 1: Brute Force Edge Subset Enumeration (O(2^E) time, O(E) space)
The most direct idea is to try every possible subset of edges and compute the resulting sum. For each subset, iterate through the selected edges, add their contribution, and verify that the subset satisfies the graph constraints. This approach guarantees the optimal answer because every combination is evaluated. The downside is exponential growth: with E edges there are 2^E possible subsets, which becomes infeasible even for moderately sized graphs.
Approach 2: Greedy Gain Selection with Graph Insight (O(E log E) time, O(E) space)
A more practical strategy is to evaluate the gain each edge contributes. Compute the potential value contributed by each edge and store it alongside the endpoints in the adjacency structure of the graph. Sort edges by their gain and iteratively select the most beneficial ones. While selecting edges, maintain any structural constraints such as endpoint usage or parity conditions using simple counters or adjacency tracking. Sorting drives the complexity to O(E log E), and the greedy choice works because selecting higher-gain edges earlier never blocks a better global configuration.
Approach 3: Mathematical Greedy Optimization (O(E) time, O(V) space)
The optimal solution avoids sorting by observing a mathematical pattern in edge gains. Instead of choosing edges directly, compute the improvement each edge provides compared to leaving it unused. Accumulate all positive improvements and track the smallest adjustment required to fix constraint violations such as parity or endpoint limits. A single pass through the edges builds the result, while node participation is tracked with simple arrays derived from the adjacency list of the graph. This relies on a greedy decision backed by a small math correction step.
Recommended for interviews: Interviewers typically expect the greedy optimization. Start by describing the brute force to show you understand the search space, then explain how analyzing edge gain eliminates the need to check every subset. The final solution runs in linear or near‑linear time and demonstrates strong reasoning about greedy choices and graph structure.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Edge Subsets | O(2^E) | O(E) | Only for very small graphs or to reason about correctness |
| Greedy with Sorted Edge Gains | O(E log E) | O(E) | General approach when evaluating edge benefit and selecting the best edges |
| Mathematical Greedy Optimization | O(E) | O(V) | Optimal solution when edge gains can be aggregated with a simple correction step |