1.unit 1 Introduction
1.unit 1 Introduction
Unit-I: Introduction
Text Books:
1.Ellis Horowitz, SartajSahni, Sanguthevar Rajasekaran, “Fundamentals of
Computer Algorithms”, 2nd Edition, Universities Press.
2.Introduction to Algorithms Thomas H. Cormen, PHI Learning
3.Harsh Bhasin, “Algorithms Design & Analysis”, Oxford University Press.
UNIT I: Introduction to
Problem Solving Concepts
1. Problem definition
2. Algorithm definition
3. Algorithm specification
4. Performance analysis of an algorithm
5. Performance Measurement
6. Asymptotic notation
7. Randomized Algorithms
Problem Definition
• Understand speech
• Translate to Chinese
Algorithm Specification
Space complexity
How much space is required
Time complexity
How much time does it take to run the algorithm
Often, we deal with estimates!
Space Complexity
100
Running Time
80
60
40
20
0
1000 2000 3000 4000
Input Size
Use a Theoretical Approach
Based on high-level description of the algorithms, rather
than language dependent implementations
Makes possible an evaluation of the algorithms that is
independent of the hardware and software environments
Pseudo-Code = a description of an algorithm that is
more structured than usual prose but
less formal than a programming language
Example: find the maximum element of an array.
Algorithm arrayMax(A, n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i 1 to n -1 do
if currentMax < A[i] then currentMax A[i]
return currentMax
Counting Primitive Operations
Example: find the maximum element of an array.
Algorithm arrayMax(A, n) No. of Operations
//Input: An array A storing n integers. 00
//Output: The maximum element in A. 00
currentMax A[0] 01 time
for i 1 to n -1 do n times
if currentMax < A[i] then (n-1) times
currentMax A[i] (n-1) times
return currentMax 01 time
Total: 3n times
Estimating Running Time
Q(g(n)) =
f(n) : positive constants c1, c2, and
n0, such that n n0,
we have 0 c1g(n) f(n) c2g(n)
Example:
10n2 - 3n = Θ(n2)
What constants for n0, c1, and c2 will work?
Make c1 a little smaller than the leading coefficient, and c2 a
little bigger.
To compare orders of growth, look at the leading term.
Solution: c1n2 ≤ 10n2 −3n ≤ c2n2
c1 ≤ 10−3/n ≤ c2
For n0=1, c1 ≤ 7 and c2 ≥ 10
c1n2 ≤ n2/2−3n ≤ c2n2
Exercise: Prove that n2/2-3n= Θ(n2) c1 ≤ 1/2−3/n ≤ c2
For n0=7,
c1 ≤ 1/14 and c2 ≥ 1/2
Big-Oh (O) - notation
For a function having only
asymptotic upper bound, Big Oh
„O‟ notation is used. Let a given
function g(n), O(g(n))) is the set
of functions f(n) defined as
O(g(n))={f(n): if there exist
positive constant c and n0 such
that 0≤f(n) ≤cg(n) for all n, n n0}
10n2 + 4n + 2 ≤ C*n2
10n2 + 4n + 2 ≤ 10n2 + n2 ≤ 11n2
For n=1, 16 ≤11 not true
For n=5, 272 ≤ 275 true
So c = 11 ∀n ≥ 5, O(n2 )
6*2n + n2 ≤ 6*2n + 2n
6*2n + n2 ≤ 7*2n
6*2n + n2 ≤ C*2n
For n=1, 13 ≤ 14 not true
For n=2, 28 ≤ 28 true
So C=7 and ∀n ≥ 2, O(2n)
Big Omega (Ω) - Notation
For a function having only
asymptotic lower bound, Ω
notation is used. Let a given
function g(n), Ω(g(n))) is the set
of functions f(n) defined as
Ω(g(n)) ={f(n): if there exist
positive constant c and n0 such
that 0≤ cg(n) ≤f(n) for all n, n n0}
g(n) is an asymptotic lower bound for f(n).
f(n)=3n2+n
f(n)>=c*g(n) => 3n2+n>=3n2= Ω(n2)
f(n) = o(g(n))
If f(n) = n2 and g(n) = n3 then
check whether f(n) = o(g(n))
or not.
Definition (Little–Omega, ω()): Let f(n) and g(n) be functions that
map positive integers to positive real numbers. We say that f(n) is
ω(g(n)) (or f(n) ∈ ω(g(n))) if for any real constant c > 0, there exists an
integer constant n0 ≥ 1 such that f(n)>c*g(n) for every integer n ≥ n0.
Examples
•2≠ω(1)
•4x+2≠ω(x)
•4x+2=ω(1)
•3x2+4x+2≠ω(x2)
•3x2+4x+2=ω(x)
What is Randomized Algorithm?
We call an algorithm randomized if its behaviour is
determined not only by its input but also by values
produced by a random-number generator
An algorithm that uses random numbers to decide
what to do next anywhere in its logic is called
Randomized Algorithm.
For example, in Randomized Quick Sort, we use a
random number to pick the next pivot (or we
randomly shuffle the array). Typically, this
randomness is used to reduce time complexity or
space complexity in other standard algorithms.
the worst case running time is O(n2).
Linear Search Algorithm