Transcript Document

CSCE350 Algorithms and Data
Structure
Lecture 4
Jianjun Hu
Department of Computer Science and
Engineerintg
University of South Carolina
2009.9.
Outline
 Review of last lecture
 Properties of Asymptotic classes
 Asymptotic classes of time/space complexity
 How to analyze the Time Efficiency of non-recursive
algorithms
Last Lecture: Theoretical Analysis of
Time Efficiency
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size
Basic operation: the operation that contributes most towards the
running time of the algorithm.
input size
T(n) ≈ copC(n)
running time
execution time
for basic operation
Number of times
basic operation is
executed
Asymptotic Growth Rate
A way of comparing functions that ignores constant factors and
small input sizes
O(g(n)): class of functions f(n) that grow no faster than g(n)
Θ (g(n)): class of functions f(n) that grow at same rate as g(n)
Ω(g(n)): class of functions f(n) that grow at least as fast as g(n)
A Useful Property
If t1 (n)  O( g1 (n)) andt2 (n)  O( g2 (n))
 t1 (n)  t2 (n)  max{O( g1 (n)),O( g2 (n))}
How about the growth order of
n5
n 2  4n  5
n3  n2
n  n log n
n 2  n log n
Basic Asymptotic Efficiency Classes
1
constant
log n
logarithmic
n
linear
n log n
n log n
n2
quadratic
n3
cubic
2n
exponential
n!
factorial
You Should Know How to
sort the different asymptotical efficiency functions, such as
Exercise 2.3.5.
( n  2)! , 5 log( n  100 )10 , 2 2 n ,
0.001n 4  3n 3  1, ln 2 n, 3 n , 3n
A useful formula: Stirling’s formula
n
n!  2n  
e
n
Analyze the Time Efficiency of An
Algorithm
Nonrecursive Algorithm
ALGO RITHMFactorial(n )
f 1
Recursive Algorithm
for i  1 to n do
f  f *i
re turn f
ALGO RITHMFactorial(n )
if n  0
re turn1
e lse
re turnFactorial(n  1)* n
Matrix Multipliacation
Cworst (n)  Cbest (n)  Caverage (n)  (n3 )
Time efficiency of Nonrecursive
Algorithms
Steps in mathematical analysis of nonrecursive algorithms:
 Decide on parameter n indicating input size
 Identify algorithm’s basic operation
 Determine worst, average, and best case for input of size n
 Set up summation for C(n) reflecting algorithm’s loop structure
 Simplify summation using standard formulas (see Appendix A)
Useful Formulas in Appendix A
Make sure to be familiar with them
n
n
 ca c a
i
i 1
n
 (a
i 1
i
i 1
i
n
n
i 1
i 1
 bi )   ai   bi
n( n  1)
2
i

1

2

...

n



(
n
)

2
i 1
Prove by Induction
n
n
k
k 1
i


(
n
) ...

i 1
Element Uniqueness
Check whether all the elements in a given array are distinct
 Input: An array A[0…n-1]
 Output: Return “true” if all the elements in A are distinct and
“false” otherwise
ALGO RITHMUniqueElements( A[0..n  1])
for i  0 to n-2 do
for j  i  1 to n-1 do
if A[i ]  A[ j ] re turnfalse
re turntrue
n  2 n 1
Cworst (n)  (n 2 )
 1
i 0 j i 1
Selection sort
Cworst (n)  Cbest (n)  Caverage (n)  (n )
2
Insertion Sort
Cworst (n)  (n ), Cbest (n)  (n), Caverage (n)  ?
2
Insert Sorting—running time cases
 The best case input is an array that is already sorted. In this case
insertion sort has a linear running time (i.e., O(n)). During each
iteration, the first remaining element of the input is only
compared with the right-most element of the sorted subsection of
the array.
 The worst case input is an array sorted in reverse order. In this
case every iteration of the inner loop will scan and shift the entire
sorted subsection of the array before inserting the next element.
For this case insertion sort has a quadratic running time (i.e.,
O(n2)).
 The average case is also quadratic, which makes insertion sort
impractical for sorting large arrays. However, insertion sort is one
of the fastest algorithms for sorting arrays containing fewer than
ten elements.
Improvement of Insert sorting
 Shell sort: two simple variants requiring O(n3/2) and O(n4/3)
running time.
 binary insertion sort
 In 2004 Bender, Farach-Colton, and Mosteiro published a
new variant of insertion sort called library sort or gapped
insertion sort high probability in O(n log n) time
Example: Find the Number of Binary
Digits
Find the Number of Binary Digits in the Binary Representation
of a Positive Decimal Integer
ALGO RITHMBinary( n )
// In pu t: A positivede cim alin te ge rn
// O u tpu t: Th e n u m be rof bin arydigits
//
i n n' s bin aryre pre se n ta
tion
count  1
wh ilen  1 do
count  count  1
n  n/ 2
re tu rncount
How to identify inefficiency and speed
it up?
What is it doing?
Time complexity?
How to speed it up?
How to identify inefficiency and speed
it up?
2.4.7 Matrix multiplication
For i0 to n-1 do
n 1 n 1 n 1
for j 0 to n-1 do
C[i,j]0
i 0 j 0 k 0
for k0 to n-1 do
C[i,j] C[i,j]+A[i,k]*B[k,j]
1
n3    n3  n2