Ds Module4
Ds Module4
To
Algorithms
References
Definition :
algorithm
while n # 0 do
r = m mod n
m=n
n=r
return m
Flow chart :
Natural Language:
start
Algorithm Exchange
// This algorithm exchanges
the value of x and y.
Read x, y
Step1:
Read two numbers
Step2 : Temp = x
x=y
Exchange x and y y = temp
Step3 :
Output the result
Print x, y
step4:
Terminate.
stop
Pseudo code representation
Algorithm Exchange
{
read x, y;
temp = x;
x = y;
y = temp;
print x, y;
}
Points to remember
Algorithm sort
for i := 1 to n do
{
Examine a[i] to a[n] and suppose the smallest
element is at a[j];
Interchange a[i] and a[j];
}
Pseudo code for above algorithm
{
for i = 1 to n do
{
J:=i;
for k:= i+1 to n do
If (a[k] < a[j]) then j:=k;
t:=a[i];
a[i]:=a[j];
a[j]:=t;
}
}
Sample algorithms
1. Largest of three numbers
Algorithm largest
Step 1 : Input three numbers { read a, b, c }
Step2 : Assume a is largest { big = a }
Step 3 : Obtain the largest of a and b
{ if ( b > big )
big = b
end if }
step 4 : Obtain the largest of a, b and c
{ if ( c > big )
big = c
end if }
step 5 : Output the largest
print big
step 6 : Finished .
stop.
2. Sum of first n natural numbers
Step5 : stop.
Algorithm design and analysis process
Understanding the problem
Design an algorithm
Prove correctness
c. Orders of growth
Running Time
80
difficult to determine. 0
1000 2000 3000 4000
We focus on the worst case Input Size
running time.
Easier to analyze
Crucial to applications such as
games, finance and robotics
Experimental Studies
9000
Write a program
8000
implementing the algorithm 7000
Run the program with 6000
Time (ms)
inputs of varying size and 5000
composition 4000
2000
System.currentTimeMillis() to
1000
get an accurate measure of 0
the actual running time 0 50 100
Algorithm arrayMax(A, n)
currentMax A[0] 2
{ initialize counter i to 1} 1
for i 1 to n 1 do n
if A[i] currentMax then 2(n 1)
currentMax A[i] 2(n 1)
{ increment counter i } 2(n 1)
return currentMax 1
……contd
To summarize, the number of primitive
operations executed by the algorithm
At least are( best case): 2+1+n+4(n-1)+1=5n
(because the assignment statement is not
executed)
At most are (worst case): 2+1+n+6(n-1)+1=7n-2
(because the assignment statement is executed)
3 if n==1
T(n) = {
T(n-1) +7 otherwise
Asymptotic Algorithm Analysis
The asymptotic analysis of an algorithm determines the
running time in big-Oh notation
To perform the asymptotic analysis
We find the worst-case number of primitive operations
executed as a function of the input size
We express this function with big-Oh notation
Example:
We determine that algorithm arrayMax executes at most 7n
2 primitive operations
We say that algorithm arrayMax “runs in O(n) time”
word asymptotic means the study of function of
parameter “n” grows larger & larger without bound
Seven Important Functions (§3.3)
Seven functions that
often appear in 1E+29
algorithm analysis: 1E+27 Cubic
T(n)
1E+15
Quadratic n2 1E+13
Cubic n3 1E+11
1E+9
Exponential 2n
1E+7
1E+5
In a log-log chart, the 1E+3
1E+1
slope of the line 1E-1
1E-1 1E+2 1E+5 1E+8
corresponds to the
growth rate of the n
function
Constant Factors
The growth rate is 1E+25 Quadratic
1E+23 Quadratic
not affected by 1E+21 Linear
constant factors or 1E+19
Linear
1E+17
lower-order terms 1E+15
T(n)
1E+13
Examples 1E+11
102n 105 is a linear 1E+9
1E+7
function 1E+5
105n2 108n is a 1E+3
quadratic function 1E+1
1E-1
1E-1 1E+1 1E+3 1E+5 1E+7 1E+9
n
The efficiency analysis frame work concentrates on the order
of growth of an algorithms basic operations count as the
principal indicator of the algorithm’s efficiency.
c.g(n)
f(n)
no n
The upper bound of f(n) indicates that function f(n) will not
consume more than the specified time of c*g(n).
f(n)
c.g(n)
no n
The lower bound of f(n) indicates that function f(n) will consume
more than the specified time of c*g(n).
ex: n3 € Ω(n2)
n3 ≥ n2 for all n≥0.
i.e. c=1 & n0=0.
∂ ( big theta)Notation:
c1.g(n)
f(n)
c2.g(n)
no n
Examples: Big O notation.
1.Given f(n) = 3n + 2, prove that f(n) € O(n).
sum = 0;
for( i = 0; i < n; i++ )
sum = sum + i;
The running time is O(n)
B. Nested loops :
sum = 0;
for( i = 0; i < n; i++)
for( j = 0; j < n; j++)
sum++;
sum = 0; O(n2)
for( i = 0; i < n; i++)
for( j = 0; j < 2n; j++) sum++;
sum = 0; O(n2):
for( i = 0; i < n ; i++)
for( j = 0; j < i ; j++)
sum++;
Log n n nlogn n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65536
5 32 160 1024 32768 4294967296
Computing Prefix Averages
We further illustrate
35
asymptotic analysis with two X
algorithms for prefix 30 A
averages 25
The i-th prefix average of an
20
array X is average of the first
(i 1) elements of X: 15
A[i] X[0] X[1] … X[i])/(i+1) 10