1-1
Design and Analysis of Algorithms – Unit I
Chapter 1
Chapter 1
Introduction
Introduction
1-2
Design and Analysis of Algorithms – Unit I
Algorithm
 An Algorithm is a sequence of unambiguous
instructions for solving a problem,
 i.e., for obtaining a required output for any
legitimate input in a finite amount of time.
1-3
Design and Analysis of Algorithms – Unit I
Notion of algorithm
“computer”
Algorithmic solution
problem
algorithm
input output
1-4
Design and Analysis of Algorithms – Unit I
PSEUDOCODE
 Pseudocode (pronounced SOO-doh-kohd) is a detailed yet readable
description of what a computer program or algorithm must do, expressed in
a formally-styled natural language rather than in a programming language.
 It is sometimes used as a detailed step in the process of developing a
program.
 It allows programmers to express the design in great detail and provides
programmers a detailed template for the next step of writing code in a
specific programming language.
1-5
Design and Analysis of Algorithms – Unit I
Formatting and Conventions in Pseudocoding
 INDENTATION in pseudocode should be identical to its
implementation in a programming language. Try to indent
at least four spaces.
 The pseudocode entries are to be cryptic, AND SHOULD
NOT BE PROSE. NO SENTENCES.
 No flower boxes in pseudocode.
 Do not include data declarations in pseudocode.
1-6
Design and Analysis of Algorithms – Unit I
Some Keywords That Should be Used
 For looping and selection,
• Do While...EndDo;
• Do Until...Enddo;
• Case...EndCase;
• If...Endif;
• Call ... with (parameters); Call; Return ....; Return;
When; Always use scope terminators for loops and
iteration.
1-7
Design and Analysis of Algorithms – Unit I
Some Keywords …
 As verbs, use the words
• generate, Compute, Process,
• Set, reset,
• increment,
• calculate,
• add, sum, multiply, ...
• print, display,
• input, output, edit, test , etc.
1-8
Design and Analysis of Algorithms – Unit I
Methods of finding GCD
M - 1
M - 2
M - 3
1-9
Design and Analysis of Algorithms – Unit I
Euclid’s Algorithm
Problem: Find gcd(m,n), the greatest common divisor of two
nonnegative, not both zero integers m and n
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
Euclid’s algorithm is based on repeated application of equality
gcd(m,n) = gcd(n, m mod n)
until the second number becomes 0, which makes the problem
trivial.
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
1-10
Design and Analysis of Algorithms – Unit I
Two descriptions of Euclid’s algorithm
Step 1 If n = 0, return m and stop; otherwise go to Step 2
Step 2 Divide m by n and assign the value fo the remainder to r
Step 3 Assign the value of n to m and the value of r to n. Go to
Step 1.
while n ≠ 0 do
r ← m mod n
m← n
n ← r
return m
1-11
Design and Analysis of Algorithms – Unit I
Other methods for computing gcd(m,n)
Consecutive integer checking algorithm
Step 1 Assign the value of min{m,n} to t
Step 2 Divide m by t. If the remainder is 0, go to Step 3;
otherwise, go to Step 4
Step 3 Divide n by t. If the remainder is 0, return t and stop;
otherwise, go to Step 4
Step 4 Decrease t by 1 and go to Step 2
1-12
Design and Analysis of Algorithms – Unit I
Other methods for gcd(m,n) [cont.]
Middle-school procedure
Step 1 Find the prime factorization of m
Step 2 Find the prime factorization of n
Step 3 Find all the common prime factors
Step 4 Compute the product of all the common prime factors
and return it as gcd(m,n)
Is this an algorithm?
1-13
Design and Analysis of Algorithms – Unit I
Sieve of Eratosthenes
Input: Integer n ≥ 2
Output: List of primes less than or equal to n
for p ← 2 to n do A[p] ← p
for p ← 2 to n do
if A[p]  0 //p hasn’t been previously eliminated from the list
j ← p* p
while j ≤ n do
A[j] ← 0 //mark element as eliminated
j ← j + p
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1-14
Design and Analysis of Algorithms – Unit I
Termination of Euclid’s Algorithm
 The second number of the pair gets smaller with each
iteration and cannot become negative:
 Indeed, the new value of n is r = m mod n, which is always
smaller than n.
 Eventually, r becomes zero, and the algorithms stops.
1-15
Design and Analysis of Algorithms – Unit I
Compute the GCD of 120 and 23.
1-16
Design and Analysis of Algorithms – Unit I
Fundamentals of Algorithmic
Problem Solving
1-17
Design and Analysis of Algorithms – Unit I
Fundamentals of Algorithmic Problem
Solving
 Algorithm = Procedural Solutions to Problem
 NOT an answer, BUT rather specific instructions of
getting answers.
 Therefore, requires steps in designing and analyzing an
algorithm
1-18
Design and Analysis of Algorithms – Unit I
Algorithm Design & Analysis Process
1-19
Design and Analysis of Algorithms – Unit I
Step 1: Understand the Problem
 Before designing an algorithm - understand completely the
problem given.
 Read the problem’s description carefully and ask
questions if you have any doubts about the problem,
 Do a few small examples by hand, think about special
cases, and ask questions again if needed.
1-20
Design and Analysis of Algorithms – Unit I
 An input to an algorithm specifies an instance of the
problem the algorithm solves.
 It is very important to specify exactly the range of instances
the algorithm needs to handle.
 Failing which – the algorithm works correctly for some
inputs , but crashes on some boundary values.
 Remember that a correct algorithm is not one that works
most of the time but one that works correctly for all
legitimate inputs.
Step 1: Understand the Problem
1-21
Design and Analysis of Algorithms – Unit I
Step 2: Ascertaining the capabilities
of a computational device
 Algorithms designed to be executed on machines that
executes intstructions one after another are called
sequential algorithms.
 Algorithms that take advantage of computers that can
execute operations concurrently are called parallel
algorithms.
1-22
Design and Analysis of Algorithms – Unit I
Step 3: Choosing between Exact &
Approximate Problem Solving
 Solving the problem exactly - Exact algorithms
 Solving the problem approximately - Approximation algorithms
 Why approximation algorithms?
1. Problems cannot be solved exactly.
Eg. Extracting square roots, solving non-linear equations
2. Available exact algorithms are unacceptably slow because of
problem’s complexity
Eg. Traveling Salesman Problem
3. Approx. Algs can be a part of algorithms that solve the
problem exactly.
1-23
Design and Analysis of Algorithms – Unit I
Step 4: Deciding on Appropriate Data
Structures
 In the new world of object-oriented programming, data
structures remain important for both design and analysis of
algorithms.
 However, we will assume a very basic data structure for
now and concentrate on the algorithm side.
1-24
Design and Analysis of Algorithms – Unit I
Step 5: Algorithm Design Techniques
 An algorithm design technique (or “strategy” or
“paradigm”) is a general approach to solving problems
algorithmically that is applicable to a variety of problems
from different areas of computing.
 Eg. Brute force, Divide-and-Conquer, Transform-and-
Conquer
 Importance:
1. Provide guidance for designing algorithms for new
problems.
2. To classify algorithms according to an underlying
design idea.
1-25
Design and Analysis of Algorithms – Unit I
Step 6: Methods of Specifying an
Algorithm
 Pseudocode,
Pseudocode, a mixture of a natural language and
a mixture of a natural language and
programming language-like constructs.
programming language-like constructs.
 flowchart
flowchart, a method of expressing an algorithm by a
, a method of expressing an algorithm by a
collection of connected geometric shapes containing
collection of connected geometric shapes containing
descriptions of the algorithm’s steps.
descriptions of the algorithm’s steps.
1-26
Design and Analysis of Algorithms – Unit I
Step 7: Proving an Algorithm’s
Correctness
 Prove algorithm’s correctness = prove that the algorithm
yields a required result for every legitimate input in a finite
amount of time.
 For an approximation algorithm, correctness means to be
able to show that the error produced by the algorithm does
not exceed a predefined limit.
1-27
Design and Analysis of Algorithms – Unit I
Step 8: Analyzing an Algorithm
1. Efficiency
Time efficiency indicates how fast the algorithm runs.
space efficiency indicates how much extra memory the
algorithm needs.
2. Simplicity
3. Generality
Design an algorithm for a problem posed in more
general terms.
Design an algorithm that can handle a range of inputs
that is natural for the problem at hand.
1-28
Design and Analysis of Algorithms – Unit I
Step 9: Coding the algorithm
 More than implementation
More than implementation
 Peril of incorrect & inefficient implementation
Peril of incorrect & inefficient implementation
 Require testing & debugging
Require testing & debugging
 Require code optimizing
Require code optimizing
1-29
Design and Analysis of Algorithms – Unit I
Important Problem Types
1-30
Design and Analysis of Algorithms – Unit I
Important Problem Types
 Sorting
Sorting
 Searching
Searching
 String processing
String processing
 Graph problems
