SlideShare a Scribd company logo
1 of 49
Download to read offline
INTRODUCTION TO ALGORITHMS
By Dr. N. Subhash Chandra
1
Fundamentals of Algorithm
Analysis
CONTENTS
1) Introduction
2) Definition of algorithm
3) Algorithmic Problem solving
4) Methods of Specifying an Algorithm
5) Steps for writing an algorithm(Pseudo code)
6) Sample Problems and Algorithms
7) Quiz
8) Important Questions
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 2
Algorithm Background
•Algorithm: The word algorithm came from the name of a Persian mathematician Abu
Jafar Mohammed Ibn Musa Al Khowarizmi (ninth century).
•An algorithm is simply s set of rules used to perform some calculations either by hand or
more usually on a machine (computer).
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 3
Definition of Algorithm
Definition:
An algorithm is a finite sequence of unambiguous instructions for solving a particular
problem. It must satisfy the specific characteristics.
4
Characteristics 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 terminates after a finite number of steps.
• Efficiency: Every instruction must be very basic and runs in short time with
effective results better than human computations.
5
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 6
Understand the Problem
Selected the appropriate Design
Techniques and Data Structures
Write the Computational Solutions
(Algorithms)
Validate the Algorithms
Analysis the Algorithms
Real World Problem
Existing Design Techniques
Iterative method
Recursive Method
Brute-force
Divide and Conquer
Greedy Method
Dynamic Programming
Brach and Bound Method
Backtracking Method
Optimization Method
Random Method
Existing Data Structures
Array
Linked List
Stack
Queue
Binary Tree
Binary Search Tree
Heap Tree
Red Black Tree
Quad Tree
Un-directed Graph
Directed Graph
Planar Graph
Selected the efficient Algorithm
Implement the algorithm for selected
language
Testing Program
Algorithmic Problem solving
Issues for Algorithm
• There are various issues in the study of algorithms;
1. How to devise algorithms: The creation of an algorithm is a logical activity which may never be fully automated.
2. How to express algorithms: We shall express all of our algorithms using the best principlesof structuring.
3. How to validate algorithms:After creation of algorithms is to validate algorithms. The process of checking an
algorithm computes the correctanswer for all possible legal inputs is called algorithm validation. The purpose of
validation of algorithm is to find whether algorithm works properly without being dependent upon programming
languages.
4. How to analyze algorithms:Analysis of algorithm is a task of determining how much computing time and storage is
required by an algorithm.Analysis of algorithms is also calledperformance analysis.The behavior of algorithm in
best case, worst case and average caseneeds to be obtained.
5. How to test a program: Testing a program really consists of two phases:
i) Debugging: While debugging a program, it is checked whether program produces faulty results for valid set of input
and if it is found then the program has to be corrected.
ii) Profiling or performance measuring: Profiling is a process of measuring time and space required by a corrected
program for valid set of inputs.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 7
How to devise algorithms?
Creating an algorithm is an art which may never be fully automated. In this research we
want to study more design techniques and select appropriate design techniques to devise
the new useful algorithm. The most important design techniques are
(a) Brute-force or exhaustive search
(b) Divide and Conquer
(c) Greedy Algorithms
(d) Dynamic Programming
(e) Branch and Bound Algorithm
(f) Randomized Algorithm
(g) Backtracking
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 8
How to validate algorithms?
Once an algorithm is devised, it is necessary to show that it computes the correct answer for all
possible legal inputs.
It helps to the next process for writing program code in a particular programming language.
This phase is also called program proving or program verification.
The proof of correctness requires that the solution be stated in two forms.
First form, In this form a program which is annotated by a set of assertations about the input and
output variables of the program. Then these assertations are often expresses in the predicate
calculus.
Second form is called specification, and this expressed in the predicate calculus.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 9
How to analysis algorithms?
Efficiency of an algorithm can be analyzed at two different stages, before and after
implementation.
 Before implementation(priori analysis), analysis of algorithms refers to the task of
