ADVANCED ALGORITHMS 2 LECTURE 5 ANNOUNCEMENTS Homework 1 due on - - PowerPoint PPT Presentation

advanced algorithms
SMART_READER_LITE
LIVE PREVIEW

ADVANCED ALGORITHMS 2 LECTURE 5 ANNOUNCEMENTS Homework 1 due on - - PowerPoint PPT Presentation

1 LECTURE 5: DYNAMIC PROGRAMMING ADVANCED ALGORITHMS 2 LECTURE 5 ANNOUNCEMENTS Homework 1 due on Monday Sep 10, 11:59 PM Instructor office hours Thursday -> Friday (1 - 2 pm) Comment on notes JeffEs material


slide-1
SLIDE 1

ADVANCED ALGORITHMS

LECTURE 5: DYNAMIC PROGRAMMING

1

slide-2
SLIDE 2

LECTURE 5

ANNOUNCEMENTS

▸ Homework 1 due on Monday Sep 10, 11:59 PM ▸ Instructor office hours Thursday -> Friday (1 - 2 pm) ▸ Comment on notes — JeffE’s material ▸ Solutions to HW0 on course page ▸ Pecking order problem of HW1

2

vertices

birds

O

O

could be cycles

O

O

slide-3
SLIDE 3

LECTURE 5

LAST CLASS

3

▸ Divide and conquer

Break instance into smaller pieces, solve pieces separately, combine solutions (*)

Merge-sort, multiplying n digit numbers (time << n2)

Median (and k’th smallest element) in O(n) time (no sorting!)

Key idea: approximate median to break array into “nearly equal” pieces

All analyses by writing recursion, solving (tree, plug-and-chug, …)

n 58

1

Master theorem

Notes onhomepage

Akira Bazzitheorem

Jeff E's notes

slide-4
SLIDE 4

LECTURE 5

OVERLAP IN RECURSIVE CALLS

4

▸ Divide & conquer: usually sub-problems don’t interact “much” ▸ What if they do? Can run into wastefulness — solving same sub-problem

many times…

▸ Motivation for dynamic programming

slide-5
SLIDE 5

LECTURE 5

TODAY’S PLAN

5

▸ Continue example — subset sum ▸ Knapsack problem ▸ Traveling salesman

slide-6
SLIDE 6

LECTURE 5

RECAP: SUBSET SUM

6

Problem: given a set of non-negative integers A[0], A[1], …, A[n-1], and an integer S, find if there is a subset of A[i] whose sum is S.

▸ Trivial algorithm: 2n time — check all subsets ▸ Sometimes better: divide into two halves — check all possibilities for

how sum is divided

T(n) = S ⋅ T(n/2) ⟹ T(n) = Slog2 n = nlog2 S

Sn s

L

s I

Alo All f Aln i

It

fl

nitlogg

2

5

30

i 5

7 4 15 3

8

22

1

29

slide-7
SLIDE 7

LECTURE 5

TRY 2

7

▸ See if first element is picked or not

Problem: given a set of non-negative integers A[0], A[1], …, A[n-1], and an integer S, find if there is a subset of A[i] whose sum is S.

time

  • nwhenwehauf.in

Suppose we

can write

S

Afi

1 Afia

tafia

correctnessis

Alo is NOT

Alo is used

immediate

we

usedto write

fw

arepdqkijy.ua.lt

f

E

Seeif

we

can write

seeif S Alo

can be written as

5 using All

Aln i

using

Afi

An i

I

slide-8
SLIDE 8

LECTURE 5

RUNNING TIME

8

c

n

runtime

when

we have m Afit's

1

n

2T

n

l

I

Thi

ThF fin

theomania

In

Fast

2

2

t

t

2 t l

n

n

EE

2

2

almost

as bad as bruteforce

slide-9
SLIDE 9

LECTURE 5

LOOKING MORE CLOSELY — EXAMPLE

