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
Each node has an associated
๐ฏ Profit Calculation: Each node is assigned a 1-based position in the processing order. The total profit is the sum of
๐ Topological Order: A linear ordering where for every directed edge
Goal: Return the maximum possible profit achievable with an optimal topological order.
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
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
โ Quadratic Growth
Space Complexity
O(nยฒ)
O(n) recursion depth and O(n) for storing current ordering
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code