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

advanced algorithms
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

ADVANCED ALGORITHMS

LECTURE 6: DP, GREEDY ALGORITHMS

1

slide-2
SLIDE 2

LECTURE 6

ANNOUNCEMENTS

▸ Homework 1 due on Monday Sep 10, 11:59 PM ▸ Video issues last class ▸ Comment on notes — JeffE’s material

2

slide-3
SLIDE 3

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

emojis

slide-4
SLIDE 4

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!

if Afi

are not

integers

If

all bet safe

slide-5
SLIDE 5

LECTURE 6

TODAY’S PLAN

5

▸ Conclude DP — a couple more examples from a high level ▸ Greedy algorithms

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

f

3g

2

10

7y

Split into

cases based on whether thefirst

element is

in the sub sequence

  • r not

recursive

call on remaining list

slide-8
SLIDE 8

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

His t Afi

AH find

MI

Ali Alief

AlnB

  • uput i

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

slide-9
SLIDE 9

Now

we can argue inductively that

It

Mislaid

Ali HI

rtl

r.u.i.by

I

sftaIhifhey

student's

suggestion does Not work

a

I

bsg

Ml

  • InD
slide-10
SLIDE 10

LECTURE 6

#(SUB-PROBLEMS) AND RUN TIME

8

sub problems

n

Running time

n

Moira

defining

MIS

iskey

  • therwise

procedure isn't

correct

should be carefulabout defining the

right sub problem

slide-11
SLIDE 11

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.

slide-12
SLIDE 12

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

F 3,6

  • n

fin

Nn al

D n

Dynamicorithn

2n nu

key

define sub problem verycarefully

slide-13
SLIDE 13

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.

slide-14
SLIDE 14

LECTURE 6

RECURSIVE ALGORITHM

12

slide-15
SLIDE 15

LECTURE 6

SUB-PROBLEMS, RUNNING TIME

13

slide-16
SLIDE 16

LECTURE 6

SUB-PROBLEMS, RUNNING TIME

14

slide-17
SLIDE 17

LECTURE 6

CAN WE DO BETTER?

15

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

slide-18
SLIDE 18

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

  • ften

give insights abouthow to solve

sometimes theyactually

slide-19
SLIDE 19

LECTURE 6

EXAMPLE

17

Scheduling: given set of jobs with processing times p1, p2, …, pn, find an

  • rder of processing so that sum of completion times is minimized

1i

2 4 i

3 C 1

C

I 3 i Cz

7

Cg

10

Intuitive

idea

do jobs with

small

first

Process

in order of increasing pi

9rdgfnh.fm

slide-20
SLIDE 20

LECTURE 6

NATURAL HEURISTIC?

18

slide-21
SLIDE 21

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

  • rder

I

suppose we swap

j

ti

tiz

t

ti tti.tpizmtiztk.TK

i

Pi Epi

i similarly pi Epi

slide-22
SLIDE 22

LECTURE 6

CORRECTNESS — PROOF 2

20

slide-23
SLIDE 23

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

  • c

i

i

i

l

i

y

C

  • x Gn

n

slide-24
SLIDE 24

LECTURE 6

GREEDY ALGORITHM

22

Go throughchildren

Ci Cz

a

C

  • O

For each child assign

gift from the nomaining

gifts that has the

most value

Xcx

Greedy does not always

G

X

10

find best

solution

slide-25
SLIDE 25

LECTURE 6

CORRECTNESS

23

slide-26
SLIDE 26

LECTURE 6

CORRECTNESS

24