SlideShare a Scribd company logo
1 of 144
SCHOOL OF COMPUTING
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
10211CS202-DESIGN AND ANALYSIS OF
ALGORITHMS
UNIT 1
Course Outcomes
INTRODUCTION TO DESIGN AND ANALYSIS OF ALGORITHM
COURSE DETAILS
• Course Code : 10211CS202
• Course Name : Design and Analysis of Algorithms
• Category : Program Core
• Pre- Requisites :10211CS102-Data Structures
• No of Credits : 4
• No of Hours/Week : 6
Preamble
For an engineer, problem solving is not about just solving a problem somehow but
about solving the problem in the most effective and efficient way. Two key skills
that a software professional needs are (1) to choose suitable data structures to
store the information part of the problem, and (2) use of efficient algorithms
for developing a programming solution of a given problem. Selection of a
particular data structure greatly influences the characteristics of the obtained
solution that include efficiency (performance, or speed), space (memory)
requirements, scalability, reuse, and robustness (or reliability). The other equally
important skill is to choose a suitable problem solving technique to apply to a
particular problem. Acquiring these skills, greatly enhances the problem solving
skills of the learner.
Course Objectives
• To gain experience in fundamental techniques used for effective problem
solving in computing.
• Analyze the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• Demonstrate a familiarity with major algorithms and data structures.
• Apply important algorithmic design paradigms and methods of analysis.
• Synthesize efficient algorithms in common engineering design situations.
• Familiarizing students with specific algorithms for a number of important
computational problems like sorting, searching, and graphs, …etc,.
INTRODUCTION
Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving –
Important Problem Types – Analysis of Algorithm Efficiency - Introduction to
Asymptotic Notations– Solving Recurrence relations – Master’s theorem -
Mathematical analysis for Recursive and Nonrecursive algorithms.
(Case Study: Design a gaming application like Chess, Candy crush etc.)
Text Books
1. AnanyLevitin, “Introduction to the Design and Analysis of Algorithms”,
Third Edition, Pearson Education, 2012.
UNIT-1
Design and Analysis of Algorithms
• Analysis: predict the cost of an algorithm in terms of resources and
performance
• Design: design algorithms which minimize the cost
To Study ?
 Design and Analysis of Algorithm is very important for designing
algorithm to solve different types of problems in the branch of computer
science and information technology.
 To introduces the fundamental concepts of Designing Strategies,
Complexity analysis of Algorithms, followed by problems on Graph
Theory and Sorting methods.
 It is the core of computer science, and, in all fairness, can be said to be
relevant to most of science, business, and technology
What is an algorithm?
Algorithm is a set of steps to complete a task.
For example, Task: to make a cup of tea.
Algorithm:
• add water and milk to the kettle,
• boil it, add tea leaves,
• Add sugar, and then serve it in cup.
Algorithm
Computer algorithm
What is Computer algorithm?
• ‘’a set of steps to accomplish or complete a task that is described precisely
enough that a computer can run it’’.
Described precisely:
very difficult for a machine to know how much water, milk to be added etc.
in the above tea making algorithm.
These algorithms run on computers or computational devices.
Forexample, GPS in our smartphones, Google hangouts.
GPS uses shortest path algorithm. Online shopping uses cryptography
which uses RSA algorithm
Characteristics of Algorithms
The main characteristics of algorithms are as follows −
• Algorithms must have a unique name
• Algorithms should have explicitly defined set of inputs and outputs
• Algorithms are well-ordered with unambiguous operations
• Algorithms halt in a finite amount of time.
• Algorithms should not run for infinity, i.e., an algorithm must end at some
point
Pseudocode
• It is one of the methods that could be used to represent an algorithm.
• It is not written in a specific syntax
• Cannot be executed
• Can be read and understood by programmers who are familiar with
different programming languages.
• Transformation from pseudo code to the corresponding program code
easier.
• Pseudo code allows to include control structures such as WHILE, IF-
THEN-ELSE, REPEAT-UNTIL, FOR, and CASE, which are available in
many high level languages.
Difference between Algorithm &Pseudocode
Algorithm Pseudo code
A finite set of instructions or
logic, written in order, to
accomplish a certain predefined
task.
A generic way of describing an algorithm
without using any specific programming
language-related notations.
It is just the core logic (solution)
of a problem
It is an outline of a program, written in a
form which can easily be converted into
real programming statements.
Easy to understand the logic of a
problem
Can be read and understood by
programmers who are familiar with
different programming languages.
Can be expressed either as an
informal high level description as
pseudo code or using a flowchart.
It is not written in a specific syntax. It
allows to include control structures such as
WHILE, IF-THEN-ELSE, REPEAT-
UNTIL, FOR, and CASE, which are
present in many high level languages.
Difference between Algorithm &Pseudocode
Problem: Suppose there are 60 students in the class. How will you
calculate the number of absentees in the class?
Pseudo Approach:
1.Initialize a variable called as Count to zero, absent to zero, total to 60
2.FOR EACH Student PRESENT DO the following:
Increase the Count by One
3.Then Subtract Count from total and store the result in absent
4.Display the number of absent students
Algorithmic Approach:
1.Count <- 0, absent <- 0, total <- 60
2.REPEAT till all students counted
Count <- Count + 1
3.absent <- total - Count
4.Print "Number absent is:" , absent
What is an 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.
What is an Algorithm ?
What is an Algorithm ?
What is an Algorithm ?
What is an Algorithm ?
What is an Algorithm ?
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode Efficiency
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
Pseudocode
1.1 NOTION OF AN ALGORITHM
FIGURE 1.1 The notion of the algorithm.
• It is a step by step procedure with the input to solve the problem in a finite
amount of time to obtain the required output.
The notion of the algorithm illustrates some important points:
• The non-ambiguity requirement for each step of an algorithm
cannot be compromised.
• The range of inputs for which an algorithm works has to be
specified carefully.
• The same algorithm can be represented in several different ways.
• There may exist several algorithms for solving the same problem.
• Algorithms for the same problem can be based on very different
ideas and can solve the problem with dramatically different speeds.
1.1 NOTION OF AN ALGORITHM
• Input : Zero / more quantities are externally supplied.
• Output : At least one quantity is produced.
• Definiteness : Each instruction is clear and unambiguous.
• Finiteness : If the instructions of an algorithm is traced then for
all cases the Algorithm must terminate after a finite
number of steps.
• Efficiency : Every instruction must be very basic and runs in
short time.
CHARACTERISTICS OF AN ALGORITHM
Algorithm Examples
• Algorithm to add two numbers
• Algorithm to find the largest among three numbers
• Algorithm to find all the roots of the quadratic equation
• Algorithm to find the factorial
• Algorithm to check prime number
• Algorithm of Fibonacci series Step 1: Start
Step 2: Declare variables num1, num2
and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign
the result to sum. sum←num1+num2
Step 5: Display sum Step
6: Stop
EXAMPLE: SORTING
 Statement of problem:
 Input:
A sequence of n numbers
<a1, a2, …, an>
 Output:
A reordering of the input sequence
<b1, b2, …, bn>
so that bi ≤ bj whenever i < j
EXAMPLE: SORTING
SELECTION SORT
• Input:
array a[0], …, a[n-1]
• Output:
array a sorted in non-decreasing order
• Algorithm:
for i=0 to n-1
swap a[i] with smallest of a[i],…a[n-1]
Steps for writing an algorithm
1. An algorithm is a procedure. It has two parts; the first part is head and
the second part is body.
2. The Head section consists of keyword Algorithm and Name of the
algorithm with parameter list. E.g. Algorithm name1(p1, p2,…,p3) The
head section also has the following:
1. //Problem Description:
2. //Input:
3. //Output:
3. In the body of an algorithm various programming constructs like if, for,
while and some statements like assignments are used.
4. The compound statements may be enclosed with { and } brackets. if, for,
while can be closed by endif, endfor, endwhile respectively. Proper indention
is must for block.
5. Comments are written using // at the beginning.
6. The identifier should begin by a letter and not by digit. It contains alpha
numeric letters after first letter. No need to mention data types.
7. The left arrow “←” used as assignment operator. E.g. v←10
8. Boolean operators (TRUE, FALSE), Logical operators (AND, OR, NOT) and
Relational operators (<,<=, >, >=,=, ≠, <>) are also used.
9. Input and Output can be done using read and write.
10. Array[], if then else condition, branch and loop can be also used in
algorithm.
Steps for writing an algorithm
Example:
• The greatest common divisor(GCD) of two nonnegative integers m and n
(not- both-zero), denoted gcd(m, n), is defined as the largest integer that
divides both m and n evenly, i.e., with a remainder of zero.
• Euclid’s algorithm is based on applying repeatedly the equality gcd(m, n)
= gcd(n, m mod n), where m mod n is the remainder of the division of m by
n, until m mod n is equal to 0. Since gcd(m,0) = m, the last value of m is
also the greatest common divisor of the initial m and n. gcd(60, 24) can be
computed as follows:gcd(60, 24)= gcd(24, 12) = gcd(12, 0) = 12.
Steps for writing an algorithm
Euclid’s algorithm for computing gcd(m, n) in simple steps
Step 1 If n = 0, return the value of m as the answer and stop; otherwise,
proceed to Step 2.
Step 2 Divide m by n and assign the value of the remainder to r.
Step 3 Assign the value of n to m and the value of r to n. Go to Step 1.
Euclid’s algorithm for computing gcd(m, n) expressed in pseudocode
• ALGORITHM Euclid_gcd(m, n)
//Computes gcd(m, n) by Euclid’s algorithm
//Input: Two nonnegative, not-both-zero integers m and n
//Output: Greatest common divisor of m and n
while n ≠ 0 do
r ←m mod n m←n
n←r
return m
Steps for writing an algorithm
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
• A sequence of steps involved in designing and analyzing an algorithm is
shown in the figure below.
1. Understanding the Problem
2. Ascertaining the Capabilities of the Computational Device
3. Choosing between Exact and Approximate Problem Solving
4. Algorithm Design Techniques
5. Designing an Algorithm and Data Structures
6. Methods of Specifying an Algorithm
7. Proving an Algorithm’s Correctness
8. Analyzing an Algorithm
9. Coding an Algorithm
FIGURE 1.2 Algorithm design and analysis process.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
1. Understanding the Problem
 This is the first step in designing of algorithm.
 Read the problem’s description carefully to understand the problem
statement completely.
 Ask questions for clarifying the doubts about the problem.
 Identify the problem types and use existing algorithm to find solution.
 Input (instance) to the problem and range of the input get fixed.
