CS3000:&Algorithms&&&Data Jonathan&Ullman - - PowerPoint PPT Presentation

cs3000 algorithms data jonathan ullman
SMART_READER_LITE
LIVE PREVIEW

CS3000:&Algorithms&&&Data Jonathan&Ullman - - PowerPoint PPT Presentation

Latex ed.tv web based Overleaf CS3000:&Algorithms&&&Data Jonathan&Ullman Lecture&2:& Divide&and&Conquer:&Mergesort Asymptotic&Analysis Jan&8,&2020


slide-1
SLIDE 1

CS3000:&Algorithms&&&Data Jonathan&Ullman

Lecture&2:&

  • Divide&and&Conquer:&Mergesort
  • Asymptotic&Analysis

Jan&8,&2020

Overleaf

web based

Latex ed.tv

slide-2
SLIDE 2

Divide&and&Conquer&Algorithms

slide-3
SLIDE 3

Divide&and&Conquer&Algorithms

  • Split your&problem&into&smaller&subproblems
  • Recursively&solve&each&subproblem
  • Combine the&solutions&to&the&subprobelms

διαίρει'και'βασίλευε! FPhilip&II&of&Macedon

slide-4
SLIDE 4
  • Examples:
  • Mergesort:&sorting&a&list
  • Binary&Search:&searching&in&a&sorted&list
  • Karatsuba’s&Algorithm:&multiplying&intergers
  • Finding&the&median&of&a&list
  • Fast&Fourier&Transform
  • Key,Tools:
  • Correctness:&proof&by&induction
  • Running&Time&Analysis:&recurrences
  • Asymptotic&Analysis

Divide&and&Conquer&Algorithms

slide-5
SLIDE 5

Sorting

11 3 42 28 17 8 2 15 2 3 8 11 15 17 28 42 Given&a&list&of&! numbers,& put&them&in&ascending&order

"[1]

"[!]

slide-6
SLIDE 6

A&Simple&Algorithm

11 3 42 28 17 8 2 15

slide-7
SLIDE 7

A&Simple&Algorithm:&Insertion&Sort

11 3 42 28 17 8 2 15 2 3 8 11 15 17 28 42 11 3 15 28 17 8 2 42

Repeat ! − 1 times. Find&the& maximum Swap&it&into& place,&repeat

  • n&the&rest

11 3 15 2 17 8 28 42

e

to

r

I

slide-8
SLIDE 8

A&Simple&Algorithm:&Insertion&Sort

11 3 42 28 17 8 2 15 11 3 15 28 17 8 2 42

Find&the& maximum Swap&it&into& place,&repeat

  • n&the&rest

Running,Time:

n

I

n

l n

i 11

2

i

re

f 1 i

i

slide-9
SLIDE 9

Divide&and&Conquer:&Mergesort

11 3 42 28 17 8 2 15

Split

11 3 42 28 17 8 2 15 3 11 28 42 2 8 15 17 2 3 8 11 15 17 28 42

Recursively, Sort Merge Recursively, Sort

slide-10
SLIDE 10
  • Key,Idea:,If&', ) are&sorted&lists&of&length&!,&then&we&can&

merge&them&into&a&sorted&list&* of&length&2! in&time&,!

  • Merging&two&sorted&lists&is&faster&than&sorting&from&scratch

3 11 28 42 2 8 15 17 ' )

Divide&and&Conquer:&Mergesort

*

Assorted

b

t

issued

11 111111111

R assorted

thief Aish

41h1 1111

than all of Lt R

T

2

3

8

11

slide-11
SLIDE 11

Merging

Merge(L,R): Let n ← len(L) + len(R) Let A be an array of length n j ← 1, k ← 1, For i = 1,…,n: If (j > len(L)): // L is empty A[i] ← R[k], k ← k+1 ElseIf (k > len(R)): // R is empty A[i] ← L[j], j ← j+1 ElseIf (L[j] <= R[k]): // L is smallest A[i] ← L[j], j ← j+1 Else: // R is smallest A[i] ← R[k], k ← k+1 Return A

slide-12
SLIDE 12

Merging

MergeSort(A): If (len(A) = 1): Return A // Base Case Let . ← ⌈012(*) 5 ⁄ ⌉ // Split Let L ← A[1:m], R ← A[m+1:n] Let L ← MergeSort(L) // Recurse Let R ← MergeSort(R) Let A ← Merge(L,R) // Merge Return A

Let

n ten A

slide-13
SLIDE 13
  • Claim:,The&algorithm&Mergesort is&correct

Correctness&of&Mergesort

a

Argue that

Merge

works

If

L R

