DAA - Unit1
DAA - Unit1
Prepared by
Dr L Josephine Usha
Asst.Professor/CSE
SRM Institute of Science and Technology
Course Description
Analysi
Engineer Environ Project
CO Design/ s, Individua
ing Problem Modern Society & ment & Commun Managem Life-long
develop Design, Ethics l & Team
knowled analysis Tool Usage Culture Sustaina ication ent & Learning
ment Resear work
ge bility Finance
ch
CLO1 2 1 2 1 - - - - - 3 - 3
CLO2 2 1 2 1 - - - - - 3 - 3
CLO3 2 1 2 1 - - - - - 3 - 3
CLO4 2 1 2 1 - - - - - 3 - 3
CLO5 2 1 2 1 - - - - - 3 - 3
• Cannot be executed
• Can be read and understood by programmers who are familiar with different programming
languages.
• 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 b/w Algorithm and Pseudocode
Can be expressed either as an informal high level It is not written in a specific syntax. It allows to include control
description as pseudo code or using a flowchart. structures such as WHILE, IF-THEN-ELSE, REPEAT-UNTIL, FOR,
and CASE, which are present in many high level languages.
Notion of Algorithm: Example
• Disadvantage:
• This Algorithm does not work properly when one of its input numbers is zero.
Notion of Algorithm
• Middle School procedure
Disadvantage:
• This algorithm is slower than
Euclid’s algorithm
• If the prime numbers list is very
long sorted list, the efficiency
of this algorithm is very less.
Fundamentals Of Algorithmic Problem Solving
Fundamentals Of Algorithmic Problem Solving
• The steps in designing and analyzing an algorithm are
• Understanding the Problem
• Ascertaining the Capabilities of a Computational Device
• Choosing between Exact and Approximate Problem Solving
• Deciding on Appropriate Data Structures
• Algorithm Design Techniques
• Methods of specifying an Algorithm
• Proving an Algorithm’s Correctness
• Analyzing an Algorithm
• Coding an Algorithm
Important Problem Types
1. Sorting
2. Searching
3. String processing
4. Graph problems
5. Combinational problems
6. Geometric problems
7. Numerical problems
Important Problem Types
1. Sorting:
✓ Rearranging the items of a given list in ascending order.
✓ The list may be numbers, alphabets, character strings records (in schools, libraries, companies).
✓ Every sorting algorithm has 2 properties.
a) Stable
b) In place
a) Stable:
If an algorithm preserves the relative order of any 2 equal elements in its input, then the
algorithm is called stable. Eg, When sorting the students based on University rank, students with same
rank must be ordered.
b) In-place:
If an algorithm does not require extra memory, except, for a few memory units, then the
algorithm is said to be in place.
Important Problem Types
2. Searching:
Finding a given value called a search key in a given set. There are many algorithms for
searching.
a. Sequential,
b. Binary
Important Problem Types
Important Problem Types
Important Problem Types
Important Problem Types
Important Problem Types
Introduction To Algorithm Design
Why to design an algorithm?
• General approaches to the construction of efficient solutions to problems
• They provide templates suited for solving a broad range of diverse problems.
• They can be translated into common control and data structures provided by most high-
level languages.
• The temporal and spatial requirements of the algorithms which result can be precisely
analyzed.
Algorithm Design Approaches
• Based on the architecture
• Top Down Approach
• Bottom up Approach
Algorithm Design Techniques
1. Brute Force
• To solve a problem based on the problem’s statement and definitions
of the concepts involved.
• Easiest approach to apply
• Useful for solving small – size instances of a problem.
• Some examples of brute force algorithms are:
• Computing an (a > 0, n a nonnegative integer) by multiplying a*a*…
*a
• Computing n!
• Selection sort, Bubble sort
• Sequential search
Algorithm Design Techniques
2. Divide-and-Conquer & Decrease-and-Conquer
• Step 1
Split the given instance of the problem into several smaller sub-instances
• Step 2
Independently solve each of the sub-instances
• Step 3
Combine the sub-instance solutions.
• With the divide-and-conquer method the size of the problem instance is reduced by a
factor (e.g. half the input size),
• With the decrease-and-conquer method the size is reduced by a constant.
Algorithm Design Techniques
6. Branch-and-bound
• Branch and bound is used when we can evaluate each node using the cost
and utility functions.
• At each step we choose the best node to proceed further.
• Branch-and bound algorithms are implemented using a priority queue.
• The state-space tree is built in a breadth-first manner.
• Example:
• 8-puzzle problem.
• N queens problem
Recollect
• Backtracking
Unit 4
• Branch and Bound Unit 5
Algorithm Analysis
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
• The analytical frame work for analyzing both the time and space efficiency are,
3) Orders of Growth
✓ Algorithms efficiency can be investigated as a function of some parameter ‘n’ indicating the algorithm’s input
size.
✓ Computing the product of two n x n matrices. Two measures of input size are
✓ If an algorithm examines individual characters of the input, then the size can be measured as, the number of
characters/elements(N).
✓ If an algorithm works by processing words, then the input size is number of words.
✓ If the size of inputs for an algorithm involves to finding the number of binary digits in the n’s binary
representation is
Fundamentals of the Analysis of Algorithm Efficiency
2. Units for Measuring Running Time:
✓ It concerns with the units used to measure the algorithms running time.
✓ One possible approach to measure the algorithm’s efficiency is to count the number of
times each of the algorithm’s operation is executed
✓ Basic Operation
Fundamentals of the Analysis of Algorithm Efficiency
3. Orders of growth:
✓ For large values of n, the functions order of growth, that counts the number of basic
operation.
Fundamentals of the Analysis of Algorithm Efficiency
4. Worst case, Best case & Average case Efficiencies:
✓ There are many algorithms for which the running time, not only depends on the input size, but also on
the specifics of a particular input.
Fundamentals of the Analysis of Algorithm Efficiency
4. Worst case, Best case & Average case Efficiencies:
Fundamentals of the Analysis of Algorithm Efficiency
✓ In the case of the successful search the probability of the first match occurring in the ith position of the
list is p/n.
Asymptotic Notations and its Properties
To compare and rank the order of growth
Asymptotic Notations and its Properties
Asymptotic Notations and its Properties
Asymptotic Notations and its Properties
Asymptotic Notations and its Properties
Asymptotic Notations and its Properties
Asymptotic Notations and its Properties
Basic Efficiency Classes
Algorithm Analysis
• Space Complexity
• The amount of memory space required by the algorithm in its life cycle.
• A fixed part For example simple variables & constant used and program size etc.
• A variable part For example dynamic memory allocation, recursion stacks space etc.
• The first loop runs in O(n) time, the second - O(n^2) time, the maximum is O(n^2)
if C
S1;
else
S2;
The running time is the maximum of the running times of S1 and S2.
Ans : O(n^5)
d.
b. sum = 0;
sum = 0; for( i = 0; i < n; i++)
for( i = 0; i < n; i++) sum++;
for( j = 0; j < i; j++) val = 1;
sum++; for( j = 0; j < n*n; j++)
Ans : O(n^2) val = val * j;
Ans : O(n^2)
Mathematical Analysis of recursive Algorithms
Mathematical Analysis of recursive Algorithms
• Example 1: Computing the factorial function F(n) = n!, for an arbitrary non–
negative integer n
Mathematical Analysis of recursive Algorithms
• Example 2: Solve the Towers of Hanoi puzzle
• Problem:
• Given ‘n’ disks of different sizes and 3 pegs. Initially, all the disks on
the first peg, in order of size (largest on the bottom and smallest on the
top). Move all the disks to the third peg, using the second one as
auxiliary. The constraints are
1. Move only one disk at a time
2. Don’t place a larger disk on top of a smaller one.
Mathematical Analysis of recursive Algorithms
Mathematical Analysis of recursive Algorithms
• Solution:
• To move n > 1 disks from peg 1 to peg 3, move recursively (n-1)
disks from peg 1 to peg 2 using peg 3 as auxiliary.
• Then move the largest disk directly from peg 1 to peg 3 and finally,
move recursively (n-1) disks from peg 2 to peg 3 using peg 1 as
auxiliary.
• If n=1, simply move the single disk directly from the source peg 1 to
destination peg 3.
Mathematical Analysis of recursive Algorithms
• Apply the general plan to this problem.
• The input size is the number of disks ‘n’.
• The algorithm’s basic operation is moving the disk from one peg to another.
• The number of moves M(n) depends on ‘n’ only and so the recurrence
equation is given as
M(n) = M(n-1) + 1+ M(n-1)for n>1
Mathematical Analysis of recursive Algorithms
Mathematical Analysis of recursive Algorithms
Mathematical Analysis of recursive Algorithms
• Example 3: Find the number of binary digits in the binary representation of a
positive decimal integer.
Mathematical Analysis of recursive Algorithms
Mathematical Analysis of recursive Algorithms
Mathematical Analysis of recursive Algorithms
QUIZ
Question No: 1
• Two main measures for the efficiency of an algorithm are
A. Processor and memory
B. Complexity and capacity
C. Time and space
D. Data and space
• Answer : Time and space
Question No: 2
The space factor when determining the efficiency of algorithm is
measured by
A. Counting the maximum memory needed by the algorithm
B. Counting the minimum memory needed by the algorithm
C. Counting the average memory needed by the algorithm
D. Counting the maximum disk space needed by the algorithm