Top Banner
SC 282 – Algorithms Daniel Stefankovic – CSB 620 [email protected] TA: Girts Folkmanis – CSB 614 [email protected] www.cs.rochester.edu/~stefanko/Teaching/06CS282
41

CSC 282 – Algorithms Daniel Stefankovic – CSB 620 [email protected] TA: Girts Folkmanis – CSB 614 [email protected] stefanko/Teaching/06CS282.

Dec 16, 2015

Download

Documents

Gerard Lawson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

CSC 282 – Algorithms

Daniel Stefankovic – CSB 620 [email protected]

TA: Girts Folkmanis – CSB 614 [email protected]

www.cs.rochester.edu/~stefanko/Teaching/06CS282

Page 2: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Grading formula

25% - homework30% - quizzes25% - midterm (Tu, Oct. 24)30% - final (Th, Dec. 21)

Page 3: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

25% - homework • turn in before class on the due date • no late homework accepted• two lowest homework scores dropped

30% - quizzes• 1 each week • 10 min• closed book• no make-up quizzes• two lowest quiz grades dropped

Page 4: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

What is an algorithm?

algorithm = problem-solving procedure

Algoritmi de numero Indorum (Al-Khwarizmi Concerning the Hindu Art of Reckoning)

CORRECTNESSEFFICIENCY

Page 5: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Problem: is n a prime?

PRIMALITY:INSTANCE: a natural number nQUESTION: is n a prime?

Is 12345678987654321 a prime?

Page 6: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Problem: is n a prime?

Is 12345678987654321 a prime?

Algorithm 1:1 for k from 2 to n-1 do 2 if k divides n then RETURN “composite”3 RETURN “prime”

Page 7: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Problem: is n a prime? Algorithm 1:1 for k from 2 to n-1 do 2 if k divides n then RETURN “composite”3 RETURN “prime”

Algorithm 2:1 for k from 2 to √n do 2 if k divides n then RETURN “composite”3 RETURN “prime”

Page 8: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Problem: is n a prime?

CORRECT?

Algorithm 2:1 for k from 2 to √n do 2 if k divides n then RETURN “composite”3 RETURN “prime”

Page 9: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Problem: is n a prime?

RSA cryptosystem needs primeswith 1024-4096 bits.

Running time of our algorithms: Algorithm 1: 21024 – 24096

Algorithm 2:

Page 10: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Problem: is n a prime?

RSA cryptosystem needs primeswith 1024-4096 bits.

Running time of our algorithms: Algorithm 1: 21024 – 24096

Algorithm 2: 2512 – 22048

NOT EFFICIENT

Page 11: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

What means efficient?

running time is bounded by a polynomial in the input size

“efficient program using other efficient as subroutines is efficient”

Page 12: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Input sizeHow many bits needed to represent n?

Page 13: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Input sizeHow many bits needed to represent n?

√n polynomial(log n) ???

Algorithm 2:1 for k from 2 to √n do 2 if k divides n then RETURN “composite”3 RETURN “prime”

log n

Page 14: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

What means efficient?

running time is bounded by a polynomial in the input size

More refined classification asymptotic notation

Page 15: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Asymptotic notation

DEF: Let f,g: N R+. We say

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

if ( C) ( n) f(n) C . g(n)

Page 16: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Asymptotic notation DEF: Let f,g: N R+. We say f(n) = O(g(n)) if ( C) ( n) f(n) C . g(n)

1 n2 + n3 = O(n4)2 n2 / log(n) = O(n . log n)3 5n + log(n) = O(n)4 nlog n = O(n100) 5 3n = O(2n . n100)

Page 17: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Asymptotic notation

1 n2 + n3 = O(n4)2 n2 / log(n) O(n . log n)3 5n + log(n) = O(n)4 nlog n O(n100) 5 3n O(2n . n100)

Page 18: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Asymptotic notation

1 n! = O(3n)2 n +1 = O(n)3 2n+1 = O(2n)4 (n+1)! = O(n!)5 1+c+c2+…+cn = O(cn)6 1+c+c2+…+cn = O(1)

