cs 758/858: algorithmsruml/cs758/slides/slides-1.pdf · algorithms today algorithms algorithms...

31
CS 758/858: Algorithms Algorithms This Class Complexity Wheeler Ruml (UNH) Class 1, CS 758 – 1 / 24 Prof. Wheeler Ruml TA William Doyle http://www.cs.unh.edu/~ruml/cs758 4 handouts: course info, schedule, slides, asst 1 2 online handouts: programming tips, formulas

Upload: doandan

Post on 27-Aug-2019

249 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

CS 758/858: Algorithms

Algorithms

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 1 / 24

Prof. Wheeler RumlTA William Doyle

http://www.cs.unh.edu/~ruml/cs758

4 handouts:course info, schedule, slides, asst 1

2 online handouts:programming tips, formulas

Page 2: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Algorithms

Algorithms

Algorithms Today

Definition

Why?

The Word

The Founder

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 2 / 24

Page 3: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Algorithms Today

Algorithms

Algorithms Today

Definition

Why?

The Word

The Founder

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 3 / 24

web: search, caching, crypto networking: load balancing, synchronization, failover machine learning: data mining, recommendation, prediction bioinformatics: alignment, matching, clustering hardware: design, simulation, verification business: allocation, planning, scheduling AI: robotics, games

Page 4: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Definition

Algorithms

Algorithms Today

Definition

Why?

The Word

The Founder

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 4 / 24

Algorithm

precisely defined mechanical steps terminates input and related output

What might we want to know about it?

Page 5: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Why?

Algorithms

Algorithms Today

Definition

Why?

The Word

The Founder

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 5 / 24

Computer scientist 6= programmer

understand program behavior have confidence in results, performance know when optimality is abandoned solve ‘impossible’ problems sets you apart (eg, Amazon.com)

CPUs aren’t getting faster Devices are getting smaller Software is the differentiator ‘Software is eating the world’ — Marc Andreessen, 2011 Everything is computation

Page 6: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

The Word: Abu ‘Abdallah Muh.ammad ibn Musa al-Khwarizmı

Algorithms

Algorithms Today

Definition

Why?

The Word

The Founder

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 6 / 24

780-850 ADBorn in Uzbekistan,

worked in Baghdad.Solution of linear and

quadratic equations.Founder of algebra.Popularized arabic numerals,

decimal positional numbers→ algorism (manipulatingdigits)→ algorithm.

The Compendious Book on

Calculation by Completion

and Balancing, 830.

Page 7: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

The Founder: Donald E. Knuth

Algorithms

Algorithms Today

Definition

Why?

The Word

The Founder

This Class

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 7 / 24

invented algorithm analysisThe Art of Computer

Programming, vol. 1, 1968

developed TEX,literate programming

many famous studentspublished in MAD magazine

Page 8: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

This Class

Algorithms

This Class

Relations

Topics

Course Mechanics

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 8 / 24

Page 9: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Relations

Algorithms

This Class

Relations

Topics

Course Mechanics

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 9 / 24

requires 531/659 (formal thinking), 515 (data structures, C) some intentional overlap! central, thus now required same content, assignments, grading both semesters continuous improvement!

Page 10: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Topics

Algorithms

This Class

Relations

Topics

Course Mechanics

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 10 / 24

‘Greatest Hits’

1. data structures: trees, tries, hashing2. algorithms: divide-and-conquer, dynamic programming,

greedy, graphs3. correctness: invariants4. complexity: time and space5. NP-completeness: reductions

Not including

1. (much) computability2. (many) randomized algorithms3. parallel algorithms4. distributed algorithms5. numerical algorithms, eg: crypto, linear algebra6. geometric algorithms7. on-line or ‘run forever’ algorithms8. fancy analysis

Page 11: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Course Mechanics

Algorithms

This Class

Relations

Topics

Course Mechanics

Complexity

Wheeler Ruml (UNH) Class 1, CS 758 – 11 / 24

names → faces sign up sheet General information

contact, books, C, due dates, collaboration, piazza.com

Schedule

wildcard slot

Expectations

50/4=12.5; 50/3=16.7 last year median 12, mean 13.6

Feedback is always needed and appreciated.

eg, EOLQs. Try coming to my office hours!

Page 12: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Complexity

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 12 / 24

Page 13: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Sorting

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 13 / 24

Bubble Sort Selection Sort Insertion Sort Shell Sort Merge Sort Heap Sort Quick Sort

How to sort one million records?

Page 14: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Sorting

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 13 / 24

Bubble Sort Selection Sort Insertion Sort Shell Sort Merge Sort Heap Sort Quick Sort

How to sort one million records?

How to sort one billion 16-bit integers?

Page 15: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Sorting

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 13 / 24

