You are on page 1of 5

Algorithm

• An algorithm is a sequence of
unambiguous instructions for solving a
Algorithms, Design and
problem, i.e., for obtaining a required
Analysis output for any legitimate input in a
Introduction. finite amount of time.

Computing Prefix Averages Prefix Averages (Quadratic)


• asymptotic analysis examples: • The following algorithm computes prefix averages in
35
two algorithms for prefix averages X
quadratic time by applying the definition
• The i-th prefix average of an 30
A Algorithm prefixAverages1(X, n)
array X is average of the first (i + 25 Input array X of n integers
1) elements of X:
A[i] = (X[0] + X[1] + … + X[i])/(i+1) 20 Output array A of prefix averages of X
15 1. A ← new array of n integers
• Computing the array A of prefix
2. for i ← 0 to n − 1 do
averages of another array X has 10
applications to financial analysis 3. s ← X[0]
5 4. for j ← 1 to i do
0 5. s ← s + X[j]
1 2 3 4 5 6 7 6. A[i] ← s / (i + 1)
7. return A

Prefix Averages (Linear, non-


recursive) Prefix Averages (Linear)
• The following algorithm computes prefix averages in • The following algorithm computes prefix averages in
linear time by keeping a running sum linear time by computing prefix sums (and averages)
Algorithm prefixAverages2(X, n) Algorithm recPrefixSumAndAverage(X, A, n)
Input array X of n ≥ 1 integer.
Input array X of n integers
Empty array A; A is same size as X.
Output array A of prefix averages of X Output array A[0]…A[n-1] changed to hold prefix averages of X.
A ← new array of n integers returns sum of X[0], X[1],…,X[n-1]
s←0 1. if n=1
for i ← 0 to n − 1 do 2. A[0] ← X[0]
s ← s + X[i] 3. return A[0]
4. tot ← recPrefixSumAndAverage (X,A,n-1)
A[i] ← s / (i + 1) 5. tot ← tot + X[n-1]
return A 6. A[n-1] ← tot / n
7. return tot;

1
Selection sort Insertion sort

Mystery algorithm What is an algorithm?


• Recipe, process, method, technique,
for i := 1 to n - 1 do procedure, routine,… with following
max := i ; requirements:
1. Finiteness
for j := i + 1 to n do b terminates after a finite number of steps
if |A[ j, i ]| > |A[ max, i ]| then max := j ; 2. Definiteness
for k := i to n + 1 do b rigorously and unambiguously specified
swap A[ i, k ] with A[ max, k ]; 3. Input
b valid inputs are clearly specified
for j := i + 1 to n do
for k := n + 1 downto i do 4. Output
b can be proved to produce the correct output given a
A[ j, k ] := A[ j, k ] - A[ i, k ] * A[ j, i ] / A[ i, i ] ; valid input
5. Effectiveness

Pseudocode Pseudocode
• Mixture of English, math Very High-level • Mixture of English, math
expressions, and computer pseudocode: expressions, and computer Detailed pseudocode
code code
• Less detailed than a Algorithm arrayMax(A, n) • Less detailed than a Algorithm arrayMax(A, n)
program Input array A of n integers program Input array A of n integers
• Preferred notation for Output maximum element of A • Preferred notation for Output maximum element of A
describing algorithms currentMax ← A[0] describing algorithms currentMax ← A[0]
• Hides program design Step through each element in A, • Hides program design for i ← 1 to n − 1 do
issues updating currentMax when a issues if A[i] > currentMax then
• Can write at different levels bigger element is found • Can write at different levels currentMax ← A[i]
of detail. return currentMax of detail. return currentMax

2
Theoretical analysis of time
Pseudocode Details efficiency
Time efficiency is analyzed by determining the
• Control flow number of repetitions of the basic operation as a
• Method call
– if … then … [else …] var.method (arg [, arg…]) function of input size
– while … do … • Return value
– repeat … until … return expression
– for … do … • Expressions • Basic operation: the operation that contributes
– Indentation replaces braces ← Assignment most towards the running time of the algorithm
(like = in Java)
• Method declaration
= Equality testing
Algorithm method (arg [, arg …]) (like == in Java) input size
Input … n2 Superscripts and other
Output … mathematical formatting
allowed
T(n) ˜ copC(n)
running time execution time Number of times
for basic operation basic operation is
executed

Input size and basic operation


examples Counting Primitive
Operations (§1.1)
• Worst-case primitive operations count, as a
Problem Input size measure Basic operation
function of the input size
Search for key in list of Number of items in list
Key comparison Algorithm arrayMax(A, n) # operations
n items n
currentMax ← A[0] 2
Multiply two matrices of Floating point for i ← 1 to n − 1 do 1+n
floating point numbers
Dimensions of matrices
multiplication if A[i] > currentMax then 2(n − 1)
currentMax ← A[i] 2(n − 1)
Floating point { increment counter i } 2(n − 1)
Compute a n n
multiplication return currentMax 1
Visiting a vertex or Total 7n − 2
Graph problem #vertices and/or edges
traversing an edge