Graph problems
 Combinatorial problems
Combinatorial problems
 Geometric problems
Geometric problems
 Numerical problems
Numerical problems
1-31
Design and Analysis of Algorithms – Unit I
Sorting
 The
The sorting problem
sorting problem asks us to rearrange the items of a
asks us to rearrange the items of a
given list in ascending order.
given list in ascending order.
 we usually need to
we usually need to
sort lists of numbers,
sort lists of numbers,
characters from an alphabet,
characters from an alphabet,
character strings,
character strings,
 records similar to those maintained by schools about
records similar to those maintained by schools about
their students,
their students,
libraries about their holdings,
libraries about their holdings,
companies about their employees.
companies about their employees.
1-32
Design and Analysis of Algorithms – Unit I
Searching
 The
The searching problem
searching problem deals with finding a given value,
deals with finding a given value,
called a
called a search key
search key, in a given set (or a multiset, which
, in a given set (or a multiset, which
permits several elements to have the same value).
permits several elements to have the same value).
1-33
Design and Analysis of Algorithms – Unit I
String Processing
 A
A string
string is a sequence of characters from an alphabet.
is a sequence of characters from an alphabet.
 String of particular interest:
String of particular interest:
1. Text string – comprises letters, numbers, and
1. Text string – comprises letters, numbers, and
special characters
special characters
2. Bit string – comprises zeros and ones
2. Bit string – comprises zeros and ones
3. Gene sequence
3. Gene sequence
 Mainly
Mainly string matching problem
string matching problem: searching for a given
: searching for a given
word in a text
word in a text
1-34
Design and Analysis of Algorithms – Unit I
Graph Problems
 A
A graph
graph can be thought of as a collection of points called
can be thought of as a collection of points called
vertices, some of which are connected by line segments
vertices, some of which are connected by line segments
called edges.
called edges.
 Used for modeling a wide variety of real-life applications.
Used for modeling a wide variety of real-life applications.
 Basic graph algorithms include:
Basic graph algorithms include:
1.
1. Graph traversal algorithms
Graph traversal algorithms - How can one visit all
- How can one visit all the
the
points in a network?
points in a network?
2.
2. Shortest-path algorithms
Shortest-path algorithms - What is the best
- What is the best
Introduction route between two cities?
Introduction route between two cities?
3.
3. Topological sorting
Topological sorting for graphs with directed edges
for graphs with directed edges
1-35
Design and Analysis of Algorithms – Unit I
Combinatorial Problems
 combinatorial problems:
combinatorial problems: problems that ask (explicitly or
problems that ask (explicitly or
implicitly) to find a combinatorial object—such as a
implicitly) to find a combinatorial object—such as a
permutation, a combination, or a subset—that satisfies
permutation, a combination, or a subset—that satisfies
certain constraints and has some desired property (e.g.,
certain constraints and has some desired property (e.g.,
maximizes a value or minimizes a cost).
maximizes a value or minimizes a cost).
1. Combinatorial grows extremely fast with problem size
1. Combinatorial grows extremely fast with problem size
2. No known algorithm solving most such problems
2. No known algorithm solving most such problems
exactly in an acceptable amount of time.
exactly in an acceptable amount of time.
1-36
Design and Analysis of Algorithms – Unit I
Geometric Problems
 Geometric algorithms
Geometric algorithms deal with geometric objects such as
deal with geometric objects such as
points, lines, and polygons.
points, lines, and polygons.
2 class problems:
2 class problems:
 The
The closest pair problem
closest pair problem: given
: given n
n points in the plane, find
points in the plane, find
the closest pair among them.
the closest pair among them.
 The
The convex hull problem
convex hull problem asks to find the smallest convex
asks to find the smallest convex
polygon that would include all the points of a given set. If
polygon that would include all the points of a given set. If
1-37
Design and Analysis of Algorithms – Unit I
Convex hull problem
1-38
Design and Analysis of Algorithms – Unit I
Numerical Problems
 Numerical problems
Numerical problems, another large special area of
, another large special area of
applications, are problems that involve mathematical
applications, are problems that involve mathematical
objects of continuous nature: solving equations and systems
objects of continuous nature: solving equations and systems
of equations, computing definite integrals, evaluating
of equations, computing definite integrals, evaluating
functions, and so on.
functions, and so on.
1-39
Design and Analysis of Algorithms – Unit I
Fundamentals of Analysis of algorithm efficiency
1-40
Design and Analysis of Algorithms – Unit I
Analysis of algorithms
Analysis of algorithms
 Issues:
Issues:
• correctness
correctness
• time efficiency
time efficiency
• space efficiency
space efficiency
• optimality
optimality
 Approaches:
Approaches:
• theoretical analysis
theoretical analysis
• empirical analysis
empirical analysis
1-41
Design and Analysis of Algorithms – Unit I
Theoretical analysis of time efficiency
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of
Time efficiency is analyzed by determining the number of
repetitions of the
repetitions of the basic operation
basic operation as a function of
as a function of input size
input size
 Basic operation
Basic operation: the operation that contributes the most
: the operation that contributes the most
towards the running time of the algorithm
towards the running time of the algorithm
T
T(
(n
n)
) ≈
≈ c
cop
opC
C(
(n
n)
)
running time execution time
for basic operation
or cost
Number of times
basic operation is
executed
input size
Note: Different basic operations may cost differently!
1-42
Design and Analysis of Algorithms – Unit I
Input size and basic operation examples
Input size and basic operation examples
Problem Input size measure Basic operation
Searching for key
in a list of n items
Number of list’s
items, i.e. n
Key comparison
Multiplication of
two matrices
Matrix dimensions or
total number of
elements
Multiplication of
two numbers
Checking primality
of a given integer n
n’size = number of
digits (in binary
representation)
Division
Typical graph
problem
#vertices and/or
edges
Visiting a vertex
or traversing an
edge
1-43
Design and Analysis of Algorithms – Unit I
Empirical analysis of time efficiency
Empirical analysis of time efficiency
 Select a specific (typical) sample of inputs
Select a specific (typical) sample of inputs
 Use physical unit of time (e.g., milliseconds)
Use physical unit of time (e.g., milliseconds)
or
or
Count actual number of basic operation’s executions
Count actual number of basic operation’s executions
 Analyze the empirical data
Analyze the empirical data
1-44
Design and Analysis of Algorithms – Unit I
Efficiencies
 Worst Case Efficiency:
• Is its efficiency for the worst case input of size n, which
is an input of size n for which the algorithm runs the
longest among all possible inputs of that size
• C
Cworst
worst(
(n
n)
)
 Best-case efficiency:
Best-case efficiency:
• Is its efficiency for the worst case input of size n, which
is an input of size n for which the algorithm runs the
fastest among all possible inputs of that size
• C
Cbest
best(
(n
n)
)
1-45
Design and Analysis of Algorithms – Unit I
Amortized efficiency
• It applies not to a single run of an
algorithm, but rather to a sequence of
operations performed on the same data
structure
1-46
Design and Analysis of Algorithms – Unit I
Best-case, average-case, worst-case
Best-case, average-case, worst-case
For some algorithms, efficiency depends on form of input:
For some algorithms, efficiency depends on form of input:
 Worst case: C
Worst case: Cworst
worst(
(n
n) – maximum over inputs of size
) – maximum over inputs of size n
n
 Best case: C
Best case: Cbest
best(
(n
n) – minimum over inputs of size
) – minimum over inputs of size n
n
 Average case: C
Average case: Cavg
avg(
(n
n) – “average” over inputs of size
) – “average” over inputs of size n
n
• Number of times the basic operation will be executed on typical
Number of times the basic operation will be executed on typical
input
input
• NOT the average of worst and best case
NOT the average of worst and best case
• Expected number of basic operations considered as a random
Expected number of basic operations considered as a random
variable under some assumption about the probability
variable under some assumption about the probability
distribution of all possible inputs. So, avg = expected under
distribution of all possible inputs. So, avg = expected under
uniform distribution.
uniform distribution.
1-47
Design and Analysis of Algorithms – Unit I
Example: Sequential search
Example: Sequential search
 Worst case
Worst case
 Best case
Best case
 Average case
Average case
n key comparisons
1 comparisons
(n+1)/2, assuming K is in A
1-48
Design and Analysis of Algorithms – Unit I
Types of formulas for basic operation’s count
Types of formulas for basic operation’s count
 Exact formula
Exact formula
e.g., C(
e.g., C(n
n) =
) = n
n(
(n
n-1)/2
-1)/2
 Formula indicating order of growth with specific
Formula indicating order of growth with specific
multiplicative constant
multiplicative constant
e.g., C(
e.g., C(n
n)
) ≈
≈ 0.5
0.5 n
n2
2
 Formula indicating order of growth with unknown
