




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Data Structures and Algorithms; Subject: Computer Science and Engineering ; University: University of Nebraska - Lincoln; Term: Unknown 1989;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Design and Analysis of Algorithms - Chapter 2 1
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
Design and Analysis of Algorithms - Chapter 2 2
Design and Analysis of Algorithms - Chapter 2 3
Design and Analysis of Algorithms - Chapter 2 4
Time efficiency is analyzed by determining the number of repetitions of the basic operation as a function of input size
I Basic operation: the operation that contributes most towards the running time of the algorithm.
T ( n ) ≈ copC ( n ) running time (^) execution time for basic operation
Number of times basic operation is executed
input size
Design and Analysis of Algorithms - Chapter 2 5
Problem Input size measure Basic operation
Visiting a vertex or Graph problem #vertices and/or edges traversing an edge
Floating point Compute a n multiplication n
Floating point Dimensions of matrices multiplication Multiply two matrices of floating point numbers
Search for key in list of items n Number of items in list n Key comparison
Design and Analysis of Algorithms - Chapter 2 6
I Select a specific (typical) sample of inputs
I Use physical unit of time (e.g., milliseconds)
I Count actual number of basic operations
I Analyze the empirical data
Design and Analysis of Algorithms - Chapter 2 7
For some algorithms efficiency depends on type of input:
I Worst case: W( n ) – maximum over inputs of size n
I Best case: B( n ) – minimum over inputs of size n
I Average case: A( n ) – “average” over inputs of size n
I Problem: Given a list of n elements and a search key K , find an element equal to K , if any. I Algorithm: Scan the list and compare its successive elements with K until either a matching element is found ( successful search ) of the list is exhausted ( unsuccessful search ) I Worst case
I Best case
I Average case
Design and Analysis of Algorithms - Chapter 2 9
Types of formulas for basic operation count
I Exact formula e.g., C( n ) = n ( n -1)/
I Formula indicating order of growth with specific multiplicative constant e.g., C( n ) ≈ 0.5 n^2
I Formula indicating order of growth with unknown multiplicative constant e.g., C( n ) ≈ cn^2
Design and Analysis of Algorithms - Chapter 2 10
I Most important: Order of growth within a constant multiple as n →∞
I Example:
I See table 2.
Design and Analysis of Algorithms - Chapter 2 11
Design and Analysis of Algorithms - Chapter 2 12
I A way of comparing functions that ignores constant factors and small input sizes
I O( g ( n )): class of functions f ( n ) that grow no faster than g ( n )
I Θ ( g ( n )): class of functions f ( n ) that grow at same rate as g ( n )
I Ω( g ( n )): class of functions f ( n ) that grow at least as fast as g ( n )
see figures 2.1, 2.2, 2.
Design and Analysis of Algorithms - Chapter 2 19
. 5. 5 . 5
5
5
N
−
→∞
Design and Analysis of Algorithms - Chapter 2 20
I f ( n ) is O( g ( n )) if order of growth of f ( n ) ≤ order of growth of g ( n ) (within constant multiple) I There exist positive constant c and non-negative integer n 0 such that
f ( n ) ≤ c g ( n ) for every n ≥ n 0 Examples: I 10 n is O(2 n^2 )
I 5 n +20 is O(10 n )
Design and Analysis of Algorithms - Chapter 2 21
n! factorial
2 n exponential
n^3 cubic
n^2 quadratic
n log n n log n
n linear
log n logarithmic
1 constant
Design and Analysis of Algorithms - Chapter 2 22
Time efficiency of nonrecursive algorithms
Steps in mathematical analysis of nonrecursive algorithms:
I Decide on parameter n indicating input size
I Identify algorithm’s basic operation
I Determine worst , average , and best case for input of size n
I Set up summation for C(n) reflecting algorithm’s loop structure
I Simplify summation using standard formulas (see Appendix A)
Design and Analysis of Algorithms - Chapter 2 23
I Matrix multiplication
I Selection sort
I Insertion sort
I Mystery Algorithm
Design and Analysis of Algorithms - Chapter 2 24
Design and Analysis of Algorithms - Chapter 2 25
Design and Analysis of Algorithms - Chapter 2 26
Design and Analysis of Algorithms - Chapter 2 27
for i := 1 to n - 1 do
max := i ; for j := i + 1 to n do if |A[ j, i ]| > |A[ max, i ]| then max := j ; for k := i to n + 1 do swap A[ i, k ] with A[ max, k ]; for j := i + 1 to n do for k := n + 1 downto i do A[ j, k ] := A[ j, k ] - A[ i, k ] * A[ j, i ] / A[ i, i ] ;
Design and Analysis of Algorithms - Chapter 2 28
Design and Analysis of Algorithms - Chapter 2 29
I Proof by (strong) induction:
Design and Analysis of Algorithms - Chapter 2 30
( 1 ) ( ) ( 1 ) 1 ( 1 )
Nowprovefor the 1 case:
Assumetheoremholdsforall , 1
and ( 1 ) ( ) ( 1 ).
Weknowthat ( 1 ) ( ) ( 1 )
2 4 2 2
1 1 1 1
Basecases: 0 1 0 1
> + − ≥ + = +
≤ ≤
= + −
> + −
= ≥ =
= ≥ =
= ≥ =
TN TN TN F(N) F(N-) F N
N
k k N
FN FN FN
TN TN TN
T( ) F( ).
T() F().
T( ) F( ) ,
Design and Analysis of Algorithms - Chapter 2 37
log (^) X AB = logX A + logX B log (^) X A/B = log (^) X A – log (^) X B log (^) X AB^ = B logX A
Design and Analysis of Algorithms - Chapter 2 38
I Theorem: log (^) A B = log (^) C B / log (^) CA
Design and Analysis of Algorithms - Chapter 2 39
Design and Analysis of Algorithms - Chapter 2 40
I Proof by Gauss when 9 years old (?!):
1
N
i
Design and Analysis of Algorithms - Chapter 2 41
1
N
i
Design and Analysis of Algorithms - Chapter 2 42
I Proof:
N
i
k k
1
1
1 k
2 k
3 k
N k
1
1
1
1 1
|
=
∑ (^) ∫
k
N k N
i
N k k
Design and Analysis of Algorithms - Chapter 2 43
Proof:
=
∑
N N
i
i
... 1 ... 1
( 1 ... )( 1 )
2 1 2 1
2
N
A A A A A A A
A A A A
0
=
∑
N
N
i
Design and Analysis of Algorithms - Chapter 2 44
Proof:
1
=
∑
N N
i
i
A A A A A A A
A A A A N N N
N
2
... ...
( ... )( 1 )
1
=
∑
N N
i
Design and Analysis of Algorithms - Chapter 2 45
i i
i
ik k i
i
nk
imk
i
n
im
ik
i
i i
i
i
i i
i i i
i
n
im
n
im
ax x ax
a a
ca c a
a b a b
c c cn m
∑ ∑
∑ ∑
∑ ∑
∑ ∑ ∑
∑ ∑
=
=
=
= = − +
= =+
= = ( )
1 ( 1 )