Counting Primitive Defining Worst [W(n)], Best


Operations (§1.1) [B(N)], and Average [A(n)]
• Best-case primitive operations count, as a • Let In = set of all inputs of size n.
function of the input size • Let t(i) = # of primitive ops by alg on input i.
• W(n) = maximum t(i) taken over all i in In
Algorithm arrayMax(A, n) # operations
currentMax ← A[0] 2 • B(n) = minimum t(i) taken over all i in I n
for i ← 1 to n − 1 do
if A[i] > currentMax then
1+n
2(n − 1)
• A(n) = ∑ p(i )t (i,) p(i) = prob. of i occurring.
i ∈I n
currentMax ← A[i] 0
{ increment counter i } 2(n − 1) • We focus on the worst case
return currentMax 1 – Easier to analyze
– Usually want to know how bad can algorithm be
Total 5n
– average-case requires knowing probability; often
difficult to determine

3
Arithmetic Progression Prefix Averages (Linear)
7 • The following algorithm computes prefix averages in
• The running time of linear time by computing prefix sums (and averages)
prefixAverages1 is 6
O(1 + 2 + …+ n) Algorithm recPrefixSumAndAverage(X, A, n) T(n) operations
5 Input array X of n ≥ 1 integer.
• The sum of the first n integers
Empty array A; A is same size as X.
is n(n + 1) / 2 4
Output array A[0]…A[n-1] changed to hold prefix averages of X.
– There is a simple visual
proof of this fact 3 returns sum of X[0], X[1],…,X[n-1] #operations
if n=1 1
• Thus, algorithm 2 A[0] ← X[0] 3
prefixAverages1 runs in O(n2)
1 return A[0] 2
time tot ← recPrefixSumAndAverage (X,A,n-1) 3+T(n-1)
0 tot ← tot + X[n-1] 4
1 2 3 4 5 6 A[n-1] ← tot / n 4
return tot; 1

Empirical analysis of time


Prefix Averages, Linear
efficiency
• Recurrence equation • Select a specific (typical) sample of inputs
– T(1) = 6
– T(n) = 13 + T(n-1) for n>1.
• Use physical unit of time (e.g., milliseconds)
• Solution of recurrence is
– T(n) = 13(n-1) + 6 OR
• T(n) is O(n).
• Count actual number of basic operations

• Analyze the empirical data

Best-case, average-case, Types of formulas for basic


worst-case
For some algorithms efficiency depends on type of operation count
input:
• Exact formula
• Worst case: W(n) – maximum over inputs of size e.g., C(n) = n(n-1)/2
n
• Formula indicating order of growth with
specific multiplicative constant
• Best case: B(n) – minimum over inputs of size
n e.g., C(n) ˜ 0.5 n2

• Average case: A(n) – “average” over inputs of size • Formula indicating order of growth with
n unknown multiplicative constant
– Number of times the basic operation will be executed on e.g., C(n) ˜ cn2
typical input

4
Time efficiency of nonrecursive
algorithms Example: Sequential search
Steps in mathematical analysis of nonrecursive • Problem: Given a list of n elements and a
algorithms: search key K, find an element equal to K, if
any.
• Decide on parameter n indicating input siz
• Algorithm: Scan the list and compare its
• Identify algorithm ’s basic operatio successive elements with K until either a
• Determine worst, average, and best case for input of matching element is found (successful
size n
search) of the list is exhausted (unsuccessful
• Set up summation for C(n) reflecting algorithm ’s loop
structure
search)
• Simplify summation using standard formulas (see • Worst case
Appendix A)
• Best case

Prefix Averages (Quadratic) Time efficiency of recursive


• The following algorithm computes prefix averages in
algorithms
Steps in mathematical analysis of recursive algorithms:

quadratic time by applying the definition


• Decide on parameter n indicating input size
Algorithm prefixAverages1(X, n) • Identify algorithm ’s basic operation
Input array X of n integers
Output array A of prefix averages of X #operations • Determine worst, average, and best case for input of size
n
1. A ← new array of n integers n
2. for i ← 0 to n − 1 do n • Set up a recurrence relation and initial condition(s) for
3. s ← X[0] 2n C(n)-the number of times the basic operation will be
4. for j ← 1 to i do 1 + 2 + …+ (n − 1) executed for an input of size n (alternatively count
5. s ← s + X[j] 3(1 + 2 + …+ (n − 1)) recursive calls).
6. A[i] ← s / (i + 1) 4n • Solve the recurrence to obtain a closed form or estimate
7. return A 1 the order of magnitude of the solution (see Appendix B)

You might also like