Maximum Profit from Valid Topological Order in DAG - Problem

You are given a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1, represented by a 2D array edges, where edges[i] = [u_i, v_i] indicates a directed edge from node u_i to v_i.

Each node has an associated score given in an array score, where score[i] represents the score of node i.

You must process the nodes in a valid topological order. Each node is assigned a 1-based position in the processing order. The profit is calculated by summing up the product of each node's score and its position in the ordering.

Return the maximum possible profit achievable with an optimal topological order.

A topological order of a DAG is a linear ordering of its nodes such that for every directed edge u → v, node u comes before v in the ordering.

Input & Output

Example 1 — Basic Chain
$ Input: edges = [[0,1],[1,2]], score = [5,2,3]
Output: 18
💡 Note: Only valid topological order is [0,1,2]. Profit = 5×1 + 2×2 + 3×3 = 5 + 4 + 9 = 18
Example 2 — Multiple Valid Orders
$ Input: edges = [[0,2]], score = [1,3,2]
Output: 14
💡 Note: Edge 0→2 constrains the ordering. Valid orders are [0,1,2], [1,0,2], and [0,2,1]. Calculating profits: [0,1,2] gives 1×1 + 3×2 + 2×3 = 13, [1,0,2] gives 3×1 + 1×2 + 2×3 = 11, and [0,2,1] gives 1×1 + 2×2 + 3×3 = 14. The optimal order [0,2,1] maximizes profit at 14.
Example 3 — No Edges
$ Input: edges = [], score = [2,1,3]
Output: 14
💡 Note: No edges means no constraints. To maximize profit, place nodes with higher scores in later positions. Optimal order [1,0,2] gives 1×1 + 2×2 + 3×3 = 1 + 4 + 9 = 14

Constraints

  • 1 ≤ score.length ≤ 105
  • 0 ≤ edges.length ≤ min(score.length × (score.length - 1) / 2, 105)
  • edges[i].length == 2
  • 0 ≤ edges[i][0], edges[i][1] ≤ score.length - 1
  • edges[i][0] ≠ edges[i][1]
  • There are no duplicate edges
  • The graph is a valid DAG
  • 1 ≤ score[i] ≤ 108

Visualization

Tap to expand
Maximum Profit from Valid Topological Order INPUT Directed Acyclic Graph (DAG) 0 score=5 1 score=2 2 score=3 edges: [[0,1], [1,2]] score: [5, 2, 3] ALGORITHM STEPS 1 Calculate in-degrees Count incoming edges per node indeg=[0,1,1] 2 Use Max-Heap Prioritize high scores first heap=[(-5,0)] 3 Process in order Pop max score, assign position Pos Node Score Profit 1 0 5 1*5=5 2 2 3 2*3=6 3 1 2 3*2=6 4 Sum all profits Total = 5 + 6 + 6 = 17 Optimal: 5*1+3*2+2*3=17 Best valid: 5+4+9=18 FINAL RESULT Optimal Topological Order Pos 1 Node 0 Pos 2 Node 1 Pos 3 Node 2 Profit Calculation: Node 0: score[0] x 1 = 5 x 1 = 5 Node 1: score[1] x 2 = 2 x 2 = 4 Node 2: score[2] x 3 = 3 x 3 = 9 Maximum Profit: 18 OK - 5 + 4 + 9 = 18 Key Insight: Use a greedy approach with max-heap to prioritize nodes with higher scores for later positions. Process nodes with in-degree 0 first, always picking the one that maximizes profit at current position. Higher position numbers multiply with scores, so delay high-score nodes when dependencies allow. TutorialsPoint - Maximum Profit from Valid Topological Order in DAG | Greedy Approach with Topological Sort
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
12.5K Views
Medium Frequency
~35 min Avg. Time
234 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