Formula indicating order of growth with unknown
multiplicative constant
multiplicative constant
e.g., C(
e.g., C(n
n)
) ≈
≈ cn
cn2
2
1-49
Design and Analysis of Algorithms – Unit I
Order of growth
Order of growth
 Most important: Order of growth within a constant multiple
Most important: Order of growth within a constant multiple
as
as n
n→∞
→∞
 Example:
Example:
• How much faster will algorithm run on computer that is
How much faster will algorithm run on computer that is
twice as fast?
twice as fast?
• How much longer does it take to solve problem of double
How much longer does it take to solve problem of double
input size?
input size?
1-50
Design and Analysis of Algorithms – Unit I
Values of some important functions as
Values of some important functions as n
n 
 

1-51
Design and Analysis of Algorithms – Unit I
Asymptotic Notations
 O (Big-Oh)-notation
 Ω (Big-Omega) -notation
 Θ (Big-Theta) -notation
1-52
Design and Analysis of Algorithms – Unit I
Asymptotic order of growth
Asymptotic order of growth
A way of comparing functions that ignores constant factors and
A way of comparing functions that ignores constant factors and
small input sizes
small input sizes (because?)
(because?)
 O(
O(g
g(
(n
n)): class of functions
)): class of functions f
f(
(n
n) that grow
) that grow no faster
no faster than
than g
g(
(n
n)
)
 Θ
Θ(
(g
g(
(n
n)): class of functions
)): class of functions f
f(
(n
n) that grow
) that grow at same rate
at same rate as
as g
g(
(n
n)
)
 Ω
Ω(
(g
g(
(n
n)): class of functions
)): class of functions f
f(
(n
n) that grow
) that grow at least as fast
at least as fast as
as g
g(
(n
n)
)
1-53
Design and Analysis of Algorithms – Unit I
O-notation
O-notation
Definition:
Definition: A function t
A function t(
(n
n) is said to be in O(
) is said to be in O(g
g(
(n
n)), denoted t
)), denoted t(n)
(n)

 O(g(n)) is bounded above by some constant multiple of g(n)
O(g(n)) is bounded above by some constant multiple of g(n)
for all large n,
for all large n, i.e., there exist positive constant
i.e., there exist positive constant c
c and non-
and non-
negative integer
negative integer n
n0
0 such that
such that
f
f(
(n
n)
) ≤
≤ c g
c g(
(n
n) for every
) for every n
n ≥
≥ n
n0
0
1-54
Design and Analysis of Algorithms – Unit I
Big-oh
Big-oh
1-55
Design and Analysis of Algorithms – Unit I

-notation
-notation
 Formal definition
Formal definition
• A function
A function t(n)
t(n) is said to be in
is said to be in 
(g(n)),
(g(n)), denoted
denoted t(n)
t(n) 


(g(n)),
(g(n)), if
if t(n)
t(n) is bounded below by some constant
is bounded below by some constant
multiple of
multiple of g(n)
g(n) for all large
for all large n
n, i.e.,
, i.e., if there exist some
if there exist some
positive constant c and some nonnegative integer
positive constant c and some nonnegative integer n
n0
0
such that
such that
t(n)
t(n) 
 cg(n) for all n
cg(n) for all n 
 n
n0
0
1-56
Design and Analysis of Algorithms – Unit I
Big-omega
Big-omega
1-57
Design and Analysis of Algorithms – Unit I

-notation
-notation
 Formal definition
Formal definition
• A function
A function t(n)
t(n) is said to be in
is said to be in 
(g(n)),
(g(n)), denoted
denoted t(n)
t(n) 


(g(n)),
(g(n)), if
if t(n)
t(n) is bounded both above and below by
is bounded both above and below by
some positive constant multiples of
some positive constant multiples of g(n)
g(n) for all large
for all large
n
n, i.e.,
, i.e., if there exist some positive constant c
if there exist some positive constant c1
1 and c
and c2
2
and some nonnegative integer
and some nonnegative integer n
n0
0 such that
such that
c
c2
2 g(n)
g(n) 
 t(n)
t(n) 
 c
c1
1 g(n) for all n
g(n) for all n 
 n
n0
0
1-58
Design and Analysis of Algorithms – Unit I
Big-theta
Big-theta
1-59
Design and Analysis of Algorithms – Unit I
Theorem
Theorem
 If t1(n)  O(g1(n)) and t2(n)  O(g2(n)), then
t1(n) + t2(n)  O(max{g1(n), g2(n)}).
• The analogous assertions are true for the
The analogous assertions are true for the 
-notation
-notation
and
and 
-notation.
-notation.
Proof. There exist constants c1, c2, n1, n2 such that
t
t1
1(n)
(n) 
 c
c1
1*
*g
g1
1(n),
(n), for all
for all n
n 
 n
n1
1
t
t2
2(n)
(n) 
 c
c2
2*
*g
g2
2(n),
(n), for all
for all n
n 
 n
n2
2
Define
Define c
c3
3 = c
= c1
1 + c
+ c2
2 and
and n
n3
3 = max{n
= max{n1
1,n
,n2
2}. Then
}. Then
t
t1
1(n) + t
(n) + t2
2(n)
(n) 
 c
c3
3*
*max{g
max{g1
1(n), g
(n), g2
2(n)},
(n)}, for all
for all n
n 
 n
n3
3
1-60
Design and Analysis of Algorithms – Unit I
Some properties of asymptotic order of growth
Some properties of asymptotic order of growth
 f
f(
(n
n)
) 
 O(
O(f
f(
(n
n))
))
 f
f(
(n
n)
) 
 O(
O(g
g(
(n
n)) iff
)) iff g
g(
(n
n)
) 
(
(f
f(n))
(n))
 If
If f
f (
(n
n)
) 
 O(
O(g
g (
(n
n)) and
)) and g
g(
(n
n)
) 
 O(
O(h
h(
(n
n)) , then
)) , then f
f(
(n
n)
) 
 O(
O(h
h(
(n
n))
))
Note similarity with
Note similarity with a
a ≤
≤ b
b
 If
If f
f1
1(
(n
n)
) 
 O(
O(g
g1
1(
(n
n)) and
)) and f
f2
2(
(n
n)
) 
 O(
O(g
g2
2(
(n
n)) , then
)) , then
f
f1
1(
(n
n) +
) + f
f2
2(
(n
n)
) 
 O(max{
O(max{g
g1
1(
(n
n),
), g
g2
2(
(n
n)})
)})
Also,
Also, 
1
1
i
i
n
n 
(
(f
f(
(i
i)) =
)) = 
 (
(
1
1
i
i
n
n f
f(
(i
i))
))
1-61
Design and Analysis of Algorithms – Unit I
Establishing order of growth using limits
Establishing order of growth using limits
lim
lim T
T(
(n
n)/
)/g
g(
(n
n) =
) =
0
0 order of growth of T
T(
(n)
n) < order of growth of g
g(
(n
n)
)
c
c > 0
> 0 order of growth of T
T(
(n)
n) = order of growth of g
g(
(n
n)
)
∞
∞ order of growth of T
T(
(n)
n) > order of growth of g
g(
(n
n)
)
n
n→∞
→∞
1-62
Design and Analysis of Algorithms – Unit I
L’Hôpital’s rule and Stirling’s formula
L’Hôpital’s rule and Stirling’s formula
L’Hôpital’s rule: If
L’Hôpital’s rule: If lim
limn
n
 f
f(
(n
n) =
) = lim
limn
n
 g(n
g(n) =
) = 
 and
and
the derivatives
the derivatives f
f´,
´, g
g´ exist, then
´ exist, then
Stirling’s formula:
Stirling’s formula: n
n!
! 
 (2
(2
n
n)
)1/2
1/2
(
(n
n/e)
/e)n
n
f
f(
(n
n)
)
g
g(
(n
n)
)
lim
lim
n
n

=
f
f ´(
´(n
n)
)
g
g ´(
´(n
n)
)
lim
lim
n
n

Example: log
Example: log n
n vs.
vs. n
n
Example: 2
Example: 2n
n
vs.
vs. n
n!
!
1-63
Design and Analysis of Algorithms – Unit I
Orders of growth of some important functions
Orders of growth of some important functions
 All logarithmic functions log
All logarithmic functions loga
a n
n belong to the same class
belong to the same class

(log
(log n
n)
) no matter what the logarithm’s base
no matter what the logarithm’s base a
a > 1 is
> 1 is
because
because
 All polynomials of the same degree
All polynomials of the same degree k
k belong to the same class:
belong to the same class:
a
ak
kn
nk
k
+
+ a
ak
k-1
-1n
nk
k-1
-1
+ … +
+ … + a
a0
0 
 
(
(n
nk
k
)
)
 Exponential functions
Exponential functions a
an
n
have different orders of growth for different
have different orders of growth for different
a
a’s
’s
 order
order log
log n <
n < order
order n
n

