Professional Documents
Culture Documents
Chapter01-Basic Concepts and Notations
Chapter01-Basic Concepts and Notations
Chapter01-Basic Concepts and Notations
Notations
Objectives
At the end of the lesson, the student should be able to:
Problem domain
Machine domain
Synthesis of algorithms
Algorithm
Output - set of one or more resulting quantities; also called the range
of the algorithm
10
Addressing Methods
11
Addressing Methods
Memory pool or avail list - source of the nodes from which linked
structures are built
class AvailList {
Node head;
AvailList(){
head = null;
}
AvailList(Node n){
head = n;
}
12
Addressing Methods
Two basic procedures that manipulate the avail list are getNode and
retNode, which requests for a node and returns a node respectively.
Node getNode(){
Node a;
if ( head == null) {
return null; /* avail list is empty */
} else {
a = head.link; /* assign node to return to a */
head = head.link.link;
return a;
}
head.link = n;
13
Mathematical Functions
x mod y = x
=x-y*
x/y
if y = 0
if y <> 0
14
Mathematical Functions
Identities
x=x
x=x
- x = x
x + y <= x + y
x = x + x mod 1
z ( x mod y ) = zx mod zy
15
Complexity of Algorithms
Algorithm Efficiency
input size
Instruction type
machine speed
quality of source code of the algorithm implementation
quality of the machine code generated from the source code by the
compiler
16
Complexity of Algorithms
Formal definition:
g(n) = O(f(n)) if there exists two constants c and n0 such that
| g(n) | <= c * | f(n) | for all n >= n0.
17
Complexity of Algorithms
Rule for Sums
Suppose that T1(n) = O( f(n) ) and T2(n) = O( g(n) ).
Then, t(n) = T1(n) + T2(n) = O( max( f(n), g(n) ) ).
Proof: By definition of the O-notation,
T1(n) <= c1 f(n)
for n >= n1 and
T2(n) <= c2 g(n) for n >= n2.
Let n0 = max(n1, n2). Then
T1(n) + T2(n)
<= c1 f(n) + c2 g(n)
<= (c1 + c2) max(f(n),g(n))
<= c max ( f(n), g(n) )
Thus,
n >= n0.
n >= n0.
n >= n0.
18
Complexity of Algorithms
Examples
Consider the algorithm below :
for (i=1; i <= n, i++)
for (j=1; j <= n, j++)
// steps which take O(1) time
Since the steps in the inner loop will take n + n-1 + n-2 + ... + 2 + 1 times, then the
running time is
n( n+1 ) / 2
= n2 / 2 + n / 2
= O( n2 )
19
Complexity of Algorithms
Examples
LINEAR SEARCH ALGORITHM
1
2
3
4
5
6
T( n ) = 3n + 3 so that T( n ) = O( n )
found = false;
loc
= 1;
Since g( n ) <= c f( n ) for n >= n 0, then
while ((loc <= n) && (!found)){
if (item == a[loc]found = true;
else loc = loc + 1;
3n + 3 <= c n
}
STATEMENT
1
2
3
4
5
# of times executed
1
1
n+1
n
n
3n + 3 <= c
= 3 + 3 / n <= c
--------n
Thus c = 4 and n0 = 3.
20
Complexity of Algorithms
Description
Algorithm
O(1)
O(log2n)
Constant
Logarithmic
Binary Searc h
O(n)
O(n log2n)
Linear
Sequential Search
O(n2)
O(n3)
Quadratic
Insertion Sort
Cubic
Floyds Algorithm
O( 2n )
Exponential
Heapsort
F(n)
log2n
n
n log2n
n2
n3
2n
Running Time
19.93 microseconds
1.00 seconds
19.93 seconds
11.57 days
317.10 centuries
Eternity
21
Complexity of Algorithms
General rules on determining the running time of an algorithm
FOR loops
Analysis is done from the inner loop going outward. The total
running time of a statement inside a group of for loops is the running
time of the statement multiplied by the product of the sizes of all the
for loops.
CONSECUTIVE STATEMENTS
At most the running time of the statement inside the for loop times
the number of iterations.
IF/ELSE
Never more than the running time of the test plus the larger of the
running times of the conditional block of statements.
Data Structures Basic Concepts and Notations
22
Summary
23