9 A[] = {1, 2, 3, 5, 7, 9, 10, 11}; S = 20

TE

t

is

3,5 20

E3 B

b 19

3 5.3

s

20

3,5 18

3

3,51 17

N

L

I

I

sit

17

5,7

s 17

1

startindex _2

Sum needed 17

slide-10
SLIDE 10

LECTURE 5

AVOIDING MULTIPLE SOLVES ON “SAME” INSTANCE

10

▸ Store answers!

How much are we storing?

Running time

Howmany differentsub problems

can

  • ne have 7

cc 2

if

I

any

n

possibilities

possible

  • f distinct instances is

E

n

sa

slide-11
SLIDE 11

Modi

thm

In each recursive step first

check if

we have solved this

instance before

If

so read off

the

answer

If not

we store the

answer after

computation

Answers to computation

can be stored in an n

Sti

array

start index

Sam

needed

slide-12
SLIDE 12

LECTURE 5

IS THIS POLYNOMIAL?

11

subtle point

when

we say poly time algorithm we mean

polynomial

in

INPUT SIZE

go

01

all

an i

S

S

2

  • g att flog aft

flog.amif ilg

Runningtime

n.SE

e udaiistn

Input size an

Cfznz.sn

andsaySE Running time

n I

fan

n 2

slide-13
SLIDE 13

LECTURE 5

“KNAPSACK” / COIN CHANGE

12

Problem: suppose we are given coins of denominations 1c, 5c, 10c, 20c, 25c, 50c. Make change for $1.90 using the fewest # of coins.

▸ Heuristics?

a

206 201C 50

50d 50k

254,104,5

Greedy

Atways

use the largest coin

that youCAN use

E

slide-14
SLIDE 14

LECTURE 5

RECURSIVE ALGORITHM

13

denominations

do d

dn

target sum

S S

  • n't

used

use do once The dotwice

1

eachis a recursive call

s

using

d

idnB

and

7

do using

it

3

tank

and

Hans 2

S 2do

3

i

2tans3

n

slide-15
SLIDE 15

LECTURE 5

RUNNING TIME

14

All

recursive calls

are of the type

S

set of

denominations

I

1

Sum needed

suffix of

do

D Space

needed to store answers

01ns

Time

needed

Ofn

S

Edo

slide-16
SLIDE 16

LECTURE 5

GENERAL IDEA OF DYNAMIC PROGRAMMING

15

▸ Write down recursive algorithm ▸ Observe “common sub-problems” in recursion ▸ Capture sub-problems “succinctly”

slide-17
SLIDE 17

LECTURE 5

TRAVELING SALESMAN PROBLEM

16

Problem: given an undirected graph with edge weights, find a path that visits every vertex precisely once and returns to start.

slide-18
SLIDE 18

LECTURE 5

TRAVELING SALESMAN PROBLEM

17

Problem: given an undirected graph with edge weights, find a path that visits every vertex precisely once and returns to start.

▸ Trivial algorithm?

slide-19
SLIDE 19

LECTURE 5

TRAVELING SALESMAN PROBLEM

18

Problem: given an undirected graph with edge weights, find a path that visits every vertex precisely once and returns to start.

▸ Trivial algorithm? ▸ Many heuristics for geometric cases

slide-20
SLIDE 20

LECTURE 5

RECURSIVE ALGORITHM

19

slide-21
SLIDE 21

LECTURE 5

RECURSIVE ALGORITHM — CORRECTNESS

20

slide-22
SLIDE 22

LECTURE 5

RECURSIVE ALGORITHM — CORRECTNESS

21

slide-23
SLIDE 23

LECTURE 5

SUB-PROBLEMS, RUNNING TIME

22

slide-24
SLIDE 24

LECTURE 5

SUB-PROBLEMS, RUNNING TIME

23

slide-25
SLIDE 25

LECTURE 5

CAN WE DO BETTER?

24

▸ Likely not — NP hard problem ▸ We will see that finding “approximately good” solution possible!