Bubble Sort Selection Sort Insertion Sort Shell Sort Merge Sort Heap Sort Quick Sort

How to sort one million records?

How to sort one billion 16-bit integers?

How to sort one trillion 4-bit integers?

Page 16: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Counting Sort

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 14 / 24

For n numbers in the range 0 to k:

1. for x from 0 to k

2. count[x] ← 03. for each input number x4. increment count[x]5. for x from 0 to k

6. do count[x] times7. emit x

Page 17: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Counting Sort

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 14 / 24

For n numbers in the range 0 to k:

1. for x from 0 to k

2. count[x] ← 03. for each input number x4. increment count[x]5. for x from 0 to k

6. do count[x] times7. emit x

Correctness?

Complexity?

Page 18: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Correctness

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 15 / 24

property 1: output is in sorted orderproof sketch: output loop increments x, never decrements

Page 19: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Correctness

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 15 / 24

property 1: output is in sorted orderproof sketch: output loop increments x, never decrements

property 2: output contains same numbers as inputinvariant:

Page 20: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Correctness

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 15 / 24

property 1: output is in sorted orderproof sketch: output loop increments x, never decrements

property 2: output contains same numbers as inputinvariant: for each value,

remaining input + sum of counts = totalproof sketch:

Page 21: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Correctness

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 15 / 24

property 1: output is in sorted orderproof sketch: output loop increments x, never decrements

property 2: output contains same numbers as inputinvariant: for each value,

remaining input + sum of counts = totalproof sketch:initialized/established: before line 3maintained: through lines 3–4at termination: no remaining input

each number printed count timestherefore, output has same numbers as input

Page 22: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Counting Sort

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 16 / 24

For n numbers in the range 0 to k:

1. for x from 0 to k

2. count[x] ← 03. for each input number x4. increment count[x]5. for x from 0 to k

6. do count[x] times7. emit x

Correctness? Yes.

Complexity?

Page 23: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Complexity

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 17 / 24

RAM model: no cacheorder of growthworst-case

[ try with previous slide ]

Page 24: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Counting Sort

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 18 / 24

For n numbers in the range 0 to k:

1. for x from 0 to k O(k)2. count[x] ← 03. for each input number x O(n)4. increment count[x]5. for x from 0 to k O(k + n)6. do count[x] times7. emit x

O(k + n+ k + n) = O(2n+ 2k) = O(n+ k) 6= O(n lgn)

Page 25: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Order Notation

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 19 / 24

ignore constant factorsingore ‘start-up costs’upper bound

Page 26: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Order Notation

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 19 / 24

ignore constant factorsingore ‘start-up costs’upper bound

n0

f(n)

c·g(n)

f(n) = O(g(n))

eg, running time is O(n logn)

Page 27: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

O()

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 20 / 24

O(g(n)) = f(n) : there exist positive constants c, n0

such that f(n) ≤ cg(n) for all n ≥ no

We can upper-bound (the tail of) f by scaling up g.

Note non-transitive use of =. Pronounced ‘is’.

Eg:

1. 0.002x2 − 35, 456x+ 280

2. O(n2) vs O(n3)3. O(2n) vs O(3n)4. O(2n) vs O(2n+2) vs O(22n) vs O(nn)

“What is n?”

Page 28: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Examples

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 21 / 24

is n3 = O(n2)0.2x2 − 456x+ 220

10n2 + 5nO(n2) vs O(n3)

Page 29: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

And Friends

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 22 / 24

Upper bound (‘order of’):O(g(n)) = f(n) : there exist positive constants c, n0

such that f(n) ≤ cg(n) for all n ≥ no

Lower bound:Ω(g(n)) = f(n) : there exist positive constants c, n0

such that cg(n) ≤ f(n) for all n ≥ no

Tight bound:Θ(g(n)) = f(n) : there exist positive constants c1, c2, n0

such that c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ no

Page 30: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

Asymptotics

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 23 / 24

Upper bound (’dominated by’):

o(g(n)) = f(n) : limn→∞

f(n)g(n) = 0

Lower bound (’dominates’):

ω(g(n)) = f(n) : limn→∞

f(n)g(n) =∞

Page 31: CS 758/858: Algorithmsruml/cs758/slides/slides-1.pdf · Algorithms Today Algorithms Algorithms Today Definition Why? The Word The Founder This Class Complexity Wheeler Ruml (UNH)

EOLQs

Algorithms

This Class

Complexity

Sorting

Counting Sort

Correctness

Counting Sort

Complexity

Counting Sort

Order Notation

O()

Examples

And Friends

Asymptotics

EOLQs

Wheeler Ruml (UNH) Class 1, CS 758 – 24 / 24

What’s still confusing? What question didn’t you get to ask today? What would you like to hear more about?

Please write down your most pressing question about algorithmsand put it in the box on your way out.Thanks!