
Lectures on Greedy Algorithms and Dynamic Programming. COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski. Overview. Previous lectures: Algorithms based on recursion - call to the same procedure to solve the problem for the smaller-size sub-input(s)
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
COMP 523: Advanced Algorithmic Techniques
Lecturer: Dariusz Kowalski
Greedy Algorithms and Dynamic Programming
Previous lectures:
These lectures:
Greedy Algorithms and Dynamic Programming
Algorithm is greedy if :
Analyzing optimal greedy algorithms by showing that:
Greedy Algorithms and Dynamic Programming
Input: set of intervals on the line, represented by pairs of points (ends of intervals)
Output: the largest set of intervals such that none two of them overlap
Generic greedy solution:
Greedy Algorithms and Dynamic Programming
Select the interval that starts earliest
(but is not overlapping the already chosen intervals)
Underestimated solution!
optimal
algorithm
Greedy Algorithms and Dynamic Programming
Select the shortest interval
(but not overlapping the already chosen intervals)
Underestimated solution!
optimal
algorithm
Greedy Algorithms and Dynamic Programming
Select the interval intersecting the smallest number of remaining intervals
(but still is not overlapping the already chosen intervals)
Underestimated solution!
optimal
algorithm
Greedy Algorithms and Dynamic Programming
Select the interval that ends first
(but still is not overlapping the already chosen intervals)
Hurray! Exact solution!
Greedy Algorithms and Dynamic Programming
Algorithm gives non-overlapping intervals:
obvious, since we always choose an interval which does
not overlap the previously chosen intervals
The solution is exact:
Let:
We show that A must be as large as Opt
Greedy Algorithms and Dynamic Programming
Let A = {A1,…,Ak} and Opt = {B1,…,Bm} be sorted.
By definition of Opt we have km.
Fact: for every ik, Ai finishes not later than Bi.
Proof: by induction.
For i = 1 by definition of the first step of the algorithm.
From i -1 to i : Suppose that Ai-1 finishes not later than Bi-1.
From the definition of a single step of the algorithm, Ai is the first interval that finishes after Ai-1 and does not overlap it.
If Bifinished before Ai then it would overlap some of the previous A1,…, Ai-1 and consequently - by the inductive assumption - it would overlap or end before Bi-1, which would be a contradiction.
Bi-1
Bi
Ai
Ai-1
Greedy Algorithms and Dynamic Programming
Theorem:A is the exact solution.
Proof: we show that k=m.
Suppose to the contrary that k<m.
We already know that Ak finishes not later than Bk.
Hence we could add Bk+1 to A and obtain a bigger solution by the algorithm - a contradiction.
Bk-1
Bk
Bk+1
Ak
Ak-1
algorithm finishes selection
Greedy Algorithms and Dynamic Programming
Efficient implementation:
Time complexity: O(n log n + n) = O(n log n)
Greedy Algorithms and Dynamic Programming
READING:
EXERCISE:
Greedy Algorithms and Dynamic Programming
Greedy Algorithms and Dynamic Programming
Algorithm is greedy if :
Analyzing optimal greedy algorithms by showing that:
Greedy Algorithms and Dynamic Programming
Input: weighted graph G = (V,E)
Output: spanning tree such that the sum of weights is not bigger than the sum of weights of any other spanning tree
Spanning tree: subgraph with
2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
Greedy Algorithms and Dynamic Programming
Properties of spanning trees:
MST cycle property:
2
2
1
1
1
1
cycle
2
2
3
3
Greedy Algorithms and Dynamic Programming
Consider sets of nodes A and V - A
Theorem:
Every MST must contain at least one edge of weight a
from set F
A
A
2
2
1
1
1
1
2
2
3
3
Greedy Algorithms and Dynamic Programming
Let e be the edge in F with the smallest weight - for simplicity
assume that such edge is unique. Suppose to the contrary that
e is not in some MST. Consider one such MST.
Add e to MST - a cycle is obtained, in which e has weight not smaller
than any other weight of edge in this cycle, by the MST cycle property.
Since the two ends of e are in different sets A and V - A,
there is another edge f in the cycle and in F. By definition of e,
such f must have a bigger weight than e, which is a contradiction.
A
A
2
2
1
1
1
1
2
2
3
3
Greedy Algorithms and Dynamic Programming
Kruskal’s algorithm:
otherwise we remove it
Remark: we always have a partial forest
2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
Greedy Algorithms and Dynamic Programming
Prim’s algorithm:
select one having the smallest weight
Remark: we always have a connected partial tree
root
2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
Greedy Algorithms and Dynamic Programming
Follows from the crucial observations:
Kruskal’s algorithm:
Prim’s algorithm:
Greedy Algorithms and Dynamic Programming
There are implementations using
achieving time complexity
O(m log n)
where n is the number of nodes and m is the
number of edges in a given graph G
Greedy Algorithms and Dynamic Programming
READING:
EXERCISES:
Greedy Algorithms and Dynamic Programming
Implementation of Prim’s algorithm using PQ
Greedy Algorithms and Dynamic Programming
Input: weighted graph G = (V,E)
Output: spanning tree such that the sum of weights is not bigger than the sum of weights of any other spanning tree
Spanning tree: subgraph with
2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
Greedy Algorithms and Dynamic Programming
Consider sets of nodes A and V - A
Theorem:
Every MST must contain at least one edge of weight a
from set F
A
A
2
2
1
1
1
1
2
2
3
3
Greedy Algorithms and Dynamic Programming
Prim’s algorithm:
select one which has the smallest weight
Remark: we always have a connected partial tree
root
2
2
2
1
1
1
1
1
1
2
2
2
3
3
3
Greedy Algorithms and Dynamic Programming
Set of n elements, each has its priority value (key)
Operations provided in time O(log n):
Greedy Algorithms and Dynamic Programming
2
Heap: rooted (almost) complete binary tree, each node has its
Required property:
in each subtree the smallest key is always in the root
4
3
7
5
6
2
3
4
7
5
6
Greedy Algorithms and Dynamic Programming
PQ operations:
Additional supporting operation:
Updating the pointer to the rigth-most leaf on the lowest level of the tree, after each operation (take, add, remove)
Greedy Algorithms and Dynamic Programming
Construction:
(which will be defined in the next slide)
Greedy Algorithms and Dynamic Programming
Smallest key element: trivially read from the root
Adding new element:
otherwise stop
Remark: finding the next last leaf may require to search through the path up and then down (exercise)
Greedy Algorithms and Dynamic Programming
Removing element:
swap the elements and continue going up until reaching smaller parent or the root,
or
swap it with the smallest of its children and continue going down until reaching a node with no smaller child or a leaf
Greedy Algorithms and Dynamic Programming
2
2
3
4
3
1
7
5
6
1
7
5
6
4
add 1 at the end
swap 1 and 4
2
3
4
7
5
6
1
2
3
1
7
5
6
4
1
3
2
1
3
2
7
5
6
4
swap 1 and 2
Greedy Algorithms and Dynamic Programming
7
5
6
4
2
6
3
3
4
3
4
6
4
7
5
6
7
5
7
5
remove 2 and
swap 6 and 3
removing 2
swap 2 and last element
2
3
4
7
5
6
6
3
4
7
5
3
6
4
7
5
3
5
4
swap 6 and 5
3
5
4
7
6
Greedy Algorithms and Dynamic Programming
7
6
Greedy Algorithms and Dynamic Programming
Input: graph is given as an adjacency list
Time complexity: O(m log n)
where n is the number of nodes, m is the number of edges
Greedy Algorithms and Dynamic Programming
READING:
EXERCISES:
Greedy Algorithms and Dynamic Programming
Two problems:
Greedy Algorithms and Dynamic Programming
Dynamic Programming (DP):
Similar to:
Greedy Algorithms and Dynamic Programming
(Weighted) Interval scheduling:
Input: set of intervals (with weights) on the line, represented by pairs of points - ends of intervals
Output: the largest (maximum sum of weights) set of intervals such that none two of them overlap
Greedy algorithm doesn’t work for weighted case!
Greedy Algorithms and Dynamic Programming
Greedy algorithm:
Exact solution of unweighted case.
weight 1
weight 3
weight 1
Greedy algorithm gives total weight 2 instead of optimal 3
Greedy Algorithms and Dynamic Programming
p(1)=0
weight 1
p(2)=1
weight 3
p(3)=0
weight 2
weight 1
p(4)=2
Greedy Algorithms and Dynamic Programming
OPT(j) = max{ wj + OPT(p(j)) , OPT(j-1) }
Proof:
If jth interval is in the optimal solution O then the other intervals in O are among intervals 1,…,p(j).
Otherwise search for solution among firstj-1 intervals.
p(1)=0
weight 1
p(2)=1
weight 3
p(3)=0
weight 2
weight 1
p(4)=2
Greedy Algorithms and Dynamic Programming
( intuitively M[j] stores optimal solution OPT(j) )
Algorithm
p(1)=0
weight 1
p(2)=1
weight 3
p(3)=0
weight 2
weight 1
p(4)=2
Greedy Algorithms and Dynamic Programming
Time: O(n log n)
Memory: O(n) - additional array M
p(1)=0
weight 1
p(2)=1
weight 3
p(3)=0
weight 2
weight 1
p(4)=2
Greedy Algorithms and Dynamic Programming
Popular problem from word processing and computational biology
Alignment A:
set of pairs (i1,j1),…,(ik,jk) such that
Greedy Algorithms and Dynamic Programming
Alignment A:
X = c t t t c t c c
| | | | |
Y = t c t t c c
Another largest alignment A:
X = c t t t c t c c
| | | | |
Y = t c t t c c
Greedy Algorithms and Dynamic Programming
Optimal alignment OPT(i,j) for prefixes of X and Y of lengths i and j respectively:
OPT(i,j) = max{ ij + OPT(i-1,j-1) , OPT(i,j-1) , OPT(i-1,j) }
where ij equals 1 if xi = yj, otherwise is equal to -
Proof:
If xi = yj in the optimal solution O then the optimal alignment contains one match (xi , yj) and the optimal solution for prefixes of length i-1 and j-1 respectively.
Otherwise at most one end is matched. It follows that either x1x2…xi-1 is matched only with letters from y1y2…ym or y1y2…yj-1 is matched only with letters from x1x2…xn. Hence the optimal solution is either the same as for OPT(i-1,j) or for OPT(i,j-1).
Greedy Algorithms and Dynamic Programming
Algorithm
max{ ij + M[i-1,j-1] , M[i,j-1] , M[i-1,j] }
Greedy Algorithms and Dynamic Programming
Time: O(nm)
Memory: O(nm)
Greedy Algorithms and Dynamic Programming
Input: matrix M[0..n,0..m] containing OPT values
Algorithm
Greedy Algorithms and Dynamic Programming
Generalization of alignment problem
Greedy Algorithms and Dynamic Programming
Alignment A: (4 gaps of cost each, 1 mismatch of cost ct)
X = c t t t c t c c
| | | ^ |
Y = t c t t c c
Largest alignment A: (4 gaps)
X = c t t t c t c c
| | | | |
Y = t c t t c c
Greedy Algorithms and Dynamic Programming
Optimal alignment OPT(i,j) for prefixes of X and Y of lengths i and j respectively:
OPT(i,j) = min{ ij + OPT(i-1,j-1) , + OPT(i,j-1) , + OPT(i-1,j) }
Proof:
If xiand yj are (mis)matched in the optimal solution O then the optimal alignment contains one (mis)match (xi , yj) of costijand the optimal solution for prefixes of length i-1 and j-1 respectively.
Otherwise at most one end is (mis)matched. It follows that either x1x2…xi-1 is (mis)matched only with letters from y1y2…ym or y1y2…yj-1 is (mis)matched only with letters from x1x2…xn. Hence the optimal solution is either the same as counted for OPT(i-1,j) or for OPT(i,j-1),plus the penalty gap .
Algorithm and complexity remain the same.
Greedy Algorithms and Dynamic Programming
READING:
EXERCISES:
Greedy Algorithms and Dynamic Programming
Greedy Algorithms and Dynamic Programming
Greedy Algorithms and Dynamic Programming