CMP 452 Merged
CMP 452 Merged
ALGORITHMS (3 UNITS)
LECTURE NOTE
M. O. ODIM (PHD.)
2 Analysis of Algorithm
Monday, March 9, 2020
Course Contents
Analysis of algorithms using Big oh concepts.
Computational complexity theory, P, NP, NP-hard, NP –
complete problems
Cryptographic algorithms,
advanced recursion theory.
Analysis of Various forms of algorithms .
Halstead measure, lines –of-code, cyclomatic etc.
1-3
Lecture Hours/Grading Policy
Lecture hours
Mondays, 9:00-10:00 AM, Venue: LR3
Tuesdays, 10:00am-12:00 Noon, Venue: LR5
Attendance: 5%
Mid Semester Tests: 15%
Assignments, mini project, practical, term paper, review articles,
etc.: 20%
End of Semester Exam: 60%
NOTE: 80% Attendance is mandatory for sitting for the exam
Recommended Texts
Textbook
Introduction to Design & Analysis of Algorithms
Anany Levitin, 3rd Ed., Pearson, 2012
Others
Introduction to Design & Analysis Computer Algorithm 3rd, Sara Baase,
Allen Van Gelder, Adison-Wesley, 2000.
Algorithms, Richard Johnsonbaugh, Marcus Schaefer, Prentice Hall, 2004.
5 Analysis of Algorithm
Monday, March 9, 2020
Course Objectives
This course introduces students to the analysis and
design of computer algorithms. On completion of this
course, students should be able to:
Analyze the asymptotic performance of algorithms.
Demonstrate a familiarity with major algorithms and data
structures.
Apply important algorithmic design paradigms and methods of
analysis.
6 Analysis of Algorithm
Monday, March 9, 2020
What is an Algorithm?
Algorithm
is any well-defined computational procedure that takes some
value, or set of values, as input and produces some value, or set
of values, as output.
is thus a sequence of computational steps that transform the
input into the output.
is a tool for solving a well - specified computational problem.
Any special method of solving a certain kind of problem
(Webster Dictionary)
a step-by-step procedure, which defines a set of instructions to
be executed in a certain order to get the desired output.
7 Analysis of Algorithm
Monday, March 9, 2020
What is an algorithm?
Recipe, process, method, technique, procedure, routine,…
with the following requirements:
1. Finiteness
b terminates after a finite number of steps
2. Definiteness
b rigorously and unambiguously specified
3. Clearly specified input
b valid inputs are clearly specified
4. Clearly specified/expected output
b can be proved to produce the correct output given a valid input
5. Effectiveness
b steps are sufficiently simple and basic
1-8
What is an algorithm?
(working definition)
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.
problem
algorithm
1-9
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.
12
Monday, March 9, 2020
Why study algorithms?
Theoretical importance
the core of computer science
Practical importance
A practitioner’s toolkit of known algorithms
Framework for designing and analyzing algorithms for new
problems
1-13
Basic Issues Related to Algorithms
How to design algorithms
Various design techniques exist
How to express algorithms
Natural language, such as English-like expression
Flowchart
Pseudocode
Proving correctness
Mathematical induction
Could be very difficult for approximate algorithms
Efficiency (or complexity) analysis
Theoretical (Mathematical) analysis
Empirical analysis
Algorithm Visualization
Optimality
Determining a feasible algorithmic solution
Obtaining a global solution
1-14
Algorithm design strategies
(techniques)
Dynamic programming
Divide and conquer
1-15
Forms of expressing algorithms
Natural (Human written) Language,
e.g., English Like expression
Flowchart:
a collection of connected geometric shapes containing
description of the algorithms steps
Pseudocode:
A mixture of a natural language and programming language-like
constructs.
searching
string processing
graph problems
combinatorial problems
geometric problems
numerical problems
1-24
Sorting (I)
Rearrange the items of a given list in ascending order.
Input: A sequence of n numbers <a1, a2, …, an>
Output: A reordering <a´1, a´2, …, a´n> of the input sequence such that a´1≤ a´2 ≤ … ≤
a´n.
Why sorting?
Help searching
Algorithms often use sorting as a key subroutine.
Sorting key
A specially chosen piece of information used to guide sorting. E.g., sort student records
by names.
1-25
Sorting (II)
Examples of sorting algorithms
Selection sort
Bubble sort
Insertion sort
Merge sort
Heap sort …
Evaluate sorting algorithm complexity: the number of key comparisons.
Two properties
Stability: A sorting algorithm is called stable if it preserves the relative order of any two equal
elements in its input.
In place : A sorting algorithm is in place if it does not require extra memory, except, possibly
for a few memory units.
1-26
Selection Sort
Algorithm SelectionSort(A[0..n-1])
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
for i 0 to n – 2 do
min i
for j i + 1 to n – 1 do
if A[j] < A[min]
min j
swap A[i] and A[min]
1-27
Searching
Find a given value, called a search key, in a given set.
Examples of searching algorithms
Sequential search
Binary search …
Input: sorted array a_i < … < a_j and key x;
m (i+j)/2;
while i < j and x != a_m do
if x < a_m then j m-1
else i m+1;
if x = a_m then output a_m;
Time: O(log n)
1-28
String Processing
A string is a sequence of characters from an alphabet.
Text strings: letters, numbers, and special characters.
String matching: searching for a given word/pattern in a
text.
Examples:
(i) searching for a word or phrase on WWW or in a Word document
(ii) searching for a short read in the reference genomic sequence
1-29
Graph Problems
Informal definition
A graph is a collection of points called vertices, some of which are
connected by line segments called edges.
Modeling real-life problems
Modeling WWW
Communication networks
Project scheduling …
Examples of graph algorithms
Graph traversal algorithms
Shortest-path algorithms
Topological sorting
1-30
Fundamental data structures
list b graph
array b tree and binary tree
linked list b set and dictionary
string
stack
queue
priority queue/heap
1-31
Linear Data Structures
Arrays
◼ Arrays
A sequence of n items of the same data type ◼ fixed length (need preliminary
that are stored contiguously in computer reservation of memory)
memory and made accessible by specifying a ◼ contiguous memory locations
value of the array’s index.
◼ direct access
Linked List
◼ Insert/delete
A sequence of zero or more nodes each
containing two kinds of information: some ◼ Linked Lists
data and one or more links called pointers to ◼ dynamic length
other nodes of the linked list. ◼ arbitrary memory locations
Singly linked list (next pointer)
◼ access by following links
Doubly linked list (next + previous pointers)
◼ Insert/delete
… .
a1 a2 an
1-32
Stacks and Queues
Stacks
A stack of plates
insertion/deletion can be done only at the top.
LIFO
Two operations (push and pop)
Queues
A queue of customers waiting for services
Insertion/enqueue from the rear and deletion/dequeue from the front.
FIFO
Two operations (enqueue and dequeue)
1-33
Priority Queue and Heap
9 6 8 5 2 3
1-34
Graphs
Formal definition
A graph G = <V, E> is defined by a pair of two sets: a finite set
V of items called vertices and a set E of vertex pairs called
edges.
Undirected and directed graphs (digraphs).
What’s the maximum number of edges in an undirected
graph with |V| vertices?
Complete, dense, and sparse graphs
A graph with every pair of its vertices connected by an edge is
called complete, K|V|
1 2
3 4
1-35
Graph Representation
Adjacency matrix
n x n boolean matrix if |V| is n.
The element on the ith row and jth column is 1 if there’s an
edge from ith vertex to the jth vertex; otherwise 0.
The adjacency matrix of an undirected graph is symmetric.
Adjacency linked lists
A collection of linked lists, one for each vertex, that contain all
the vertices adjacent to the list’s vertex.
Which data structure would you use if the graph is a 100-node star shape?
0111 2 3 4
0001
0001 4
0000
4
1-36
Weighted Graphs
Weighted graphs
Graphs or digraphs with numbers assigned to the edges.
5
1 2
6 7
9
3 4
8
1-37
Graph Properties -- Paths and Connectivity
Paths
A path from vertex u to v of a graph G is defined as a sequence of
adjacent (connected by an edge) vertices that starts with u and ends
with v.
Simple paths: All edges of a path are distinct.
Path lengths: the number of edges, or the number of vertices – 1.
Connected graphs
A graph is said to be connected if for every pair of its vertices u and v
there is a path from u to v.
Connected component
The maximum connected subgraph of a given graph.
1-38
Graph Properties -- Acyclicity
Cycle
A simple path of a positive length that starts and ends a the same
vertex.
Acyclic graph
A graph without cycles
DAG (Directed Acyclic Graph)
1 2
3 4
1-39
Trees
Trees
A tree (or free tree) is a connected acyclic graph.
Forest: a graph that has no cycles but is not necessarily connected.
Properties of trees
For every two vertices in a tree there always exists exactly one simple
path from one of these vertices to the other. Why?
Rooted trees:The above property makes it possible to select an arbitrary
vertex in a free tree and consider it as the root of the so called rooted tree.
Levels in a rooted tree.
rooted
|E| = |V| - 1 3
◼ 1 3 5
4 1 5
2 4
2
1-40
Rooted Trees (I)
Ancestors
For any vertex v in a tree T, all the vertices on the simple path
from the root to that vertex are called ancestors.
Descendants
All the vertices for which a vertex v is an ancestor are said to be
descendants of v.
Parent, child and siblings
If (u, v) is the last edge of the simple path from the root to vertex v,
u is said to be the parent of v and v is called a child of u.
Vertices that have the same parent are called siblings.
Leaves
A vertex without children is called a leaf.
Subtree
A vertex v with all its descendants is called the subtree of T rooted
at v.
1-41
Ordered Trees
Ordered trees
An ordered tree is a rooted tree in which all the children of each vertex are
ordered.
Binary trees
A binary tree is an ordered tree in which every vertex has no more than
two children and each children is designated s either a left child or a right
child of its parent.
Binary search trees
Each vertex is assigned a number.
A number assigned to each parental vertex is larger than all the numbers in
its left subtree and smaller than all the numbers in its right subtree.
log2n h n – 1, where h is the height of a binary tree and n the size.
9 6
6 8 3 9
5 2 3 2 5 8
1-42
43 Analysis of Algorithm Monday, March 9, 2020
CMP 452 - DESIGN & ANALYSIS
OF ALGORITHMS (3 UNITS
LECTURE NOTE II
Fundamentals of the Analysis of Algorithm Efficiency
M. O. ODIM
Analysis Issues/Concerns:
How good is the algorithm?
Correctness
time efficiency
space efficiency
Does there exist a better algorithm?
Lower bounds
Optimality
The Analysis Framework
Gives a general framework for analyzing the efficiency of algorithms.
50 Analysis of Algorithm
Monday, March 9, 2020
Measuring an Input’s Size
Obvious Observation
Almost all algorithms run longer on larger inputs.
E.g. , it takes longer to sort larger arrays, multiply larger matrices,
and so on.
Therefore, an algorithm’s efficiency is investigated as a
function of some parameter n indicating the algorithm’s input
size
Simply put, the larger the input size, the longer it takes the
algorithm to run, all things being equal.
However, there are situations where the choice of a parameter
indicating an input size does matter.
E.g. computing the product of two n × n matrices
The choice of an appropriate size metric can be influenced by
operations of the algorithm in question.
Units for Measuring Running Time
use some standard unit of time measurement—a second, or
millisecond, and so on—to measure the running time of a program
implementing the algorithm.
Drawbacks to such an approach
dependence on the speed of a particular computer,
dependence on the quality of a program implementing the algorithm and of
the compiler used in generating the machine code,
and the difficulty of clocking the actual running time of the program
Need a metric that does not depend on these extraneous factors.
A possible approach: count the number of times each of the algorithm’s
operations is executed.
This approach is both excessively difficult and, usually unnecessary.
What to do: identify the most important operation of the algorithm,
called the basic operation, the operation contributing the most to the total
running time, and compute the number of times the basic operation is
executed.
Identifying the basic operation of an algorithm
usually the most time-consuming operation in the algorithm’s innermost loop
E.g. For most sorting algorithms: the basic operation is a key comparison of
element being sorted.
As another example, algorithms for mathematical problems typically involve
some or all of the four arithmetical operations:
addition,
subtraction,
multiplication, and
division.
Of the four, the most time-consuming operation is division, followed by
multiplication and then addition and subtraction, with the last two usually
considered together.
Thus, the established framework for the analysis of an algorithm’s time efficiency
suggests measuring it by counting the number of times the algorithm’s basic
operation is executed on inputs of size n.
Definition
Let cop be the execution time of an algorithm’s
basic operation on a particular computer, and let C(n) be the number of
times this operation needs to be executed for this algorithm.
Then we can estimate the running time T (n) of a program implementing this
algorithm on that computer by the formula
T (n) ≈ copC(n).
Drawbacks
the count C(n) does not contain any information about operations that are
not basic
The count itself is often computed only approximately.
the constant cop is also an approximation whose reliability is not always easy
p
to assess.
Nevertheless, the formula can give a reasonable estimate of the
algorithm’s running time.
It also makes it possible to answer such questions as “How much faster
would this algorithm run on a machine that is x times faster than the one
we have?”
Consider the following example:
, how much longer will the algorithm
Assuming that run if we double its input?
size?
Note
▪ the question was answered without actually knowing the value of cop: it
was neatly cancelled out in the ratio.
▪Also note that ½ , the multiplicative constant in the formula for the count
C(n), was also cancelled out.
▪Thus, the efficiency analysis framework ignores multiplicative constants
and concentrates on the count’s order of growth to within a constant
multiple for large-size inputs.
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
or cost executed
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Best-case, average-case, worst-case
Worst case
n key comparisons
(n+1)/2, assuming K is in A
Average case
Determining the worst case
The worst-case efficiency of an algorithm is its efficiency for the worst-case
input of size n, which is an input (or inputs) of size n for which the
algorithm runs the longest among all possible inputs of that size.
To determine the worst case:
analyze the algorithm to see what kind of inputs yield the largest
value of the basic operation’s count C(n) among all possible inputs of
size n and then compute this worst-case value Cworst(n).
The worst-case analysis provides very important information about
an algorithm’s efficiency by bounding its running time from above.
i.e.,
it guarantees that for any instance of size n, the running time will not
exceed Cworst(n), its running time on the worst-case inputs.
Determining the best case
The best case does not mean the smallest input; it means the input
of size n for which the algorithm runs the fastest.
To determine this case:
First, we determine the kind of inputs for which the count C(n)
will be the smallest among all possible inputs of size n.
Then ascertain the value of C(n) on these most convenient inputs.
For example, the best-case inputs for sequential search are lists of
size n with their first element equal to a search key; accordingly, Cbest(n) =
1 for this algorithm.
The analysis of the best-case efficiency is not nearly as important
as that of the worst-case efficiency.
Note that neither the worst-case analysis nor its best-case
counterpart yields the necessary information about an algorithm’s
behaviour on a “typical” or “random” input. This is the information
that the average-case efficiency seeks to provide.
To analyze the algorithm’s averagecase efficiency, we must make
some assumptions about possible inputs of size n.
Consider again sequential search.
The standard assumptions:
(a) the probability of a successful search is equal to p (0 ≤ p ≤ 1)
The probability of the first match occurring in the ith position of the
list is the same for every i.
we therefore can find the average number of key comparisons
Cavg(n) as follows.
In the case of a successful search, the probability of the first match
occurring in the ith position of the list is p/n for every i,
the number of comparisons made by the algorithm in such a
situation is obviously i.
In the case of an unsuccessful search, the number of comparisons
will be n with the probability of such a search being (1− p).
Recall
E(x) = Σxi p(xi) = x1p(x1)+x2p(x2) + ... + xnp(xn), for i =1 to n
Thus,
if p = 1 (the search must be successful), the average number of key
comparisons made by sequential search is (n + 1)/2; that is, the algorithm will
inspect, on average, about half of the list’s elements.
If p = 0 (the search must be unsuccessful), the average number of key
comparisons will be n because the algorithm will inspect all n elements on all
such inputs.
Types of formulas for basic operation’s count
Exact formula
e.g., C(n) = n(n-1)/2
Example:
How much faster will algorithm run on computer that is twice as fast?
How much longer does it take to solve problem of double input size?
Asymptotic Notations and Basic Efficiency Classes
➢ Computer scientists use three notations:
O (big oh),
Ω(big omega), and
Θ (big theta)
to compare and rank the order of growth of an algorithm’s basic
operation count as the principal indicator of the algorithm’s efficiency.
A way of comparing functions that ignores constant factors and small
input sizes.
O(g(n)): class of functions t(n) that grow no faster than g(n)
Θ(g(n)): class of functions t(n) that grow at same rate as g(n)
Ω(g(n)): class of functions t(n) that grow at least as fast as g(n)
Big-oh
Big-omega
Big-theta
Establishing order of growth using the definition
O-notation
Formal Definition:
t(n) is in O(g(n)), denoted f(n) O(g(n)), if t(n) is bounded above by
some constant multiple of g(n) for all large n, i.e., if order of growth
of t(n) ≤ order of growth of g(n) (within constant multiple),
implies, there exist positive constant c and non-negative integer n0
such that
t(n) ≤ c g(n) for every n ≥ n0
Examples:
10n is in O(n2)
5n+20 is in O(n)
100n + 5 ∈ O(n2).
O-notation
formal proof of one of the assertions: 100n + 5 ∈ O(n2). Indeed,
Formal definition
A function t(n) is said to be in (g(n)), denoted t(n) (g(n)), if
t(n) is bounded both above and below by some positive constant
multiples of g(n) for all large n, i.e., if there exist some positive
constant c1 and c2 and some nonnegative integer n0 such that
c2 g(n) t(n) c1 g(n) for all n n0
Exercises: prove the following using the above definition
10n2 (n2)
0.3n2 - 2n (n2)
(1/2)n(n+1) (n2)
>=
(g(n)), functions that grow at least as fast as g(n)
=
(g(n)), functions that grow at the same rate as g(n)
g(n)
<=
O(g(n)), functions that grow no faster than g(n)
Some properties of asymptotic order of growth
f(n) O(f(n))
Implication: The algorithm’s overall efficiency will be determined by the part with a
larger order of growth, i.e., its least efficient part.
For example, 5n2 + 3nlogn O(n2)
Proof. There exist constants c1, c2, n1, n2 such that
t1(n) c1*g1(n), for all n n1
t2(n) c2*g2(n), for all n n2
Define c3 = c1 + c2 and n3 = max{n1,n2}. Then
t1(n) + t2(n) c3*max{g1(n), g2(n)}, for all n n3
Using Limits for Comparing Orders of Growth
The asymptotic notations, rarely used in practice
comparing the order of growth of two functions.
A more convenient approach is computing the
limit of the ratio of the functions.
Three fundamental case are
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
L’Hôpital’s rule and Stirling’s formula
f(n) f ´(n)
lim = lim
g(n) g ´(n)
n→ n→
Example: 2n vs. n!
Orders of growth of some important functions
order log n < order n (>0) < order an < order n! < order nn
Basic asymptotic efficiency classes
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Basic asymptotic efficiency classes
(2)
Check whether the number of times the basic op. is executed may
vary on different inputs of the same size. (If it may, the worst,
average, and best cases must be investigated separately.)
Solve the recurrence (or, at the very least, establish its solution’s
order of growth) by backward substitutions or another method.
Example 1: Compute the factorial function F(n) = n! for an
arbitrary nonnegative integer n.
Solution:
By definition
n!= 1 . . . . . (n − 1) . n = (n − 1)! . n for n ≥ 1 and
0!= 1
Therefore, the function computing n! could be expressed as
F(n) = F(n − 1) . n
with the following recursive algorithm
1 3
n-1 n-1
1 1 1 1 1 1 1 1
Example 3: Counting #bits
A(n) = A(
n /)2+1, A(1) = 0
A( 2 k) = A( 2)k+−1,
1 A( ) = 1 20
(using the Smoothness Rule)
= (A( ) + 1) + 1 = A( ) + 2 k −2
k −2 2
= A(
2) + i
= A( 2 k) −+ik = k + 0
=
2 k −k
log 2 n
Smoothness Rule
Let f(n) be a nonnegative function defined on the set of natural
numbers. f(n) is call smooth if it is eventually nondecreasing and
f(2n) ∈ Θ (f(n))
Functions that do not grow too fast, including logn, n, nlogn, and n
where >=0 are smooth.
Smoothness rule
Let T(n) be an eventually nondecreasing function and f(n) be a
smooth function. If
T(n) ∈ Θ (f(n)) for values of n that are powers of b,
where b>=2, then
T(n) ∈ Θ (f(n)) for any n.
Fibonacci numbers
The Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
Characteristic equation: r2 - r -1 = 0
Examples:
T(n) = T(n/2) + 1
T(n) = 2T(n/2) + n Θ(log n)
Each line corresponds to one iteration of the algorithm, i.e., a pass through the
list’s tail to the right of the vertical bar; an element in bold indicates the
smallest element found. Elements to the left of the vertical bar are in their final
positions and are not considered in this and subsequent iterations.
Practice: Obtain the number of counts of the basic operation of this algorithm
and determine its order of growth.
The action of the algorithm on the list 89, 45, 68, 90, 29, 34,
17 is illustrated as an example
First two passes of bubble sort on the list 89, 45, 68, 90, 29, 34, 17. A new line is shown
after a swap of two elements is done. The elements to the right of the vertical bar are in
their final positions and are not considered in subsequent iterations of the algorithm.
Exercise: complete the n-I passes for sorting of the list, obtain the number of key swaps
and determine its order of growth. Also, practice the other numerous applications
examples on the text.
147 Monday, March 9, 2020
Decrease-and-Conquer
based on exploiting the relationship between a solution to a given
instance of a problem and a solution to its smaller instance.
Once such a relationship is established, it can be exploited either top
down or bottom up.
The former leads naturally to a recursive implementation, although, an
ultimate implementation may well be nonrecursive. The bottom-up
variation is usually implemented iteratively, starting with a solution to
the smallest instance of the problem; it is called sometimes the
incremental approach.
There are three major variations of decrease-and-conquer:
decrease by a constant
decrease by a constant factor
variable size decrease
Analysis Issues/Concerns:
• How good is the algorithm?
– Correctness
– time efficiency
– space efficiency
• Does there exist a better algorithm?
– Lower bounds
– Optimality
Approaches:
• Theoretical analysis
– For Mathematical Analysis (Non recursive & Recursive )
Algorithms
• Empirical analysis
• Algorithm Visualization
Correctness
• Does the input/output relation match algorithm requirement?
Amount of work done (aka complexity)
• Basic operations to do task finite amount of time
Amount of space used
• Memory used
1 Monday, May
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. 31, 2021
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-12
Measuring an Input’s Size
Obvious Observation
• Almost all algorithms run longer on larger inputs.
• E.g. , it takes longer to sort larger arrays, multiply larger
matrices, and so on.
Therefore, an algorithm’s efficiency is investigated
as a function of some parameter n indicating the
algorithm’s input size
Simply put, the larger the input size, the longer it
takes the algorithm to run, all things being equal.
However, there are situations where the choice of a
parameter indicating an input size does matter.
• E.g. computing the product of two n × n matrices
The choice of an appropriate size metric can be
influenced by operations of the algorithm in
question.
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-13
Units for Measuring Running Time
use some standard unit of time measurement—a second, or
millisecond, and so on—to measure the running time of a
program implementing the algorithm.
Drawbacks to such an approach
• dependence on the speed of a particular computer,
• dependence on the quality of a program implementing the algorithm
and of the compiler used in generating the machine code,
• and the difficulty of clocking the actual running time of the program
Need a metric that does not depend on these extraneous
factors.
A possible approach: count the number of times each of the
algorithm’s operations is executed.
This approach is both excessively difficult and, usually
unnecessary.
What to do: identify the most important operation of the
algorithm, called the basic operation, the operation contributing
the most to the total running time, and compute the number of
times the basic operation is executed.
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-14
Identifying the basic operation of an algorithm
T (n) ≈ copC(n).
Drawbacks
• the count C(n) does not contain any information about operations that
are not basic
• The count itself is often computed only approximately.
• the constant cop is also an approximation whose reliability is not always
p
easy to assess.
Nevertheless, the formula can give a reasonable estimate of the
algorithm’s running time.
It also makes it possible to answer such questions as “How much
faster would this algorithm run on a machine that is x times faster
than the one we have?”
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-16
Consider the following example:
, how much longer will the
Assuming that
algorithm run if we double its
input?
size?
Note
▪ the question was answered without actually knowing the value of cop: it was
neatly cancelled out in the ratio.
▪Also note that ½ , the multiplicative constant in the formula for the count C(n),
was also cancelled out.
▪Thus, the efficiency analysis framework ignores multiplicative constants and
concentrates on the count’s order of growth to within a constant multiple for
Monday, May 31, 2021
large-size inputs.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 ed., Ch. 2
nd 2-17
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
or cost executed
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Worst case
n key comparisons
Best case
1 comparisons
Average case
Monday, May 31, 2021
(n+1)/2, assuming K is in A
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-21
Determining the worst case
The worst-case efficiency of an algorithm is its efficiency for
the worst-case input of size n, which is an input (or inputs) of
size n for which the algorithm runs the longest among all
possible inputs of that size.
To determine the worst case:
analyze the algorithm to see what kind of inputs yield the
largest value of the basic operation’s count C(n) among all
possible inputs of size n and then compute this worst-case value
Cworst(n).
The worst-case analysis provides very important information
about an algorithm’s efficiency by bounding its running time
from above. i.e.,
it guarantees that for any instance of size n, the running time
will not exceed Cworst(n), its running time on the worst-case
Monday, May 31, 2021
inputs. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 ed., Ch. 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
nd 2-22
Determining the best case
The best case does not mean the smallest input; it means
the input of size n for which the algorithm runs the fastest.
To determine this case:
First, we determine the kind of inputs for which the count
C(n) will be the smallest among all possible inputs of size n.
Then ascertain the value of C(n) on these most convenient
inputs.
For example, the best-case inputs for sequential search are
lists of size n with their first element equal to a search key;
accordingly, Cbest(n) = 1 for this algorithm.
The analysis of the best-case efficiency is not nearly as
important as that of the worst-case efficiency.
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-23
Note that neither the worst-case analysis nor its best-case
counterpart yields the necessary information about an
algorithm’s behaviour on a “typical” or “random” input.
This is the information that the average-case efficiency
seeks to provide.
Exact formula
e.g., C(n) = n(n-1)/2
Example:
• How much faster will algorithm run on computer that is
twice as fast?
O-notation
Formal Definition:
t(n) is in O(g(n)), denoted t(n) O(g(n)), if t(n) is bounded
above by some constant multiple of g(n) for all large n, i.e., if
order of growth of t(n) ≤ order of growth of g(n) (within
constant multiple), implies, there exist positive constant c
and non-negative integer n0 such that
t(n) ≤ c g(n) for every n ≥ n0
Examples:
10n is in O(n2)
5n+20 is in O(n)
100n + 5
Monday, May 31, 2021
∈ O(n 2).
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-40
O-notation
formal proof of one of the assertions: 100n + 5 ∈ O(n2).
Indeed,
Formal definition
• A function t(n) is said to be in (g(n)), denoted t(n)
(g(n)), if t(n) is bounded below by some constant
multiple of g(n) for all large n, i.e., if there exist some
positive constant c and some nonnegative integer n0
such that
t(n) cg(n) for all n n0
Formal definition
• A function t(n) is said to be in (g(n)), denoted t(n)
(g(n)), if t(n) is bounded both above and below by
some positive constant multiples of g(n) for all large
n, i.e., if there exist some positive constant c1 and c2
and some nonnegative integer n0 such that
c2 g(n) t(n) c1 g(n) for all n n0
Exercises: prove the following using the above definition
• 10n2 (n2)
• 0.3n2 - 2n (n2)
• (1/2)n(n+1) (n2)
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-43
>=
(g(n)), functions that grow at least as fast as g(n)
=
(g(n)), functions that grow at the same rate as g(n)
g(n)
<=
O(g(n)), functions that grow no faster than g(n)
n linear Ο(n)
n3 cubic Ο(n3)
2n exponential 2Ο(n)
n! factorial 2Ο(n!)
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-45
Basic asymptotic efficiency classes
f(n) O(f(n))
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
order log n < order n (>0) < order an < order n! < order nn
(2)
Solution:
By definition
n!= 1 . . . . . (n − 1) . n = (n − 1)! . n for n ≥ 1 and
0!= 1
Therefore, the function computing n! could be expressed as
F(n) = F(n − 1) . n
with the following recursive algorithm
1 3
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
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-78
Tree of calls for the Tower of Hanoi Puzzle
n-1 n-1
1 1 1 1 1 1 1 1
A(n) = A( n / 2 ) + 1, A(1) = 0
Characteristic equation: r 2 - r -1 = 0
Examples:
• T(n) = T(n/2) + 1 Θ(log n)
• T(n) = 2T(n/2) + n Θ(nlog n)
• T(n) = 3T(n/2) + n Θ(nlog23)
• T(n) = T(n/2) + n Θ(n)
Monday, May 31, 2021
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-88
Empirical Analysis of Algorithms
by
M. O. Odim (Ph.D.)
Computer Science Department, Redeemer’s
University
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-1
Algorithm Design Techniques
Brute force
Decrease – and – Conquer
Divide – and – Conquer
Transform – and – Conquer
Dynamic Programming
Analysis of Algorithm 2
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-2
Brute force
a straightforward approach to solving a problem, usually directly based on the
problem statement and definitions of the concepts involved.
The “force” is that of a computer and not that of one’s intellect. "Just do it!”
often, the strategy indeed that is easiest to apply.
Examples: Selection Sort and Bubble Sort
Analysis of Algorithm 3
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-3
Selection Sort
scanning the entire given list to find its smallest element and exchange it
with the first element, putting the smallest element in its final position in
the sorted list.
Then we scan the list, starting with the second element, to find the smallest
among the last n − 1 elements and exchange it with the second element,
putting the second smallest element in its final position.
Generally, on the ith pass through the list, which we number from 0 to n −
2, the algorithm searches for the smallest item among the last n − i
elements and swaps it with Ai :
Analysis of Algorithm 4
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-4
After n − 1 passes, the list is sorted.
Analysis of Algorithm 5
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-5
An Example of the selection sort
As an example, the action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is
illustrated below:
Each line corresponds to one iteration of the algorithm, i.e., a pass through the
list’s tail to the right of the vertical bar; an element in bold indicates the
smallest element found. Elements to the left of the vertical bar are in their final
positions and are not considered in this and subsequent iterations.
Practice: Obtain the number of counts of the basic operation of this algorithm
and determine its order of growth.
Analysis of Algorithm 6
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-6
Bubble Sort
Analysis of Algorithm 7
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-7
Bubble Sort
The action of the algorithm on the list 89, 45, 68, 90, 29, 34, 17 is
illustrated as an example
Analysis of Algorithm 8
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-8
Bubble Sort
First two passes of bubble sort on the list 89, 45, 68, 90, 29, 34, 17. A new line is
shown after a swap of two elements is done. The elements to the right of the
vertical bar are in their final positions and are not considered in subsequent
iterations of the algorithm.
Exercise: complete the n-I passes for sorting of the list, obtain the number of key
swaps and determine its order of growth. Also, practice the other numerous
applications examples on the text.
9
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-9
Decrease-and-Conquer
based on exploiting the relationship between a solution to a given instance of a problem
and a solution to its smaller instance.
Once such a relationship is established, it can be exploited either top down or bottom
up.
The former leads naturally to a recursive implementation, although, an ultimate
implementation may well be nonrecursive. The bottom-up variation is usually
implemented iteratively, starting with a solution to the smallest instance of the problem;
it is called sometimes the incremental approach.
There are three major variations of decrease-and-conquer:
• decrease by a constant
• decrease by a constant factor
• variable size decrease
1
Monday, June 20, 2022 0
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-10
In the decrease-by-a-constant
variation, the size of an
instance is reduced by the
same constant on each
iteration of the algorithm.
Typically, this constant is
equal to one (see fig. below),
although other constant size
reductions do happen
occasionally.
Analysis of Algorithm 1
Monday, June 20, 2022 1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-11
Decrease-by-a-Constant-Factor Algorithms
E.g. Binary Search
Analysis of Algorithm 12
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-12
Binary Search
a remarkably efficient algorithm
for searching in a sorted array.
works by comparing a search key
K with the array’s middle element
A[m].
If they match, the algorithm
stops; otherwise, the same
operation is repeated recursively
for the first half of the array if K
<A[m], and for the second half if
K >A[m]:
Design and Analysis of Algorithms 13
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-13
Binary Search
Analysis of Algorithm 14
Monday, June 20, 2022
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-14
The decrease-by-a-constant-
factor technique suggests
reducing a problem instance
by the same constant factor
on each iteration of the
algorithm.
In most applications, this
constant factor is equal to
two. The decrease-by-half
idea is illustrated in the fig.
below
Analysis of Algorithm 1
Monday, June 20, 2022 5
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-15
In the variable-size-decrease
variety of decrease-and-
conquer, the size-reduction
pattern varies from one
iteration of an algorithm to
another.
Analysis of Algorithm 1
Monday, June 20, 2022 6
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-16
Divide – and - Conquer
Divide-and-conquer algorithms work according to the
following general plan:
1. A problem is divided into several subproblems of the same type, ideally of
about equal size.
2. The subproblems are solved (typically recursively, though sometimes a
different algorithm is employed, especially when subproblems become small
enough).
3. If necessary, the solutions to the subproblems are combined to get a solution to
the original problem.
Analysis of Algorithm 1
Monday, June 20, 2022 7
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-17
Analysis of Algorithm 1
Monday, June 20, 2022 8
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-18
Analysis of Algorithm 1
Monday, June 20, 2022 9
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-19
Computational Complexity Theory
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-21
COMPUTATIONAL COMPLEXITY THEORY
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-22
Reasons for drawing the intractability line
First, we cannot solve arbitrary Table 3.1 Values (some approximate) of several
instances of intractable problems functions important for analysis of algorithms
in a reasonable amount of time
unless such instances are very
small (See the entries of the
efficiency classes).
Second, although there might be
a huge difference between the
running times n O(p(n)) for
polynomials of drastically
different degrees, there are very
few useful polynomial-time Third, polynomial functions possess
algorithms with the degree of a
polynomial higher than three. In many convenient properties; in
addition, polynomials that bound particular, both the sum and
running times of algorithms do composition of two polynomials are
not usually have extremely large always polynomials too.
coefficients.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-23
Fourth, the choice of this class has led to a development of
an extensive theory called computational complexity, which
seeks to classify problems according to their inherent
difficulty. And according to this theory, a problem’s
intractability remains the same for all principal models of
computations and all reasonable input-encoding schemes
for the problem under consideration.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-24
Basic Notions and Ideas of Complexity Theory
P and NP Problems
Most problems can be solved in polynomial time by some
algorithm. They include
• computing the product and the greatest common divisor of
two integers,
• sorting a list, searching for a key in a list or for a pattern in
a text string,
• checking connectivity and acyclicity of a graph, and
finding a minimum spanning tree and shortest paths in a
weighted graph.
• Informally, we can think about problems that can be
solved in polynomial time as the set that computer science
theoreticians call P. A more formal definition include in P
only decision problems, which are problems with yes/no
answers. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2 ed., Ch. 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
nd 2-25
P and NP Problems
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-26
Some important problems with no polynomial time algorithms
found nor the impossibility of such an algorithm been proved
Hamiltonian circuit problem Determine whether a given graph
has a Hamiltonian circuit—a path that starts and ends at the
same vertex and passes through all the other vertices exactly
once.
Traveling salesman problem Find the shortest tour through n
cities with known positive integer distances between them
(find the shortest Hamiltonian circuit in a complete graph
with positive integer weights).
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-27
Knapsack problem: Find the most valuable subset of n items of
given positive integer weights and values that fit into a
knapsack of a given positive integer capacity.
Partition problem: Given n positive integers, determine whether
it is possible to partition them into two disjoint subsets with
the same sum.
Bin-packing problem: Given n items whose sizes are positive
rational numbers not larger than 1, put them into the smallest
number of bins of size 1.
Graph-coloring problem: For a given graph, find its chromatic
number, which is the smallest number of colors that need to
be assigned to the graph’s vertices so that no two adjacent
vertices are assigned the same color.
Integer linear programming problem: Find the maximum (or
minimum) value of a linear function of several integer-valued
variables subject to a finite set of constraints in the form of
linear equalities and inequalities.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-28
A nondeterministic algorithm
DEFINITION 3 A nondeterministic algorithm is a two-stage procedure that takes
as its input an instance I of a decision problem and does the following.
Nondeterministic (“guessing”) stage: An arbitrary string S is generated that can
be thought of as a candidate solution to the given instance I (but may be complete
gibberish as well).
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-29
Deterministic (“verification”) stage: A deterministic
algorithm takes both I and S as its input and outputs yes if S
represents a solution to instance I. (If S is not a solution to
instance I , the algorithm either returns no or is allowed not
to halt at all.)
DEFINITION 4 Class NP is the class of decision problems
that can be solved by nondeterministic polynomial
algorithms. This class of problems is called
nondeterministic polynomial.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-30
Most decision problems are in NP. First of all, this class
includes all the problems in P:
P ⊆ NP.
This is true because, if a problem is in P, we can use the
deterministic polynomialtime algorithm that solves it in the
verification-stage of a nondeterministic algorithm that simply
ignores string S generated in its nondeterministic (“guessing”)
stage.
But NP also contains the Hamiltonian circuit problem, the
partition problem, decision versions of the travelling salesman,
the knapsack, graph coloring, and many hundreds of other
difficult combinatorial optimization problems. The halting
problem, on the other hand, is among the rare examples of
decision problems that are known not to be in NP.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-31
This leads to the most important open question of
theoretical computer science:
Is P a proper subset of NP, or are these two classes, in fact,
the same? We can put this symbolically as
P = NP?
Note that P = NP would imply that each of many hundreds
of difficult combinatorial decision problems can be solved
by a polynomial-time algorithm, although computer
scientists have failed to find such algorithms despite their
persistent efforts over many years.
Moreover, many well-known decision problems are known
to be “NP-complete” which seems to cast more doubts on the
possibility that P = NP.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-32
NP-Complete Problems
problem in NP that is as difficult as any other problem in this
class because, by definition, any other problem in NP can be
reduced to it in polynomial time
DEFINITION 5 A decision problem D1 is said to be
polynomially reducible to a decision problem D2, if there
exists a function t that transforms instances of D1 to instances
of D2 such that:
1. t maps all yes instances of D1 to yes instances of D2 and all no
instances of D1 to no instances of D2
2. t is computable by a polynomial time algorithm
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-33
NP-Complete Problems
Implication of the Definition
• if a problem D1 is polynomially reducible to some problemD2 that can be solved in polynomial
time, then problem D1 can also be solved in polynomial time.
DEFINITION 6
A decision problem D is said to be NP-complete if:
• 1. it belongs to class NP
• 2. every problem in NP is polynomially reducible to D
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 2 2-34