• A correct algorithm is not one that works most of the time, but one that
works correctly for all legitimate inputs.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
2. Ascertaining the Capabilities of the Computational Device
– In random-access machine (RAM), instructions are executed one after
another (The central assumption is that one operation at a time).
– Majority of the algorithms can be programmed for a computer closely
resembling the von Neumann machine. (random-access machine (RAM)
model)
– Algorithms designed to be executed on such machines are called sequential
algorithms.
– Algorithms that take advantage of concurrent execution capability are
called parallel algorithms.
– Should not worry about the speed and amount of memory of a computer at
if you design an algorithm for a scientific purpose.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
3. Choosing between Exact and Approximate Problem Solving
– The next principal decision is to choose between solving the problem
exactly or solving it approximately.
– An algorithm used to solve the problem exactly and produce correct
result is called an exact algorithm.
– If the problem is so complex and not able to get exact solution, then we
have to choose an algorithm called an approximation algorithm. i.e.,
produces an approximate answer. E.g., extracting square roots, solving
nonlinear equations, and evaluating definite integrals.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
Algorithm Design Techniques
4. 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
Algorithms+ Data Structures = Programs
5. Though Algorithms and Data Structures are independent, but they are
combined together to develop program. Hence the choice of proper data
structure is required before designing the algorithm.
 Implementation of algorithm is possible only with the help of Algorithms
and Data Structures
 Algorithmic strategy / technique / paradigm are a general approach by
which many problems can be solved algorithmically. E.g., Brute Force,
Divide and Conquer, Dynamic Programming, Greedy Technique and so on.
6. Methods of Specifying an Algorithm
• There are three ways to specify an algorithm. They are:
• Natural language
• Pseudocode
FIGURE 1.3 Algorithm Specifications
• Pseudocode and flowchart are the two options that are most widely used
nowadays for specifying algorithms.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
a) Natural Language
• It is very simple and easy to specify an algorithm using natural language. But many
times specification of algorithm by using natural language is not clear and thereby
we get brief specification.
• Step 1: Read the first number, say a. Step 2: Read the first number, say b.
• Step 3: Add the above two numbers and store the result in c. Step 4: Display the
result from c.
Example: An algorithm to perform addition of two numbers. Such a specification
creates difficulty while actually implementing it. Hence many programmers prefer
to have specification of algorithm by means of Pseudocode.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
b) Pseudocode
– Pseudocode is a mixture of a natural language and programming language
constructs. Pseudocode is usually more precise than natural language.
– For Assignment operation left arrow “←”, for comments two slashes “//”
,if condition, for, while loops are used.
ALGORITHM Sum(a,b)
//Problem Description: This algorithm performs addition of two numbers
//Input: Two integers a and b
//Output: Addition of two integers c←a+b
return c
This specification is more useful for implementation of any language.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
c) Flowchart
In the earlier days of computing, the dominant method for specifying algorithms
was a flowchart, this representation technique has proved to be inconvenient.
Flowchart is a graphical representation of an algorithm. It is a a method of
expressing an algorithm by a collection of connected geometric shapes containing
descriptions of the algorithm’s steps.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
FIGURE 1.4 Flowchart symbols and Example for two integer addition.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
7. Proving an Algorithm’s Correctness
 Once an algorithm has been specified then its correctness must be proved.
 An algorithm must yields a required result for every legitimate input in a finite
amount of time.
For example, the correctness of Euclid’s algorithm for computing the greatest common
divisor stems from the correctness of the equality gcd(m, n) = gcd(n, m mod n).
 A common technique for proving correctness is to use mathematical induction
because an algorithm’s iterations provide a natural sequence of steps needed for
such proofs.
 The notion of correctness for approximation algorithms is less straightforward
than it is for exact algorithms. The error produced by the algorithm should not
exceed a predefined limit.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
8. Analyzing an Algorithm
 For an algorithm the most important is efficiency. In fact, there are two kinds
of algorithm efficiency. They are:
 Time efficiency, indicating how fast the algorithm runs, and
 Space efficiency, indicating how much extra memory it uses.
 The efficiency of an algorithm is determined by measuring both time efficiency
and space efficiency.
 So factors to analyze an algorithm are:
» Time efficiency of an algorithm
» Space efficiency of an algorithm
» Simplicity of an algorithm
» Generality of an algorithm
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
9. Coding an Algorithm
 The coding / implementation of an algorithm is done by a suitable
programming language like C, C++, JAVA.
 The transition from an algorithm to a program can be done either incorrectly or
