Unit I
Unit I
INTRODUCTION TO ALGORITHM
Informal Definition:
Formal Definition:
Algorithm Specification:
Algorithm can be described in three ways.
1. Natural language like English:
1
When this way is choused care should be taken, we should
ensure that each & every statement is definite.
2. Graphic representation called flowchart:
This method will work well when the algorithm is small& simple.
3. Pseudo-code Method:
In this method, we should typically describe algorithms as
program, which resembles language like Pascal & algol.
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max),
1. Space Complexity:
The space complexity of an algorithm is the amount of
money it needs to run to compilation.
2. Time Complexity:
The time complexity of an algorithm is the amount of computer time
it needs to run to compilation.
Space Complexity:
The part typically includes the instruction space (ie. Space for the
code), space for simple variable and fixed-size component variables
(also called aggregate) space for constants, and so on.
constant.
{
s=0.0;
f
o
r
I
=
1
t
o
5
n
d
o
s
=
s
+
a
[
I
]
;
return s;
}
Time Complexity:
Algorithm:
Algorithm sum(a,n)
{
s= 0.0;
c
o
u
n
t
c
o
u
n
t
+
1
;
f
o
r
I
=
1
t
o
d 7
o
{
c
o
u
n
t
=
c
o
u
n
t
+
1
;
s
=
s
+
a
[
I
]
;
c
o
u
n
t
=
c
o
u
n
t
+
1
;
} 8
c
o
u
n
t
=
c
o
u
n
t
+
1
;
c
o
u
n
t
=
c
o
u
n
t
+
1
;
r
e
t
u
r
n
s
;
}
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
ASYMPTOTIC NOTATIONS
2. Omega(Ω) notation
3. Theta(ɵ) notation
O(n),
2. Omega(Ω) notation:
Example
f(n)=3n+2 then prove that f(n) = Ω(g(n))
Let
f
(
n
)
=
3
n
+
2
,
c
=
3
,
g
(
1
n 2
)
=
n
i
f
n
=
1
3(1)+2 ≥ 3(1)
5 ≥ 3 (T)
3n+2 ≥ 4n for all n ≥ 1
This is in the form of f(n) ≥ c*g(n) for all n
= Ω(n).
3. Theta(ɵ) notation:
1
Example: 3
f(n)=3n+2 then Prove that f(n) = θ(g(n))
Lower bound = 3n+2 ≥ 3n for all n ≥ 1
c1=3,g(n)=n,n0=1 Upper Bound = 3n+2 ≤ 4n
for all n ≥ 2
c2=4, g(n)=n , n0=2 3(n) ≤ 3n+2 ≤ 4(n) for all n, n ≥ 2
This is in the form of c1*g(n) ≤ f(n) ≤ c2* g(n) for all n≥n0
Where c1=3, c2=4, g(n)=n, n0=2
The solution to this trade-off problem is to use Dynamic Table (or Arrays) . The idea is to
increase the size of the table whenever it becomes full. Following are the steps to follow
when the table becomes full.
1) Allocate memory for larger table size, typically twice the old table.
2) Copy the contents of the old table to a new table.
3) Free the old table.
1
1. Complexity: Amortized analysis5
can be complex, especially when multiple
operations are involved, making it difficult to implement and understand.
2. Limited applicability: Amortized analysis may not be suitable for all types of
algorithms, especially those with highly unpredictable behavior or those that
depend on external factors like network latency or I/O operations.
3. Lack of precision: Although amortized analysis provides a more accurate
prediction of average-case complexity than worst-case analysis, it may not
always provide a precise estimate of the actual performance of an algorithm,
especially in cases where there is high variance in the cost of operations.
Thus, the total amortized cost of insertion in the Red-Black Tree is O(n).
1
6