0% found this document useful (0 votes)
8 views

Module-1 PPT

The document covers the analysis and design of algorithms, including definitions, techniques for problem-solving, and methods for analyzing algorithm efficiency. It discusses various algorithms such as Euclid’s for GCD, Sieve of Eratosthenes, and sorting algorithms like selection sort and bubble sort, along with their time and space complexities. Additionally, it introduces asymptotic notations for measuring efficiency and provides examples of mathematical analysis for both nonrecursive and recursive algorithms.

Uploaded by

Rajeshwari K S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Module-1 PPT

The document covers the analysis and design of algorithms, including definitions, techniques for problem-solving, and methods for analyzing algorithm efficiency. It discusses various algorithms such as Euclid’s for GCD, Sieve of Eratosthenes, and sorting algorithms like selection sort and bubble sort, along with their time and space complexities. Additionally, it introduces asymptotic notations for measuring efficiency and provides examples of mathematical analysis for both nonrecursive and recursive algorithms.

Uploaded by

Rajeshwari K S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Analysis and Design of

Algorithms

Module-1
Algorithm Definition
Notion of a Diagram
Euclid’s Algorithm for GCD
Another algorithm to find GCD
School Procedure
Sieve of Eratosthenes
Algorithm
Fundamentals of Algorithmic Problem Solving
Contd…
Understanding the pr0blem
Ascertaining the Capabilities of the computational
Device
Choosing between Exact and Approximate
problem solving
Algorithm Design Techniques
Designing an algorithm and data Structure
Methods of Specifying an Algorithm
Proving an algorithm correctness
Analyzing an algorithm
Coding an algorithm
Fundamentals of the Analysis of Algorithm
Efficiency
Analysis Framework
Time efficiency(Complexity)
Space efficiency(Complexity)
Measuring an Input’s Size
Units for measuring Running Time
Order of Growth
Worst-case, Best-case, and Average-case
Efficiencies
Recapitulation of the Analysis Framework
Units for measuring Running Time

Count the number of times each of the


algorithm’s operations is executed.
Identify the important operation of the
algorithm called as basic operation, the
operation contributing the most to the total
running time and compute the number of
times the basic operation is executed.
The basic operation is usually the most time-
consuming operation in the algorithm's
innermost loop.
Contd…
Order of Growth
Worst-case, Best-case, and Average-case Efficiencies
Contd…
Worst case efficiency: Cworst(n)=n
Best case efficiency: Cbest(n)=1
Average case efficiency:
 P-probability of a successful search
 The probability of the first match occurring in the i th
position of the list is the same for every i.
Recapitulation of the Analysis Framework
Asymptotic Notations and Basic
Efficiency classes
O(big oh)
Ω(big omega)
Ѳ(big theta)
t(n) and g(n) can be any nonnegative
functions defined on the set of natural
numbers.
t(n) will be an algorithm’s running time and
g(n) will be some simple function to compare
the count with.
O(g(n)) is the set of all functions with a
smaller or same order of growth as g(n).
Examples:

Examples: The first two functions are linear


and have a smaller order of growth than
g(n)=n2. Last one is quadratic and has the
same order of growth as n2.
Ω(g(n)) stands for the set of all functions with
larger or same order of growth as g(n).
Examples:
O-Notation
Contd…
Ω-Notation
Contd…
Ѳ-Notation
Contd…
Property Involving the Asymptotic Notations
Using limits for comparing Orders of
Growth
Three principal cases :
Basic Asymptotic Efficiency Classes
Mathematical Analysis of Nonrecursive
Algorithms
Example 1: Consider the problem of finding
the value of the largest element in a list of n
numbers.
Input size: the number of elements in the
array
Operations of that are execute in the for loop
Comparison operation: A[i]>maxval
Assignment operation: maxval<-A[i]
Basic operation: Comparison operation:
A[i]>maxval
C(n) the number of times this Comparison is
executed
Find a formula expressing it as a function of size
n
General plan for Analyzing Time Efficiency
of Nonrecursive Algorithms
Two basic rules of sum manipulation
Example 2: consider the element uniqueness
problem: Check whether all the elements in a
given array are distinct.
Analysis
Input’s size : the number of elements in the array
Basic operation: Comparison of two elements
The number of element comparisons will depend
not only on n but also on whether there are equal
elements in the array.
C worst(n) is the largest among all arrays of size n.
Two kinds of worst-case inputs:
 Arrays with no equal elements
 Arrays in which the last two are the only pair of