very inefficiently. Implementing an algorithm correctly is necessary. The
Algorithm power should not reduced by inefficient implementation.
 Standard tricks like computing a loop’s invariant (an expression that does not
change its value) outside the loop, collecting common subexpressions,
replacing expensive operations by cheap ones, selection of programming
language and so on should be known to the programmer.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
Coding an Algorithm-Contd
– Typically, such improvements can speed up a program only by a constant
factor, whereas a better algorithm can make a difference in running time by
orders of magnitude. But once an algorithm is selected, a 10–50% speedup
may be worth an effort.
– It is very essential to write an optimized code (efficient code) to reduce the
burden of compiler.
FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
1.2 IMPORTANT PROBLEM TYPES
The most important problem types are:
• Sorting
• Searching
• String processing
• Graph problems
• Combinatorial problems
• Geometric problems
• Numerical problems
i. Sorting
– The sorting problem is to rearrange the items of a given list in nondecreasing
(ascending) order.
– Sorting can be done on numbers, characters, strings or records.
– To sort student records in alphabetical order of names or by student number or
by student grade-point average. Such a specially chosen piece of information is
called a key.
– An algorithm is said to be in-place if it does not require extra memory, E.g.,
Quick sort.
– A sorting algorithm is called stable if it preserves the relative order of any two
equal elements in its input.
1.2 IMPORTANT PROBLEM TYPES
https://visualgo.net/bn/sorting
ii. Searching
– The searching problem deals with finding a given value, called a search key,
in a given set.
– E.g., Ordinary Linear search and fast binary search.
iii. String processing
– A string is a sequence of characters from an alphabet.
– Strings comprise letters, numbers, and special characters; bit strings, which
comprise zeros and ones; and gene sequences, which can be modeled by strings
of characters from the four- character alphabet {A, C, G, T}. It is very useful in
bioinformatics.
– Searching for a given word in a text is called string matching
1.2 IMPORTANT PROBLEM TYPES
iv. Graph problems
– A graph is a collection of points called vertices, some of which are connected
by line segments called edges.
– Some of the graph problems are graph traversal, shortest path algorithm,
topological sort, traveling salesman problem and the graph-coloring problem
and so on.
v. Combinatorial problems
– These are problems that ask, explicitly or implicitly, to find a combinatorial
object such as a permutation, a combination, or a subset that satisfies certain
constraints.
– A desired combinatorial object may also be required to have some additional
property such s a maximum value or a minimum cost.
1.2 IMPORTANT PROBLEM TYPES
– In practical, the combinatorial problems are the most difficult problems in
computing.
– The traveling salesman problem and the graph coloring problem are
examples of combinatorial problems.
vi. Geometric problems
– Geometric algorithms deal with geometric objects such as points, lines, and
polygons.
– Geometric algorithms are used in computer graphics, robotics, and tomography.
– The closest-pair problem and the convex-hull problem are comes under this
category.
1.2 IMPORTANT PROBLEM TYPES
vii. Numerical problems
– Numerical problems are problems that involve mathematical equations,
systems of equations, computing definite integrals, evaluating functions, and so
on.
– The majority of such mathematical problems can be solved only
approximately.
1.2 IMPORTANT PROBLEM TYPES
1.3 FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM
EFFICIENCY
• The efficiency of an algorithm can be in terms of time and space. The algorithm
efficiency can be analyzed by the following ways.
• Analysis Framework.
• Asymptotic Notations and its properties.
• Mathematical analysis for Recursive algorithms.
• Mathematical analysis for Non-recursive algorithms.
1.4 Analysis Framework
• There are two kinds of efficiencies to analyze the efficiency of any algorithm. They
are:
• Time efficiency, indicating how fast the algorithm runs, and
• Space efficiency, indicating how much extra memory it uses. The algorithm
analysis framework consists of the following:
ɷ Measuring an Input’s Size
ɷ Units for Measuring Running Time
ɷ Orders of Growth
ɷ Worst-Case, Best-Case, and Average-Case Efficiencies
ɷ Recapitulation of the Analysis Framework
1. Measuring an Input’s Size
– An algorithm’s efficiency is defined as a function of some parameter n
indicating the algorithm’s input size. In most cases, selecting such a parameter
is quite straightforward. For example, it will be the size of the list for problems
of sorting, searching.
– For the problem of evaluating a polynomial p(x) = anxn + . . . + a0 of degree n,
the size of the parameter will be the polynomial’s degree or the number of its
coefficients, which is larger by 1 than its degree.
– In computing the product of two n × n matrices, the choice of a parameter
indicating an input size does matter.
1.4 Analysis Framework
i. Measuring an Input’s Size(contd)
• Consider a spell-checking algorithm. If the algorithm examines individual
characters of its input, then the size is measured by the number of characters.
• In measuring input size for algorithms solving problems such as checking primality
of a positive integer n. the input is just one number.
• The input size by the number b of bits in the n’s binary representation is
b=(log2 n)+1.
1.4 Analysis Framework
ii. Units for Measuring Running Time
• Some standard unit of time measurement such as a second, or millisecond, and so
on can be used to measure the running time of a program after implementing the
algorithm.
• Drawbacks
– Dependence on the speed of a particular computer.
– Dependence on the quality of a program implementing the algorithm.
– The compiler used in generating the machine code.
• The difficulty of clocking the actual running time of the program.
1.4 Analysis Framework
ii. Units for Measuring Running Time(contd)
• So, we need metric to measure an algorithm’s efficiency that does not depend
on these extraneous factors.
• One possible approach is to count the number of times each of the algorithm’s
operations is executed. This approach is excessively difficult.
• The most important operation (+, -, *, /) of the algorithm, called the basic
operation. Computing the number of times the basic operation is executed is
easy. The total running time is determined by the basic operation count.
1.4 Analysis Framework
iii. Orders of Growth
• A difference in running times on small inputs is not what really distinguishes
efficient algorithms from inefficient ones.
• For example, the greatest common divisor of two small numbers, it is not
immediately clear how much more efficient Euclid’s algorithm is compared to the
other algorithms, the difference in algorithm efficiencies becomes clear for larger
numbers only.
• For large values of n, it is the function’s order of growth that counts just like the
Table 1.1, which contains values of a few functions particularly important for
analysis of algorithms.
1.4 Analysis Framework
TABLE 1.1 Values (approximate) of several functions important for analysis of
algorithms
n √n log2n n n log2n n2 n3 2n n!
1 1 0 1 0 1 1 2 1
2 1.4 1 2 2 4 4 4 2
4 2 2 4 8 16 64 16 24
8 2.8 3 8 2.4•101 64 5.1•102 2.6•102 4.0•104
10 3.2 3.3 10 3.3•101 102 103 103 3.6•106
16 4 4 16 6.4•101 2.6•102 4.1•103 6.5•104 2.1•1013
102 10 6.6 102 6.6•102 104 106 1.3•1030 9.3•10157
103 31 10 103 1.0•104 106 109
Very big
computation
104 102 13 104 1.3•105 108 1012
105 3.2•102 17 105 1.7•106 1010 1015
106 103 20 106 2.0•107 1012 1018
1.4 Analysis Framework
iv. Worst-Case, Best-Case, and Average- Case Efficiencies
Consider Sequential Search algorithm some search key K
ALGORITHM Sequential Search(A[0..n -1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n - 1] and a search key K
//Output: The index of the first element in A that matches K or -1 if there are no
// matching elements
i ←0
while i < n and A[i] ≠ K do
i ←i + 1 if i < n return i else return -1
1.4 Analysis Framework
• Clearly, the running time of this algorithm can be quite different for the same list
size n.
• In the worst case, there is no matching of elements or the first matching element
can found at last on the list. In the best case, there is matching of elements at first
on the list.
Worst-case efficiency
– The worst-case efficiency of an algorithm is its efficiency for the worst
case input of size n.
– The algorithm runs the longest among all possible inputs of that size.
– For the input of size n, the running time is Cworst(n) = n.
1.4 Analysis Framework
Best case efficiency
– The best-case efficiency of an algorithm is its efficiency for the best case input
of size n.
– The algorithm runs the fastest among all possible inputs of that size n.
– In sequential search, If we search a first element in list of size n. (i.e. first
element equal to a search key), then the running time is Cbest(n) = 1
1.4 Analysis Framework
• Average case efficiency
– The Average case efficiency lies between best case and worst case.
– To analyze the algorithm’s average case efficiency, we must make some
assumptions about possible inputs of size n.
– The standard assumptions are that
• The probability of a successful search is equal to p (0 ≤ p ≤ 1) and
• The probability of the first match occurring in the ith position of the list is
the same for every i.
1.4 Analysis Framework
1.4 Analysis Framework
Yet another type of efficiency is called 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.
Average case efficiency(contd)
1.5 ASYMPTOTIC NOTATIONS & ITS PROPERTIES
• Asymptotic notation is a notation, which is used to take meaningful statement about
the efficiency of a program.
• The efficiency analysis framework concentrates on the order of growth of an
algorithm’s basic operation count as the principal indicator of the algorithm’s
efficiency.
• To compare and rank such orders of growth, computer scientists use three notations,
they
• O - Big oh notation
• Ω - Big omega notation
• Θ - Big theta notation
1<logn< <n<nlogn<n2 <n3 <………….<2n<3n<….nn
1<logn< <n<2n<n2 <n3
• O - Big oh notation…………….Upper Bound
• Ω - Big omega notation………..Lower Bound
• Θ - Big theta notation…………..Average Bound
Which is useful??
Θ - Big theta notation…………..Average Bound
If you cannot specify the exact place of any function out these then we can show the
upper bound of that function
1.5 ASYMPTOTIC NOTATIONS & ITS PROPERTIES
1. O - Big oh notation
Definition
• A function f(n) = O(g(n)) if and if there exist positive constants C and n0
Such that f(n) <=C*g(n)Ɐ n> n0
1<logn< <n<2 n<n2 <n3
Example
f(n)=2n+3
2n+3<=……..
2n+3<=10n………….n=1 5<=10
f(n)= 2n+3
g(n)=n
C=10
There fore f(n)=O(n)
2n^2+3n^2
F(n)=O(n^2)
F(n)=O(2^n)
2n+3<=2n+3n
1. O - Big oh notation
Why is this so important ?
Let’s say you have 2 sorting applications,
 Application A which uses bubble sort and
 Application B which uses merge sort
 Lets say that for input sizes of around 30 elements
 Application A is 1,000x faster than application B at sorting.
 If you never have to sort much more that 30 elements then its obvious that you
should prefer application A, as it is much faster at these input sizes. However, if
you find that you may have to sort ten million items then what you’d expect is
that application B actually ends up being thousands of times faster than
application A in this case, entirely due to the way each algorithm scales
1. O - Big oh notation
O(1)
• It dosent contain loop, recursion
int temp = x;
x = y;
y = temp;
loopvariablesisincremented/decrementedbya
constantamount
for(inti=1;i<=n; i+=c)
{
//someexpressions
}
O(n)
• nested loops isequalto the numberof times the innermost
statement isexecuted
for(inti=1;i<=n;i+=c)
{
for (intj=1;j <=n;j+=c)
{
//someexpressions
}
}
Selectionsort andInsertion Sort haveO(n2)timecomplexity
O(n^2)
O(log n)
Theloopvariablesisdivided/multipliedbyaconstantamount
for(inti=1;i<=n;i*=c)
{
//someexpressions
}
for(inti=n;i>0;i/=c)
{
//someexpressions
}
BinarySearchhasO(Logn)timecomplexity.
Quiz-1
1. What is the time, space complexity of following code:
filter_none
brightness_4
int a = 0, b = 0;
for (i = 0; i < N; i++) {
a = a + rand();
}
for (j = 0; j < M; j++) {
b = b + rand();
Options:
O(N * M) time, O(1) space
O(N + M) time, O(N + M) space
O(N + M) time, O(1) space
O(N * M) time, O(N + M) space
Answer :
O(N + M) time, O(1) space
Explanation:
The first loop is O(N) and the second loop is O(M). Since we don’t know which
is bigger, we say this is O(N + M). This can also be written as O(max(N, M)).
Since there is no additional space being utilized, the space complexity is
constant / O(1)
Quiz-1
O(n^3)
• To simplify the running time estimation, for a function f(n),we ignore the
constants and lower order terms
• 7n-2 =O(n)
• 3n3 +20n2+5 =O(n3)
• 3logn+5 =O(logn)
1. O - Big oh notation
Seven function that often appear in algorithm analysis:
Comparing Growth Rates
Notation Common Name Example
O(1) Constant Time Array Index Look up
O(log n) Logarithmic Time Binary Search
O(n) Linear Time Linear Search
O(n log n) Log-Linear Time Heap Sort
O(n2) Quadratic Time Bubble Sort
O(nc) Polynomial
O(cn) Exponential Travelling Salesman Problem
O(n!) Factorial Brute Force Travelling Salesman
O(nn) N to the N
1. O - Big oh notation
1. O - Big oh notation
1. O - Big oh notation
1. O - Big oh Complexity Chart
1. What is the time complexity of following code:
int a = 0;
for (i = 0; i < N; i++) {
for (j = N; j > i; j--) {
a = a + i + j;
}
}
Quiz-2
Options:
O(N)
O(N*log(N))
O(N * Sqrt(N))
O(N*N)
Answer :
O(N*N)
Explanation:
The above code runs total no of times
= N + (N – 1) + (N – 2) + … 1 + 0
= N * (N + 1) / 2
= 1/2 * N^2 + 1/2 * N
O(N^2) times.
Quiz-2
2. What is the time complexity of following code:
int i, j, k = 0;
for (i = n / 2; i <= n; i++) {
for (j = 2; j <= n; j = j * 2) {
k = k + n / 2;
}
}
Options:
O(n)
O(nLogn)
O(n^2)
O(n^2Logn)
Quiz-2
Answer
O(nLogn)
Explanation: If you notice, j keeps doubling till it is less than or equal to n.
Number of times, we can double a number till it is less than n would be log(n).
Let’s take the examples here.
for n = 16, j = 2, 4, 8, 16
for n = 32, j = 2, 4, 8, 16, 32
So, j would run for O(log n) steps.
i runs for n/2 steps.
So, total steps = O(n/ 2 * log (n)) = O(n*logn)
Quiz-3
3. Give the correct matching for the following
pairs:
• A.O(logn)
• B.O(nlogn)
• C.O(n^2)
• 1.Selectionsort
• 2. Binarysearch
• 3. Mergesort
Quiz-2
Example 2: Prove the assertions 100n + 5 ∈ 0(n2).
Proof: 100n + 5 ≤ 100n + n (for all n ≥ 5)
= 101n
≤ 101n2 (therefore n ≤ n2)
Since, the definition gives us a lot of freedom in choosing specific values for constants
c and n0. We have c=101 and n0=5
1. O - Big oh notation
Example 3: Prove the assertions 100n + 5 ∈ 0(n).
Proof: 100n + 5 ≤ 100n + 5n (for all n ≥ 1)
= 105n
i.e., 100n + 5 ≤ 105n
i.e., t(n) ≤ cg(n)
Therefore 100n + 5 ∈ 0(n) with c=105 and n0=1
1. O - Big oh notation
Example:
t(n)<=c*g(n)
t(n)=2n+2 g(n)=n^2
Step 1:
t(1)=2(1)+2 g(n)=1
t(1)=4<=1……..condition false
Step 2:
t(2)=2(2)+2 g(n)=4
t(1)=6<=4……..condition false
Step 3:
t(3)=2(3)+2 g(n)=9
t(1)=8<=9……..condition True
t(n) ∈ O g(n) when n=3
1. O - Big oh notation
2. Ω - Big omega notation
• Big Ω = Best Case
• Big Ω is used to describe the lower bound(best case)
• A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is
bounded below by some positive 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.
• Where t(n) and g(n) are nonnegative functions defined on the set of natural
numbers.
• Ω = Asymptotic lower bound = Useful for best case analysis = Loose bound
FIGURE 1.6 Big-omega notation: t (n) ∈ Ω (g(n)).
2. Ω - Big omega notation
Example 4: Prove the assertions
n3+10n2+4n+2 ∈ Ω (n2).
Proof: n3+10n2+4n+2 ≥ n2 (for all n ≥ 0)
i.e., by definition t(n) ≥ cg(n), where
c=1 and n0=0
Example:
t(n)>=c*g(n)
t(n)=2n+2 g(n)=n
Step 1
t(0)=2(0)+2 g(n)=0
=2
t(0)>=g(0)
Step 2
t(1)=2(1)+2 g(n)=1
=4
t(1)>=g(1)
t(n) ∈ Ω g(n) when n=0
2. Ω - Big omega notation
Big Θ =Average Case(average time that the algorithm will use to produce)
The algorithm has a complexity with lower bound = upper bound , say that
algorithm has a complexity
O(n log n) and Ω (n log n)
It actually has the complexity of Θ (n log n), which means the running time of
that algorithm always falls in n log n is the best and worst case
F(n)= Θ (n log n)
3. Θ - Big theta notation
Example:
C1g(n)<=t(n)<=c2g(n)
n<=t(n)<=n^2
n<=2n+2<=n^2
3. Θ - Big theta notation
Θ = Asymptotic tight bound = Useful for average case analysis
FIGURE 1.7 Big-theta notation: t (n) ∈ Θ(g(n)).
3. Θ - Big theta notation
Try ?
5n+2
Big O, Ω, Θ
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 Ω and Θ notations as well.)
PROOF: The proof extends to orders of growth the following simple fact about
four arbitrary real numbers a1, b1, a2, b2: if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2
max{b1, b2}.
Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 and some nonnegative
integer n1 such that
t1(n) ≤ c1g1(n) for all n ≥ n1.
Similarly, since t2(n) ∈ O(g2(n)),
t2(n) ≤ c2g2(n) for all n ≥ n2.
3. Θ - Big theta notation
Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so
that we can use both inequalities. Adding them yields the following:
t1(n) + t2(n) ≤ c1g1(n) + c2g2(n)
≤ c3g1(n) + c3g2(n)
= c3[g1(n) + g2(n)]
≤ c32 max{g1(n), g2(n)}.
Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants c and n0 required by the
definition O being 2c3 = 2 max{c1, c2} and max{n1, n2}, respectively.
The property implies that the algorithm’s overall efficiency will be determined by the
part with a higher order of growth, i.e., its least efficient part.
3. Θ - Big theta notation
therefore t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n),
g2(n)}).
Basic rules of sum manipulation Summation formulas
3. Θ - Big theta notation
MATHEMATICAL ANALYSIS FOR RECURSIVE
ALGORITHMS General Plan for Analyzing the Time Efficiency of Recursive
Algorithms
• Decide on a parameter (or parameters) indicating an input’s size.
• Identify the algorithm’s basic operation.
• Check whether the number of times the basic operation is executed can
vary on different inputs of the same size; if it can, the worst-case, average-
case, and best-case efficiencies must be investigated separately.
• Set up a recurrence relation, with an appropriate initial condition, for the
number of times the basic operation is executed.
• Solve the recurrence or, at least, ascertain the order of growth of its
solution.
EXAMPLE 1: Compute the factorial function F(n) = n! for an arbitrary nonnegative
integer n. Since n!= 1•……• (n − 1) • n = (n − 1)! • n, for n ≥ 1 and 0!= 1 by
definition, we can compute F(n) = F(n − 1) • n with the following recursive
algorithm.
ALGORITHM F(n)
//Computes n! recursively 5!=5X4X3X2X1
//Input: A nonnegative integer n =5X4!
//Output: The value of n! 4X3!
if n = 0 return 1 3X2!
else return F(n − 1) * n 2X1!
1X0!
MATHEMATICAL ANALYSIS FOR RECURSIVE
Algorithm analysis
For simplicity, we consider n itself as an indicator of this algorithm’s input size.
i.e. 1.
The basic operation of the algorithm is multiplication, whose number of executions we
denote M(n). Since the function F(n) is computed according to the formula F(n) =
F(n −1)•n for n > 0.
The number of multiplications M(n) needed to compute it must satisfy the equality
M(n) = M(n-1) + 1 for n > 0
To compute To multiply
F(n-1) F(n-1) by n
M(n − 1) multiplications are spent to compute F(n − 1), and one more multiplication is
needed to multiply the result by n.
MATHEMATICAL ANALYSIS FOR RECURSIVE
Recurrence relations
The last equation defines the sequence M(n) that we need to find. This equation defines
M(n) not explicitly, i.e., as a function of n, but implicitly as a function of its value at
another point, namely n − 1. Such equations are called recurrence relations or
recurrences.
Solve the recurrence relation M(n) = M(n − 1) + 1, i.e., to find an explicit
formula for
M(n) in terms of n only.
To determine a solution uniquely, we need an initial condition that tells us
MATHEMATICAL ANALYSIS FOR RECURSIVE
the value with which the sequence starts. We can obtain this value by inspecting the
condition that makes the algorithm stop its recursive calls:
if n = 0 return 1.
This tells us two things. First, since the calls stop when n = 0, the smallest value of n
for which this algorithm is executed and hence M(n) defined is 0. Second, by
inspecting the pseudocode’s exiting line, we can see that when n = 0, the algorithm
performs no multiplications.
Thus, the recurrence relation and initial condition for the algorithm’s number of
multiplications
M(n):
M(n) = M(n −1) + 1 for n > 0, M(0) = 0for n = 0.
MATHEMATICAL ANALYSIS FOR RECURSIVE
Method of backward substitutions
M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1
= [M(n − 2) + 1]+ 1
= M(n − 2) + 2 substitute M(n − 2) = M(n − 3) + 1
= [M(n − 3) + 1]+ 2
= M(n − 3) + 3
…
= M(n − i) + i
…
= M(n − n) + n
= n.
Therefore M(n)=n
MATHEMATICAL ANALYSIS FOR RECURSIVE
EXAMPLE 2:
Consider educational workhorse of recursive algorithms: the Tower of Hanoi puzzle.
We have n disks of different sizes that can slide onto any of three pegs. Consider A
(source), B (auxiliary), and C (Destination). Initially, all the disks are on the first
peg in order of size, the largest on the bottom and the smallest on top. The goal is to
move all the disks to the third peg, using the second one as an auxiliary.
MATHEMATICAL ANALYSIS FOR RECURSIVE
FIGURE 1.8 Recursive solution to the Tower of Hanoi puzzle.
MATHEMATICAL ANALYSIS FOR RECURSIVE
ALGORITHM TOH(n, A, C, B)
//Move disks from source to destination recursively
//Input: n disks and 3 pegs A, B, and C
//Output: Disks moved to destination as in the source order.
if n=1
Move disk from A to C else
Move top n-1 disks from A to B using C TOH(n - 1, A, B, C)
Move the one disk left on A to C using B
Move top n-1 disks from B to C using A TOH(n - 1, B, C, A)
MATHEMATICAL ANALYSIS FOR RECURSIVE
Recurrence Relations
M(n)=M(n-1)+1+M(n-1)
Initial Condition
M(1)=1
M(n)=2M(n-1)+1 …………………for n>1
M(n-1)=2[M(n-2)]+1
M(n)=2[2[M(n-2)]+1]+1
=22[M(n-2)]+2+1
M(n-2)=2[M(n-3)]+1
= 22 [2[M(n-3)]+1]2+1
= 23 M(n-3)+ 22 +2+1
…..
…..
MATHEMATICAL ANALYSIS FOR RECURSIVE
= 2i M(n-i)+ 2i-1+......2+1 20+21 + 22+…..2k
= 2i M(n-i)+2i-1+1 -1
= 2i M(n-i)+2 i -1
…….
……
……………………………………………….i=n-1(recursive function)
2n-1 M(n-n-1)+2 n-1 -1
2n-1+2 n-1 -1
MATHEMATICAL ANALYSIS FOR RECURSIVE
MATHEMATICAL ANALYSIS FOR RECURSIVE
EXAMPLE 3: An investigation of a recursive version of the algorithm which
finds the number of binary digits in the binary representation of a positive
decimal integer.
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 return 1
else return BinRec(⎝n/2])+ 1
MATHEMATICAL ANALYSIS FOR RECURSIVE
Algorithm analysis
The number of additions made in computing BinRec(⎝n/2]) is A(⎝n/2]), plus
one more addition is made by the algorithm to increase the returned value by
1. This leads to the recurrence A(n) = A(⎝n/2]) + 1 for n > 1. then, the initial
condition is A(1) = 0.
The standard approach to solving such a recurrence is to solve it
only for n = 2k A(2k) = A(2k−1) + 1 for k > 0, A(20) = 0.
MATHEMATICAL ANALYSIS FOR RECURSIVE
backward substitutions
A(2k) = A(2k−1) + 1 substitute A(2k−1) = A(2k−2) + 1
= [A(2k−2) + 1]+ 1= A(2k−2) + 2 substitute A(2k−2) = A(2k−3) + 1
= [A(2k−3) + 1]+ 2 = A(2k−3) + 3 . . .
. . .
= A(2k−i) + i
. . .
= A(2k−k) + k.
Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original variable n
= 2k and hence k = log2 n,
A(n) = log2 n ϵ Θ (log2 n).
MATHEMATICAL ANALYSIS FOR RECURSIVE
MATHEMATICALANALYSIS FOR NON-RECURSIVE
ALGORITHMS General Plan for Analyzing the Time
Efficiency of Nonrecursive Algorithms
• Decide on a parameter (or parameters) indicating an input’s size.
• Identify the algorithm’s basic operation (in the innermost loop).
• Check whether the number of times the basic operation is executed
depends only on the size of an input. If it also depends on some additional
property, the worst-case, average-case, and, if necessary, best-case
efficiencies have to be investigated separately.
• Set up a sum expressing the number of times the algorithm’s basic
operation is executed.
• Using standard formulas and rules of sum manipulation either find a closed
form formula for the count or at the least, establish its order of growth.
Algorithm analysis
• The measure of an input’s size here is the number of elements in the array, i.e., n.
• There are two operations in the for loop’s body:
 The comparison A[i]> maxval and
 The assignment maxval←A[i].
