Algorithms · Greedy Method and Fractional Knapsack
Optimization Problems and Exhaustive Search
Overview
An optimization problem asks for the maximum or minimum of some property, such as cost, profit, reliability, risk, shortest path, or minimal spanning tree.
Exhaustive search solves such problems by enumerating every possible solution, but it takes exponential time, so better paradigms — the greedy method and dynamic programming — are needed.
Key concepts
- Optimization problem: a problem in which some property is to be maximized or minimized. Examples of such properties: minimize cost, maximize profit, maximize reliability, minimize risk, shortest path in a graph (minimize distance/cost), minimal spanning tree (minimize total edge weight).
- Exhaustive search: find all possible solutions, then pick the one with the least (or greatest) value. It may take exponential time, of order .
- Greedy method: a programming paradigm that solves only a subset of optimization problems — the problem must have certain properties for greedy to apply.
When it applies, the solution is generally polynomial: order of or . - Dynamic programming: a programming paradigm that can solve any optimization problem, but its time may be polynomial (order of ) or exponential (order of ).
How it works
- Take a graph with nodes A, B, C, D, E and a weight on every edge.
- Ask, for example: what is the shortest path from A to C?
- Exhaustive search solves it by finding all possible paths from A to C, then choosing the path with the least value.
- With edges, each edge may or may not be present in a path — two chances per edge — so on the order of paths may have to be examined, and the minimum found among them. This is an order algorithm.
- Enumerating all solutions and then finding the minimum takes a lot of time when the problem size is large, so other methods are wanted: the greedy method and dynamic programming.
- Greedy applies only when the problem has the required properties; dynamic programming applies to all problems, but is worth applying only when it reduces the time to polynomial.
Formulas
- Exhaustive search over paths: with edges, each edge either present or absent, so the number of paths examined is of order ; time complexity .
- Greedy method (when applicable): time complexity generally polynomial, order of or .
- Dynamic programming: time complexity either polynomial, order of , or exponential, order of .
Comparison
| Aspect | Greedy Method | Dynamic Programming | Exhaustive Search |
|---|---|---|---|
| Applicability | Only some optimization problems (problem must have certain properties) | Optimization problems with optimal substructure (and overlapping subproblems) | Any problem (enumerates all solutions) |
| Typical time complexity | Polynomial: order of or | Order of (polynomial) or order of (exponential) | Order of (exponential) |
| When it is useful | When the problem satisfies the required properties | When it reduces the time to polynomial, order of | Baseline; used when nothing better is available |
| When it is not useful | For problems lacking the required properties (may not give the optimal solution) | When it still gives order of — as good as exhaustive search | Always expensive for large problem sizes |
Revision summary
- An optimization problem is one where a property is maximized or minimized.
- Exhaustive search: enumerate all solutions, then take the minimum/maximum; with edges each edge is present or absent, giving order of paths and time.
- The greedy method cannot solve all optimization problems — only a subset having the required properties; given a problem, greedy may not give the optimal solution.
- When greedy applies, the solution is generally polynomial ( or ).
- Dynamic programming can solve optimization problems that have optimal substructure and overlapping subproblems, but its time may be polynomial () or exponential ().
- Apply dynamic programming only when it gives polynomial time; if it still gives , it is as good as exhaustive search.
- The methods are best understood through examples rather than through theory about the properties a problem must contain.