Chap 1
Chap 1
1
Contents
1.1 Algorithms
1.2 The Importance of Developing Efficient
Algorithms
1.2.1 Sequential Search vs. Binary Search
1.2.2 The Fibonacci Sequence
1.3 Analysis of Algorithms
1.3.1 Time Complexity Analysis
1.3.2 Applying the Theory
1.4 Order
1.4.1 An Intuitive Introduction to Order
1.4.2 A Rigorous Introduction to Order
2
1.1 Algorithms
◼ Problem
◼ Ex. 1.1: Sorting - Sort the list S of n numbers in
nondecreasing order. The answer is the numbers in
sorted sequence.
◼ Ex. 1.2: Searching - Determine whether the number
x is in the list S of n numbers. The answer is yes if x
is in S and no if it is not.
◼ Ex) Partition – Decide whether a given multiset
A= {a1, … , an} of n positive integers has a partition P
such that ai = ai
iP iP
3
Parameters, Instance, and Solution
6
1.2 Importance of Developing Efficient
Algorithms
7
Algorithm 1.1 Sequential Search
9
Algorithm 1.5 Binary Search (2/2)
void binsearch ( int n,
const keytype S[ ],
keytype x,
index& location)
{
index low, high, mid;
low = 1; high = n;
location = 0;
while (low <= high && location == 0) {
mid = (low + high) / 2;
if (x == S[mid])
location = mid;
else if (x < S[mid])
high = mid – 1
else
low = mid + 1;
}
}
10
1.2.2 Fibonacci Sequence
f 0 = 0,
f1 = 1,
f n = f n −1 + f n − 2 ( n 2 )
11
Algorithm 1.6 n-th Fibonacci Term (Recursive)
T ( n) 2 n/2
(proof by induction in Th. 1.1)
13
Algorithm 1.7 n-th Fibonacci Term (Iterative)
◼ Problem: Determine the n-th term in the Fibonacci
Sequence.
◼ Inputs: a nonnegative integer n.
◼ Outputs: fib2, the n-th term in the Fibonacci Sequence.
int fib2 (int n)
{
index i;
int f[0..n]; ◼ dynamic programming:
f[0] = 0; compute (n+1) terms
if (n > 0) {
f[1] = 1;
for (i = 2; i <= n; i++)
f[i] = f[i – 1] + f[i – 2];
}
return f[n];
}
14
15
1.3 Analysis of Algorithms
◼ Efficiency
◼ Space complexity: memory
16
1.3.1 Time Complexity Analysis
◼ Want a measure independent of
◼ Computer
◼ Programming language
◼ Programmer
◼ Complex details of algorithms (pointer setting,
incrementing of loop indices)
◼ Not want # of CPU cycles or instructions
◼ Ex) binary search is more efficient than sequential
search
◼ # of comparisons: log n < n
◼ Algorithm’s efficiency: # of basic operations
executed as a function of input size
17
Algorithm 1.2 Add Array Members
◼ Problem: Add all the numbers in the array S of n
numbers.
◼ Inputs: positive integer n, array of numbers S indexed
from 1 to n.
◼ Outputs: sum, the sum of the numbers in S.
18
Algorithm 1.3 Exchange Sort
◼ Problem: Sort n keys in nondecreasing order.
◼ Inputs: positive integer n, array of keys S indexed
from 1 to n.
◼ Outputs: the array S containing the keys in
nondecreasing order.
19
Algorithm 1.4 Matrix Multiplication
◼ Problem: Determine the product of two n x n matrices.
◼ Inputs: a positive integer n, 2D arrays of numbers A and B, each
of which has both its rows and columns indexed from 1 to n.
◼ Outputs: a 2D array of numbers C, which has both its rows and
columns indexed from 1 to n, containing the product of A and B.
void matrixmult ( int n,
const number A[ ] [ ],
const number B[ ] [ ],
number C[ ] [ ])
{
index i, j, k;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
C[i][j] = 0; /* b.o. → e.t. = a
for (k = 1; k <= n; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j]; /* b.o. → e.t. = b
}
}
20
Input size and Basic operation
◼ Input size
◼ Sequential search, binary search, add array
members, exchange sort: array S of n keys
◼ Matrix multiplication: n, # of rows and columns
◼ Graph: n, e, # of nodes and edges
◼ Fibonacci number: log n + 1, # of binary digits to
encode n (n is input not input size)
◼ Basic operation
◼ Single instruction or group of instructions
◼ Execution time is independent of n
◼ Ex) search: comparison
21
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) {
Example C[i][j] = 0; /* b.o. → e.t. = a
for (k = 1; k <= n; k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j];
/* b.o. → e.t. = b
◼ Ex) matrix multiplication }
n = 10 → a 10 + b 10 b 10
2 3 3
◼
n = 100 → a 10 + b 10 b 10
4 6 6
◼
22
Time Complexity Analysis
24
1.3.2 Applying the Theory
◼ Alg. A B
◼ T(n) n n2
= 2
n 1000 t n t
?
25
1.4 Order
◼ 1.4.1 An Intuitive Introduction to Order
◼ a, b, c, d: constants
an + b ( n) linear
an 2 + bn + c ( n 2 ) quadratic
an 3 + bn 2 + cn + d ( n 3 ) cubic
(log n) ( n) ( n log n) ( n 2 ) ( n 3 ) ( 2n )
26
27
28
1.4.2 A Rigorous Introduction to Order
◼ Def. Big O: for a given complexity function f(n), O(f(n))
is the set of complexity functions g(n) for which there
exists some positive real constant c and some
nonnegative integer N s.t. for all n ≥ N,
g(n) c f(n) : asymptotic upper bound
n 1 n2 n1 n O( n 2 )
1
n( n − 1) / 2 n 2 n0 n( n − 1) / 2 O( n 2 )
2
32
Order
( f ( n))
is the set of complexity functions g(n) for which
there exists some positive real constant c and d and
some nonnegative integer N s.t. for all n ≥ N,
c f(n) g(n) d f(n)
Ex) n( n − 1) / 2 ( n )
2
◼
33
34
Small o
1
◼ Ex) n c n2 for n
g( n) f ( n) c
N
35
Theorem 1.2
If g( n) o( f ( n)) then g( n) O( f ( n)) − ( f ( n))
That is, g(n) is in O(f(n)) but not in ( f ( n))
O( f ( n))
more ( f ( n)) more
efficient complex
o( f ( n)) ( f ( n))
36