+ All Categories
Home > Documents > [do haskins - student.cs.uwaterloo.ca

[do haskins - student.cs.uwaterloo.ca

Date post: 13-Mar-2022
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
10
A. Lubiw, U. Waterloo Tutorial 1 CS 341 F21 TAXONOMY This course mainly covers: Serial, deterministic, exact Why deterministic? Implications: - we want absolute guarantees. - w e want t o learn about analysis (and this is harderfor randomized) [do not use haskins - do not use maps. Tutorial1 1 of 12
Transcript

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

TAXONOMY•••

•••

•••

This course mainly covers:Serial, deterministic, exact

Why deterministic?

Implications:

→•

- w e wan t absoluteguarantees.- w e w a n t t o l e a r n about analysis( a n d t h i s i s h a rd e r f o r randomized)

[do not u s e haskins- d o no t u s e maps.

Tutorial1 1 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

word-RAM model: for input of n integers, assume a word is large enough to address n locations, thus each word has log n bits. So the input integers can have O(log n) bits, but bigger integers must be split into multiple words.

••••

•Unlikely… split large

integers into arrays of words. Watch cost of

operations!But, large enough to hold an address of an object in a data structure? Yes! If the data structure fits in RAM…

bit model: count bits and bit operations.

Examples:a. given a permutation of 1. . n (e.g. to sort)b. given a number (e.g. to test if it’s prime)c. given a list of n natural numbers s1, s2, . . . sn (e.g. to sort)

-

→ •

Tutorial1 2 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

Examples:a. given a permutation of 1. . n (e.g. to sort)b. given a number (e.g. to test if it’s prime)c. given a list of n natural numbers s1, s2, . . . sn (e.g. to sort)

Which model should we use and what is the input size?

- o r n numbers i n range 1 . . c - n

( a ) u s e word R AM - e a c h input number f i t s i n o n e w o r d .sorting 0Canloga )b i tmodel: input s i z e 0 h loga )

sorting

!

b eg) comparisons × g . a )p e r comparison.

(b ) u s e b i t model.o r u s e wordRAM-a l l ows only constant word s i z e

- s o s a m e a s b i t mode l .

( c ) u s e word7A M b u t pay attention t o s i z e so f numbers.

Tutorial1 3 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

Why worst case?

But how do we know how much time 3 will take

on input 4?

We don’t know how much time

an individual stepin the program

takes!

CS 341 F19 Lecture 2

- 10 -

O -

-

N-

O ( N ) ← T m n ← TM( I )

wa n t absoluteguarantee.why 0 C . ) for@f) ) - t o ignore whichmach ine

- t o p a y a t t e n t i o nt o b i gp i c t u re .Tutorial1 4 of 12

Tricky Quest ion2 Algs. w i t h r u n t i m e s :

0 6 2 ) 0Canlogin)

fw h i c h i s better? - w e can't say.

could be ( n )

I f 0 - t h e n @ (align) better t h a n Ocr)

Tutorial1 5 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

big O

is like ≤is like <is like =

For numbers, we have a total order.

Is big O a total order?

:)

Fo r any × ,y either k e y← y× > y

No. r e s i n t o buildoscillating fµ v e r s u s

"

Tutorial1 6 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

big O

is like ≤is like <is like =

For numbers, we have a total order.

Is big O a total order? No.

Challenge Exercise: find functions f, g, such that neither is big O of the other, but use only +, x, log, exponentiation. Don’t use ceiling, floor, sin, etc.

Tutorial1 7 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

Try to analyze this yourself!One possible solution is given in these slides…

Analyzing a program with branching

-

Tutorial1 8 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

Question 1. Suppose you want to store an array and Add elements to it. When the array is full, create a new array of twice the size and copy the current array into it.

Suppose the array has n elements in it and we call Add. How much time will Add take in the worst case?

Suppose we start with an array of size 1 and Add n elements. How much time does this take in the worst case?

a-adf.ge/AddetgfAaddddFIa-AddeTgelf

t r e e

D-G ) i f w e need t o double

0 c m

SEE z i = Offs") = 0 ( n ) .

Tutorial1 9 of 12

A. Lubiw, U. WaterlooTutorial 1CS 341 F21

Reductions

Question 2. Given n points in the plane, pi = (xi,yi) sort by x then by y.

Suppose you have a program, Sort, that sorts a list of n numbers in O(n log n). How can you sort n points in the plane in O(n log n)?

1. suppose you can modify/generalize the Sort program2. suppose you cannot. Give a reduction — you may call Sort more than once3. Give a many-one (“one-shot”) reduction — you may only call Sort once.

Y : 3 • 4y . - 2 @ Ig .He • 3 orderY:L ⑥ 2

× : e X--2 × : 3

-

-

1 . replace < f o r n umbe r s by comparing pointsi f × , a 2 4 t h e n p , < p ,i f 7C, 7 I z - r p , 7 P 2i f × , = 7 4

. - -

2 . Need stables o r t . s o f t by y , then-bye.3 . challenge- conve r t h i , y : ) t o a singlenumber

Tutorial1 10 of 12


Recommended