Attribution Non-Commercial (BY-NC)

19 views

Attribution Non-Commercial (BY-NC)

- Big-O Algorithm Complexity Cheat Sheet
- 01. Introduction to Datastructure 2
- Algorithms in Bioinformatics
- 1.2 AsymptoticNotation2
- Appendix F - Sorting- A Deeper Look
- Stack Data Structure
- Assembler Practicas
- BigO
- Pset1 Solutions
- Growth
- A Greedy Branch-And-Bound Inclusion-Exclusion Algorithm for Calculating the Exact Multi-State Network Reliability
- Inter-Universal Teichmuller Theory III
- DS_UNIT_I_R16.pdf
- 10
- Lecture8 Dan Simon
- Recurrences Slides
- sol4.pdf
- Programming excercise
- Combination of beliefs on hybrid DSm models
- Calculus if variations

You are on page 1of 40

Huma Zafar

Caelyn McAulay, Alejandro Salinger

http://www.student.cs.uwaterloo.ca/˜cs136/

Lectures: 5 sections, each Tuesday and Thursday

Textbooks: “How to Design Programs” (HtDP) by Felleisen, Flatt,

Findler, Krishnamurthi (http://www.htdp.org) and “C

Programming: A Modern Approach” (CP:AMA) by K. N. King.

final exam + bonus

both be greater than 50%

Where to begin?

An Idea

Type (ADT)

Implementation

Space

(Racket, C, ...)

Complexity

An Idea

A collection of items such that:

add remove

• We can add an item to the collection

• We keep

track of the order in which items were added 4

18

• We can see

if there is at least one item in the collection

Abstract Data Types (ADTs)

This idea can be precisely described as an Abstract Data Type

(ADT)

• A set of operations that can be performed on the data

This specification does not depend on any specific programming

language

ADT Stack

Our idea is called a Stack

[a1 , a2 , . . . , an ] such that n ≥ 0

We will allow the following five operations:

• Add an item to the top of a stack (push)

• Remove an item from the top of a stack (pop)

• Look at the item at the top of a stack (peek)

• Check if the stack is empty (stack-empty?)

However, these descriptions are not precise

Specification of Operations

For each ADT operation, we must specify:

• Preconditions: What must be true before performing this

operation?

what will be true after the operation has been performed?

satisfied?

Specification of Stack Operations

make-stack

• Parameters: None

• Pre: True (i.e. there are no preconditions)

• Post: The empty stack S = [] is produced

push

• Pre: True

• Post: The stack S 0 = [x, a1 , . . . , an ] is produced

Implementation of an ADT

Implementation: Code to make each operation work as intended

• How data is organized and stored within the language

Additional software engineering considerations:

• Controlling access to those details

Implementation of ADT Stack

;; stack1.ss

#lang racket

(provide make-stack push pop peek stack-empty?)

(define (push item S) (cons item S))

(define pop rest)

(define peek first)

(define stack-empty? empty?)

Using this Implementation

;; stack-test.ss

#lang racket

(require "stack1.ss")

What can go wrong here?

The implementation in stack1.ss works

only be added, removed, or accessed at the top of the stack:

A Better Implementation

;; stack2.ss

#lang racket

(provide make-stack push pop peek stack-empty?)

;; ’hide’ the data using a structure

(define-struct stk (lst))

(define (make-stack) (make-stk empty))

(define (push v s) (make-stk (cons v (stk-lst s))))

(define (pop s) (make-stk (rest (stk-lst s))))

(define (peek s) (first (stk-lst s)))

(define (stack-empty? s) (empty? (stk-lst s)))

Introduction to Algorithm Analysis

(define (len lst)

(cond

[(empty? lst) 0]

[else (+1 (len (rest lst)))]))

Let T (n) be the running time of len when applied to a list of length

n (measured as the number of substitution steps required)

A condensed trace

Consider the case where n =4

(len ’(p q r s))

⇒ . . . ;; simplification of cond

⇒ (+ 1 (len ’(q r s)))

⇒ . . . ;; T(3) steps

⇒ (+ 1 3)

⇒4

steps

We can give the value of T (n) as a recurrence relation

a n=0

T (n) =

b + T (n − 1) n > 0

Solution: T (n) = a + bn

Another Example

(define (sum-of-divisors n)

(foldr + 0

(filter (lambda (y) (zero? (remainder n y)))

(build-list n (lambda (x) (add1 x))))))

A quicker analysis:

Big-oh Notation

A convenient way to classify the “size” of a function. Common

classifications include:

• O(1) (constant)

• O(log n) (logarithmic)

• O(n) (linear)

• O(n log n) (loglinear)

• O(n2 ) (quadratic)

• O(2n ) (exponential)

Informally, use the leading term and ignore coefficients

sum-of-divisors has running time

T (n) = an + bn + cn = (a + b + c)n ∈ O(n)

O(n) is called linear running time

Formal Definition of Big-oh

f (n) is O(g(n)) if there exist positive constants c and

Definition:

n0 such that f (n) ≤ c · g(n) for all n ≥ n0

“c · g(n) eventually dominates f (n)”

Example: f (n) = 3n2 + 2 and g(n) = n2

2

c g(n) = 5n

f(n) = 3n2 + 2

n0 = 1 n

Big-oh Proofs

• 3n3 + 4n2 − 3n + 10 is O(n3 )

• 3n3 + 4n2 − 3n + 10 is O(n9 )

• 2n2 + 1 is not O(n)

• If f (n) ∈ O(g(n)) and g(n) ∈ O(h(n)) then

f (n) ∈ O(h(n))

Back to Algorithm Analysis

From now on, we will use big-oh notation to describe the running

times of functions (don’t worry about ”exact” running times)

O(1) (Constant running time): cons, first, rest, empty?, and, or, not,

numeric compuations on bounded numbers, operations on

structures, operations on symbols

comparisons

Addition Rule: If function A takes O(f (n)) time and function B

takes O(g(n)) time, then executing A followed by B takes

O(f (n) + g(n)) time.

For example, build-list, filter, and foldr each take O(n) time

O(n + n + n) = O(3n) = O(n) time

Multiplication Rule: If function A takes O(f (n)) time, then

executing A O(g(n)) times takes O(f (n) · g(n)) time in total.

(cond

[(empty? lst) false]

[(equal? (first lst) target) true]

[else (reverse-search (reverse (rest lst)) target)]))

