14.24 Advanced Greedy Techniques
The classical greedy algorithms presented earlier in this chapter are often introduced as isolated ideas: - Interval scheduling - Huffman coding - Fractional knapsack
14.24 Advanced Greedy Techniques
The classical greedy algorithms presented earlier in this chapter are often introduced as isolated ideas:
- Interval scheduling
- Huffman coding
- Fractional knapsack
- Kruskal's algorithm
- Dijkstra's algorithm
As problems become more sophisticated, however, greedy solutions rarely appear in such a pure form. Instead, they combine multiple techniques:
- Sorting plus heaps
- Greedy plus binary search
- Greedy plus graph traversal
- Greedy plus dynamic maintenance
- Greedy plus amortized analysis
This chapter explores several advanced patterns that appear frequently in competitive programming, technical interviews, distributed systems, and production optimization software.
The goal is to move beyond simple greedy selection and understand how greedy reasoning integrates with larger algorithmic frameworks.
Greedy Plus Sorting
The most common advanced pattern combines:
Sort
+
Greedy scan
The sort exposes structure.
The scan exploits it.
We encountered this repeatedly:
| Problem | Sort Key |
|---|---|
| Activity selection | Finish time |
| Course scheduling | Deadline |
| Fractional knapsack | Density |
| Merge intervals | Start time |
| Rescue boats | Weight |
In many practical problems, identifying the correct sort order is the hardest step.
Once the ordering is discovered, the greedy logic becomes straightforward.
Example: Maximum Number of Events
Events:
| Event | Start | End |
|---|---|---|
| A | 1 | 2 |
| B | 2 | 3 |
| C | 3 | 4 |
Goal:
Attend the maximum number of events.
A useful approach:
Sort by start day.
Process days in order.
Use heap of ending days.
This combines sorting and priority queues.
Greedy Plus Heap
Many advanced greedy algorithms involve dynamically changing candidate sets.
Pattern:
Sort input
Add candidates
Choose best candidate
Remove expired candidates
Repeat
Examples:
- Event scheduling
- Refueling stops
- Course scheduling
- Streaming optimization
The heap provides efficient access to the current best option.
Example: Maximum Events Attended
Process days chronologically.
Maintain:
Min-heap of event ending days
At each day:
- Add newly available events.
- Remove expired events.
- Attend the event ending earliest.
The greedy rule:
Use the opportunity that disappears first.
This is a recurring scheduling principle.
Greedy Plus Binary Search
Sometimes the greedy algorithm answers:
Can value X be achieved?
Binary search determines:
Largest feasible X
or
Smallest feasible X
This pattern is extremely common.
Example: Aggressive Cows
Positions:
1 2 4 8 9
Place:
3 cows
Goal:
Maximize minimum distance.
Greedy subproblem:
Can cows be placed
with minimum distance D?
Algorithm:
Place first cow.
Repeatedly place next cow
at earliest valid position.
If placement succeeds:
D is feasible.
Binary search over:
D
finds the optimum.
Complexity
Binary search:
$$ O(\log M) $$
Greedy check:
$$ O(n) $$
Total:
$$ O(n \log M) $$
where:
M
is the search range.
Greedy Plus Graph Traversal
Some graph algorithms appear greedy but also perform traversal.
Examples:
- Prim
- Dijkstra
- A*
- Best-first search
These algorithms maintain:
Frontier
and repeatedly select:
Most promising vertex
The greedy choice determines expansion order.
Example: Dijkstra
Maintain:
Tentative distances
Greedy rule:
Choose smallest distance.
Traversal updates neighboring vertices.
This combination of greedy selection and graph exploration is one of the most important algorithmic patterns.
Greedy Plus Union-Find
Kruskal demonstrates another advanced combination.
Components:
Sorting
+
Greedy edge selection
+
Disjoint sets
The sort identifies candidate edges.
Union-find determines whether the edge remains safe.
Without union-find:
Cycle detection becomes expensive.
The combination produces:
$$ O(E \log E) $$
performance.
Greedy Plus Sweep Line
Many geometric and interval problems use:
Event sorting
+
Greedy processing
The algorithm scans through a timeline.
Events enter and leave an active set.
The greedy decision depends on the current active state.
Example: Platform Allocation
Events:
Arrival
Departure
Sort all events.
Maintain:
Current active trains
Maximum active count gives the answer.
Although simple, this pattern scales to far more sophisticated geometric algorithms.
Greedy Plus Monotonic Structures
Monotonic stacks and queues provide another advanced design technique.
Instead of keeping all candidates:
Remove candidates
that can never become optimal.
This dramatically reduces complexity.
Example: Remove K Digits
Maintain:
Increasing stack
When a smaller digit appears:
Remove larger digits.
The stack contains only candidates that may still belong to the optimal solution.
This is greedy pruning.
Example: Largest Rectangle
Maintain:
Increasing heights
When a lower height appears:
Resolve taller bars.
Again, the structure stores only useful candidates.
Greedy Plus Amortized Analysis
Some greedy algorithms appear expensive.
Example:
For each element:
While condition:
pop stack
This resembles:
$$ O(n^2) $$
analysis.
But every element:
Push once
Pop once
Therefore:
$$ O(n) $$
total.
Amortized analysis is often essential for understanding greedy performance.
Online Greedy Algorithms
Many classical algorithms assume complete knowledge of the input.
Real systems often do not.
Input arrives gradually.
Decisions must be made immediately.
This creates online algorithms.
Example: CPU Scheduling
Jobs arrive over time.
The scheduler cannot see future arrivals.
Greedy policies include:
- Shortest remaining time
- Earliest deadline
- Highest priority
The algorithm operates with incomplete information.
Despite that limitation, greedy policies often perform remarkably well.
Competitive Analysis
Online algorithms are frequently evaluated through:
Competitive ratio
Definition:
$$ \frac{\text{Online cost}} {\text{Optimal offline cost}} $$
The optimal offline algorithm knows the future.
Competitive analysis measures the penalty of uncertainty.
Example: Ski Rental Problem
Choices:
Rent daily
Buy permanently
Future usage duration is unknown.
A greedy strategy:
Rent until rental cost
equals purchase cost.
Achieves a competitive ratio of:
$$ 2 $$
This is one of the classical results in online algorithms.
Greedy Approximation Algorithms
Some problems are NP-hard.
Optimal greedy algorithms do not exist.
However, greedy approximations may provide provable guarantees.
Example: Set Cover
Universe:
U
Collection of subsets:
S1, S2, ..., Sn
Goal:
Cover all elements.
Greedy rule:
Choose subset covering
most uncovered elements.
This is not optimal.
However, it achieves a logarithmic approximation guarantee.
This extends greedy thinking into optimization theory.
Example: Vertex Cover
Greedy rule:
Choose both endpoints
of an uncovered edge.
Result:
2-approximation
Again:
- Not exact.
- Provably close.
Greedy reasoning remains useful even without optimality.
Multi-Criteria Greedy
Some practical problems optimize several objectives simultaneously.
Examples:
- Cost
- Latency
- Reliability
- Resource usage
The challenge is defining:
Best candidate
Common approaches:
Weighted Score
$$ score = a \cdot cost + b \cdot latency + c \cdot reliability $$
Lexicographic Ordering
Optimize:
- Reliability
- Cost
- Latency
in sequence.
Constraint-Based Optimization
Maximize:
Profit
subject to:
Budget
Latency
Capacity
These approaches appear frequently in real-world systems.
Recognizing Advanced Greedy Problems
Several clues suggest an advanced greedy solution.
Dynamic Candidate Set
Use:
Heap
Feasibility Threshold
Use:
Binary search + greedy
Connectivity Constraints
Use:
Union-find
Ordered Events
Use:
Sweep line
Dominated Candidates
Use:
Monotonic stack
Streaming Input
Use:
Online greedy
Recognizing these combinations is often more important than recognizing the greedy rule itself.
Common Mistakes
Treating Every Optimization Problem as Pure Greedy
Many advanced problems require auxiliary techniques.
Ignoring Dynamic State
Candidate sets often change continuously.
Missing Feasibility Checks
Greedy selection may require validation through binary search or data structures.
Using Expensive Updates
Without the correct structure:
Heap
Union-find
Stack
performance can degrade dramatically.
Assuming Local Success Implies Global Success
Advanced greedy algorithms still require proofs.
The surrounding technique does not eliminate the need for correctness arguments.
A Design Framework
When facing a new problem:
Step 1
Identify the greedy choice.
Step 2
Determine whether candidates are:
Static
or
Dynamic
Step 3
Choose supporting structure:
Heap
Stack
Queue
Tree
Union-find
Step 4
Look for:
Binary search
Graph traversal
Sweep line
integration opportunities.
Step 5
Prove correctness.
Step 6
Analyze complexity.
This workflow captures many modern greedy solutions.
Takeaway
Advanced greedy algorithms rarely rely on a single idea. They often combine greedy reasoning with sorting, heaps, binary search, graph traversal, union-find structures, sweep-line processing, amortized analysis, online decision-making, or approximation techniques. The greedy component still provides the local decision rule, but supporting structures and auxiliary algorithms make that rule practical and scalable. Understanding these combinations allows you to recognize and solve a much broader class of optimization problems than classical textbook examples alone.