CSC 282 - PowerPoint PPT Presentation

About This Presentation
Title:

CSC 282

Description:

CSC 282 Algorithms – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 42
Provided by: csRoch
Category:
Tags: csc | quizzes

less

Transcript and Presenter's Notes

Title: CSC 282


1
CSC 282 Algorithms
Daniel Stefankovic CSB 620 stefanko_at_cs.rochest
er.edu TA Girts Folkmanis CSB 614
gfolkman_at_cs.rochester.edu www.cs.rochester.edu/
stefanko/Teaching/06CS282
2
Grading formula
25 - homework 30 - quizzes 25 - midterm
(Tu, Oct. 24) 30 - final (Th, Dec.
21)
3
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

4
What is an algorithm?
algorithm problem-solving procedure
Algoritmi de numero Indorum (Al-Khwarizmi
Concerning the Hindu Art of Reckoning)
CORRECTNESS EFFICIENCY
5
Problem is n a prime?
PRIMALITY INSTANCE a natural number n QUESTION
is n a prime?
Is 12345678987654321 a prime?
6
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
Is 12345678987654321 a prime?
7
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 vn do 2 if
k divides n then RETURN composite 3 RETURN
prime
8
Problem is n a prime?
Algorithm 2 1 for k from 2 to vn do 2 if
k divides n then RETURN composite 3 RETURN
prime
CORRECT?
9
Problem is n a prime?
RSA cryptosystem needs primes with 1024-4096
bits.
Running time of our algorithms Algorithm 1
21024 24096 Algorithm 2
10
Problem is n a prime?
RSA cryptosystem needs primes with 1024-4096
bits.
Running time of our algorithms Algorithm 1
21024 24096 Algorithm 2 2512 22048
NOT EFFICIENT
11
What means efficient?
efficient program using other efficient as
subroutines is efficient
running time is bounded by a polynomial in the
input size
12
Input size
How many bits needed to represent n?
13
Input size
How many bits needed to represent n?
log n
Algorithm 2 1 for k from 2 to vn do 2 if
k divides n then RETURN composite 3 RETURN
prime
vn ? polynomial(log n) ???
14
What means efficient?
running time is bounded by a polynomial in the
input size
More refined classification asymptotic
notation
15
Asymptotic notation
DEF Let f,g N ? R. We say
f(n) O(g(n)) if (? C) (? n) f(n) ? C . g(n)
16
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)
17
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)
18
Asymptotic notation
1 n! O(3n) 2 n 1 O(n) 3 2n1
O(2n) 4 (n1)! O(n!) 5 1cc2cn
O(cn) 6 1cc2cn O(1)
n! n.(n-1).(n-2) 3.2.1
19
Asymptotic notation
1 n! ? O(3n) 2 n1 O(n) 3 2n1
O(2n) 4 (n1)! ? O(n!) 5 1cc2cn
O(cn) for cgt1 6 1cc2cn O(1)
for clt1
20
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))
21
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
the running time (as a function of input-size)
ideally we would like f(n) such that
T(n) ?(f(n))
22
An applied problem
INSTANCE n points in the plane SOLUTION a tour
connecting the points MEASURE the length of the
tour
23
An applied problem
INSTANCE n points in the plane SOLUTION a tour
connecting the points MEASURE the length of the
tour
24
An applied problem
INSTANCE n points in the plane SOLUTION a tour
connecting the points MEASURE the length of the
tour
25
An efficient algorithm?
26
A correct algorithm
  • best ? (1 2 3 n)
  • for each permutation ?
  • if cost(?)ltcost(best) then
  • best??

EFFICIENCY?
27
Sorting
SORTING INSTANCE a sequence of n numbers
a1,a2, , an SOLUTION
reordering bi of the input such that b1? b2?
? bn
28
Insertion sort
i ? 1 while i ? n do j ? i while j ? 2 and
a j-1 gt a j do swap a j ,a j-1
j ? j - 1 i ? i 1
29
Insertion sort correctness?
i ? 1 while i ? n do j ? i while j ? 2 and
a j-1 gt a j do swap a j ,a j-1
j ? j - 1 i ? i 1
30
Insertion sort running time?
31
Insertion sort running time?
The worst-case running time of insertion sort is
?(n2).
32
Merge 2 sorted lists
MERGE INSTANCE 2 lists xi, yi such that
x1 ? x2 ? ? xn y1 ?
y2 ? ? ym SOLUTION ordered merge
33
Merge 2 sorted lists
1 i ? 1, j ? 1 2 while i ? n and j ? n do 3
if xi ? yj then 4 output xi, i ? i 15
else 6 output yj, j ? j 1 7 output
remaining elements
34
Mergesort
MERGE-SORT(a,l,r) if l lt r then m ? ?
(lr)/2 ? MERGE-SORT(a,I,m)
MERGE-SORT(a,m1,r) MERGE(a,l,m,r)
35
Mergesort
Running time?
36
Mergesort
Running time?
... n
n/2
n/2
n/4
n/4
n/4
n/4
Depth ?
37
Mergesort
Running time?
... n
n/2
n/2
n/4
n/4
n/4
n/4
Depth log n
38
Mergesort
Time spent on merge?
... n
n/2
n/2
n/4
n/4
n/4
n/4
Depth log n
39
Mergesort
Time spent on merge?
O(n)
O(n)
O(n)
Depth log n
O(n.logn)
40
Mergesort
recurrence T(n) T(n/2) ?(n) if ngt1
T(1) ?(1)
41
RAM model
r0
r1
Program
r2
r3
r4
r5
.
  • Each register holds an integer
  • Operations
  • simple arithmetic
  • if-then, goto, etc.

.
.
memory
Write a Comment
User Comments (0)
About PowerShow.com