Best Case vs. Worst Case

But what if target is the first item in lst?

Best Case: For an input of size n, what contents of the input will

give the function its quickest running time?

• reverse-search has a best case running time of O(1) when (first

lst) equals target

It is incorrect to say that “the best case running time for a function is

O(1) when n=0” (running times must hold for all n)

Worst Case: For an input of size n, what contents of the input will

give the function its slowest running time?

• reverse-search has a worst case running time of O(n2 ) when

target is not in lst

Some functions have the same best and worst case running times;

here the contents of the input do not matter

The Tabular Method of Analysis

;; The Insertion Sort algorithm

;; isort: (listof num) → (listof num)

(define (isort alon)

(cond

[(empty? alon) empty]

[else (insert (first alon)

(isort (rest alon)))]))

;; insert: num (listof num) → (listof num)

(define (insert n alon)

(cond

[(empty? alon) (cons n empty)]

[(< n (first alon)) (cons n alon)]

[else (cons (first alon)

(insert n (rest alon)))])))

Example with Local

;; Finding the maximum of a list

;; list-max: (listof num) → num

(define (list-max alon)

(cond

[(empty? (rest alon)) (first alon)]

[else

(local [(define max-rest (list-max (rest alon)))]

(cond

[(> (first alon) max-rest) (first alon)]

[else max-rest]))]))

Example using Recursion Depth

;; Compute the binary representation of a natural number

;; binary-digit-list: nat → (listof nat[≥1])