are sorted

lists

then

Merge L R

are sorted

b

Prove

that

Merge or

works

for every lot A

Mergesort A

returns

A

in sorted order

slide-14
SLIDE 14

Stint

For every

n EIN

for every lot A of

size

n

Mergesort

A

works

InductiveHyp

Hln

A

lot A of

siren

MSCA work

Base

Case

H

l is

true

  • bviously

HCM

Inductive Step

Vn Hln Hln 11

am

i

Hht

Let

A be

a listof size

n 11

HIM

R

are

lists of

size

In

f NSCC

is sorted

R

MS R

issorted

IH

A

Merge

L R

is sorted

Part la

slide-15
SLIDE 15

a

We will argue

that

throughout the

execution

  • f

Merge the following

are

all

true

A L R

are sorted

A

c

everything

  • n

LTR

At

the

start

there

are

true

because

At

each step they

remain true

because

slide-16
SLIDE 16

Running&Time&of&Mergesort

MergeSort(A): If (n = 1): Return A Let . ← ⌈8 5 ⁄ ⌉ Let L ← A[1:m] R ← A[m+1:n] Let L ← MergeSort(L) Let R ← MergeSort(R) Let A ← Merge(L,R) Return A

1

n

the running time

  • n

inputs of

size

n

I

as

1

n

2 1

El

en

ITH

C

TEA

Hen

Cn

slide-17
SLIDE 17

Recursion&Trees

9 ! = 2 ⋅ 9 ! 2 ⁄ + ,! 9 1 = ,

en

I

l

F

2x CLE

e

I

I

1nF

1

4xcCE

en

I

im

mn

BBDDC.no

  • gzln7
slide-18
SLIDE 18

Recursion&Trees

9 ! = 2 ⋅ 9 ! 2 ⁄ + ,! 9 1 = ,

! !/2 !/2 !/4 !/4 !/4 !/4

1 1

Level 1 2 logB ! C Work&at&this&Level ,! 2 ⋅ ,! 2 = ,! 4 ⋅ ,! 4 = ,! 2D ⋅ ,! 2D = ,! 2EFGH I ⋅ , = ,!

Input&Size&at&this&Level

slide-19
SLIDE 19
  • Claim:,9 ! = ,! logB 2!

Proof&by&Induction

9 ! = 2 ⋅ 9 ! 2 ⁄ + ,! 9 1 = ,

slide-20
SLIDE 20
  • Sort&a&list&of&! numbers&in&,! logB 2! time
  • Can&actually&sort&anything&that&allows&comparisons
  • No&comparison&based&algorithm&can&be&faster
  • DivideFandFconquer
  • Break&the&list&into&two&halves,&sort&each&one&and&merge
  • Key&Fact:&Merging&is&easier&than&sorting
  • Proof&of&correctness
  • Proof&by&induction
  • Analysis&of&running&time
  • Recurrences

Mergesort&Summary

slide-21
SLIDE 21

Asymptotic&Analysis

slide-22
SLIDE 22
  • We&don’t&have&enough&information&to&precisely&

predict&the&running&time&of&an&algorithm

  • The&running&time&will&depend&on&the&size&of&the&input
  • The&running&time&might&depend&on&the&input&itself
  • Don’t&know&what&machine&will&run&the&algorithm
  • Impractical&to&precisely&count&operations

Analyzing&Running&Time

slide-23
SLIDE 23
  • Do&we&really&need&to&worry&about&this&problem?
  • Mostly&we&want&to&compare&algorithms,&so&we&can&select&

the&right&one&for&the&job

  • Mostly&we&don’t&care&about&small&inputs,&we&care&about&

how&the&algorithm&will&scale

Asymptotic&Order&Of&Growth

slide-24
SLIDE 24
  • Asymptotic,Analysis:,How&does&the&running&time&

grow&as&the&size&of&the&input&grows?

Asymptotic&Order&Of&Growth

slide-25
SLIDE 25
  • “Big@Oh”,Notation: J ! = K L !

if&there&exists& M ∈ 0,∞ Qand&!R ∈ Qℕ such&that&J ! ≤ M ⋅ L ! for&every&! ≥ !R.

  • Asymptotic&version&of&J ! ≤ L !

Asymptotic&Order&Of&Growth

n t I

n

10

50

Xz

slide-26
SLIDE 26
  • “Big@Oh”,Notation: J ! = K L !

if&there&exists& M ∈ 0,∞ Qand&!R ∈ Qℕ such&that&J ! ≤ M ⋅ L ! for&every&! ≥ !R.

  • Asymptotic&version&of&J ! ≤ L !
  • Roughly&equivalent&versions:
  • ∃W, X ≥ 0QQQQ∀! ∈ ℕQQQQQJ ! ≤ W ⋅ L ! + X
  • lim

