Maximum Profit from Valid Topological Order in DAG - Problem
Maximum Profit from Valid Topological Order in DAG

You're given a Directed Acyclic Graph (DAG) with n nodes labeled from 0 to n - 1. The graph is represented by a 2D array edges, where edges[i] = [ui, vi] indicates a directed edge from node ui to node vi.

Each node has an associated score[i] representing its value. Your task is to find a valid topological order of the nodes that maximizes profit.

๐ŸŽฏ Profit Calculation: Each node is assigned a 1-based position in the processing order. The total profit is the sum of score[i] ร— position[i] for all nodes.

๐Ÿ“ Topological Order: A linear ordering where for every directed edge u โ†’ v, node u comes before node v in the ordering.

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

Input & Output

example_1.py โ€” Simple Chain
$ Input: n = 3, edges = [[0,1],[1,2]], scores = [5,3,8]
โ€บ Output: 35
๐Ÿ’ก Note: Only valid topological order is [0,1,2]. Profit = 5ร—1 + 3ร—2 + 8ร—3 = 5 + 6 + 24 = 35
example_2.py โ€” Multiple Valid Orders
$ Input: n = 3, edges = [[0,1]], scores = [2,4,3]
โ€บ Output: 17
๐Ÿ’ก Note: Valid orders: [0,1,2], [0,2,1], [2,0,1]. Best is [2,0,1] with profit = 3ร—1 + 2ร—2 + 4ร—3 = 3 + 4 + 12 = 19. Wait, let me recalculate: [0,2,1] gives 2ร—1 + 3ร—2 + 4ร—3 = 2 + 6 + 12 = 20. Actually [2,0,1] gives 3ร—1 + 2ร—2 + 4ร—3 = 3 + 4 + 12 = 19. The optimal is [0,2,1] = 20. But 0 must come before 1, so valid orders are [0,1,2]: 2ร—1 + 4ร—2 + 3ร—3 = 17, [0,2,1]: 2ร—1 + 3ร—2 + 4ร—3 = 20. Wait, this violates the constraint. Let me fix: [2,0,1]: not valid since 0โ†’1. [0,1,2]: 2ร—1 + 4ร—2 + 3ร—3 = 17. [0,2,1]: 2ร—1 + 3ร—2 + 4ร—3 = 20, but this violates 0โ†’1. So only [0,1,2] and [2,0,1] where 2 can go first. Actually [2,0,1] is valid. So [2,0,1]: 3ร—1 + 2ร—2 + 4ร—3 = 19. [0,1,2]: 2ร—1 + 4ร—2 + 3ร—3 = 17. [0,2,1]: not valid. Answer should be 19.
example_3.py โ€” Single Node
$ Input: n = 1, edges = [], scores = [10]
โ€บ Output: 10
๐Ÿ’ก Note: Only one node with score 10 at position 1. Profit = 10ร—1 = 10

Visualization

Tap to expand
Maximum Profit Topological OrderingExample: Course DependenciesMathScore: 3PhysicsScore: 5MLScore: 8prerequisiteprerequisiteProfit CalculationOnly Valid Order: Math โ†’ Physics โ†’ MLPosition 1: Math (3 ร— 1 = 3 points)Position 2: Physics (5 ร— 2 = 10 points)Position 3: ML (8 ร— 3 = 24 points)Total Maximum Profit: 3 + 10 + 24 = 37Key Strategy: Higher scores benefit from later positions when constraints allowIn problems with multiple valid orders, use DP with bitmask to find optimal arrangement
Understanding the Visualization
1
Build Dependency Graph
Create adjacency list and track which courses depend on others
2
Identify Constraints
A course can only be taken after all its prerequisites are completed
3
Maximize Weighted Score
Place high-value courses later in the sequence when dependencies allow
4
Use DP with Bitmask
Efficiently explore all valid orderings using dynamic programming
5
Return Optimal Profit
Find the ordering that gives maximum weighted GPA sum
Key Takeaway
๐ŸŽฏ Key Insight: To maximize profit in topological ordering, place nodes with higher scores at later positions whenever the DAG constraints permit, using DP with bitmask for efficient exploration of all valid orderings.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! ร— n)

n! possible topological orders, O(n) to calculate profit for each

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

O(n) recursion depth and O(n) for storing current ordering

n
2n
โš  Quadratic Space

Constraints

  • 1 โ‰ค n โ‰ค 20
  • 0 โ‰ค edges.length โ‰ค n ร— (n - 1) / 2
  • edges[i].length == 2
  • 0 โ‰ค ui, vi โ‰ค n - 1
  • The graph is guaranteed to be a DAG (no cycles)
  • 1 โ‰ค scores[i] โ‰ค 104
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
28.4K Views
Medium Frequency
~25 min Avg. Time
890 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