(define (binary-digit-list n)

(cond

[(zero? n) empty]

[(even? n) (cons 0 (binary-digit-list (quotient n 2)))]

[else (cons 1 (binary-digit-list (quotient n 2))]))

True or False?

• Analysis of an algorithm’s running time depends on the

computer used to run the algorithm

• If Algorithm A has O(n) running time and Algorithm B has

O(n2 ) running time, then A will always perform faster than B

• f (n) = loga n is O(logb n) where a and b are two different

positive constants

occurs when n = 0

Order Notation

An alternative definition of Big-O (from a document on the Handouts

web page) is:

there exist constants c and n0 such that for all n ≥ n0 ,

f (n) ≤ c · g(n).

The growth rates of sample members of these sets is shown on the

next slide.

18000

16000 O(log2 n)

O(n)

O(n log2 n)

14000

O(n2)

O(n3)

12000

10000

8000

6000

4000

2000

0

50

1

0

10

15

20

25

30

35

40

45

50

55

60

65

CS 136 Winter 2011 01: Introduction to CS 136 35

Does it really work?

Jon Bentley describes an experiment in Programming Pearls, p. 75.

The problem is to take a list of N real numbers and return the

maximum sum found in any contiguous sublist. For example:

O(n2 ), O(n lg n), and O(n). To prove that constant factors don’t

matter much, he deliberately tried to make the constant factors of

the O(n3 ) and O(n) algorithms differ by as much as possible.

O(n) on a TRS-80 hobbyist O(n3 ) on a Cray supercom-

computer puter

O(n3 ) algorithm: Cray-1, finely-tuned Fortran, 3.0n3 nanoseconds

O(n) algorithm: TRS-80, interpreted Basic, 19.5n milliseconds =

19, 500, 000n nanoseconds

n O(n) O(n3 )

10 .2 secs .000003 secs

100

1,000

10,000

100,000

1,000,000

O(n3 ) algorithm: Cray-1, finely-tuned Fortran, 3.0n3 nanoseconds

O(n) algorithm: TRS-80, interpreted Basic, 19.5n milliseconds =

19, 500, 000n nanoseconds

n O(n) O(n3 )

10 .2 secs .000003 secs

1,000 20 secs

10,000

100,000

1,000,000

Goals of this module

You should understand the idea of an ADT, the examples given, and

how we can provide several implementations of an ADT. We will be

using ADTs in lecture and on assignments.

Scheme module.

in describing the running time of functions.

assignments and to provide analyses of your own code.

- Big-O Algorithm Complexity Cheat SheetUploaded byNilesh
- 01. Introduction to Datastructure 2Uploaded byfaisal
- Algorithms in BioinformaticsUploaded byAlbateh
- 1.2 AsymptoticNotation2Uploaded bySrikar Chintala
- Appendix F - Sorting- A Deeper LookUploaded byZoltan Loboda
- Stack Data StructureUploaded byTech_MX
- Assembler PracticasUploaded byscorpio19
- BigOUploaded byTôm Sú
- Pset1 SolutionsUploaded byKartik Dutta
- GrowthUploaded byjitensoft
- A Greedy Branch-And-Bound Inclusion-Exclusion Algorithm for Calculating the Exact Multi-State Network ReliabilityUploaded byAmir Javan
- Inter-Universal Teichmuller Theory IIIUploaded byseanbyram
- DS_UNIT_I_R16.pdfUploaded bykoti093
- 10Uploaded byMukul Suryawanshi
- Lecture8 Dan SimonUploaded by11111
- Recurrences SlidesUploaded byPranjal Katlana
- sol4.pdfUploaded byAashish D
- Programming excerciseUploaded byGaurav Arora
- Combination of beliefs on hybrid DSm modelsUploaded byAnonymous 0U9j6BLllB
- Calculus if variationsUploaded byDevraj Mandal
- DynamicRodCutting.pdfUploaded byFahimAbrar
- Max Sherman, Bogo-sort is Sort of SlowUploaded byJavier Llaca
- 3.2 Performance EvaluationsUploaded byChetan Solanki
- DS BITSUploaded byRaghunath Rampur
- next.docxUploaded bybhatiaharryjassi
- 23. Format. Hum - A Deteriorating Inventory Model With Linear, Exponential Demand With ShortageUploaded byImpact Journals
- Cost Sheet DumpUploaded byHasan Shadab
- stackcppUploaded bySikander Shamsi Sheikh
- DescriptionUploaded bynorthcat
- 04.03 Imprecise Task Schedule Optimization [I].pdfUploaded byxiblack

- Coursang5.PDFUploaded byAbel Ganz
- Electronic Cam Motion Generation With Special ReferenceUploaded byMillah Hanifah
- Divide and Conquer-3Uploaded bymdhuq1
- parallel.docxUploaded byashi
- 1307.0411v2Uploaded byivi
- Characterizing Quantum Supremacy in Near-Term DevicesUploaded byYash
- Mipt 2014 Burunduk1.EnUploaded byVishal Golcha
- Number Theoretic AlgorithmsUploaded byNetaji Sagaram
- design analysis of algorithmUploaded byParinyas Singh
- Algorithms TutorialsUploaded byanubhavjain28dec
- Analysis of a Local Search Heuristic for Facility Location Problem - Madhukar R.korupolu, C.greg Plaxton and Rajmohan RajaramanUploaded byVuong Bui Viet
- A Modified Method for Order Reduction of Large Scale Discrete SystemsUploaded byEditor IJACSA
- Lec05 Brute ForceUploaded byAldi
- Introduction to Algorithms and Problem SolvingUploaded byNoman Ali
- Bcs-042 Www.ignouassignmentguru.comUploaded byRoniy Roy
- (eBook) - Java - Genetic AlgorithmUploaded byvikasmgamit
- assign1-hints.pdfUploaded byKpsmurugesan Kpsm
- Algorithm Design by Jon Kleinberg and Eva Tardos, Tsinghua University Press (2005)Uploaded bySups Cooŀ
- AlgorithmsUploaded byVincent Tan
- is2004.pdfUploaded byRajan Chittoor
- Notes+ +All+LecturesUploaded byCory Ocker
- Project Problems for UndergraduatesUploaded bySnehal
- Algorithms BasicsUploaded bymamcapiral25
- 1409.0252Uploaded byMichael Burgess
- Rectangle Bin PackUploaded byjose5000@latinmail.com
- MCS-031Uploaded byBhargav Hathi
- CS502 Full MaterialUploaded bySami Signs
- BinderUploaded bylev76
- Lab13 Sorting (2)Uploaded bys_gamal15
- Rao Lecture 17Uploaded bySakura2709