equal elements.
Analysis
Input’s size : matrix order n
Innermost loop has two operations;
 multiplication and addition
Basic operation- multiplication
Compute total number of multiplications M(n)
One multiplication executed on each repletion
of the algorithm's innermost loop i.e k ranging
from the lower bound 0 to upper bound n-1.
The number of multiplications made for every
pair of specific values of variables i and j is
Example 4: Finds the number of binary digits
in the binary representation of positive
decimal integer.
Analysis
Basic operation: Comparison operation which
is not inside the loop but rather the
Comparison n>1 that determines whether the
loop’s body will be executed .
Number of times the Comparison will be
executed is larger than the number of
repetitions of the loop’s body by exactly 1.
The n value is halved on each repetition of
the loop i.e
log2n.
Mathematical Analysis of Recursive
Algorithms
Analysis
Input’s size: n
Basic operation: Multiplication
Number of execution times represented as
M(n)
The function F(n) will be computed as
 F(n)=F(n-1)*n for n>0
The number of multiplications M(n)
computed as called as
recurrence
relation
Contd…
Initial condition: if n=0 return 1
Two things to observe
Call stops when n=0 ,the smallest value of n for
which this algorithm is executed, hence M(n)=0
By inspecting the pseudo code, no multiplications
are performed when n=0
So initial condition is set up as:
Contd…
Setting up the recurrence relation and initial
condition for the algorithm’s number of
multiplications M(n):

The Factorial function F(n) by itself,


defined by recurrence
Contd…
Solving the recurrence relations using
method of backward substitutions

General formula: M(n-i)+i


General plan for Analyzing the time
efficiency of Recursive Algorithm
Example 2: Tower of Hanoi
Algorithm
Analysis
Example 3: Converting Decimal number to
binary representation
Contd…
Solve this recurrence relation by putting
n=2k as per smoothness rule
Brute Force
Selection sort
Start selection sort by scanning the entire
list to find the smallest element.
Exchange smallest element with first
element- means the smallest element will be
in its final position in the sorted list.
Next start with second element to find the
smallest element among the last n-1 elements
and exchanging with the second element-
second smallest element in its final position.
Contd…
Generally on the ith pass through the list,
which we number from 0 to n-2, the algorithm
searches for the smallest item along the last n-
I elements and swaps it with Ai:
Selection Sort Algorithm
Selection sort problem
Analysis
Bubble sort
Bubble sort Algorithm
Problem
Analysis
Sequential Search
The algorithm compares successive elements
of a given list with a given search key until
successful search encountered or the list is
exhausted not finding the match.
Append the search key at the end to avoid the
checking of end of list.
If the given elements are sorted, search can
be stopped after key greater than or equal to
element.
Algorithm
Brute Force string Matching
Given a string of n characters called the text and a
string of m characters(m<=n) called pattern, find a
substring of the text that matched the pattern.
We want to find i- the index of the leftmost character
of the first matching substring in the text.
working
Align the pattern against the first m characters of the
text and start matching the corresponding pair of
characters from left to right until either all the m pairs
of the characters match or mismatching pattern is
encountered.
Shift the pattern to the one right position and resume
the characters comparison, starting again with the
first character of the pattern and its counter part in
the text.
Note that the last position in the text that can still be
a beginning of matching substring is n-m. Beyond that
position, there are not enough characters to match the
entire pattern. So no comparisons are required .
Algorithm
Problem
Analysis
The algorithm shifts the pattern almost always after
a single character comparison.
Worst case: the algorithm may have to make all m
comparisons before shifting the pattern, and this
can happen for each of the n-m+1 tries.
 So m(n-m+1) character comparisons- O(mn).
For a typical word in natural language test, most
shifts would happen after very few comparisons. So
the average case is better than worst case
efficiency.
For searching random tests, it’s linear .
 So Ѳ(n).

You might also like