I→] ^ I _ I < ∞

Asymptotic&Order&Of&Growth

An

3nt 6

gln

n

3h 16

n

zt En

slide-27
SLIDE 27
  • “Big@Oh”,Notation: J ! = K L !

if&there&exists& M ∈ 0,∞ Qand&!R ∈ Qℕ such&that&J ! ≤ M ⋅ L ! for&every&! ≥ !R.

  • Which&of&these&statements&are&true?
  • 3!B + ! = K !B
  • !b = K !B
  • 10!c = K !d
  • logB! = K logB

!

Ask&the&Audience

slide-28
SLIDE 28
  • Constant,factors,can,be,ignored
  • ∀, > 0QQQQ,! = K !
  • Smaller,exponents,are,Big@Oh,of,larger,exponents
  • ∀W < XQQQQ!f = K !g
  • Any,logarithm,is,Big@Oh,of,any,polynomial,
  • ∀W, h > 0QQQ logB

f Q! = K !i

  • Any,polynomial,is,Big@Oh,of,any,exponential
  • ∀QW > 0, X > 1QQQ!f = K XI
  • Lower,order,terms,can,be,dropped
  • !B + !b/B + ! = K !B

BigFOh&Rules

n

login

n

Oln

0cm

slide-29
SLIDE 29
  • The&notation&J ! = K L !

is&weird—do&not& take&it&too&literally

A&Word&of&Caution

slide-30
SLIDE 30
  • “Big@Omega”,Notation: J ! = Ω L !

if&there& exists&M ∈ 0, ∞ Qand&!R ∈ Qℕ s.t.&J ! ≥ M ⋅ L ! for&every&! ≥ !R.

  • Asymptotic&version&of&J ! ≥ L !
  • Roughly&equivalent&to&lim

I→] ^ I _ I > 0

  • “Big@Theta”,Notation: J ! = Ω L !

if&there& exists&Mk ≤ MB ∈ 0,∞ Qand&!R ∈ Qℕ such&that&&&&&& cB ⋅ L ! ≥ J ! ≥ Mk ⋅ L ! for&every&! ≥ !R.

  • Asymptotic&version&of&J ! = L !
  • Roughly&equivalent&to&0 < lim

I→] ^ I _ I < ∞

Asymptotic&Order&Of&Growth

f 0cg Aregs

0cg

slide-31
SLIDE 31
  • We,usually,write,running,time,as,a,Big@Theta
  • Exact&time&per&operation&doesn’t&appear
  • Constant&factors&do&not&appear
  • Lower&order&terms&do&not&appear
  • Examples:
  • 30 logB! + 45 = Θ log!
  • ,! logB2! = Θ ! log!

,C

I Dpk

= Θ !B

  • We,usually,“think,asymptotically”
  • We&don’t&even&discuss&constants&when&they&won’t&affect&

the&asymptotic&running&time

Asymptotic&Running&Times

slide-32
SLIDE 32
  • “Little@Oh”,Notation: J ! = q L !

if&for&every& M > 0 there&exists&!R ∈ Qℕ s.t.&J ! < M ⋅ L ! for& every&! ≥ !R.

  • Asymptotic&version&of&J ! < L !
  • Roughly&equivalent&to&lim

I→] ^ I _ I = 0

  • “Little@Omega”,Notation: J ! = r L !

if&for& every&M > 0 there&exists&!R ∈ Qℕ such&that&&&&&&&&&&&&& J ! > M ⋅ L ! for&every&! ≥ !R.

  • Asymptotic&version&of&J ! > L !
  • Roughly&equivalent&to&lim

I→] ^ I _ I = ∞

Asymptotic&Order&Of&Growth

slide-33
SLIDE 33
  • “Little@Oh”,Notation: J ! = q L !

if&for&every& M > 0 there&exists&!R ∈ Qℕ s.t.&J ! < M ⋅ L ! for& every&! ≥ !R.

  • Which&of&these&statements&are&true?
  • 3!B + ! = q !B
  • !b = q !B
  • 10!c = q !d
  • logB! = q logB

!

Ask&the&Audience

slide-34
SLIDE 34
  • Rank&the&following&functions&in&increasing&order&of&

growth&(i.e.&J

k, J B, J b, J c so&that&J D = K(J Dsk))

  • ! logB!
  • !B
  • 100!
  • 3EFGH I

Ask&the&Audience!

slide-35
SLIDE 35

Why&Asymptotics Matter