1.0 - Dynamic Programming
- A dynamic programming solution might apply to an optimisation problem with:
- Optimal substructure, e.g. the longest common subsequence problem, and
- Overlapping subproblems
- Dynamic programming solutions solve a problem by
- Solving all sub-problems (once each) and,
- Using those solutions to choose the sub-problem that will give the optimal answer.
2.0 - Greedy Problems
A greedy algorithm may be found for optimisation problems with
- Optimal sub-structure and
- The greedy choice property
Greedy problems solve problems by making a greedy choice (locally optimal choice) and solving (only) the chosen sub-problem
- If applicable, prefer these to dynamic programming problem since we have fewer sub-problems to solve.
2.1 - Greedy Choice Property
- Some optimisation problems with optimal substructure have the greedy-choice property:
- Given a problem, we know which sub-problem will yield an optimal solution without having to calculate the solutions to all of the sub-problems it depends on (meaning that we donβt have to compute all sub-problems)
- To solve a problem, we can make a greedy choice (a locally optimal choice) about which sub-problem to solve, and then just solve that one
- If a problem has the greedy choice property, then locally optimal choices lead to globally optimal solutions
2.2 - Greedy Algorithms
π± Here are some of the Greedy Algorithms that we have already seen:
- Primβs Minimum Spanning Tree Algorithm
- The minimum spanning tree of a weighted graph G, that is a superset of tree T (a connected acyclic sub-graph of G) is either:
- Base case: T if the tree is already spanning
- Recursive case: where is the least weight edge leaving T
- The minimum spanning tree of a weighted graph G, that is a superset of tree T (a connected acyclic sub-graph of G) is either:
- Kruskalβs Minimum Spanning Tree Algorithm
- The minimum spanning tree of a weighted graph G, that is a superset of a forest of trees T (a spanning acyclic sub-graph of G) is either:
- Base case: T if it is already connected
- Recursive case: is the least weight edge connecting any two trees in the forest T
- The minimum spanning tree of a weighted graph G, that is a superset of a forest of trees T (a spanning acyclic sub-graph of G) is either:
- Dijkstraβs Single-Source Shortest Path Algorithm
- The shortest path tree of a weighted graph G from a source vertex s that is a superset of a shortest-path tree from T from s to of the vertices in G is either:
- Base case: , if is empty
- Recursive case: where is the edge connecting vertex to the vertex that is the closest to (has the highest priority in )
- The shortest path tree of a weighted graph G from a source vertex s that is a superset of a shortest-path tree from T from s to of the vertices in G is either:
2.3 - Activity Selection Problem
π± Find the combination (subset) of tasks that maximises the number of activities in a finite amount of time:
-
Given:
- A list of tasks,
- And their start and finishing times and
-
Which subset of tasks maximises the number of activities?
- Each of the tasks are represented as a pair of start and finishing times
-
Greedy Strategy: Always pick the compatible activity (no overlap) that finishes first.
-
We first want to sort the activities on finishing times - for activities, this takes time
-
Accumulate compatible activities in a set,, initialised to contain the fisrt activity
-
Pick the next activity that starts after the latest finish time so far
- The loop is , so the dominant factor is the sorting
Greedy-Activity-Selector(s, f)
n = s.length
A = { a1 } // The activity that finishes first
k = 1
for m = 2 to n
if s[m] >= f[k]
A = A u { am }
k = m
return A
- In this example from above, we choose first as it has the earliest finishing time - this gives the most time for all other tasks - we need to choose tasks that start at earliest
- Then, cannot be chosen since itβs starting time is before βs finishing time.
- Likewise, we cannot choose
- However, we can choose , as itβs starting time is equal to the finishing time of - we now need to choose tasks that start at earliest
- Then, cannot be chosen as itβs starting time is , and it overlaps with .
- Likewise, we cannot choose
- We can choose .
Proof
-
Suppose your algorithm generates the optimal set of tasks
-
Prove that the set doesnβt exist
-
Suppose that the sequences above are sorted by finishing time.
-
Additionally, suppose that and (that is, is the first time the sequences differ.)
-
Letβs argue that we could replace in the sequence by
- Since is compatible with activities and then is compatible with
- Additionally, since the finishing times are compatible, we have that is compatible with
-
By the same argument, we can replace by .
-
If the sequence is longer, then we have a contradiction, as we have already found that a shorter sequence is optimal (and thus this sequence is not optimal)
-
Otherwise, if theyβre of equal length, then they are the same sequence
2.4 - Knapsack
- Consider a set of items, each with a value , and weight .
- What is the maximum value you can fit into a knapsack, holding a maximum total weight of ?
- Problem 1 - Fractional Knapsack
- You may take part amounts (fractions) of items
- Greedy strategy: Take as much as possible of the item that maximises
- This is optimal, and can be solved using the greedy approach.
- Problem 2 - Binary Knapsack
- You must take all or none of each item
- Greedy strategy: Take all of the item that maximises
- This is not optimal
- Problem 1 - Fractional Knapsack
2.4.1 - Fractional Knapsack
- Consider a case where we have three items and a knapsack with 50 units of space.
- From the asribed values and weights, we can compute the value of each item
- Item 1:
- Item 2:
- Item 3:
- The greedy strategy works for the fractional case, in which you start with the item with the most value per weight.
- However, this doesnβt work for the binary case:
- Consider in the binary case using the greedy strategy.
- Weβd first choose Item 1 and then item 2, leaving 20 units of space unoccupied (value of $160)
- However, the optimal strategy is to take item 2 and item 3 (value of $220)
2.4.2 - Binary Knapsack
-
Let there be items, where:
- The value of the item is
- The weight of the item is
-
Define the recurrence:
To be the maximum value of the items, that can fit into a knapsack of size