(
(
>0) < order
>0) < order a
an
n
< order
< order n
n! < order
! < order n
nn
n
a
n
n b
b
a log
/
log
log 
1-64
Design and Analysis of Algorithms – Unit I
Basic asymptotic efficiency classes
Basic asymptotic efficiency classes
1
1 constant
constant
log
log n
n logarithmic
logarithmic
n
n linear
linear
n
n log
log n
n n-
n-log
log-n
-n
n
n2
2
quadratic
quadratic
n
n3
3
cubic
cubic
2
2n
n
exponential
exponential
n
n!
! factorial
factorial
1-65
Design and Analysis of Algorithms – Unit I
Plan for analyzing nonrecursive algorithms
Plan for analyzing nonrecursive algorithms
General Plan for Analysis
General Plan for Analysis
 Decide on parameter
Decide on parameter n
n indicating
indicating input size
input size
 Identify algorithm’s
Identify algorithm’s basiyc operation
basiyc operation
 Determine
Determine worst
worst,
, average
average, and
, and best
best cases for input of size
cases for input of size n
n
 Set up a sum for the number of times the basic operation is
Set up a sum for the number of times the basic operation is
executed
executed
 Simplify the sum using standard formulas and rules
Simplify the sum using standard formulas and rules
1-66
Design and Analysis of Algorithms – Unit I
Useful summation formulas and rules
Useful summation formulas and rules

l
l
i
i
n
n1 = 1+1+…+1 =
1 = 1+1+…+1 = n
n -
- l
l + 1
+ 1
In particular,
In particular, 
l
l
i
i
n
n1 =
1 = n
n - 1 + 1 =
- 1 + 1 = n
n 
 
(
(n
n)
)

1
1
i
i
n
n i
i = 1+2+…+
= 1+2+…+n
n =
= n
n(
(n
n+1)/2
+1)/2 
 n
n2
2
/2
/2 
 
(
(n
n2
2
)
)

1
1
i
i
n
n i
i2
2
= 1
= 12
2
+2
+22
2
+…+
+…+n
n2
2
=
= n
n(
(n
n+1)(2
+1)(2n
n+1)/6
+1)/6 
 n
n3
3
/3
/3 
 
(
(n
n3
3
)
)

0
0
i
i
n
n a
ai
i
= 1
= 1 +
+ a
a +…+
+…+ a
an
n
= (
= (a
an
n+1
+1
- 1)/(
- 1)/(a
a - 1) for any
- 1) for any a
a 
 1
1
In particular,
In particular, 
0
0
i
i
n
n 2
2i
i
= 2
= 20
0
+ 2
+ 21
1
+…+ 2
+…+ 2n
n
= 2
= 2n
n+1
+1
- 1
- 1 
 
(2
(2n
n
)
)