determining how much computing time and storage space an algorithm require.
 This is the challenging area which require great mathematical skill.(Quantitative
judgement about the algorithms)
An algorithm perform in the best case, the worst case or on the average case are typical.
Dept. of CSE, CVR College of Engineering, Hyderabad 10
Algorithm Solutions for a Given problem
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 11
How to test a program?
After implementation(posterior analysis), Testing a program consists of two phases:
(a) Debugging
(b) Profiling
Debugging is the process of executing programs on sample data sets to determine whether
the faulty results occur if so to correct them.
Profiling or performance measurement is the process of executing a correct program on
data sets and measuring time and space it takes to compute the results.
Dept. of CSE, CVR College of Engineering, Hyderabad 12
4. Methods of Specifying an Algorithm
There are three ways to specify an algorithm. They are:
a. Natural language
b. Pseudocode
c. Flowchart
Pseudocode and flowchart are the two options that are most widely used nowadays for
specifying algorithms.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 13
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.
Example: An algorithm to perform addition of two numbers.
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.
Such a specification creates difficulty while implementing it. Hence many
programmers prefer specification of algorithm by means of Pseudocode.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 14
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 method of
expressing an algorithm by a
collection of connected geometric
shapes containing descriptions of the
algorithm’s steps.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 15
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.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 16
Steps for writing an algorithm(Pseudo code):
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,…,pn)
The head section also has the following:
//Problem Description:
//Input:
//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 open and closed by {, } respectively. Proper
indention is must for block.
5. Comments are written using // at the beginning.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 17
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.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 18
Operators and Conditional Statement(Pseudo code):
There are two Boolean values TRUE and FALSE. Logical operators: AND, OR, NOT.
• Relational operators: <, >, ≥,,=,.Arithmetic operators: +, -, *, /, %;
The conditional statement if-then or if-then-else is written in the following form. If (condition) then (statement)
• If (condition) then {Block-1 }else { Block-2}
• ‘If’ is a powerful statement used to make decisions based as a condition. If a condition is true the particular block of statements
are execute.
Example
if(a>b) then
{
write("a is big");
}
else
{
write("b is big");
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 19
Case Statement(Pseudo code):
Case statement
case
{
:(condition -1): (statement-1)
:(condition -2): (statement-2)
:(condition -n): (statement-n)
..............
..............
else :(statement n+1);
}
If condition -1 is true, statement -1 executed and the case statement is exited. If statement -1 is false,
condition -2 is evaluated. If condition -2 is true, statement-2 executed and so on. If none of the conditions
are true, statement –(n+1) is executed and the case statement is exited. The else clause is optional.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 20
for loop statement(Pseudo code):
Loop statements: For loop:
The general form of the for loop is
for variable:=value1 to value2 step value3 do
{
Statement -1;
Statement -2;
...
...
Statement -n;
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 21
Example 2:
for i:=1 to 10 step 2 do
{
write(i); //displaying numbers from 1 to 10 i:=i+2;
}
Example 1:
for i:=1 to 10 do
{
write(i); //displaying numbers from 1 to 10 i:=i+1;
}
While Statement(Pseudo code):
while loop:
The general form of whileis-
while (condition) do
{
statement 1;
statement 2;
...
...
statement n;
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 22
Example
i:=1;
while(i<=n) do
{
write (i);
i:=i+1;
}
repeat-until statement(Pseudo code):
Repeat-until loop:
The general form of repeat-until is-
repeat
{
statement 1;
statement 2;
...
...
statement n;
}until (condition);
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 23
Example
i:=1;
repeat
{
write (i);
i:=i+1;
}
until (i>10);
Break, Array and function Statement(Pseudo code):
Break: this statement is exit from the loop.
Elements of array are accessed using [ ].
• For example, if A is an one-dimensional array, then ith element can be accessed
using A[i].
• If A is two-dimensional array, then (i, j)th element can be accessed using A[i,j].
Procedures (functions): There is only one type of procedure:
An algorithm consists of a heading and a body.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 24
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 25
SAMPLE ALGORITHMS
Write an algorithm to find maximum element in
an array with size ‘n’.
Algorithm Max(A, n)
// This algorithm return the maximum element in the given array
A with size n
{
maximum=A[1];
for i=2 to n do
if A[i]>maximum then
maximum=A[i];
return maximum;
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 26
VERIFICATION
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 27
GIVEN DATA : 12 5 56 34 45
SIZE OF THE DATA : n=5
STEP 1 MAX=12
STEP 2 MAX=12 SINCE CURRENT DATA=5 WHICH IS LESS THAN MAX
STEP 3 MAX=56 SINCE CURRENT DATA=56 WHICH IS GREATER THAN MAX
STEP 4 MAX = 56 SINCE CURRENT DATA=34 WHICH IS LESS THAN MAX
STEP 5 MAX=56 SINCE CURRENT DATA=45 WHICH IS LESS THAN MAX
Write a linear sort algorithm to sort the data in an
array with size ‘n’.
Ans:
Algorithm LinearSort(A, n)
// This algorithm return the sorted(ascending order) array for the given array A with size n
{
for i=1 to n-1 do{
for j=i+1 to n do
if A[i]>A[j] then
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
return A;
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 28
VALIDATE THE ALGORITHM
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 29
VERIFY THE ALGORITHM
PASS 1: Fix the first minimum element in the
first position
INDEX A I=1, J=2 I=1,J=3 I=1,J=4 I=1,J=5
1 23 12 12 7 7
2 12 23 23 23 23
3 18 18 18 18 18
4 7 7 7 12 12
5 20 20 20 20 20
RULE 1 : IF A[I]>A[J] THEN INTERCHANGE(A[I], A[J]) ELSE NO-ACTION
3. Write an algorithm for finding max and min in an
array of n elements.
Algorithm MaxMin(A, n)
// This algorithm return the max and min values in a given array A, n- size of the array.
{
maximum:=A[1];
minimum:=A[1];
for i:=2 to n do
{
if A[i]<maximum then maximum:=A[i];
if A[i]>minimum then minimum:=A[i];
}
return([minimum, maximum]);
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 30
4. Write an algorithm for Bubble Sort an array of n
elements.
Algorithm BubbleSort(A, n)
// This algorithm return the sorted(ascending order) array for the given array A with size n
{
for i=1 to n-1 do{
for j=1 to n-i do
if A[j]>A[j+1] then {
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 31
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 32
BUBBLE SORT
VERIFY THE ALGORITHM
PASS 1: Fix the largest element at last
position
INDEX KEY-A I=1 I=2 I=3 I=4
1 23 12 12 12 12
2 12 23 18 18 18
3 18 18 23 7 7
4 7 7 7 23 20
5 20 20 20 20 23
RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE NO-
ACTION
PASS 2: Fix the second largest data in its
position
INDEX KEY-A I=1 I=2 I=3
1 12 12 12 12
2 18 18 7 7
3 7 7 18 18
4 20 20 20 20
5 23 23 23 23
RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE
NO-ACTION
PASS 3: Fix the third largest data in its
position
INDEX KEY-A I=1 I=2
1 12 7 7
2 7 12 12
3 18 18 18
4 20 20 20
5 23 23 23
RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE NO-
ACTION
PASS 4: Fix the fourth largest data in its
position
INDEX KEY-A I=1
1 7 7
2 12 12
3 18 18
4 20 20
5 23 23
RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE
NO-ACTION
5. Write an algorithm for Selection Sort an array of n
elements.
Algorithm SelectionSort(A, n)
// This algorithm return the sorted(ascending order) array for the given array A with size n
{
for i:=1 to n-1 do{
pos:=i;
for j:=i+1 to n do{
if A[j]<A[pos] then pos:=j;
}
temp=A[pos]
A[pos]:=A[i];
A[i]:=temp
}
return(A);
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 33
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 34
SELECTION SORT
VERIFY THE ALGORITHM
PASS 1: Fix the first min data in its positon
INDEX KEY-A I=1,pos=2 I=1,pos=2 I=1,pos=4
I=1,
pos=4
1 23 23 23 23 7
2 12 12 12 12 12
3 18 18 18 18 18
4 7 7 7 7 23
5 20 20 20 20 20
RULE 1 : Find the minimum element position and interchange A[i] and A[pos]
PASS 2: Fix the second minimum data in its positon
INDEX KEY-A I=2, pos=2 I=2,pos=2 I=2, pos=2
1 7 7 7 7
2 12 12 12 12
3 18 18 18 18
4 23 23 23 23
5 20 20 20 20
PASS 3: Fix the third minimum data in its positon
INDEX KEY-A I=3,pos=3
i=3,
pos=3
1 7 7 7
2 12 12 12
3 18 18 18
4 23 23 23
5 20 20 20
PASS 4: Fix the fourth minimum data in its positon
INDEX KEY-A I=4, pos=5
1 7 7
2 12 12
3 18 18
4 23 20
5 20 23
6. Write an algorithm for Insertion Sort an array of n
elements.
Algorithm Insertion Sort(A, n)
// This algorithm return the sorted(ascending order) array for the given array A with size n
{
for i:=2 to n do{
insertdata :=A[i];
j:=i-1;
while (j > 0 AND A[j] > insertdata){
A[j+1]=A[j];
j:=j-1;
}
A[j+1]:=insertdata;
}
return(A);
}
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 35
Performance Analysis:
• Performance analysis or analysis of algorithms refers to the task of determining
the efficiency of an algorithm
• To judge an algorithm, particularly two things are taken into consideration
Space complexity
Time complexity
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 36
• Space Complexity
The space needed by an algorithm is the sum of a fixed part and a variable part.
• Space complexity S(P) = C + Sp(Instance characteristics)
• The fixed part includes space for
• Instructions
• Simple variables
• Fixed size componentvariables
• Space for constants, Etc..
• The variable part includes space for
• Component variables whose size is dependant on the particular problem instance being solved
• Recursion stack space, Etc..
Examples
Algorithm NEC (float x, float y, float z)
{
Return (X + Y+Y*Z + (X + Y+Z)) /(X+ Y) + 4.0;
}
In the above algorithm, there are no instance characteristics and the space
needed by X, Y, Z is independent of instance characteristics, therefore we can
write, S(XYZ) =3+0=3 One space each for X, Y and Z
Space complexity is O(1).
Algorithm ADD ( float [], intn)
{
sum := 0.0; for i:=1 to n do
sum:=sum +X[i]; return sum;
}
Here, atleast n words since X must be large enough to hold the n elements to be summed.
Here the problem instances is characterized by n, the number of elements to be summed. So,
we can write, S(ADD) =3+n 3-one each for n, I and sum Where n- is for
Time Complexity
The time complexity of a problem is
The number of steps that it takes to solve an instance of the
problem as a function of the size of the input (usually measured
in bits), using the most efficient algorithm.
Priori analysis
Posteriori analysis or run (execution) time.
Time complexity T(P) = C + TP(n)
In general it can be two measured in ways:
By Bruit force method Time= Compile Time +Run Time
Example:
TP(n) = CaADD(n)+ CsSUB(n)+ CmMUL(n)+
CdDIV(n)+……………..
By using step count method.
Time complexity cases
3 4 5 6 7 9 10 12 15
A 1 2 3 4 5 6 7 8 9
Best Case: Inputs are provided in such a way that the
minimum time is required to process them. O(1)
Average Case:The amount of time the algorithm takes
on an average set of inputs. O(n/2)
Worst Case: The amount of time the algorithm takes
on the worst possible set of inputs . O(n)
Example: Linear Search
Contd….
2. Global variable count method:
Statement S/e Frequency Total steps
1. Algorithm Sum(a, n) 0 - 0
2. { 0 - 0
3. s:=0; 1 1 1
4. for i:=1 to n do 1 n+1 n+1
5. s:=s+a[i]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3 steps
Ex:
Time complexity of Matrix addition
Time complexity of Fibonacci number
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 45
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 46
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 47
Contd….
Step count method have two approaches:
1. Global variable count method:
Example:
Algorithm Sum(a, n)
{
s:=0;
for i:=1 to n do
{
s:=s+a[i];
}
return s;
}
Algorithm sum with count statement added
count:=0;
Algorithm Sum(a,n)
{
s:=0;
count:=count+1;
for i:=1 to n do
{
count:=count +1;
s:=s+a[i];
count:=count+1;
}
count:=count+1; //for last time of for loop
count:=count+1; //for return statement
return s;
}
Thus the total number of steps are 2n+3
Important questions
(1) Define algorithm
(2) What is pseudo code?
(3) How to device an algorithm?
(4) How to validate an algorithm?
(5) How to analysis an algorithm?
(6) How to test a program?
(7) What are the characteristics of an algorithm?
(8) Write an algorithm for linear sort.
(9) Write an algorithm for binary search
(10)Write an algorithm for selection sort.
(11)Write an algorithm for finding max and min in an array of n elements.
(12) Write an algorithm to check the give positive integer is prime or not.
(13)Write an Euclidean algorithm for finding GCD.
7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 49

More Related Content

What's hot

Algorithm Design Technique
Algorithm Design Technique Algorithm Design Technique
Algorithm Design Technique Bharat Bhushan
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartSachin Goyani
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartRabin BK
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithmsDr Geetha Mohan
 
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.pptANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.pptDaveCalapis3
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIMohamed Loey
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)Adam Mukharil Bachtiar
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
Algorithm Design Presentation
Algorithm Design PresentationAlgorithm Design Presentation
Algorithm Design PresentationKawsar Ahmed
 

What's hot (20)

Algorithm Design Technique
Algorithm Design Technique Algorithm Design Technique
Algorithm Design Technique
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Types of algorithms
Types of algorithmsTypes of algorithms
Types of algorithms
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Hashing
HashingHashing
Hashing
 
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.pptANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
ANALYSIS-AND-DESIGN-OF-ALGORITHM.ppt
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Graph algorithm
Graph algorithmGraph algorithm
Graph algorithm
 
Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)Algorithm and Programming (Introduction of Algorithms)
Algorithm and Programming (Introduction of Algorithms)
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Algorithm Design Presentation
Algorithm Design PresentationAlgorithm Design Presentation
Algorithm Design Presentation
 
Parsing
ParsingParsing
Parsing
 

Similar to Introduction to algorithms

Algorithms notes 2 tutorials duniya
Algorithms notes 2   tutorials duniyaAlgorithms notes 2   tutorials duniya
Algorithms notes 2 tutorials duniyaTutorialsDuniya.com
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithmmshoaib15
 
AOA Week 01.ppt
AOA Week 01.pptAOA Week 01.ppt
AOA Week 01.pptINAM352782
 
Ch1 principles of software development
Ch1 principles of software developmentCh1 principles of software development
Ch1 principles of software developmentHattori Sidek
 
Algorithm Identification In Programming Assignments
Algorithm Identification In Programming AssignmentsAlgorithm Identification In Programming Assignments
Algorithm Identification In Programming AssignmentsKarin Faust
 
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
 
1.1 the introduction of design and analysis of algorithm
1.1 the introduction of design and analysis of algorithm1.1 the introduction of design and analysis of algorithm
1.1 the introduction of design and analysis of algorithmMohammed khaja Jamaluddin
 
Notion of an algorithm
Notion of an algorithmNotion of an algorithm
Notion of an algorithmNisha Soms
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfHarshNagda5
 
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
 
11 Unit 1 Problem Solving Techniques
11  Unit 1 Problem Solving Techniques11  Unit 1 Problem Solving Techniques
11 Unit 1 Problem Solving TechniquesPraveen M Jigajinni
 
Algorithm Specification and Data Abstraction
Algorithm Specification and Data Abstraction Algorithm Specification and Data Abstraction
Algorithm Specification and Data Abstraction Ashutosh Satapathy
 

Similar to Introduction to algorithms (20)

Algorithms notes 2 tutorials duniya
Algorithms notes 2   tutorials duniyaAlgorithms notes 2   tutorials duniya
Algorithms notes 2 tutorials duniya
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithm
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
AOA Week 01.ppt
AOA Week 01.pptAOA Week 01.ppt
AOA Week 01.ppt
 
Ch1 principles of software development
Ch1 principles of software developmentCh1 principles of software development
Ch1 principles of software development
 
Algorithm Identification In Programming Assignments
Algorithm Identification In Programming AssignmentsAlgorithm Identification In Programming Assignments
Algorithm Identification In Programming Assignments
 
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
 
1.1 the introduction of design and analysis of algorithm
1.1 the introduction of design and analysis of algorithm1.1 the introduction of design and analysis of algorithm
1.1 the introduction of design and analysis of algorithm
 
Com Ed 6 Prelim
Com Ed 6 PrelimCom Ed 6 Prelim
Com Ed 6 Prelim
 
Module 1 python.pptx
Module 1 python.pptxModule 1 python.pptx
Module 1 python.pptx
 
Com Ed 6 Prelim
Com Ed 6 PrelimCom Ed 6 Prelim
Com Ed 6 Prelim
 
Problem solving methodology
Problem solving methodologyProblem solving methodology
Problem solving methodology
 
Testing techniques
Testing techniquesTesting techniques
Testing techniques
 
Notion of an algorithm
Notion of an algorithmNotion of an algorithm
Notion of an algorithm
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.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
 
11 Unit 1 Problem Solving Techniques
11  Unit 1 Problem Solving Techniques11  Unit 1 Problem Solving Techniques
11 Unit 1 Problem Solving Techniques
 
Algorithm Specification and Data Abstraction
Algorithm Specification and Data Abstraction Algorithm Specification and Data Abstraction
Algorithm Specification and Data Abstraction
 

More from subhashchandra197

More from subhashchandra197 (12)

Unit ii divide and conquer -4
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
 
Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
 

Recently uploaded

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
(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
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
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
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
(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...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
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
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 

Introduction to algorithms

  • 1. INTRODUCTION TO ALGORITHMS By Dr. N. Subhash Chandra 1
  • 2. Fundamentals of Algorithm Analysis CONTENTS 1) Introduction 2) Definition of algorithm 3) Algorithmic Problem solving 4) Methods of Specifying an Algorithm 5) Steps for writing an algorithm(Pseudo code) 6) Sample Problems and Algorithms 7) Quiz 8) Important Questions 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 2
  • 3. Algorithm Background •Algorithm: The word algorithm came from the name of a Persian mathematician Abu Jafar Mohammed Ibn Musa Al Khowarizmi (ninth century). •An algorithm is simply s set of rules used to perform some calculations either by hand or more usually on a machine (computer). 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 3
  • 4. Definition of Algorithm Definition: An algorithm is a finite sequence of unambiguous instructions for solving a particular problem. It must satisfy the specific characteristics. 4
  • 5. Characteristics 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 terminates after a finite number of steps. • Efficiency: Every instruction must be very basic and runs in short time with effective results better than human computations. 5
  • 6. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 6 Understand the Problem Selected the appropriate Design Techniques and Data Structures Write the Computational Solutions (Algorithms) Validate the Algorithms Analysis the Algorithms Real World Problem Existing Design Techniques Iterative method Recursive Method Brute-force Divide and Conquer Greedy Method Dynamic Programming Brach and Bound Method Backtracking Method Optimization Method Random Method Existing Data Structures Array Linked List Stack Queue Binary Tree Binary Search Tree Heap Tree Red Black Tree Quad Tree Un-directed Graph Directed Graph Planar Graph Selected the efficient Algorithm Implement the algorithm for selected language Testing Program Algorithmic Problem solving
  • 7. Issues for Algorithm • There are various issues in the study of algorithms; 1. How to devise algorithms: The creation of an algorithm is a logical activity which may never be fully automated. 2. How to express algorithms: We shall express all of our algorithms using the best principlesof structuring. 3. How to validate algorithms:After creation of algorithms is to validate algorithms. The process of checking an algorithm computes the correctanswer for all possible legal inputs is called algorithm validation. The purpose of validation of algorithm is to find whether algorithm works properly without being dependent upon programming languages. 4. How to analyze algorithms:Analysis of algorithm is a task of determining how much computing time and storage is required by an algorithm.Analysis of algorithms is also calledperformance analysis.The behavior of algorithm in best case, worst case and average caseneeds to be obtained. 5. How to test a program: Testing a program really consists of two phases: i) Debugging: While debugging a program, it is checked whether program produces faulty results for valid set of input and if it is found then the program has to be corrected. ii) Profiling or performance measuring: Profiling is a process of measuring time and space required by a corrected program for valid set of inputs. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 7
  • 8. How to devise algorithms? Creating an algorithm is an art which may never be fully automated. In this research we want to study more design techniques and select appropriate design techniques to devise the new useful algorithm. The most important design techniques are (a) Brute-force or exhaustive search (b) Divide and Conquer (c) Greedy Algorithms (d) Dynamic Programming (e) Branch and Bound Algorithm (f) Randomized Algorithm (g) Backtracking 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 8
  • 9. How to validate algorithms? Once an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs. It helps to the next process for writing program code in a particular programming language. This phase is also called program proving or program verification. The proof of correctness requires that the solution be stated in two forms. First form, In this form a program which is annotated by a set of assertations about the input and output variables of the program. Then these assertations are often expresses in the predicate calculus. Second form is called specification, and this expressed in the predicate calculus. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 9
  • 10. How to analysis algorithms? Efficiency of an algorithm can be analyzed at two different stages, before and after implementation.  Before implementation(priori analysis), analysis of algorithms refers to the task of determining how much computing time and storage space an algorithm require.  This is the challenging area which require great mathematical skill.(Quantitative judgement about the algorithms) An algorithm perform in the best case, the worst case or on the average case are typical. Dept. of CSE, CVR College of Engineering, Hyderabad 10
  • 11. Algorithm Solutions for a Given problem 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 11
  • 12. How to test a program? After implementation(posterior analysis), Testing a program consists of two phases: (a) Debugging (b) Profiling Debugging is the process of executing programs on sample data sets to determine whether the faulty results occur if so to correct them. Profiling or performance measurement is the process of executing a correct program on data sets and measuring time and space it takes to compute the results. Dept. of CSE, CVR College of Engineering, Hyderabad 12
  • 13. 4. Methods of Specifying an Algorithm There are three ways to specify an algorithm. They are: a. Natural language b. Pseudocode c. Flowchart Pseudocode and flowchart are the two options that are most widely used nowadays for specifying algorithms. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 13
  • 14. 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. Example: An algorithm to perform addition of two numbers. 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. Such a specification creates difficulty while implementing it. Hence many programmers prefer specification of algorithm by means of Pseudocode. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 14
  • 15. 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 method of expressing an algorithm by a collection of connected geometric shapes containing descriptions of the algorithm’s steps. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 15
  • 16. 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. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 16
  • 17. Steps for writing an algorithm(Pseudo code): 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,…,pn) The head section also has the following: //Problem Description: //Input: //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 open and closed by {, } respectively. Proper indention is must for block. 5. Comments are written using // at the beginning. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 17
  • 18. 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. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 18
  • 19. Operators and Conditional Statement(Pseudo code): There are two Boolean values TRUE and FALSE. Logical operators: AND, OR, NOT. • Relational operators: <, >, ≥,,=,.Arithmetic operators: +, -, *, /, %; The conditional statement if-then or if-then-else is written in the following form. If (condition) then (statement) • If (condition) then {Block-1 }else { Block-2} • ‘If’ is a powerful statement used to make decisions based as a condition. If a condition is true the particular block of statements are execute. Example if(a>b) then { write("a is big"); } else { write("b is big"); } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 19
  • 20. Case Statement(Pseudo code): Case statement case { :(condition -1): (statement-1) :(condition -2): (statement-2) :(condition -n): (statement-n) .............. .............. else :(statement n+1); } If condition -1 is true, statement -1 executed and the case statement is exited. If statement -1 is false, condition -2 is evaluated. If condition -2 is true, statement-2 executed and so on. If none of the conditions are true, statement –(n+1) is executed and the case statement is exited. The else clause is optional. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 20
  • 21. for loop statement(Pseudo code): Loop statements: For loop: The general form of the for loop is for variable:=value1 to value2 step value3 do { Statement -1; Statement -2; ... ... Statement -n; } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 21 Example 2: for i:=1 to 10 step 2 do { write(i); //displaying numbers from 1 to 10 i:=i+2; } Example 1: for i:=1 to 10 do { write(i); //displaying numbers from 1 to 10 i:=i+1; }
  • 22. While Statement(Pseudo code): while loop: The general form of whileis- while (condition) do { statement 1; statement 2; ... ... statement n; } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 22 Example i:=1; while(i<=n) do { write (i); i:=i+1; }
  • 23. repeat-until statement(Pseudo code): Repeat-until loop: The general form of repeat-until is- repeat { statement 1; statement 2; ... ... statement n; }until (condition); 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 23 Example i:=1; repeat { write (i); i:=i+1; } until (i>10);
  • 24. Break, Array and function Statement(Pseudo code): Break: this statement is exit from the loop. Elements of array are accessed using [ ]. • For example, if A is an one-dimensional array, then ith element can be accessed using A[i]. • If A is two-dimensional array, then (i, j)th element can be accessed using A[i,j]. Procedures (functions): There is only one type of procedure: An algorithm consists of a heading and a body. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 24
  • 25. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 25 SAMPLE ALGORITHMS
  • 26. Write an algorithm to find maximum element in an array with size ‘n’. Algorithm Max(A, n) // This algorithm return the maximum element in the given array A with size n { maximum=A[1]; for i=2 to n do if A[i]>maximum then maximum=A[i]; return maximum; } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 26
  • 27. VERIFICATION 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 27 GIVEN DATA : 12 5 56 34 45 SIZE OF THE DATA : n=5 STEP 1 MAX=12 STEP 2 MAX=12 SINCE CURRENT DATA=5 WHICH IS LESS THAN MAX STEP 3 MAX=56 SINCE CURRENT DATA=56 WHICH IS GREATER THAN MAX STEP 4 MAX = 56 SINCE CURRENT DATA=34 WHICH IS LESS THAN MAX STEP 5 MAX=56 SINCE CURRENT DATA=45 WHICH IS LESS THAN MAX
  • 28. Write a linear sort algorithm to sort the data in an array with size ‘n’. Ans: Algorithm LinearSort(A, n) // This algorithm return the sorted(ascending order) array for the given array A with size n { for i=1 to n-1 do{ for j=i+1 to n do if A[i]>A[j] then { temp=A[i]; A[i]=A[j]; A[j]=temp; } } return A; } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 28
  • 29. VALIDATE THE ALGORITHM 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 29 VERIFY THE ALGORITHM PASS 1: Fix the first minimum element in the first position INDEX A I=1, J=2 I=1,J=3 I=1,J=4 I=1,J=5 1 23 12 12 7 7 2 12 23 23 23 23 3 18 18 18 18 18 4 7 7 7 12 12 5 20 20 20 20 20 RULE 1 : IF A[I]>A[J] THEN INTERCHANGE(A[I], A[J]) ELSE NO-ACTION
  • 30. 3. Write an algorithm for finding max and min in an array of n elements. Algorithm MaxMin(A, n) // This algorithm return the max and min values in a given array A, n- size of the array. { maximum:=A[1]; minimum:=A[1]; for i:=2 to n do { if A[i]<maximum then maximum:=A[i]; if A[i]>minimum then minimum:=A[i]; } return([minimum, maximum]); } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 30
  • 31. 4. Write an algorithm for Bubble Sort an array of n elements. Algorithm BubbleSort(A, n) // This algorithm return the sorted(ascending order) array for the given array A with size n { for i=1 to n-1 do{ for j=1 to n-i do if A[j]>A[j+1] then { temp=A[j]; A[j]=A[j+1]; A[j+1]=temp; } } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 31
  • 32. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 32 BUBBLE SORT VERIFY THE ALGORITHM PASS 1: Fix the largest element at last position INDEX KEY-A I=1 I=2 I=3 I=4 1 23 12 12 12 12 2 12 23 18 18 18 3 18 18 23 7 7 4 7 7 7 23 20 5 20 20 20 20 23 RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE NO- ACTION PASS 2: Fix the second largest data in its position INDEX KEY-A I=1 I=2 I=3 1 12 12 12 12 2 18 18 7 7 3 7 7 18 18 4 20 20 20 20 5 23 23 23 23 RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE NO-ACTION PASS 3: Fix the third largest data in its position INDEX KEY-A I=1 I=2 1 12 7 7 2 7 12 12 3 18 18 18 4 20 20 20 5 23 23 23 RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE NO- ACTION PASS 4: Fix the fourth largest data in its position INDEX KEY-A I=1 1 7 7 2 12 12 3 18 18 4 20 20 5 23 23 RULE 1 : IF A[i]>A[i+1] THEN INTERCHANGE(A[I], A[J]) ELSE NO-ACTION
  • 33. 5. Write an algorithm for Selection Sort an array of n elements. Algorithm SelectionSort(A, n) // This algorithm return the sorted(ascending order) array for the given array A with size n { for i:=1 to n-1 do{ pos:=i; for j:=i+1 to n do{ if A[j]<A[pos] then pos:=j; } temp=A[pos] A[pos]:=A[i]; A[i]:=temp } return(A); } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 33
  • 34. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 34 SELECTION SORT VERIFY THE ALGORITHM PASS 1: Fix the first min data in its positon INDEX KEY-A I=1,pos=2 I=1,pos=2 I=1,pos=4 I=1, pos=4 1 23 23 23 23 7 2 12 12 12 12 12 3 18 18 18 18 18 4 7 7 7 7 23 5 20 20 20 20 20 RULE 1 : Find the minimum element position and interchange A[i] and A[pos] PASS 2: Fix the second minimum data in its positon INDEX KEY-A I=2, pos=2 I=2,pos=2 I=2, pos=2 1 7 7 7 7 2 12 12 12 12 3 18 18 18 18 4 23 23 23 23 5 20 20 20 20 PASS 3: Fix the third minimum data in its positon INDEX KEY-A I=3,pos=3 i=3, pos=3 1 7 7 7 2 12 12 12 3 18 18 18 4 23 23 23 5 20 20 20 PASS 4: Fix the fourth minimum data in its positon INDEX KEY-A I=4, pos=5 1 7 7 2 12 12 3 18 18 4 23 20 5 20 23
  • 35. 6. Write an algorithm for Insertion Sort an array of n elements. Algorithm Insertion Sort(A, n) // This algorithm return the sorted(ascending order) array for the given array A with size n { for i:=2 to n do{ insertdata :=A[i]; j:=i-1; while (j > 0 AND A[j] > insertdata){ A[j+1]=A[j]; j:=j-1; } A[j+1]:=insertdata; } return(A); } 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 35
  • 36. Performance Analysis: • Performance analysis or analysis of algorithms refers to the task of determining the efficiency of an algorithm • To judge an algorithm, particularly two things are taken into consideration Space complexity Time complexity 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 36
  • 37. • Space Complexity The space needed by an algorithm is the sum of a fixed part and a variable part. • Space complexity S(P) = C + Sp(Instance characteristics) • The fixed part includes space for • Instructions • Simple variables • Fixed size componentvariables • Space for constants, Etc.. • The variable part includes space for • Component variables whose size is dependant on the particular problem instance being solved • Recursion stack space, Etc..
  • 38. Examples Algorithm NEC (float x, float y, float z) { Return (X + Y+Y*Z + (X + Y+Z)) /(X+ Y) + 4.0; } In the above algorithm, there are no instance characteristics and the space needed by X, Y, Z is independent of instance characteristics, therefore we can write, S(XYZ) =3+0=3 One space each for X, Y and Z Space complexity is O(1). Algorithm ADD ( float [], intn) { sum := 0.0; for i:=1 to n do sum:=sum +X[i]; return sum; } Here, atleast n words since X must be large enough to hold the n elements to be summed. Here the problem instances is characterized by n, the number of elements to be summed. So, we can write, S(ADD) =3+n 3-one each for n, I and sum Where n- is for
  • 39. Time Complexity The time complexity of a problem is The number of steps that it takes to solve an instance of the problem as a function of the size of the input (usually measured in bits), using the most efficient algorithm. Priori analysis Posteriori analysis or run (execution) time. Time complexity T(P) = C + TP(n) In general it can be two measured in ways: By Bruit force method Time= Compile Time +Run Time Example: TP(n) = CaADD(n)+ CsSUB(n)+ CmMUL(n)+ CdDIV(n)+…………….. By using step count method.
  • 40. Time complexity cases 3 4 5 6 7 9 10 12 15 A 1 2 3 4 5 6 7 8 9 Best Case: Inputs are provided in such a way that the minimum time is required to process them. O(1) Average Case:The amount of time the algorithm takes on an average set of inputs. O(n/2) Worst Case: The amount of time the algorithm takes on the worst possible set of inputs . O(n) Example: Linear Search
  • 41. Contd…. 2. Global variable count method: Statement S/e Frequency Total steps 1. Algorithm Sum(a, n) 0 - 0 2. { 0 - 0 3. s:=0; 1 1 1 4. for i:=1 to n do 1 n+1 n+1 5. s:=s+a[i]; 1 n n 6. return s; 1 1 1 7. } 0 - 0 Total 2n+3 steps Ex:
  • 42.
  • 43. Time complexity of Matrix addition
  • 44. Time complexity of Fibonacci number
  • 45. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 45
  • 46. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 46
  • 47. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 47
  • 48. Contd…. Step count method have two approaches: 1. Global variable count method: Example: Algorithm Sum(a, n) { s:=0; for i:=1 to n do { s:=s+a[i]; } return s; } Algorithm sum with count statement added count:=0; Algorithm Sum(a,n) { s:=0; count:=count+1; for i:=1 to n do { count:=count +1; s:=s+a[i]; count:=count+1; } count:=count+1; //for last time of for loop count:=count+1; //for return statement return s; } Thus the total number of steps are 2n+3
  • 49. Important questions (1) Define algorithm (2) What is pseudo code? (3) How to device an algorithm? (4) How to validate an algorithm? (5) How to analysis an algorithm? (6) How to test a program? (7) What are the characteristics of an algorithm? (8) Write an algorithm for linear sort. (9) Write an algorithm for binary search (10)Write an algorithm for selection sort. (11)Write an algorithm for finding max and min in an array of n elements. (12) Write an algorithm to check the give positive integer is prime or not. (13)Write an Euclidean algorithm for finding GCD. 7/12/2020 Dept. of CSE, CVR College of Engineering, Hyderabad 49