n!= n.(n-1).(n-2) … 3.2.1

Page 19: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Asymptotic notation

1 n! O(3n)2 n+1 = O(n)3 2n+1 = O(2n)4 (n+1)! O(n!)5 1+c+c2+…+cn = O(cn) for c>16 1+c+c2+…+cn = O(1) for c<1

Page 20: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Asymptotic notation DEF: Let f,g: N R+. We say f(n) = (g(n)) if f(n)=O(g(n)) and g(n)=O(f(n))

Page 21: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

What means efficient? polynomial-time = running time is bounded by a polynomial in the input size, i.e., ( k) T(n) = O(nk)

More refined analysis = asymptotics for therunning time (as a function of input-size)

ideally we would like f(n) such that T(n) = (f(n))

Page 22: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

An applied problem

INSTANCE: n points in the planeSOLUTION: a tour connecting the pointsMEASURE: the length of the tour

Page 23: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

An applied problem

INSTANCE: n points in the planeSOLUTION: a tour connecting the pointsMEASURE: the length of the tour

Page 24: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

An applied problem

INSTANCE: n points in the planeSOLUTION: a tour connecting the pointsMEASURE: the length of the tour

Page 25: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

An efficient algorithm?

Page 26: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

A correct algorithm

best (1 2 3 … n)for each permutation if cost()<cost(best) then best

EFFICIENCY?

Page 27: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

SortingSORTING:INSTANCE: a sequence of n numbers a1,a2, … , an

SOLUTION: reordering bi of the input such that b1 b2 … bn

Page 28: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Insertion sorti 1while i n do j i while j 2 and a[ j-1] > a[ j ] do swap a[ j ],a[ j-1] j j - 1 i i + 1

Page 29: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Insertion sort – correctness?

i 1while i n do j i while j 2 and a[ j-1] > a[ j ] do swap a[ j ],a[ j-1] j j - 1 i i + 1

Page 30: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Insertion sort – running time?

Page 31: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Insertion sort – running time?

The worst-case running time ofinsertion sort is (n2).

Page 32: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Merge 2 sorted listsMERGE

INSTANCE: 2 lists xi, yi such that x1 x2 … xn

y1 y2 … ym SOLUTION: ordered merge

Page 33: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

1 i 1, j 12 while i n and j n do 3 if xi yj then 4 output xi, i i + 15 else 6 output yj, j j + 17 output remaining elements

Merge 2 sorted lists

Page 34: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

MERGE-SORT(a,l,r) if l < r then m (l+r)/2 MERGE-SORT(a,I,m) MERGE-SORT(a,m+1,r) MERGE(a,l,m,r)

Mergesort

Page 35: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Running time?

Mergesort

Page 36: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Running time?

Mergesort

[ … n/2 … ] [ … n/2 … ]

[ ... n … ]

[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]

Depth ?

Page 37: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Running time?

Mergesort

[ … n/2 … ] [ … n/2 … ]

[ ... n … ]

[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]

Depth = log n

Page 38: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Time spent on merge?

Mergesort

[ … n/2 … ] [ … n/2 … ]

[ ... n … ]

[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]

Depth = log n

Page 39: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

Time spent on merge?

Mergesort

[ … n/2 … ] [ … n/2 … ]

[ ... n … ]

[ … n/4 … ] [ … n/4 … ] [ … n/4 … ] [ … n/4 … ]

Depth = log n

O(n)

O(n)

O(n)

O(n.logn)

Page 40: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

recurrence

T(n)= T(n/2) + (n) if n>1 T(1)= (1)

Mergesort

Page 41: CSC 282 – Algorithms Daniel Stefankovic – CSB 620 stefanko@cs.rochester.edu TA: Girts Folkmanis – CSB 614 gfolkman@cs.rochester.edu stefanko/Teaching/06CS282.

RAM model

Program

r0

r1

r2

r3

r4

r5

memory

.

.

.• Each register holds an integer• Operations: simple arithmetic if-then, goto, etc.