(
(a
ai
i ±
± b
bi
i ) =
) = 
a
ai
i ±
± 
b
bi
i 
ca
cai
i =
= c
c
a
ai
i 
l
l
i
i
u
ua
ai
i =
= 
l
l
i
i
m
ma
ai
i +
+ 
m
m+1
+1
i
i
u
ua
ai
i
1-67
Design and Analysis of Algorithms – Unit I
Example 1: Maximum element
Example 1: Maximum element
T(n) = 
1
1
i
i
n-1
n-1 1 = n-1 =
1 = n-1 = 
(
(n
n)
) comparisons
1-68
Design and Analysis of Algorithms – Unit I
Example 2: Element uniqueness problem
Example 2: Element uniqueness problem
T(n) = 
0
0
i
i
n-2
n-2 (
(
i+1
i+1
j
j
n-1
n-1 1)
=
= 
0
0
i
i
n-2
n-2 n-i-1 = (n-1+1)(n-1)/2
= 
( )
( ) comparisons
2
n
1-69
Design and Analysis of Algorithms – Unit I
Example 3: Matrix multiplication
Example 3: Matrix multiplication
T(n) = 
0
0
i
i
n-1
n-1 
0
0
i
i
n-1
n-1 n
= 
0
0
i
i
n-1
n-1 
(
( )
= 
(
( ) multiplications
2
n
3
n
1-70
Design and Analysis of Algorithms – Unit I
Example 4: Gaussian elimination
Example 4: Gaussian elimination
Algorithm
Algorithm GaussianElimination
GaussianElimination(
(A
A[0..
[0..n
n-
-1,0..
1,0..n
n])
])
//Implements Gaussian elimination on an
//Implements Gaussian elimination on an n-
n-by
by-
-(
(n
n+1) matrix
+1) matrix A
A
for
for i
i 
 0
0 to
to n
n -
- 2
2 do
do
for
for j
j 
 i
i + 1
+ 1 to
to n
n -
- 1
1 do
do
for
for k
k 
 i
i to
to n
n do
do
A
A[
[j
j,
,k
k]
] 
 A
A[
[j
j,
,k
k]
] -
- A
A[
[i
i,
,k
k]
] 
 A
A[
[j
j,
,i
i] /
] / A
A[
[i
i,
,i
i]
]
Find the efficiency class and a constant factor improvement.
Find the efficiency class and a constant factor improvement.
for
for i
i 
 0
0 to
to n
n -
- 2
2 do
do
for
for j
j 
 i
i + 1
+ 1 to
to n
n -
- 1
1 do
do
B
B 
 A
A[
[j,i
j,i] /
] / A
A[
[i
i,
,i
i]
]
for
for k
k 
 i
i to
to n
n do
do
A
A[
[j
j,
,k
k]
] 
 A
A[
[j
j,
,k
k]
] – A
– A[
[i,k
i,k]
] * B
* B
1-71
Design and Analysis of Algorithms – Unit I
Example 5: Counting binary digits
Example 5: Counting binary digits
1-72
Design and Analysis of Algorithms – Unit I
Plan for Analysis of Recursive Algorithms
Plan for Analysis of Recursive Algorithms
 Decide on a parameter indicating an input’s size.
Decide on a parameter indicating an input’s size.
 Identify the algorithm’s basic operation.
Identify the algorithm’s basic operation.
 Check whether the number of times the basic op. is executed
Check whether the number of times the basic op. is executed
may vary on different inputs of the same size. (If it may, the
may vary on different inputs of the same size. (If it may, the
worst, average, and best cases must be investigated separately.)
worst, average, and best cases must be investigated separately.)
 Set up a recurrence relation with an appropriate initial
Set up a recurrence relation with an appropriate initial
condition expressing the number of times the basic op. is
condition expressing the number of times the basic op. is
executed.
executed.
 Solve the recurrence (or, at the very least, establish its
Solve the recurrence (or, at the very least, establish its
solution’s order of growth) by backward substitutions or
solution’s order of growth) by backward substitutions or
another method.
another method.
1-73
Design and Analysis of Algorithms – Unit I
Example 1: Recursive evaluation of
Example 1: Recursive evaluation of n
n!
!
Definition:
Definition: n
n ! = 1
! = 1 
 2
2 
 …
… 
(
(n-
n-1)
1) 
 n
n for
for n
n ≥
≥ 1 and 0! = 1
1 and 0! = 1
Recursive definition of
Recursive definition of n
n!:
!: F
F(
(n
n) =
) = F
F(
(n-
n-1)
1) 
 n
n for
for n
n ≥
≥ 1 and
1 and
F
F(
(0) = 1
0) = 1
Size:
Size:
Basic operation:
Basic operation:
Recurrence relation:
Recurrence relation:
n
multiplication
M(n) = M(n-1) + 1
M(0) = 0
1-74
Design and Analysis of Algorithms – Unit I
Solving the recurrence for M(
Solving the recurrence for M(n
n)
)
M(
M(n
n) = M(
) = M(n
n-1) + 1, M(0) = 0
-1) + 1, M(0) = 0
M(n) = M(n-1) + 1
= (M(n-2) + 1) + 1 = M(n-2) + 2
= (M(n-3) + 1) + 2 = M(n-3) + 3
…
= M(n-i) + i
= M(0) + n
= n
The method is called backward substitution.
1-75
Design and Analysis of Algorithms – Unit I
Example 2: The Tower of Hanoi Puzzle
Example 2: The Tower of Hanoi Puzzle
1
2
3
Recurrence for number of moves:
Recurrence for number of moves: M(n) = 2M(n-1) + 1
1-76
Design and Analysis of Algorithms – Unit I
Solving recurrence for number of moves
Solving recurrence for number of moves
M(
M(n
n) = 2M(
) = 2M(n
n-1) + 1, M(1) = 1
-1) + 1, M(1) = 1
M(n) = 2M(n-1) + 1
= 2(2M(n-2) + 1) + 1 = 2^2*M(n-2) + 2^1 + 2^0
= 2^2*(2M(n-3) + 1) + 2^1 + 2^0
= 2^3*M(n-3) + 2^2 + 2^1 + 2^0
= …
= 2^(n-1)*M(1) + 2^(n-2) + … + 2^1 + 2^0
= 2^(n-1) + 2^(n-2) + … + 2^1 + 2^0
= 2^n - 1
1-77
Design and Analysis of Algorithms – Unit I
Tree of calls for the Tower of Hanoi Puzzle
Tree of calls for the Tower of Hanoi Puzzle
n
n-1 n-1
n-2 n-2 n-2 n-2
1 1
... ... ...
2
1 1
2
1 1
2
1 1
2
1-78
Design and Analysis of Algorithms – Unit I
Example 3: Counting #bits
Example 3: Counting #bits
A( ) = A( ) + 1, A( ) = 1 (using the Smoothness Rule)
= (A( ) + 1) + 1 = A( ) + 2
= A( ) + i
= A( ) + k = k + 0
=
k
2 1
2 
k 0
2
2
2 
k
n
2
log
2
2 
k
i
k
2
k
k
2
A(n) = A( ) + 1, A(1) = 0
 
2
/
n

DAA_UnitIDAA_UnitIDAA_UnitIDAA_UnitIDAA_UnitIDAA_UnitI.ppt

  • 1.
    1-1 Design and Analysisof Algorithms – Unit I Chapter 1 Chapter 1 Introduction Introduction
  • 2.
    1-2 Design and Analysisof Algorithms – Unit I Algorithm  An Algorithm is a sequence of unambiguous instructions for solving a problem,  i.e., for obtaining a required output for any legitimate input in a finite amount of time.
  • 3.
    1-3 Design and Analysisof Algorithms – Unit I Notion of algorithm “computer” Algorithmic solution problem algorithm input output
  • 4.
    1-4 Design and Analysisof Algorithms – Unit I PSEUDOCODE  Pseudocode (pronounced SOO-doh-kohd) is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language rather than in a programming language.  It is sometimes used as a detailed step in the process of developing a program.  It allows programmers to express the design in great detail and provides programmers a detailed template for the next step of writing code in a specific programming language.
  • 5.
    1-5 Design and Analysisof Algorithms – Unit I Formatting and Conventions in Pseudocoding  INDENTATION in pseudocode should be identical to its implementation in a programming language. Try to indent at least four spaces.  The pseudocode entries are to be cryptic, AND SHOULD NOT BE PROSE. NO SENTENCES.  No flower boxes in pseudocode.  Do not include data declarations in pseudocode.
  • 6.
    1-6 Design and Analysisof Algorithms – Unit I Some Keywords That Should be Used  For looping and selection, • Do While...EndDo; • Do Until...Enddo; • Case...EndCase; • If...Endif; • Call ... with (parameters); Call; Return ....; Return; When; Always use scope terminators for loops and iteration.
  • 7.
    1-7 Design and Analysisof Algorithms – Unit I Some Keywords …  As verbs, use the words • generate, Compute, Process, • Set, reset, • increment, • calculate, • add, sum, multiply, ... • print, display, • input, output, edit, test , etc.
  • 8.
    1-8 Design and Analysisof Algorithms – Unit I Methods of finding GCD M - 1 M - 2 M - 3
  • 9.
    1-9 Design and Analysisof Algorithms – Unit I Euclid’s Algorithm Problem: Find gcd(m,n), the greatest common divisor of two nonnegative, not both zero integers m and n Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ? Euclid’s algorithm is based on repeated application of equality gcd(m,n) = gcd(n, m mod n) until the second number becomes 0, which makes the problem trivial. Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
  • 10.
    1-10 Design and Analysisof Algorithms – Unit I Two descriptions of Euclid’s algorithm Step 1 If n = 0, return m and stop; otherwise go to Step 2 Step 2 Divide m by n and assign the value fo the remainder to r Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. while n ≠ 0 do r ← m mod n m← n n ← r return m
  • 11.
    1-11 Design and Analysisof Algorithms – Unit I Other methods for computing gcd(m,n) Consecutive integer checking algorithm Step 1 Assign the value of min{m,n} to t Step 2 Divide m by t. If the remainder is 0, go to Step 3; otherwise, go to Step 4 Step 3 Divide n by t. If the remainder is 0, return t and stop; otherwise, go to Step 4 Step 4 Decrease t by 1 and go to Step 2
  • 12.
    1-12 Design and Analysisof Algorithms – Unit I Other methods for gcd(m,n) [cont.] Middle-school procedure Step 1 Find the prime factorization of m Step 2 Find the prime factorization of n Step 3 Find all the common prime factors Step 4 Compute the product of all the common prime factors and return it as gcd(m,n) Is this an algorithm?
  • 13.
    1-13 Design and Analysisof Algorithms – Unit I Sieve of Eratosthenes Input: Integer n ≥ 2 Output: List of primes less than or equal to n for p ← 2 to n do A[p] ← p for p ← 2 to n do if A[p]  0 //p hasn’t been previously eliminated from the list j ← p* p while j ≤ n do A[j] ← 0 //mark element as eliminated j ← j + p Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  • 14.
    1-14 Design and Analysisof Algorithms – Unit I Termination of Euclid’s Algorithm  The second number of the pair gets smaller with each iteration and cannot become negative:  Indeed, the new value of n is r = m mod n, which is always smaller than n.  Eventually, r becomes zero, and the algorithms stops.
  • 15.
    1-15 Design and Analysisof Algorithms – Unit I Compute the GCD of 120 and 23.
  • 16.
    1-16 Design and Analysisof Algorithms – Unit I Fundamentals of Algorithmic Problem Solving
  • 17.
    1-17 Design and Analysisof Algorithms – Unit I Fundamentals of Algorithmic Problem Solving  Algorithm = Procedural Solutions to Problem  NOT an answer, BUT rather specific instructions of getting answers.  Therefore, requires steps in designing and analyzing an algorithm
  • 18.
    1-18 Design and Analysisof Algorithms – Unit I Algorithm Design & Analysis Process
  • 19.
    1-19 Design and Analysisof Algorithms – Unit I Step 1: Understand the Problem  Before designing an algorithm - understand completely the problem given.  Read the problem’s description carefully and ask questions if you have any doubts about the problem,  Do a few small examples by hand, think about special cases, and ask questions again if needed.
  • 20.
    1-20 Design and Analysisof Algorithms – Unit I  An input to an algorithm specifies an instance of the problem the algorithm solves.  It is very important to specify exactly the range of instances the algorithm needs to handle.  Failing which – the algorithm works correctly for some inputs , but crashes on some boundary values.  Remember that a correct algorithm is not one that works most of the time but one that works correctly for all legitimate inputs. Step 1: Understand the Problem
  • 21.
    1-21 Design and Analysisof Algorithms – Unit I Step 2: Ascertaining the capabilities of a computational device  Algorithms designed to be executed on machines that executes intstructions one after another are called sequential algorithms.  Algorithms that take advantage of computers that can execute operations concurrently are called parallel algorithms.
  • 22.
    1-22 Design and Analysisof Algorithms – Unit I Step 3: Choosing between Exact & Approximate Problem Solving  Solving the problem exactly - Exact algorithms  Solving the problem approximately - Approximation algorithms  Why approximation algorithms? 1. Problems cannot be solved exactly. Eg. Extracting square roots, solving non-linear equations 2. Available exact algorithms are unacceptably slow because of problem’s complexity Eg. Traveling Salesman Problem 3. Approx. Algs can be a part of algorithms that solve the problem exactly.
  • 23.
    1-23 Design and Analysisof Algorithms – Unit I Step 4: Deciding on Appropriate Data Structures  In the new world of object-oriented programming, data structures remain important for both design and analysis of algorithms.  However, we will assume a very basic data structure for now and concentrate on the algorithm side.
  • 24.
    1-24 Design and Analysisof Algorithms – Unit I Step 5: Algorithm Design Techniques  An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing.  Eg. Brute force, Divide-and-Conquer, Transform-and- Conquer  Importance: 1. Provide guidance for designing algorithms for new problems. 2. To classify algorithms according to an underlying design idea.
  • 25.
    1-25 Design and Analysisof Algorithms – Unit I Step 6: Methods of Specifying an Algorithm  Pseudocode, Pseudocode, a mixture of a natural language and a mixture of a natural language and programming language-like constructs. programming language-like constructs.  flowchart flowchart, a method of expressing an algorithm by a , a method of expressing an algorithm by a collection of connected geometric shapes containing collection of connected geometric shapes containing descriptions of the algorithm’s steps. descriptions of the algorithm’s steps.
  • 26.
    1-26 Design and Analysisof Algorithms – Unit I Step 7: Proving an Algorithm’s Correctness  Prove algorithm’s correctness = prove that the algorithm yields a required result for every legitimate input in a finite amount of time.  For an approximation algorithm, correctness means to be able to show that the error produced by the algorithm does not exceed a predefined limit.
  • 27.
    1-27 Design and Analysisof Algorithms – Unit I Step 8: Analyzing an Algorithm 1. Efficiency Time efficiency indicates how fast the algorithm runs. space efficiency indicates how much extra memory the algorithm needs. 2. Simplicity 3. Generality Design an algorithm for a problem posed in more general terms. Design an algorithm that can handle a range of inputs that is natural for the problem at hand.
  • 28.
    1-28 Design and Analysisof Algorithms – Unit I Step 9: Coding the algorithm  More than implementation More than implementation  Peril of incorrect & inefficient implementation Peril of incorrect & inefficient implementation  Require testing & debugging Require testing & debugging  Require code optimizing Require code optimizing
  • 29.
    1-29 Design and Analysisof Algorithms – Unit I Important Problem Types
  • 30.
    1-30 Design and Analysisof Algorithms – Unit I Important Problem Types  Sorting Sorting  Searching Searching  String processing String processing  Graph problems Graph problems  Combinatorial problems Combinatorial problems  Geometric problems Geometric problems  Numerical problems Numerical problems
  • 31.
    1-31 Design and Analysisof Algorithms – Unit I Sorting  The The sorting problem sorting problem asks us to rearrange the items of a asks us to rearrange the items of a given list in ascending order. given list in ascending order.  we usually need to we usually need to sort lists of numbers, sort lists of numbers, characters from an alphabet, characters from an alphabet, character strings, character strings,  records similar to those maintained by schools about records similar to those maintained by schools about their students, their students, libraries about their holdings, libraries about their holdings, companies about their employees. companies about their employees.
  • 32.
    1-32 Design and Analysisof Algorithms – Unit I Searching  The The searching problem searching problem deals with finding a given value, deals with finding a given value, called a called a search key search key, in a given set (or a multiset, which , in a given set (or a multiset, which permits several elements to have the same value). permits several elements to have the same value).
  • 33.
    1-33 Design and Analysisof Algorithms – Unit I String Processing  A A string string is a sequence of characters from an alphabet. is a sequence of characters from an alphabet.  String of particular interest: String of particular interest: 1. Text string – comprises letters, numbers, and 1. Text string – comprises letters, numbers, and special characters special characters 2. Bit string – comprises zeros and ones 2. Bit string – comprises zeros and ones 3. Gene sequence 3. Gene sequence  Mainly Mainly string matching problem string matching problem: searching for a given : searching for a given word in a text word in a text
  • 34.
    1-34 Design and Analysisof Algorithms – Unit I Graph Problems  A A graph graph can be thought of as a collection of points called can be thought of as a collection of points called vertices, some of which are connected by line segments vertices, some of which are connected by line segments called edges. called edges.  Used for modeling a wide variety of real-life applications. Used for modeling a wide variety of real-life applications.  Basic graph algorithms include: Basic graph algorithms include: 1. 1. Graph traversal algorithms Graph traversal algorithms - How can one visit all - How can one visit all the the points in a network? points in a network? 2. 2. Shortest-path algorithms Shortest-path algorithms - What is the best - What is the best Introduction route between two cities? Introduction route between two cities? 3. 3. Topological sorting Topological sorting for graphs with directed edges for graphs with directed edges
  • 35.
    1-35 Design and Analysisof Algorithms – Unit I Combinatorial Problems  combinatorial problems: combinatorial problems: problems that ask (explicitly or problems that ask (explicitly or implicitly) to find a combinatorial object—such as a implicitly) to find a combinatorial object—such as a permutation, a combination, or a subset—that satisfies permutation, a combination, or a subset—that satisfies certain constraints and has some desired property (e.g., certain constraints and has some desired property (e.g., maximizes a value or minimizes a cost). maximizes a value or minimizes a cost). 1. Combinatorial grows extremely fast with problem size 1. Combinatorial grows extremely fast with problem size 2. No known algorithm solving most such problems 2. No known algorithm solving most such problems exactly in an acceptable amount of time. exactly in an acceptable amount of time.
  • 36.
    1-36 Design and Analysisof Algorithms – Unit I Geometric Problems  Geometric algorithms Geometric algorithms deal with geometric objects such as deal with geometric objects such as points, lines, and polygons. points, lines, and polygons. 2 class problems: 2 class problems:  The The closest pair problem closest pair problem: given : given n n points in the plane, find points in the plane, find the closest pair among them. the closest pair among them.  The The convex hull problem convex hull problem asks to find the smallest convex asks to find the smallest convex polygon that would include all the points of a given set. If polygon that would include all the points of a given set. If
  • 37.
    1-37 Design and Analysisof Algorithms – Unit I Convex hull problem
  • 38.
    1-38 Design and Analysisof Algorithms – Unit I Numerical Problems  Numerical problems Numerical problems, another large special area of , another large special area of applications, are problems that involve mathematical applications, are problems that involve mathematical objects of continuous nature: solving equations and systems objects of continuous nature: solving equations and systems of equations, computing definite integrals, evaluating of equations, computing definite integrals, evaluating functions, and so on. functions, and so on.
  • 39.
    1-39 Design and Analysisof Algorithms – Unit I Fundamentals of Analysis of algorithm efficiency
  • 40.
    1-40 Design and Analysisof Algorithms – Unit I Analysis of algorithms Analysis of algorithms  Issues: Issues: • correctness correctness • time efficiency time efficiency • space efficiency space efficiency • optimality optimality  Approaches: Approaches: • theoretical analysis theoretical analysis • empirical analysis empirical analysis
  • 41.
    1-41 Design and Analysisof Algorithms – Unit I Theoretical analysis of time efficiency Theoretical analysis of time efficiency Time efficiency is analyzed by determining the number of Time efficiency is analyzed by determining the number of repetitions of the repetitions of the basic operation basic operation as a function of as a function of input size input size  Basic operation Basic operation: the operation that contributes the most : the operation that contributes the most towards the running time of the algorithm towards the running time of the algorithm T T( (n n) ) ≈ ≈ c cop opC C( (n n) ) running time execution time for basic operation or cost Number of times basic operation is executed input size Note: Different basic operations may cost differently!
  • 42.
    1-42 Design and Analysisof Algorithms – Unit I Input size and basic operation examples Input size and basic operation examples Problem Input size measure Basic operation Searching for key in a list of n items Number of list’s items, i.e. n Key comparison Multiplication of two matrices Matrix dimensions or total number of elements Multiplication of two numbers Checking primality of a given integer n n’size = number of digits (in binary representation) Division Typical graph problem #vertices and/or edges Visiting a vertex or traversing an edge
  • 43.
    1-43 Design and Analysisof Algorithms – Unit I Empirical analysis of time efficiency Empirical analysis of time efficiency  Select a specific (typical) sample of inputs Select a specific (typical) sample of inputs  Use physical unit of time (e.g., milliseconds) Use physical unit of time (e.g., milliseconds) or or Count actual number of basic operation’s executions Count actual number of basic operation’s executions  Analyze the empirical data Analyze the empirical data
  • 44.
    1-44 Design and Analysisof Algorithms – Unit I Efficiencies  Worst Case Efficiency: • Is its efficiency for the worst case input of size n, which is an input of size n for which the algorithm runs the longest among all possible inputs of that size • C Cworst worst( (n n) )  Best-case efficiency: Best-case efficiency: • Is its efficiency for the worst case input of size n, which is an input of size n for which the algorithm runs the fastest among all possible inputs of that size • C Cbest best( (n n) )
  • 45.
    1-45 Design and Analysisof Algorithms – Unit I Amortized efficiency • It applies not to a single run of an algorithm, but rather to a sequence of operations performed on the same data structure
  • 46.
    1-46 Design and Analysisof Algorithms – Unit I Best-case, average-case, worst-case Best-case, average-case, worst-case For some algorithms, efficiency depends on form of input: For some algorithms, efficiency depends on form of input:  Worst case: C Worst case: Cworst worst( (n n) – maximum over inputs of size ) – maximum over inputs of size n n  Best case: C Best case: Cbest best( (n n) – minimum over inputs of size ) – minimum over inputs of size n n  Average case: C Average case: Cavg avg( (n n) – “average” over inputs of size ) – “average” over inputs of size n n • Number of times the basic operation will be executed on typical Number of times the basic operation will be executed on typical input input • NOT the average of worst and best case NOT the average of worst and best case • Expected number of basic operations considered as a random Expected number of basic operations considered as a random variable under some assumption about the probability variable under some assumption about the probability distribution of all possible inputs. So, avg = expected under distribution of all possible inputs. So, avg = expected under uniform distribution. uniform distribution.
  • 47.
    1-47 Design and Analysisof Algorithms – Unit I Example: Sequential search Example: Sequential search  Worst case Worst case  Best case Best case  Average case Average case n key comparisons 1 comparisons (n+1)/2, assuming K is in A
  • 48.
    1-48 Design and Analysisof Algorithms – Unit I Types of formulas for basic operation’s count Types of formulas for basic operation’s count  Exact formula Exact formula e.g., C( e.g., C(n n) = ) = n n( (n n-1)/2 -1)/2  Formula indicating order of growth with specific Formula indicating order of growth with specific multiplicative constant multiplicative constant e.g., C( e.g., C(n n) ) ≈ ≈ 0.5 0.5 n n2 2  Formula indicating order of growth with unknown Formula indicating order of growth with unknown multiplicative constant multiplicative constant e.g., C( e.g., C(n n) ) ≈ ≈ cn cn2 2
  • 49.
    1-49 Design and Analysisof Algorithms – Unit I Order of growth Order of growth  Most important: Order of growth within a constant multiple Most important: Order of growth within a constant multiple as as n n→∞ →∞  Example: Example: • How much faster will algorithm run on computer that is How much faster will algorithm run on computer that is twice as fast? twice as fast? • How much longer does it take to solve problem of double How much longer does it take to solve problem of double input size? input size?
  • 50.
    1-50 Design and Analysisof Algorithms – Unit I Values of some important functions as Values of some important functions as n n    
  • 51.
    1-51 Design and Analysisof Algorithms – Unit I Asymptotic Notations  O (Big-Oh)-notation  Ω (Big-Omega) -notation  Θ (Big-Theta) -notation
  • 52.
    1-52 Design and Analysisof Algorithms – Unit I Asymptotic order of growth Asymptotic order of growth A way of comparing functions that ignores constant factors and A way of comparing functions that ignores constant factors and small input sizes small input sizes (because?) (because?)  O( O(g g( (n n)): class of functions )): class of functions f f( (n n) that grow ) that grow no faster no faster than than g g( (n n) )  Θ Θ( (g g( (n n)): class of functions )): class of functions f f( (n n) that grow ) that grow at same rate at same rate as as g g( (n n) )  Ω Ω( (g g( (n n)): class of functions )): class of functions f f( (n n) that grow ) that grow at least as fast at least as fast as as g g( (n n) )
  • 53.
    1-53 Design and Analysisof Algorithms – Unit I O-notation O-notation Definition: Definition: A function t A function t( (n n) is said to be in O( ) is said to be in O(g g( (n n)), denoted t )), denoted t(n) (n)   O(g(n)) is bounded above by some constant multiple of g(n) O(g(n)) is bounded above by some constant multiple of g(n) for all large n, for all large n, i.e., there exist positive constant i.e., there exist positive constant c c and non- and non- negative integer negative integer n n0 0 such that such that f f( (n n) ) ≤ ≤ c g c g( (n n) for every ) for every n n ≥ ≥ n n0 0
  • 54.
    1-54 Design and Analysisof Algorithms – Unit I Big-oh Big-oh
  • 55.
    1-55 Design and Analysisof Algorithms – Unit I  -notation -notation  Formal definition Formal definition • A function A function t(n) t(n) is said to be in is said to be in  (g(n)), (g(n)), denoted denoted t(n) t(n)    (g(n)), (g(n)), if if t(n) t(n) is bounded below by some constant is bounded below by some constant multiple of multiple of g(n) g(n) for all large for all large n n, i.e., , i.e., if there exist some if there exist some positive constant c and some nonnegative integer positive constant c and some nonnegative integer n n0 0 such that such that t(n) t(n)   cg(n) for all n cg(n) for all n   n n0 0
  • 56.
    1-56 Design and Analysisof Algorithms – Unit I Big-omega Big-omega
  • 57.
    1-57 Design and Analysisof Algorithms – Unit I  -notation -notation  Formal definition Formal definition • A function A function t(n) t(n) is said to be in is said to be in  (g(n)), (g(n)), denoted denoted t(n) t(n)    (g(n)), (g(n)), if if t(n) t(n) is bounded both above and below by is bounded both above and below by some positive constant multiples of some positive constant multiples of g(n) g(n) for all large for all large n n, i.e., , i.e., if there exist some positive constant c if there exist some positive constant c1 1 and c and c2 2 and some nonnegative integer and some nonnegative integer n n0 0 such that such that c c2 2 g(n) g(n)   t(n) t(n)   c c1 1 g(n) for all n g(n) for all n   n n0 0
  • 58.
    1-58 Design and Analysisof Algorithms – Unit I Big-theta Big-theta
  • 59.
    1-59 Design and Analysisof Algorithms – Unit I Theorem Theorem  If t1(n)  O(g1(n)) and t2(n)  O(g2(n)), then t1(n) + t2(n)  O(max{g1(n), g2(n)}). • The analogous assertions are true for the The analogous assertions are true for the  -notation -notation and and  -notation. -notation. Proof. There exist constants c1, c2, n1, n2 such that t t1 1(n) (n)   c c1 1* *g g1 1(n), (n), for all for all n n   n n1 1 t t2 2(n) (n)   c c2 2* *g g2 2(n), (n), for all for all n n   n n2 2 Define Define c c3 3 = c = c1 1 + c + c2 2 and and n n3 3 = max{n = max{n1 1,n ,n2 2}. Then }. Then t t1 1(n) + t (n) + t2 2(n) (n)   c c3 3* *max{g max{g1 1(n), g (n), g2 2(n)}, (n)}, for all for all n n   n n3 3
  • 60.
    1-60 Design and Analysisof Algorithms – Unit I Some properties of asymptotic order of growth Some properties of asymptotic order of growth  f f( (n n) )   O( O(f f( (n n)) ))  f f( (n n) )   O( O(g g( (n n)) iff )) iff g g( (n n) )  ( (f f(n)) (n))  If If f f ( (n n) )   O( O(g g ( (n n)) and )) and g g( (n n) )   O( O(h h( (n n)) , then )) , then f f( (n n) )   O( O(h h( (n n)) )) Note similarity with Note similarity with a a ≤ ≤ b b  If If f f1 1( (n n) )   O( O(g g1 1( (n n)) and )) and f f2 2( (n n) )   O( O(g g2 2( (n n)) , then )) , then f f1 1( (n n) + ) + f f2 2( (n n) )   O(max{ O(max{g g1 1( (n n), ), g g2 2( (n n)}) )}) Also, Also,  1 1 i i n n  ( (f f( (i i)) = )) =   ( ( 1 1 i i n n f f( (i i)) ))
  • 61.
    1-61 Design and Analysisof Algorithms – Unit I Establishing order of growth using limits Establishing order of growth using limits lim lim T T( (n n)/ )/g g( (n n) = ) = 0 0 order of growth of T T( (n) n) < order of growth of g g( (n n) ) c c > 0 > 0 order of growth of T T( (n) n) = order of growth of g g( (n n) ) ∞ ∞ order of growth of T T( (n) n) > order of growth of g g( (n n) ) n n→∞ →∞
  • 62.
    1-62 Design and Analysisof Algorithms – Unit I L’Hôpital’s rule and Stirling’s formula L’Hôpital’s rule and Stirling’s formula L’Hôpital’s rule: If L’Hôpital’s rule: If lim limn n  f f( (n n) = ) = lim limn n  g(n g(n) = ) =   and and the derivatives the derivatives f f´, ´, g g´ exist, then ´ exist, then Stirling’s formula: Stirling’s formula: n n! !   (2 (2 n n) )1/2 1/2 ( (n n/e) /e)n n f f( (n n) ) g g( (n n) ) lim lim n n  = f f ´( ´(n n) ) g g ´( ´(n n) ) lim lim n n  Example: log Example: log n n vs. vs. n n Example: 2 Example: 2n n vs. vs. n n! !
  • 63.
    1-63 Design and Analysisof Algorithms – Unit I Orders of growth of some important functions Orders of growth of some important functions  All logarithmic functions log All logarithmic functions loga a n n belong to the same class belong to the same class  (log (log n n) ) no matter what the logarithm’s base no matter what the logarithm’s base a a > 1 is > 1 is because because  All polynomials of the same degree All polynomials of the same degree k k belong to the same class: belong to the same class: a ak kn nk k + + a ak k-1 -1n nk k-1 -1 + … + + … + a a0 0    ( (n nk k ) )  Exponential functions Exponential functions a an n have different orders of growth for different have different orders of growth for different a a’s ’s  order order log log n < n < order order n n  ( ( >0) < order >0) < order a an n < order < order n n! < order ! < order n nn n a n n b b a log / log log 
  • 64.
    1-64 Design and Analysisof Algorithms – Unit I Basic asymptotic efficiency classes Basic asymptotic efficiency classes 1 1 constant constant log log n n logarithmic logarithmic n n linear linear n n log log n n n- n-log log-n -n n n2 2 quadratic quadratic n n3 3 cubic cubic 2 2n n exponential exponential n n! ! factorial factorial
  • 65.
    1-65 Design and Analysisof Algorithms – Unit I Plan for analyzing nonrecursive algorithms Plan for analyzing nonrecursive algorithms General Plan for Analysis General Plan for Analysis  Decide on parameter Decide on parameter n n indicating indicating input size input size  Identify algorithm’s Identify algorithm’s basiyc operation basiyc operation  Determine Determine worst worst, , average average, and , and best best cases for input of size cases for input of size n n  Set up a sum for the number of times the basic operation is Set up a sum for the number of times the basic operation is executed executed  Simplify the sum using standard formulas and rules Simplify the sum using standard formulas and rules
  • 66.
    1-66 Design and Analysisof Algorithms – Unit I Useful summation formulas and rules Useful summation formulas and rules  l l i i n n1 = 1+1+…+1 = 1 = 1+1+…+1 = n n - - l l + 1 + 1 In particular, In particular,  l l i i n n1 = 1 = n n - 1 + 1 = - 1 + 1 = n n    ( (n n) )  1 1 i i n n i i = 1+2+…+ = 1+2+…+n n = = n n( (n n+1)/2 +1)/2   n n2 2 /2 /2    ( (n n2 2 ) )  1 1 i i n n i i2 2 = 1 = 12 2 +2 +22 2 +…+ +…+n n2 2 = = n n( (n n+1)(2 +1)(2n n+1)/6 +1)/6   n n3 3 /3 /3    ( (n n3 3 ) )  0 0 i i n n a ai i = 1 = 1 + + a a +…+ +…+ a an n = ( = (a an n+1 +1 - 1)/( - 1)/(a a - 1) for any - 1) for any a a   1 1 In particular, In particular,  0 0 i i n n 2 2i i = 2 = 20 0 + 2 + 21 1 +…+ 2 +…+ 2n n = 2 = 2n n+1 +1 - 1 - 1    (2 (2n n ) )  ( (a ai i ± ± b bi i ) = ) =  a ai i ± ±  b bi i  ca cai i = = c c a ai i  l l i i u ua ai i = =  l l i i m ma ai i + +  m m+1 +1 i i u ua ai i
  • 67.
    1-67 Design and Analysisof Algorithms – Unit I Example 1: Maximum element Example 1: Maximum element T(n) =  1 1 i i n-1 n-1 1 = n-1 = 1 = n-1 =  ( (n n) ) comparisons
  • 68.
    1-68 Design and Analysisof Algorithms – Unit I Example 2: Element uniqueness problem Example 2: Element uniqueness problem T(n) =  0 0 i i n-2 n-2 ( ( i+1 i+1 j j n-1 n-1 1) = =  0 0 i i n-2 n-2 n-i-1 = (n-1+1)(n-1)/2 =  ( ) ( ) comparisons 2 n
  • 69.
    1-69 Design and Analysisof Algorithms – Unit I Example 3: Matrix multiplication Example 3: Matrix multiplication T(n) =  0 0 i i n-1 n-1  0 0 i i n-1 n-1 n =  0 0 i i n-1 n-1  ( ( ) =  ( ( ) multiplications 2 n 3 n
  • 70.
    1-70 Design and Analysisof Algorithms – Unit I Example 4: Gaussian elimination Example 4: Gaussian elimination Algorithm Algorithm GaussianElimination GaussianElimination( (A A[0.. [0..n n- -1,0.. 1,0..n n]) ]) //Implements Gaussian elimination on an //Implements Gaussian elimination on an n- n-by by- -( (n n+1) matrix +1) matrix A A for for i i   0 0 to to n n - - 2 2 do do for for j j   i i + 1 + 1 to to n n - - 1 1 do do for for k k   i i to to n n do do A A[ [j j, ,k k] ]   A A[ [j j, ,k k] ] - - A A[ [i i, ,k k] ]   A A[ [j j, ,i i] / ] / A A[ [i i, ,i i] ] Find the efficiency class and a constant factor improvement. Find the efficiency class and a constant factor improvement. for for i i   0 0 to to n n - - 2 2 do do for for j j   i i + 1 + 1 to to n n - - 1 1 do do B B   A A[ [j,i j,i] / ] / A A[ [i i, ,i i] ] for for k k   i i to to n n do do A A[ [j j, ,k k] ]   A A[ [j j, ,k k] ] – A – A[ [i,k i,k] ] * B * B
  • 71.
    1-71 Design and Analysisof Algorithms – Unit I Example 5: Counting binary digits Example 5: Counting binary digits
  • 72.
    1-72 Design and Analysisof Algorithms – Unit I Plan for Analysis of Recursive Algorithms Plan for Analysis of Recursive Algorithms  Decide on a parameter indicating an input’s size. Decide on a parameter indicating an input’s size.  Identify the algorithm’s basic operation. Identify the algorithm’s basic operation.  Check whether the number of times the basic op. is executed Check whether the number of times the basic op. is executed may vary on different inputs of the same size. (If it may, the may vary on different inputs of the same size. (If it may, the worst, average, and best cases must be investigated separately.) worst, average, and best cases must be investigated separately.)  Set up a recurrence relation with an appropriate initial Set up a recurrence relation with an appropriate initial condition expressing the number of times the basic op. is condition expressing the number of times the basic op. is executed. executed.  Solve the recurrence (or, at the very least, establish its Solve the recurrence (or, at the very least, establish its solution’s order of growth) by backward substitutions or solution’s order of growth) by backward substitutions or another method. another method.
  • 73.
    1-73 Design and Analysisof Algorithms – Unit I Example 1: Recursive evaluation of Example 1: Recursive evaluation of n n! ! Definition: Definition: n n ! = 1 ! = 1   2 2   … …  ( (n- n-1) 1)   n n for for n n ≥ ≥ 1 and 0! = 1 1 and 0! = 1 Recursive definition of Recursive definition of n n!: !: F F( (n n) = ) = F F( (n- n-1) 1)   n n for for n n ≥ ≥ 1 and 1 and F F( (0) = 1 0) = 1 Size: Size: Basic operation: Basic operation: Recurrence relation: Recurrence relation: n multiplication M(n) = M(n-1) + 1 M(0) = 0
  • 74.
    1-74 Design and Analysisof Algorithms – Unit I Solving the recurrence for M( Solving the recurrence for M(n n) ) M( M(n n) = M( ) = M(n n-1) + 1, M(0) = 0 -1) + 1, M(0) = 0 M(n) = M(n-1) + 1 = (M(n-2) + 1) + 1 = M(n-2) + 2 = (M(n-3) + 1) + 2 = M(n-3) + 3 … = M(n-i) + i = M(0) + n = n The method is called backward substitution.
  • 75.
    1-75 Design and Analysisof Algorithms – Unit I Example 2: The Tower of Hanoi Puzzle Example 2: The Tower of Hanoi Puzzle 1 2 3 Recurrence for number of moves: Recurrence for number of moves: M(n) = 2M(n-1) + 1
  • 76.
    1-76 Design and Analysisof Algorithms – Unit I Solving recurrence for number of moves Solving recurrence for number of moves M( M(n n) = 2M( ) = 2M(n n-1) + 1, M(1) = 1 -1) + 1, M(1) = 1 M(n) = 2M(n-1) + 1 = 2(2M(n-2) + 1) + 1 = 2^2*M(n-2) + 2^1 + 2^0 = 2^2*(2M(n-3) + 1) + 2^1 + 2^0 = 2^3*M(n-3) + 2^2 + 2^1 + 2^0 = … = 2^(n-1)*M(1) + 2^(n-2) + … + 2^1 + 2^0 = 2^(n-1) + 2^(n-2) + … + 2^1 + 2^0 = 2^n - 1
  • 77.
    1-77 Design and Analysisof Algorithms – Unit I Tree of calls for the Tower of Hanoi Puzzle Tree of calls for the Tower of Hanoi Puzzle n n-1 n-1 n-2 n-2 n-2 n-2 1 1 ... ... ... 2 1 1 2 1 1 2 1 1 2
  • 78.
    1-78 Design and Analysisof Algorithms – Unit I Example 3: Counting #bits Example 3: Counting #bits A( ) = A( ) + 1, A( ) = 1 (using the Smoothness Rule) = (A( ) + 1) + 1 = A( ) + 2 = A( ) + i = A( ) + k = k + 0 = k 2 1 2  k 0 2 2 2  k n 2 log 2 2  k i k 2 k k 2 A(n) = A( ) + 1, A(1) = 0   2 / n

Editor's Notes

  • #9 Euclid’s algorithm is good for introducing the notion of an algorithm because it makes a clear separation from a program that implements the algorithm. It is also one that is familiar to most students. Al Khowarizmi (many spellings possible...) – “algorism” (originally) and then later “algorithm” come from his name.
  • #49 Example: cn2 how much faster on twice as fast computer? (2) how much longer for 2n? (4)
  • #53 Examples: 10n is O(n2) since 10n ≤ 10n2 for n ≥ 1 or 10n ≤ n2 for n ≥ 10 c n0 5n+20 is O(10n) since 5n+20 ≤ 10 n for n ≥ 4 c n0
  • #73 Note the difference between the two recurrences. Students often confuse these! F(n) = F(n-1) n F(0) = 1 for the values of n! ------------ M(n) =M(n-1) + 1 M(0) = 0 for the number of multiplications made by this algorithm