• The comparison operation is considered as the algorithm’s basic operation, because
the comparison is executed on each repetition of the loop and not the assignment.
• The number of comparisons will be the same for all arrays of size n; therefore, there
is no need to distinguish among the worst, average, and best cases here.
MATHEMATICALANALYSIS FOR NON-RECURSIVE
Let C(n) denotes the number of times this comparison is executed. The algorithm
makes one comparison on each execution of the loop, which is repeated for each
value of the loop’s variable i within the bounds 1 and n − 1, inclusive. Therefore,
the sum for C(n) is calculated as follows:
MATHEMATICALANALYSIS FOR NON-RECURSIVE
EXAMPLE 2: Consider the element uniqueness problem: check whether all the
Elements in a given array of n elements are distinct.
ALGORITHM UniqueElements(A[0..n − 1])
//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct and “false” otherwise
for i ←0 to n − 2 do
for j ←i + 1 to n − 1 do
if A[i]= A[j ] return false
return true
MATHEMATICALANALYSIS FOR NON-RECURSIVE
Algorithm analysis
• The natural measure of the input’s size here is again n (the number of elements
in the array).
• Since the innermost loop contains a single operation (the comparison of two
elements), we
• should consider it as the algorithm’s basic operation.
• The number of element comparisons depends not only on n but also on whether
there are equal elements in the array and, if there are, which array positions
they occupy. We will limit our investigation to the worst case only.
MATHEMATICALANALYSIS FOR NON-RECURSIVE
One comparison is made for each repetition of the innermost loop, i.e., for each
value of the loop variable j between its limits i + 1 and n − 1; this is repeated for
each value of the outer loop, i.e., for each value of the loop variable i between its
limits 0 and n − 2.
MATHEMATICALANALYSIS FOR NON-RECURSIVE
Design and Analysis of Algorithm ppt for unit one

More Related Content

What's hot (20)

Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Classical Planning
Classical PlanningClassical Planning
Classical Planning
 
TOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of ComputationTOC 1 | Introduction to Theory of Computation
TOC 1 | Introduction to Theory of Computation
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Brute force method
Brute force methodBrute force method
Brute force method
 
Daa
DaaDaa
Daa
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Lecture 8 dynamic programming
Lecture 8 dynamic programmingLecture 8 dynamic programming
Lecture 8 dynamic programming
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Artificial Intelligence Searching Techniques
Artificial Intelligence Searching TechniquesArtificial Intelligence Searching Techniques
Artificial Intelligence Searching Techniques
 
