ADVANCED ALGORITHMS
LECTURE 6: DP, GREEDY ALGORITHMS
1
ADVANCED ALGORITHMS 2 LECTURE 6 ANNOUNCEMENTS Homework 1 due on - - PowerPoint PPT Presentation
1 LECTURE 6: DP, GREEDY ALGORITHMS ADVANCED ALGORITHMS 2 LECTURE 6 ANNOUNCEMENTS Homework 1 due on Monday Sep 10, 11:59 PM Video issues last class Comment on notes JeffEs material 3 LECTURE 6 LAST CLASS
LECTURE 6: DP, GREEDY ALGORITHMS
1
LECTURE 6
ANNOUNCEMENTS
▸ Homework 1 due on Monday Sep 10, 11:59 PM ▸ Video issues last class ▸ Comment on notes — JeffE’s material
2
LECTURE 6
LAST CLASS
3
▸ Dynamic programming
▸
Recursive algorithms — observe that same sub-problems solved many times
▸
Key idea: avoid re-solving by storing answers!
▸
How many “different problem instances” arise?
▸
Examples: subset sum and coin change problems
t
n
s I
LECTURE 6
OVERLAP IN RECURSIVE CALLS
4 A = {1, 2, 3, 5, 7, 11, 14, 15} , S = 20 Use first element Don’t use first element A’ = {2, 3, 5, 7, 11, 14, 15} , S’ = 19 A’’ = {5, 7, 11, 14, 15} , S’ = 17 A’’ = {5, 7, 11, 14, 15} , S’ = 17
Key observation: every sub-problem has a suffix of A as the array, and some target sum in the range [0,…,S]. Thus only n(S+1) sub-problems!
are not
integers
all bet safe
LECTURE 6
TODAY’S PLAN
5
▸ Conclude DP — a couple more examples from a high level ▸ Greedy algorithms
LECTURE 6
RECAP: DYNAMIC PROGRAMMING
6
▸ Design recursive algorithm ▸ Observe that number of distinct sub-problems is “small” ▸ Store answers! (memorize) ▸ Top-down vs. Bottom-up ▸ Challenge in using DP: formalizing sub-problems more
in Hwa
LECTURE 6
POPULAR E.G.: LONGEST INCREASING SUB-SEQUENCE
7
▸ Natural recursive algorithm?
Problem: given a sequence of numbers A[0], A[1], …, A[n-1], find longest “sub-sequence” whose elements are increasing
A
107
AfD
A147 Alto
3g
2
10
7y
Split into
cases based on whether thefirst
element is
in the sub sequence
recursive
call on remaining list
procedure
M's Afo
All
Aln B
find target
increasing sub seg
that includes
Afo
Answer to original problem miax
Mls
Afi
Afia
Aln D
all
4
3
Mls
411,2 3
GI
write recursion for
Mls
1,2 3
3
Mis l
2,3
Mls
Ato
Atl
Aln B
AH find
MI
Ali Alief
AlnB
f
m
Correctness
increasing
AT
Take
anynsubseg
that includes
A
fo
Let it be Afo Afif Afif
Ali
a We know
that Afi
Alo
so it will be considered
in
the recursive
procedure
Now
we can argue inductively that
It
Ali HI
rtl
sftaIhifhey
student's
suggestion does Not work
a
I
bsg
Ml
LECTURE 6
#(SUB-PROBLEMS) AND RUN TIME
8
sub problems
n
Running time
n
Moira
defining
MIS
iskey
procedure isn't
correct
should be carefulabout defining the
right sub problem
LECTURE 6
TRAVELING SALESMAN PROBLEM
9
Problem: given undirected graph with edge wts, find a path of least total weight that visits every vertex precisely once and returns to start.
LECTURE 6
TRAVELING SALESMAN PROBLEM
10
▸ Trivial algorithm?
Problem: given undirected graph with edge wts, find a path of least total weight that visits every vertex precisely once and returns to start.
ni
can
p
check all permutations
see
which one has leastcost
Nn al
2n nu
define sub problem verycarefully
LECTURE 6
TRAVELING SALESMAN PROBLEM
11
▸ Trivial algorithm? ▸ Many heuristics for geometric cases
Problem: given undirected graph with edge wts, find a path of least total weight that visits every vertex precisely once and returns to start.
LECTURE 6
RECURSIVE ALGORITHM
12
LECTURE 6
SUB-PROBLEMS, RUNNING TIME
13
LECTURE 6
SUB-PROBLEMS, RUNNING TIME
14
LECTURE 6
CAN WE DO BETTER?
15
▸ Likely not — NP hard problem ▸ We will see that finding “approximately good” solution possible!
LECTURE 6
GREEDY ALGORITHMS
16
▸ Greedy == “myopic” ▸ Algorithm makes sequence of decisions — chooses best at each step,
without taking future into account
Greedy algorithms almost neues work
give insights abouthow to solve
sometimes theyactually
LECTURE 6
EXAMPLE
17
Scheduling: given set of jobs with processing times p1, p2, …, pn, find an
1i
2 4 i
3 C 1
C
I 3 i Cz
7
Cg
10
idea
do jobs with
small
Process
in order of increasing pi
9rdgfnh.fm
LECTURE 6
NATURAL HEURISTIC?
18
LECTURE 6
CORRECTNESS
19
Clem
Processing jobs in this order is the best possible
Contradict
Proof1
Suppose the optimal order
v u
U
couldnothave
it
iz
im
beenopt
suppose we swap
t
i
i similarly pi Epi
LECTURE 6
CORRECTNESS — PROOF 2
20
LECTURE 6
EXAMPLE — MATCHING
21
Matching: suppose we have n children, n gifts and “happiness” values Hij. Assign gifts to children to maximize “total happiness”
C
G
Hy
value ofgiftGj to
Cz 0 Ga
child C
i
i
i
l
i
y
C
n
LECTURE 6
GREEDY ALGORITHM
22
Go throughchildren
Ci Cz
a
C
For each child assign
gift from the nomaining
gifts that has the
most value
Xcx
Greedy does not always
G
X
10
solution
LECTURE 6
CORRECTNESS
23
LECTURE 6
CORRECTNESS
24