Download presentation

Presentation is loading. Please wait.

Published byGerard Lawson Modified about 1 year ago

1
CSC 282 – Algorithms Daniel Stefankovic – CSB 620 TA: Girts Folkmanis – CSB 614

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? P RIMALITY : INSTANCE: a natural number n QUESTION: is n a prime? Is a prime?

6
Problem: is n a prime? Is a prime? Algorithm 1: 1 for k from 2 to n-1 do 2 if k divides n then RETURN “composite” 3 RETURN “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 √ n do 2 if k divides n then RETURN “composite” 3 RETURN “prime”

8
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”

9
Problem: is n a prime? RSA cryptosystem needs primes with bits. Running time of our algorithms: Algorithm 1: – Algorithm 2:

10
Problem: is n a prime? RSA cryptosystem needs primes with bits. Running time of our algorithms: Algorithm 1: – Algorithm 2: – NOT EFFICIENT

11
What means efficient? running time is bounded by a polynomial in the input size “efficient program using other efficient as subroutines is efficient”

12
Input size How many bits needed to represent n?

13
Input size How 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

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 n 2 + n 3 = O(n 4 ) 2 n 2 / log(n) = O(n. log n) 3 5n + log(n) = O(n) 4 n log n = O(n 100 ) 5 3 n = O(2 n. n 100 )

17
Asymptotic notation 1 n 2 + n 3 = O(n 4 ) 2 n 2 / log(n) O(n. log n) 3 5n + log(n) = O(n) 4 n log n O(n 100 ) 5 3 n O(2 n. n 100 )

18
Asymptotic notation 1 n! = O(3 n ) 2 n +1 = O(n) 3 2 n+1 = O(2 n ) 4 (n+1)! = O(n!) 5 1+c+c 2 +…+c n = O(c n ) 6 1+c+c 2 +…+c n = O(1) n!= n.(n-1).(n-2) … 3.2.1

19
Asymptotic notation 1 n! O(3 n ) 2 n+1 = O(n) 3 2 n+1 = O(2 n ) 4 (n+1)! O(n!) 5 1+c+c 2 +…+c n = O(c n ) for c>1 6 1+c+c 2 +…+c n = O(1) for c<1

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(n k ) 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( )

27
Sorting S ORTING : INSTANCE: a sequence of n numbers a 1,a 2, …, a n SOLUTION: reordering b i of the input such that b 1 b 2 … b n

28
Insertion sort i 1 while 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

29
Insertion sort – correctness? i 1 while 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

30
Insertion sort – running time?

31
The worst-case running time of insertion sort is (n 2 ).

32
Merge 2 sorted lists M ERGE INSTANCE: 2 lists x i, y i such that x 1 x 2 … x n y 1 y 2 … y m SOLUTION: ordered merge

33
1 i 1, j 1 2 while i n and j n do 3 if x i y j then 4 output x i, i i else 6 output y j, j j output remaining elements Merge 2 sorted lists

34
M ERGE -S ORT (a,l,r) if l < r then m (l+r)/2 M ERGE -S ORT (a,I,m) M ERGE -S ORT (a,m+1,r) M ERGE (a,l,m,r) Mergesort

35
Running time? Mergesort

36
Running time? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth ?

37
Running time? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth = log n

38
Time spent on merge? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth = log n

39
Time spent on merge? Mergesort [ … n/2 … ] [... n … ] [ … n/4 … ] Depth = log n O(n) O(n.logn)

40
recurrence T(n)= T(n/2) + (n) if n>1 T(1)= (1) Mergesort

41
RAM model Program r0r0 r1r1 r2r2 r3r3 r4r4 r5r5 memory... Each register holds an integer Operations: simple arithmetic if-then, goto, etc.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google