Gradient descent method
Gradient descent methodGradient descent method
Gradient descent method
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Game playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graphGame playing (tic tac-toe), andor graph
Game playing (tic tac-toe), andor graph
 

Similar to Design and Analysis of Algorithm ppt for unit one

UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptracha49
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptBhargaviDalal4
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfNayanChandak1
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptxssuser586772
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptTekle12
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptTekle12
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDeepikaV81
 
Algorithms notes 2 tutorials duniya
Algorithms notes 2   tutorials duniyaAlgorithms notes 2   tutorials duniya
Algorithms notes 2 tutorials duniyaTutorialsDuniya.com
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptxnikshaikh786
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithmsPraveen M Jigajinni
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)praveena p
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfHarshNagda5
 

Similar to Design and Analysis of Algorithm ppt for unit one (20)

UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Chapter1.1 Introduction.ppt
Chapter1.1 Introduction.pptChapter1.1 Introduction.ppt
Chapter1.1 Introduction.ppt
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.pptChapter1.1 Introduction to design and analysis of algorithm.ppt
Chapter1.1 Introduction to design and analysis of algorithm.ppt
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Algorithms notes 2 tutorials duniya
Algorithms notes 2   tutorials duniyaAlgorithms notes 2   tutorials duniya
Algorithms notes 2 tutorials duniya
 
Module 1_ Introduction.pptx
Module 1_ Introduction.pptxModule 1_ Introduction.pptx
Module 1_ Introduction.pptx
 
Chapter 09 design and analysis of algorithms
Chapter 09  design and analysis of algorithmsChapter 09  design and analysis of algorithms
Chapter 09 design and analysis of algorithms
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 
Unit 1 python (2021 r)
Unit 1 python (2021 r)Unit 1 python (2021 r)
Unit 1 python (2021 r)
 
Lec1.ppt
Lec1.pptLec1.ppt
Lec1.ppt
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdf
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 

Recently uploaded

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 

Recently uploaded (20)

Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 

Design and Analysis of Algorithm ppt for unit one

  • 1. SCHOOL OF COMPUTING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING 10211CS202-DESIGN AND ANALYSIS OF ALGORITHMS UNIT 1
  • 2. Course Outcomes INTRODUCTION TO DESIGN AND ANALYSIS OF ALGORITHM
  • 3. COURSE DETAILS • Course Code : 10211CS202 • Course Name : Design and Analysis of Algorithms • Category : Program Core • Pre- Requisites :10211CS102-Data Structures • No of Credits : 4 • No of Hours/Week : 6
  • 4. Preamble For an engineer, problem solving is not about just solving a problem somehow but about solving the problem in the most effective and efficient way. Two key skills that a software professional needs are (1) to choose suitable data structures to store the information part of the problem, and (2) use of efficient algorithms for developing a programming solution of a given problem. Selection of a particular data structure greatly influences the characteristics of the obtained solution that include efficiency (performance, or speed), space (memory) requirements, scalability, reuse, and robustness (or reliability). The other equally important skill is to choose a suitable problem solving technique to apply to a particular problem. Acquiring these skills, greatly enhances the problem solving skills of the learner.
  • 5. Course Objectives • To gain experience in fundamental techniques used for effective problem solving in computing. • Analyze the asymptotic performance of algorithms. • Write rigorous correctness proofs for algorithms. • Demonstrate a familiarity with major algorithms and data structures. • Apply important algorithmic design paradigms and methods of analysis. • Synthesize efficient algorithms in common engineering design situations. • Familiarizing students with specific algorithms for a number of important computational problems like sorting, searching, and graphs, …etc,.
  • 6. INTRODUCTION Notion of an Algorithm – Fundamentals of Algorithmic Problem Solving – Important Problem Types – Analysis of Algorithm Efficiency - Introduction to Asymptotic Notations– Solving Recurrence relations – Master’s theorem - Mathematical analysis for Recursive and Nonrecursive algorithms. (Case Study: Design a gaming application like Chess, Candy crush etc.) Text Books 1. AnanyLevitin, “Introduction to the Design and Analysis of Algorithms”, Third Edition, Pearson Education, 2012. UNIT-1
  • 7. Design and Analysis of Algorithms • Analysis: predict the cost of an algorithm in terms of resources and performance • Design: design algorithms which minimize the cost
  • 8. To Study ?  Design and Analysis of Algorithm is very important for designing algorithm to solve different types of problems in the branch of computer science and information technology.  To introduces the fundamental concepts of Designing Strategies, Complexity analysis of Algorithms, followed by problems on Graph Theory and Sorting methods.  It is the core of computer science, and, in all fairness, can be said to be relevant to most of science, business, and technology
  • 9. What is an algorithm? Algorithm is a set of steps to complete a task. For example, Task: to make a cup of tea. Algorithm: • add water and milk to the kettle, • boil it, add tea leaves, • Add sugar, and then serve it in cup. Algorithm
  • 10. Computer algorithm What is Computer algorithm? • ‘’a set of steps to accomplish or complete a task that is described precisely enough that a computer can run it’’. Described precisely: very difficult for a machine to know how much water, milk to be added etc. in the above tea making algorithm. These algorithms run on computers or computational devices. Forexample, GPS in our smartphones, Google hangouts. GPS uses shortest path algorithm. Online shopping uses cryptography which uses RSA algorithm
  • 11. Characteristics of Algorithms The main characteristics of algorithms are as follows − • Algorithms must have a unique name • Algorithms should have explicitly defined set of inputs and outputs • Algorithms are well-ordered with unambiguous operations • Algorithms halt in a finite amount of time. • Algorithms should not run for infinity, i.e., an algorithm must end at some point
  • 12. Pseudocode • It is one of the methods that could be used to represent an algorithm. • It is not written in a specific syntax • Cannot be executed • Can be read and understood by programmers who are familiar with different programming languages. • Transformation from pseudo code to the corresponding program code easier. • Pseudo code allows to include control structures such as WHILE, IF- THEN-ELSE, REPEAT-UNTIL, FOR, and CASE, which are available in many high level languages.
  • 13. Difference between Algorithm &Pseudocode Algorithm Pseudo code A finite set of instructions or logic, written in order, to accomplish a certain predefined task. A generic way of describing an algorithm without using any specific programming language-related notations. It is just the core logic (solution) of a problem It is an outline of a program, written in a form which can easily be converted into real programming statements. Easy to understand the logic of a problem Can be read and understood by programmers who are familiar with different programming languages. Can be expressed either as an informal high level description as pseudo code or using a flowchart. It is not written in a specific syntax. It allows to include control structures such as WHILE, IF-THEN-ELSE, REPEAT- UNTIL, FOR, and CASE, which are present in many high level languages.
  • 14. Difference between Algorithm &Pseudocode Problem: Suppose there are 60 students in the class. How will you calculate the number of absentees in the class? Pseudo Approach: 1.Initialize a variable called as Count to zero, absent to zero, total to 60 2.FOR EACH Student PRESENT DO the following: Increase the Count by One 3.Then Subtract Count from total and store the result in absent 4.Display the number of absent students Algorithmic Approach: 1.Count <- 0, absent <- 0, total <- 60 2.REPEAT till all students counted Count <- Count + 1 3.absent <- total - Count 4.Print "Number absent is:" , absent
  • 15. What is an 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.
  • 16. What is an Algorithm ?
  • 17. What is an Algorithm ?
  • 18. What is an Algorithm ?
  • 19. What is an Algorithm ?
  • 20. What is an Algorithm ?
  • 41.
  • 42. 1.1 NOTION OF AN ALGORITHM FIGURE 1.1 The notion of the algorithm. • It is a step by step procedure with the input to solve the problem in a finite amount of time to obtain the required output.
  • 43. The notion of the algorithm illustrates some important points: • The non-ambiguity requirement for each step of an algorithm cannot be compromised. • The range of inputs for which an algorithm works has to be specified carefully. • The same algorithm can be represented in several different ways. • There may exist several algorithms for solving the same problem. • Algorithms for the same problem can be based on very different ideas and can solve the problem with dramatically different speeds. 1.1 NOTION OF AN ALGORITHM
  • 44. • Input : Zero / more quantities are externally supplied. • Output : At least one quantity is produced. • Definiteness : Each instruction is clear and unambiguous. • Finiteness : If the instructions of an algorithm is traced then for all cases the Algorithm must terminate after a finite number of steps. • Efficiency : Every instruction must be very basic and runs in short time. CHARACTERISTICS OF AN ALGORITHM
  • 45. Algorithm Examples • Algorithm to add two numbers • Algorithm to find the largest among three numbers • Algorithm to find all the roots of the quadratic equation • Algorithm to find the factorial • Algorithm to check prime number • Algorithm of Fibonacci series Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop
  • 46. EXAMPLE: SORTING  Statement of problem:  Input: A sequence of n numbers <a1, a2, …, an>  Output: A reordering of the input sequence <b1, b2, …, bn> so that bi ≤ bj whenever i < j
  • 48. SELECTION SORT • Input: array a[0], …, a[n-1] • Output: array a sorted in non-decreasing order • Algorithm: for i=0 to n-1 swap a[i] with smallest of a[i],…a[n-1]
  • 49. Steps for writing an algorithm 1. An algorithm is a procedure. It has two parts; the first part is head and the second part is body. 2. The Head section consists of keyword Algorithm and Name of the algorithm with parameter list. E.g. Algorithm name1(p1, p2,…,p3) The head section also has the following: 1. //Problem Description: 2. //Input: 3. //Output: 3. In the body of an algorithm various programming constructs like if, for, while and some statements like assignments are used.
  • 50. 4. The compound statements may be enclosed with { and } brackets. if, for, while can be closed by endif, endfor, endwhile respectively. Proper indention is must for block. 5. Comments are written using // at the beginning. 6. The identifier should begin by a letter and not by digit. It contains alpha numeric letters after first letter. No need to mention data types. 7. The left arrow “←” used as assignment operator. E.g. v←10 8. Boolean operators (TRUE, FALSE), Logical operators (AND, OR, NOT) and Relational operators (<,<=, >, >=,=, ≠, <>) are also used. 9. Input and Output can be done using read and write. 10. Array[], if then else condition, branch and loop can be also used in algorithm. Steps for writing an algorithm
  • 51. Example: • The greatest common divisor(GCD) of two nonnegative integers m and n (not- both-zero), denoted gcd(m, n), is defined as the largest integer that divides both m and n evenly, i.e., with a remainder of zero. • Euclid’s algorithm is based on applying repeatedly the equality gcd(m, n) = gcd(n, m mod n), where m mod n is the remainder of the division of m by n, until m mod n is equal to 0. Since gcd(m,0) = m, the last value of m is also the greatest common divisor of the initial m and n. gcd(60, 24) can be computed as follows:gcd(60, 24)= gcd(24, 12) = gcd(12, 0) = 12. Steps for writing an algorithm
  • 52. Euclid’s algorithm for computing gcd(m, n) in simple steps Step 1 If n = 0, return the value of m as the answer and stop; otherwise, proceed to Step 2. Step 2 Divide m by n and assign the value of the remainder to r. Step 3 Assign the value of n to m and the value of r to n. Go to Step 1. Euclid’s algorithm for computing gcd(m, n) expressed in pseudocode • ALGORITHM Euclid_gcd(m, n) //Computes gcd(m, n) by Euclid’s algorithm //Input: Two nonnegative, not-both-zero integers m and n //Output: Greatest common divisor of m and n while n ≠ 0 do r ←m mod n m←n n←r return m Steps for writing an algorithm
  • 53. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING • A sequence of steps involved in designing and analyzing an algorithm is shown in the figure below. 1. Understanding the Problem 2. Ascertaining the Capabilities of the Computational Device 3. Choosing between Exact and Approximate Problem Solving 4. Algorithm Design Techniques 5. Designing an Algorithm and Data Structures 6. Methods of Specifying an Algorithm 7. Proving an Algorithm’s Correctness 8. Analyzing an Algorithm 9. Coding an Algorithm
  • 54. FIGURE 1.2 Algorithm design and analysis process. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 55. 1. Understanding the Problem  This is the first step in designing of algorithm.  Read the problem’s description carefully to understand the problem statement completely.  Ask questions for clarifying the doubts about the problem.  Identify the problem types and use existing algorithm to find solution.  Input (instance) to the problem and range of the input get fixed. • A correct algorithm is not one that works most of the time, but one that works correctly for all legitimate inputs. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 56. 2. Ascertaining the Capabilities of the Computational Device – In random-access machine (RAM), instructions are executed one after another (The central assumption is that one operation at a time). – Majority of the algorithms can be programmed for a computer closely resembling the von Neumann machine. (random-access machine (RAM) model) – Algorithms designed to be executed on such machines are called sequential algorithms. – Algorithms that take advantage of concurrent execution capability are called parallel algorithms. – Should not worry about the speed and amount of memory of a computer at if you design an algorithm for a scientific purpose. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 57. 3. Choosing between Exact and Approximate Problem Solving – The next principal decision is to choose between solving the problem exactly or solving it approximately. – An algorithm used to solve the problem exactly and produce correct result is called an exact algorithm. – If the problem is so complex and not able to get exact solution, then we have to choose an algorithm called an approximation algorithm. i.e., produces an approximate answer. E.g., extracting square roots, solving nonlinear equations, and evaluating definite integrals. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 58. Algorithm Design Techniques 4. 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 Algorithms+ Data Structures = Programs 5. Though Algorithms and Data Structures are independent, but they are combined together to develop program. Hence the choice of proper data structure is required before designing the algorithm.  Implementation of algorithm is possible only with the help of Algorithms and Data Structures  Algorithmic strategy / technique / paradigm are a general approach by which many problems can be solved algorithmically. E.g., Brute Force, Divide and Conquer, Dynamic Programming, Greedy Technique and so on.
  • 59. 6. Methods of Specifying an Algorithm • There are three ways to specify an algorithm. They are: • Natural language • Pseudocode FIGURE 1.3 Algorithm Specifications • Pseudocode and flowchart are the two options that are most widely used nowadays for specifying algorithms. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 60. a) Natural Language • It is very simple and easy to specify an algorithm using natural language. But many times specification of algorithm by using natural language is not clear and thereby we get brief specification. • Step 1: Read the first number, say a. Step 2: Read the first number, say b. • Step 3: Add the above two numbers and store the result in c. Step 4: Display the result from c. Example: An algorithm to perform addition of two numbers. Such a specification creates difficulty while actually implementing it. Hence many programmers prefer to have specification of algorithm by means of Pseudocode. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 61. b) Pseudocode – Pseudocode is a mixture of a natural language and programming language constructs. Pseudocode is usually more precise than natural language. – For Assignment operation left arrow “←”, for comments two slashes “//” ,if condition, for, while loops are used. ALGORITHM Sum(a,b) //Problem Description: This algorithm performs addition of two numbers //Input: Two integers a and b //Output: Addition of two integers c←a+b return c This specification is more useful for implementation of any language. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 62. c) Flowchart In the earlier days of computing, the dominant method for specifying algorithms was a flowchart, this representation technique has proved to be inconvenient. Flowchart is a graphical representation of an algorithm. It is a a method of expressing an algorithm by a collection of connected geometric shapes containing descriptions of the algorithm’s steps. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 63. FIGURE 1.4 Flowchart symbols and Example for two integer addition. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 64. 7. Proving an Algorithm’s Correctness  Once an algorithm has been specified then its correctness must be proved.  An algorithm must yields a required result for every legitimate input in a finite amount of time. For example, the correctness of Euclid’s algorithm for computing the greatest common divisor stems from the correctness of the equality gcd(m, n) = gcd(n, m mod n).  A common technique for proving correctness is to use mathematical induction because an algorithm’s iterations provide a natural sequence of steps needed for such proofs.  The notion of correctness for approximation algorithms is less straightforward than it is for exact algorithms. The error produced by the algorithm should not exceed a predefined limit. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 65. 8. Analyzing an Algorithm  For an algorithm the most important is efficiency. In fact, there are two kinds of algorithm efficiency. They are:  Time efficiency, indicating how fast the algorithm runs, and  Space efficiency, indicating how much extra memory it uses.  The efficiency of an algorithm is determined by measuring both time efficiency and space efficiency.  So factors to analyze an algorithm are: » Time efficiency of an algorithm » Space efficiency of an algorithm » Simplicity of an algorithm » Generality of an algorithm FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 66. 9. Coding an Algorithm  The coding / implementation of an algorithm is done by a suitable programming language like C, C++, JAVA.  The transition from an algorithm to a program can be done either incorrectly or very inefficiently. Implementing an algorithm correctly is necessary. The Algorithm power should not reduced by inefficient implementation.  Standard tricks like computing a loop’s invariant (an expression that does not change its value) outside the loop, collecting common subexpressions, replacing expensive operations by cheap ones, selection of programming language and so on should be known to the programmer. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 67. Coding an Algorithm-Contd – Typically, such improvements can speed up a program only by a constant factor, whereas a better algorithm can make a difference in running time by orders of magnitude. But once an algorithm is selected, a 10–50% speedup may be worth an effort. – It is very essential to write an optimized code (efficient code) to reduce the burden of compiler. FUNDAMENTALS OF ALGORITHMIC PROBLEM SOLVING
  • 68. 1.2 IMPORTANT PROBLEM TYPES The most important problem types are: • Sorting • Searching • String processing • Graph problems • Combinatorial problems • Geometric problems • Numerical problems
  • 69. i. Sorting – The sorting problem is to rearrange the items of a given list in nondecreasing (ascending) order. – Sorting can be done on numbers, characters, strings or records. – To sort student records in alphabetical order of names or by student number or by student grade-point average. Such a specially chosen piece of information is called a key. – An algorithm is said to be in-place if it does not require extra memory, E.g., Quick sort. – A sorting algorithm is called stable if it preserves the relative order of any two equal elements in its input. 1.2 IMPORTANT PROBLEM TYPES https://visualgo.net/bn/sorting
  • 70. ii. Searching – The searching problem deals with finding a given value, called a search key, in a given set. – E.g., Ordinary Linear search and fast binary search. iii. String processing – A string is a sequence of characters from an alphabet. – Strings comprise letters, numbers, and special characters; bit strings, which comprise zeros and ones; and gene sequences, which can be modeled by strings of characters from the four- character alphabet {A, C, G, T}. It is very useful in bioinformatics. – Searching for a given word in a text is called string matching 1.2 IMPORTANT PROBLEM TYPES
  • 71. iv. Graph problems – A graph is a collection of points called vertices, some of which are connected by line segments called edges. – Some of the graph problems are graph traversal, shortest path algorithm, topological sort, traveling salesman problem and the graph-coloring problem and so on. v. Combinatorial problems – These are problems that ask, explicitly or implicitly, to find a combinatorial object such as a permutation, a combination, or a subset that satisfies certain constraints. – A desired combinatorial object may also be required to have some additional property such s a maximum value or a minimum cost. 1.2 IMPORTANT PROBLEM TYPES
  • 72. – In practical, the combinatorial problems are the most difficult problems in computing. – The traveling salesman problem and the graph coloring problem are examples of combinatorial problems. vi. Geometric problems – Geometric algorithms deal with geometric objects such as points, lines, and polygons. – Geometric algorithms are used in computer graphics, robotics, and tomography. – The closest-pair problem and the convex-hull problem are comes under this category. 1.2 IMPORTANT PROBLEM TYPES
  • 73. vii. Numerical problems – Numerical problems are problems that involve mathematical equations, systems of equations, computing definite integrals, evaluating functions, and so on. – The majority of such mathematical problems can be solved only approximately. 1.2 IMPORTANT PROBLEM TYPES
  • 74. 1.3 FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM EFFICIENCY • The efficiency of an algorithm can be in terms of time and space. The algorithm efficiency can be analyzed by the following ways. • Analysis Framework. • Asymptotic Notations and its properties. • Mathematical analysis for Recursive algorithms. • Mathematical analysis for Non-recursive algorithms.
  • 75. 1.4 Analysis Framework • There are two kinds of efficiencies to analyze the efficiency of any algorithm. They are: • Time efficiency, indicating how fast the algorithm runs, and • Space efficiency, indicating how much extra memory it uses. The algorithm analysis framework consists of the following: ɷ Measuring an Input’s Size ɷ Units for Measuring Running Time ɷ Orders of Growth ɷ Worst-Case, Best-Case, and Average-Case Efficiencies ɷ Recapitulation of the Analysis Framework
  • 76. 1. Measuring an Input’s Size – An algorithm’s efficiency is defined as a function of some parameter n indicating the algorithm’s input size. In most cases, selecting such a parameter is quite straightforward. For example, it will be the size of the list for problems of sorting, searching. – For the problem of evaluating a polynomial p(x) = anxn + . . . + a0 of degree n, the size of the parameter will be the polynomial’s degree or the number of its coefficients, which is larger by 1 than its degree. – In computing the product of two n × n matrices, the choice of a parameter indicating an input size does matter. 1.4 Analysis Framework
  • 77. i. Measuring an Input’s Size(contd) • Consider a spell-checking algorithm. If the algorithm examines individual characters of its input, then the size is measured by the number of characters. • In measuring input size for algorithms solving problems such as checking primality of a positive integer n. the input is just one number. • The input size by the number b of bits in the n’s binary representation is b=(log2 n)+1. 1.4 Analysis Framework
  • 78. ii. Units for Measuring Running Time • Some standard unit of time measurement such as a second, or millisecond, and so on can be used to measure the running time of a program after implementing the algorithm. • Drawbacks – Dependence on the speed of a particular computer. – Dependence on the quality of a program implementing the algorithm. – The compiler used in generating the machine code. • The difficulty of clocking the actual running time of the program. 1.4 Analysis Framework
  • 79. ii. Units for Measuring Running Time(contd) • So, we need metric to measure an algorithm’s efficiency that does not depend on these extraneous factors. • One possible approach is to count the number of times each of the algorithm’s operations is executed. This approach is excessively difficult. • The most important operation (+, -, *, /) of the algorithm, called the basic operation. Computing the number of times the basic operation is executed is easy. The total running time is determined by the basic operation count. 1.4 Analysis Framework
  • 80. iii. Orders of Growth • A difference in running times on small inputs is not what really distinguishes efficient algorithms from inefficient ones. • For example, the greatest common divisor of two small numbers, it is not immediately clear how much more efficient Euclid’s algorithm is compared to the other algorithms, the difference in algorithm efficiencies becomes clear for larger numbers only. • For large values of n, it is the function’s order of growth that counts just like the Table 1.1, which contains values of a few functions particularly important for analysis of algorithms. 1.4 Analysis Framework
  • 81. TABLE 1.1 Values (approximate) of several functions important for analysis of algorithms n √n log2n n n log2n n2 n3 2n n! 1 1 0 1 0 1 1 2 1 2 1.4 1 2 2 4 4 4 2 4 2 2 4 8 16 64 16 24 8 2.8 3 8 2.4•101 64 5.1•102 2.6•102 4.0•104 10 3.2 3.3 10 3.3•101 102 103 103 3.6•106 16 4 4 16 6.4•101 2.6•102 4.1•103 6.5•104 2.1•1013 102 10 6.6 102 6.6•102 104 106 1.3•1030 9.3•10157 103 31 10 103 1.0•104 106 109 Very big computation 104 102 13 104 1.3•105 108 1012 105 3.2•102 17 105 1.7•106 1010 1015 106 103 20 106 2.0•107 1012 1018 1.4 Analysis Framework
  • 82. iv. Worst-Case, Best-Case, and Average- Case Efficiencies Consider Sequential Search algorithm some search key K ALGORITHM Sequential Search(A[0..n -1], K) //Searches for a given value in a given array by sequential search //Input: An array A[0..n - 1] and a search key K //Output: The index of the first element in A that matches K or -1 if there are no // matching elements i ←0 while i < n and A[i] ≠ K do i ←i + 1 if i < n return i else return -1 1.4 Analysis Framework
  • 83. • Clearly, the running time of this algorithm can be quite different for the same list size n. • In the worst case, there is no matching of elements or the first matching element can found at last on the list. In the best case, there is matching of elements at first on the list. Worst-case efficiency – The worst-case efficiency of an algorithm is its efficiency for the worst case input of size n. – The algorithm runs the longest among all possible inputs of that size. – For the input of size n, the running time is Cworst(n) = n. 1.4 Analysis Framework
  • 84. Best case efficiency – The best-case efficiency of an algorithm is its efficiency for the best case input of size n. – The algorithm runs the fastest among all possible inputs of that size n. – In sequential search, If we search a first element in list of size n. (i.e. first element equal to a search key), then the running time is Cbest(n) = 1 1.4 Analysis Framework
  • 85. • Average case efficiency – The Average case efficiency lies between best case and worst case. – To analyze the algorithm’s average case efficiency, we must make some assumptions about possible inputs of size n. – The standard assumptions are that • The probability of a successful search is equal to p (0 ≤ p ≤ 1) and • The probability of the first match occurring in the ith position of the list is the same for every i. 1.4 Analysis Framework
  • 86. 1.4 Analysis Framework Yet another type of efficiency is called 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. Average case efficiency(contd)
  • 87. 1.5 ASYMPTOTIC NOTATIONS & ITS PROPERTIES • Asymptotic notation is a notation, which is used to take meaningful statement about the efficiency of a program. • The efficiency analysis framework concentrates on the order of growth of an algorithm’s basic operation count as the principal indicator of the algorithm’s efficiency. • To compare and rank such orders of growth, computer scientists use three notations, they • O - Big oh notation • Ω - Big omega notation • Θ - Big theta notation
  • 88. 1<logn< <n<nlogn<n2 <n3 <………….<2n<3n<….nn 1<logn< <n<2n<n2 <n3 • O - Big oh notation…………….Upper Bound • Ω - Big omega notation………..Lower Bound • Θ - Big theta notation…………..Average Bound Which is useful?? Θ - Big theta notation…………..Average Bound If you cannot specify the exact place of any function out these then we can show the upper bound of that function 1.5 ASYMPTOTIC NOTATIONS & ITS PROPERTIES
  • 89. 1. O - Big oh notation Definition • A function f(n) = O(g(n)) if and if there exist positive constants C and n0 Such that f(n) <=C*g(n)Ɐ n> n0 1<logn< <n<2 n<n2 <n3 Example f(n)=2n+3 2n+3<=…….. 2n+3<=10n………….n=1 5<=10 f(n)= 2n+3 g(n)=n C=10 There fore f(n)=O(n) 2n^2+3n^2 F(n)=O(n^2) F(n)=O(2^n) 2n+3<=2n+3n
  • 90. 1. O - Big oh notation
  • 91. Why is this so important ? Let’s say you have 2 sorting applications,  Application A which uses bubble sort and  Application B which uses merge sort  Lets say that for input sizes of around 30 elements  Application A is 1,000x faster than application B at sorting.  If you never have to sort much more that 30 elements then its obvious that you should prefer application A, as it is much faster at these input sizes. However, if you find that you may have to sort ten million items then what you’d expect is that application B actually ends up being thousands of times faster than application A in this case, entirely due to the way each algorithm scales 1. O - Big oh notation
  • 92. O(1) • It dosent contain loop, recursion int temp = x; x = y; y = temp;
  • 94. • nested loops isequalto the numberof times the innermost statement isexecuted for(inti=1;i<=n;i+=c) { for (intj=1;j <=n;j+=c) { //someexpressions } } Selectionsort andInsertion Sort haveO(n2)timecomplexity O(n^2)
  • 96. Quiz-1 1. What is the time, space complexity of following code: filter_none brightness_4 int a = 0, b = 0; for (i = 0; i < N; i++) { a = a + rand(); } for (j = 0; j < M; j++) { b = b + rand(); Options: O(N * M) time, O(1) space O(N + M) time, O(N + M) space O(N + M) time, O(1) space O(N * M) time, O(N + M) space
  • 97. Answer : O(N + M) time, O(1) space Explanation: The first loop is O(N) and the second loop is O(M). Since we don’t know which is bigger, we say this is O(N + M). This can also be written as O(max(N, M)). Since there is no additional space being utilized, the space complexity is constant / O(1) Quiz-1
  • 99. • To simplify the running time estimation, for a function f(n),we ignore the constants and lower order terms • 7n-2 =O(n) • 3n3 +20n2+5 =O(n3) • 3logn+5 =O(logn) 1. O - Big oh notation
  • 100. Seven function that often appear in algorithm analysis: Comparing Growth Rates
  • 101. Notation Common Name Example O(1) Constant Time Array Index Look up O(log n) Logarithmic Time Binary Search O(n) Linear Time Linear Search O(n log n) Log-Linear Time Heap Sort O(n2) Quadratic Time Bubble Sort O(nc) Polynomial O(cn) Exponential Travelling Salesman Problem O(n!) Factorial Brute Force Travelling Salesman O(nn) N to the N 1. O - Big oh notation
  • 102. 1. O - Big oh notation
  • 103. 1. O - Big oh notation
  • 104. 1. O - Big oh Complexity Chart
  • 105. 1. What is the time complexity of following code: int a = 0; for (i = 0; i < N; i++) { for (j = N; j > i; j--) { a = a + i + j; } } Quiz-2 Options: O(N) O(N*log(N)) O(N * Sqrt(N)) O(N*N)
  • 106. Answer : O(N*N) Explanation: The above code runs total no of times = N + (N – 1) + (N – 2) + … 1 + 0 = N * (N + 1) / 2 = 1/2 * N^2 + 1/2 * N O(N^2) times. Quiz-2
  • 107. 2. What is the time complexity of following code: int i, j, k = 0; for (i = n / 2; i <= n; i++) { for (j = 2; j <= n; j = j * 2) { k = k + n / 2; } } Options: O(n) O(nLogn) O(n^2) O(n^2Logn) Quiz-2
  • 108. Answer O(nLogn) Explanation: If you notice, j keeps doubling till it is less than or equal to n. Number of times, we can double a number till it is less than n would be log(n). Let’s take the examples here. for n = 16, j = 2, 4, 8, 16 for n = 32, j = 2, 4, 8, 16, 32 So, j would run for O(log n) steps. i runs for n/2 steps. So, total steps = O(n/ 2 * log (n)) = O(n*logn) Quiz-3
  • 109. 3. Give the correct matching for the following pairs: • A.O(logn) • B.O(nlogn) • C.O(n^2) • 1.Selectionsort • 2. Binarysearch • 3. Mergesort Quiz-2
  • 110. Example 2: Prove the assertions 100n + 5 ∈ 0(n2). Proof: 100n + 5 ≤ 100n + n (for all n ≥ 5) = 101n ≤ 101n2 (therefore n ≤ n2) Since, the definition gives us a lot of freedom in choosing specific values for constants c and n0. We have c=101 and n0=5 1. O - Big oh notation
  • 111. Example 3: Prove the assertions 100n + 5 ∈ 0(n). Proof: 100n + 5 ≤ 100n + 5n (for all n ≥ 1) = 105n i.e., 100n + 5 ≤ 105n i.e., t(n) ≤ cg(n) Therefore 100n + 5 ∈ 0(n) with c=105 and n0=1 1. O - Big oh notation
  • 112. Example: t(n)<=c*g(n) t(n)=2n+2 g(n)=n^2 Step 1: t(1)=2(1)+2 g(n)=1 t(1)=4<=1……..condition false Step 2: t(2)=2(2)+2 g(n)=4 t(1)=6<=4……..condition false Step 3: t(3)=2(3)+2 g(n)=9 t(1)=8<=9……..condition True t(n) ∈ O g(n) when n=3 1. O - Big oh notation
  • 113. 2. Ω - Big omega notation • Big Ω = Best Case • Big Ω is used to describe the lower bound(best case) • A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is bounded below by some positive 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. • Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers. • Ω = Asymptotic lower bound = Useful for best case analysis = Loose bound
  • 114. FIGURE 1.6 Big-omega notation: t (n) ∈ Ω (g(n)). 2. Ω - Big omega notation Example 4: Prove the assertions n3+10n2+4n+2 ∈ Ω (n2). Proof: n3+10n2+4n+2 ≥ n2 (for all n ≥ 0) i.e., by definition t(n) ≥ cg(n), where c=1 and n0=0
  • 115. Example: t(n)>=c*g(n) t(n)=2n+2 g(n)=n Step 1 t(0)=2(0)+2 g(n)=0 =2 t(0)>=g(0) Step 2 t(1)=2(1)+2 g(n)=1 =4 t(1)>=g(1) t(n) ∈ Ω g(n) when n=0 2. Ω - Big omega notation
  • 116. Big Θ =Average Case(average time that the algorithm will use to produce) The algorithm has a complexity with lower bound = upper bound , say that algorithm has a complexity O(n log n) and Ω (n log n) It actually has the complexity of Θ (n log n), which means the running time of that algorithm always falls in n log n is the best and worst case F(n)= Θ (n log n) 3. Θ - Big theta notation
  • 118. Θ = Asymptotic tight bound = Useful for average case analysis FIGURE 1.7 Big-theta notation: t (n) ∈ Θ(g(n)). 3. Θ - Big theta notation
  • 119. Try ? 5n+2 Big O, Ω, Θ
  • 120. 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 Ω and Θ notations as well.) PROOF: The proof extends to orders of growth the following simple fact about four arbitrary real numbers a1, b1, a2, b2: if a1 ≤ b1 and a2 ≤ b2, then a1 + a2 ≤ 2 max{b1, b2}. Since t1(n) ∈ O(g1(n)), there exist some positive constant c1 and some nonnegative integer n1 such that t1(n) ≤ c1g1(n) for all n ≥ n1. Similarly, since t2(n) ∈ O(g2(n)), t2(n) ≤ c2g2(n) for all n ≥ n2. 3. Θ - Big theta notation
  • 121. Let us denote c3 = max{c1, c2} and consider n ≥ max{n1, n2} so that we can use both inequalities. Adding them yields the following: t1(n) + t2(n) ≤ c1g1(n) + c2g2(n) ≤ c3g1(n) + c3g2(n) = c3[g1(n) + g2(n)] ≤ c32 max{g1(n), g2(n)}. Hence, t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}), with the constants c and n0 required by the definition O being 2c3 = 2 max{c1, c2} and max{n1, n2}, respectively. The property implies that the algorithm’s overall efficiency will be determined by the part with a higher order of growth, i.e., its least efficient part. 3. Θ - Big theta notation
  • 122. therefore t1(n) ∈ O(g1(n)) and t2(n) ∈ O(g2(n)), then t1(n) + t2(n) ∈ O(max{g1(n), g2(n)}). Basic rules of sum manipulation Summation formulas 3. Θ - Big theta notation
  • 123. MATHEMATICAL ANALYSIS FOR RECURSIVE ALGORITHMS General Plan for Analyzing the Time Efficiency of Recursive Algorithms • Decide on a parameter (or parameters) indicating an input’s size. • Identify the algorithm’s basic operation. • Check whether the number of times the basic operation is executed can vary on different inputs of the same size; if it can, the worst-case, average- case, and best-case efficiencies must be investigated separately. • Set up a recurrence relation, with an appropriate initial condition, for the number of times the basic operation is executed. • Solve the recurrence or, at least, ascertain the order of growth of its solution.
  • 124. EXAMPLE 1: Compute the factorial function F(n) = n! for an arbitrary nonnegative integer n. Since n!= 1•……• (n − 1) • n = (n − 1)! • n, for n ≥ 1 and 0!= 1 by definition, we can compute F(n) = F(n − 1) • n with the following recursive algorithm. ALGORITHM F(n) //Computes n! recursively 5!=5X4X3X2X1 //Input: A nonnegative integer n =5X4! //Output: The value of n! 4X3! if n = 0 return 1 3X2! else return F(n − 1) * n 2X1! 1X0! MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 125. Algorithm analysis For simplicity, we consider n itself as an indicator of this algorithm’s input size. i.e. 1. The basic operation of the algorithm is multiplication, whose number of executions we denote M(n). Since the function F(n) is computed according to the formula F(n) = F(n −1)•n for n > 0. The number of multiplications M(n) needed to compute it must satisfy the equality M(n) = M(n-1) + 1 for n > 0 To compute To multiply F(n-1) F(n-1) by n M(n − 1) multiplications are spent to compute F(n − 1), and one more multiplication is needed to multiply the result by n. MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 126. Recurrence relations The last equation defines the sequence M(n) that we need to find. This equation defines M(n) not explicitly, i.e., as a function of n, but implicitly as a function of its value at another point, namely n − 1. Such equations are called recurrence relations or recurrences. Solve the recurrence relation M(n) = M(n − 1) + 1, i.e., to find an explicit formula for M(n) in terms of n only. To determine a solution uniquely, we need an initial condition that tells us MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 127. the value with which the sequence starts. We can obtain this value by inspecting the condition that makes the algorithm stop its recursive calls: if n = 0 return 1. This tells us two things. First, since the calls stop when n = 0, the smallest value of n for which this algorithm is executed and hence M(n) defined is 0. Second, by inspecting the pseudocode’s exiting line, we can see that when n = 0, the algorithm performs no multiplications. Thus, the recurrence relation and initial condition for the algorithm’s number of multiplications M(n): M(n) = M(n −1) + 1 for n > 0, M(0) = 0for n = 0. MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 128. Method of backward substitutions M(n) = M(n − 1) + 1 substitute M(n − 1) = M(n − 2) + 1 = [M(n − 2) + 1]+ 1 = M(n − 2) + 2 substitute M(n − 2) = M(n − 3) + 1 = [M(n − 3) + 1]+ 2 = M(n − 3) + 3 … = M(n − i) + i … = M(n − n) + n = n. Therefore M(n)=n MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 129. EXAMPLE 2: Consider educational workhorse of recursive algorithms: the Tower of Hanoi puzzle. We have n disks of different sizes that can slide onto any of three pegs. Consider A (source), B (auxiliary), and C (Destination). Initially, all the disks are on the first peg in order of size, the largest on the bottom and the smallest on top. The goal is to move all the disks to the third peg, using the second one as an auxiliary. MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 130. FIGURE 1.8 Recursive solution to the Tower of Hanoi puzzle. MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 131. ALGORITHM TOH(n, A, C, B) //Move disks from source to destination recursively //Input: n disks and 3 pegs A, B, and C //Output: Disks moved to destination as in the source order. if n=1 Move disk from A to C else Move top n-1 disks from A to B using C TOH(n - 1, A, B, C) Move the one disk left on A to C using B Move top n-1 disks from B to C using A TOH(n - 1, B, C, A) MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 132. Recurrence Relations M(n)=M(n-1)+1+M(n-1) Initial Condition M(1)=1 M(n)=2M(n-1)+1 …………………for n>1 M(n-1)=2[M(n-2)]+1 M(n)=2[2[M(n-2)]+1]+1 =22[M(n-2)]+2+1 M(n-2)=2[M(n-3)]+1 = 22 [2[M(n-3)]+1]2+1 = 23 M(n-3)+ 22 +2+1 ….. ….. MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 133. = 2i M(n-i)+ 2i-1+......2+1 20+21 + 22+…..2k = 2i M(n-i)+2i-1+1 -1 = 2i M(n-i)+2 i -1 ……. …… ……………………………………………….i=n-1(recursive function) 2n-1 M(n-n-1)+2 n-1 -1 2n-1+2 n-1 -1 MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 135. EXAMPLE 3: An investigation of a recursive version of the algorithm which finds the number of binary digits in the binary representation of a positive decimal integer. ALGORITHM BinRec(n) //Input: A positive decimal integer n //Output: The number of binary digits in n’s binary representation if n = 1 return 1 else return BinRec(⎝n/2])+ 1 MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 136. Algorithm analysis The number of additions made in computing BinRec(⎝n/2]) is A(⎝n/2]), plus one more addition is made by the algorithm to increase the returned value by 1. This leads to the recurrence A(n) = A(⎝n/2]) + 1 for n > 1. then, the initial condition is A(1) = 0. The standard approach to solving such a recurrence is to solve it only for n = 2k A(2k) = A(2k−1) + 1 for k > 0, A(20) = 0. MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 137. backward substitutions A(2k) = A(2k−1) + 1 substitute A(2k−1) = A(2k−2) + 1 = [A(2k−2) + 1]+ 1= A(2k−2) + 2 substitute A(2k−2) = A(2k−3) + 1 = [A(2k−3) + 1]+ 2 = A(2k−3) + 3 . . . . . . = A(2k−i) + i . . . = A(2k−k) + k. Thus, we end up with A(2k) = A(1) + k = k, or, after returning to the original variable n = 2k and hence k = log2 n, A(n) = log2 n ϵ Θ (log2 n). MATHEMATICAL ANALYSIS FOR RECURSIVE
  • 138. MATHEMATICALANALYSIS FOR NON-RECURSIVE ALGORITHMS General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms • Decide on a parameter (or parameters) indicating an input’s size. • Identify the algorithm’s basic operation (in the innermost loop). • Check whether the number of times the basic operation is executed depends only on the size of an input. If it also depends on some additional property, the worst-case, average-case, and, if necessary, best-case efficiencies have to be investigated separately. • Set up a sum expressing the number of times the algorithm’s basic operation is executed. • Using standard formulas and rules of sum manipulation either find a closed form formula for the count or at the least, establish its order of growth.
  • 139. Algorithm analysis • The measure of an input’s size here is the number of elements in the array, i.e., n. • There are two operations in the for loop’s body:  The comparison A[i]> maxval and  The assignment maxval←A[i]. • The comparison operation is considered as the algorithm’s basic operation, because the comparison is executed on each repetition of the loop and not the assignment. • The number of comparisons will be the same for all arrays of size n; therefore, there is no need to distinguish among the worst, average, and best cases here. MATHEMATICALANALYSIS FOR NON-RECURSIVE
  • 140. Let C(n) denotes the number of times this comparison is executed. The algorithm makes one comparison on each execution of the loop, which is repeated for each value of the loop’s variable i within the bounds 1 and n − 1, inclusive. Therefore, the sum for C(n) is calculated as follows: MATHEMATICALANALYSIS FOR NON-RECURSIVE
  • 141. EXAMPLE 2: Consider the element uniqueness problem: check whether all the Elements in a given array of n elements are distinct. ALGORITHM UniqueElements(A[0..n − 1]) //Determines whether all the elements in a given array are distinct //Input: An array A[0..n − 1] //Output: Returns “true” if all the elements in A are distinct and “false” otherwise for i ←0 to n − 2 do for j ←i + 1 to n − 1 do if A[i]= A[j ] return false return true MATHEMATICALANALYSIS FOR NON-RECURSIVE
  • 142. Algorithm analysis • The natural measure of the input’s size here is again n (the number of elements in the array). • Since the innermost loop contains a single operation (the comparison of two elements), we • should consider it as the algorithm’s basic operation. • The number of element comparisons depends not only on n but also on whether there are equal elements in the array and, if there are, which array positions they occupy. We will limit our investigation to the worst case only. MATHEMATICALANALYSIS FOR NON-RECURSIVE
  • 143. One comparison is made for each repetition of the innermost loop, i.e., for each value of the loop variable j between its limits i + 1 and n − 1; this is repeated for each value of the outer loop, i.e., for each value of the loop variable i between its limits 0 and n − 2. MATHEMATICALANALYSIS FOR NON-RECURSIVE