SlideShare a Scribd company logo
DESIGN AND ANALYSIS
OFALGORITHM -
INTRODUCTION
DR. V. NIRMALA
DEPARTMENT OF AI & DS
EASWARI ENGINEERING COLLEGE
Algorithm
• An algorithm is a procedure to solve a particular problem in a finite number of steps for a finite sized
input.
• An algorithm can be implemented in more than one programming language so algorithm is a language
independent.
• Not all procedures can be called an algorithm.
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.
Characteristics of Algorithm
• Unambiguous:
Algorithm should be clear and unambiguous. Each of its steps (or phases), one and their inputs/outputs should
be clear and must lead to only meaning.
• Input:
An algorithm should have 0 or more well-defined inputs.
• Output:
An algorithm should have 1 or more well-defined outputs, and should match the desired output.
• Finiteness:
Algorithms must terminate after a finite number of steps.
• Feasibility:
Should be feasible with the available resources.
• Independent:
An algorithm should have step-by-step directions, which should be independent of any programming code.
Properties of Algorithm
Properties of Algorithm…
1. Input: An algorithm should have some inputs.
2. Output: At least one output should be returned by the algorithm after the completion of the specific task based on the
given inputs.
3. Definiteness: Every statement of the algorithm should be unambiguous.
4. Finiteness: No infinite loop should be allowed in an algorithm.
Example:
while(1<2)
{
number=number/2;
}
5. Effectiveness: Writing an algorithm is a priori process of actual implementation of the algorithm. So, a person should
analyze the algorithm in a finite amount of time with a pen and paper to judge the performance for giving the final
version of the algorithm.
Properties of Algorithm…
6. Non Ambiguity
Each step in an algorithm should be non-ambiguous. That means each instruction should be clear and precise. The
instruction in any algorithm should not denote any conflicting meaning. This property also indicates the effectiveness of
algorithm.
7. Range of Input
The range of input should be specified. This is because normally the algorithm is input driven and if the range of input is
not being specified then algorithm can go in an infinite state.
8.Multiplicity
The same algorithm can be represented into several different ways. That means we can write in simple English the
sequence of instruction or we can write it in form of pseudo code. Similarly, for solving the same problem we can write
several different algorithms.
9.Speed
The algorithmis written using some specified ideas. Bus such algorithm should be efficient and should produce the output
with fast speed.
10.Finiteness
The algorithm should be finite. That means after performing required operations it should be terminate
How to write an Algorithm?
• There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent. Algorithms
are never written to support a particular programming code.
• As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-
else), etc.
• These common constructs can be used to write an algorithm. We write algorithms in a step-by-step manner, but it is
not always the case.
• Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the
problem domain, for which we are designing a solution
• Example: Two ways of writing an algorithm
Pseudo code for expressing algorithms
• Pseudo code, as the name suggests, is a false code or is a high-level description of an algorithm.
• Pseudo code can be easily understood by someone who has basic knowledge of programming.
• Pseudo code does not have a specific syntax unlike a program that is written using syntaxes of a
particular language.
• Hence, pseudo code cannot be executed on a computer; rather it eases the work of a programmer as it
can be easily understood.
• While writing pseudo code one can use control structures that include for, while, if-then-else, repeat
until.
• These control structures are common in almost all programming languages and hence can be used
when required.
• Once we are done with the pseudo code we can easily modify it into a program in our preferred
language.
• Another advantage of pseudo code is that the running time of the program can be estimated in a
general manner using it.  A pseudo code gives us a count of fundamental operations
Difference between Algorithm and
Pseudo code
• An algorithm is a well defined sequence of instructions that provide a solution to the given problem.
• A pseudo code is a method which is used to represent an algorithm.
• An algorithm has some specific characteristics that describe the process.
• A pseudo code on the other hand is not restricted to something. It’s only objective is to represent an
algorithm in a realistic manner.
• An algorithm is written in plain English or general language.
• A pseudo code is written with a hint of programming concepts such as control structures.
• To understand the difference between an algorithm and pseudocode lets have a look into the following
example:
Advantages of pseudo code
• Pseudo code improves the readability and is the best way to start implementing an algorithm
• A pseudo code works as a rough documentation. It makes the work of a programmer easy as it
helps them understand the code better.
• It acts as a bridge between a program and an algorithm.
• It makes the process of constructing a code easy
How to write a Pseudo code
As mentioned, a pseudo code is a method used to implement an algorithm. However there are a few points to be noted
while writing a pseudo code.
They are:
1. First, arrange the tasks in a sequence so that the pseudo code can be written by following the same sequence. This will
make the process more clear and simple.
2. A pseudo code is something that can be understood by any basic programmer. However, in case of complex problems it
could be difficult to understand the main goal of the problem if it is not specified in the pseudo code. So, do include a
statement that established the main goal.
3. Follow the indentation and whitespaces as you would do while writing an actual program. Indentation helps greatly
with readability.
How to write a Pseudo code..
4. Here, both the examples have the same meaning. However, the 2nd example is more clear and understandable compared to the
1st. This is the magic of Indentation and white spaces.
5. Following a naming convention is important. Sometimes, naming in the wrong way can lead to confusion. So, naming must be
done in a simple and distinct way. Sentence casings will always help you differentiate between constants, variables, methods, and
others. This will avoid any confusion that might be present in the algorithm.
For methods use CamelCase
For constants use UPPERCASE
For variables use lowercase
6. A pseudo code is supposed to explain the code in detail. Do not keep the pseudo code abstract.
7. Use of control structures such as ‘for’, ‘while’, ‘if-then’ and ‘cases’ makes it easy while you develop a program using the
pseudo code as reference.
8. A pseudo code must be perfect in order for the code to be perfect. So do check if the pseudo code consists of any infinite loops
or any gaps.
9. A pseudo code must be must enough to be understood by a layman so do not write it in a programmatic way.
Do’s and Don’ts while writing a Pseudo
code:
Do’s:
 Make use of control structures.
 Naming conventions must be followed properly.
 Use indentation and white spaces wherever required as these are the key points for a pseudo code.
 Keep the pseudo code simple and concise.
Don’ts:
 Do not generalize the pseudo code.
 A pseudo code mustn’t be abstract.
Performance analysis of an algorithm
Performance analysis of an algorithm depends upon two factors i.e. amount of memory used and amount of compute time
consumed on any CPU.
Formally they are notified as complexities in terms of:
1. Space Complexity.
2. Time Complexity.
Space Complexity of an algorithm
Space Complexity of an algorithm is the amount of memory it needs to run to completion i.e. from start of execution to its
termination.
 Constant space complexity
 Linear space complexity
let’s have a look into a few examples to understand how to calculate the space complexity.
Performance analysis of an algorithm ..
Here, in this example, we have 4 variables which are a, b, c, and sum. All these variables are of type int
hence, each of them require 4 bytes of memory.
 Total requirement of memory would be : ((4*4)+4)  20 bytes.
Performance analysis of an algorithm ..
Here, we have considered 4 additional bytes of memory which is for
the return value. Also, for this example as the space requirement is
fixed it is known as constant space complexity.
In this example, we have 3 variables i, k, n each of which will be
assigned 4 bytes of memory. Also, for the return value 4 bytes of
memory will be assigned. Here, we also have an array of size n,
hence the memory required for that would be (4*n).
 Hence the total memory requirement would be : (4n+12)
 This value will vary based on the value of n. As the value of n
increases the memory would also increase linearly. Therefore it is
known as Linear Space Complexity.
 Depending on the complexity of the algorithm, the space
complexity can range upto quadratic or complex too. We should
always try to keep the space complexity as minimum as possible.
Performance analysis of an algorithm ..
Time complexity is most commonly evaluated by considering the number of steps required to complete the execution of an algorithm. Also,
many times we make use of the big O notation which is an asymptotic notation while representing the time complexity of an algorithm. We
will be discussing asymptotic notations in the next article.
Now, let’s dive into calculating the time complexity with the help of a few examples.
Constant time complexity: Example:
Statement – printf(“EASWARI”);
In the above example, as it is a single statement the complexity of it would be constant. Its complexity wouldn’t change as it is not dependent on any variable.
Linear time complexity:
Example:
for(i=0; i< N; i++)
{
statement;
}
Here, the complexity would be proportional to n. Hence, the time complexity would be linear.
Performance analysis of an algorithm ..
Quadratic time complexity:
Example:
for(i=0; i< N; i++)
{
for(j=0; j <N;j++)
{
statement;
}
}
Here, in this example we have a loop inside another loop. In such a case, the complexity would be proportional
to n square. Hence, it would be of type quadratic and therefore the complexity is quadratic complexity.
Performance analysis of an algorithm ..
Logarithmic time complexity:
Example:
while(low <= high)
{
mid = (low + high) / 2;
if (target < list[mid])
high = mid - 1;
else if (target > list[mid])
low = mid + 1;
else break;
}
 In this example, we are working on dividing a set of numbers into two parts. Which means that the complexity would be
proportional to the number of times n can be divided by 2, which is a logarithmic value. Hence we have a logarithmic time
complexity.
Performance analysis of an algorithm ..
Example:1 Algorithm for Transpose of a Matrix
1. Create an auxiliary dummy_matrix that stores the transpose of the_matrix.
2. For a row in the first matrix, make it the first column in the newly constructed matrix.
3. Move to the next row and do it for all the rows. B[i][j] = A[j][i] , B is Transpose of A.
4. After completing all the rows, print the new_matrix, it is a Transpose of the given_matrix.
Time Complexity O(n2)
 where n is the maximum of r1 and c1.
 Here we simply run two loops first loop run r1 times and the second loop runs c1 times.
 And inside the second loop, we just change the location of the current element in the auxiliary matrix.
Space Complexity O(m2)
 where m is the maximum of r1 and c1.
 Here we create extra space for storing the transpose of the given_matrix.
 Here we also declared r1*c1 size for taking input from the given matrix.
Performance analysis of an algorithm ..
Example:2
How to find the time complexity of an algorithm
Given the following algorithm:
public Integer sum EvenNumbers(Integer N)
{
int sum = 0;
for (int number = 1; number <= N; number++)
if ((number % 2) == 0)
sum = sum + number;
return sum;
}
First, we split the code into individual operations and then compute how many times it is being executed as is shown in the following table
Now, we need to sum how many times each operation is executing.
Time complexity = 1 + 1 + N + N + N + N + 1 => 4N + 3 .
Asymptotic Notation
Asymptotic notations are the mathematical notations used to describe the run time complexity of an
algorithm
Types of asymptotic notations:
1. Big-O notation
2. Omega notation
3. Theta notation
4. Little oh notation
Asymptotic Notation…
Big-O Notation (O-notation)
 Big O Notation is a mathematical notation that helps us
to analyze how complex an algorithm is in terms of time
and space.
 Big-O notation represents the upper bound of the
running time of an algorithm.
 Thus, it gives the worst-case complexity of an
algorithm
O(g(n)) = { f(n): there exist positive constants c and n0
such that
0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
Asymptotic Notation…
Omega Notation (Ω-notation)
Omega notation represents the lower bound of the
running time of an algorithm. Thus, it provides the best
case complexity of an algorithm.
Ω(g(n)) = { f(n): there exist positive constants c and n0
such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }
The above expression can be described as a function f(n)
belongs to the set Ω(g(n)) if there exists a positive
constant c such that it lies above cg(n), for sufficiently
large n.
For any value of n, the minimum time required by the
algorithm is given by Omega Ω(g(n)).
Asymptotic Notation…
Theta Notation (Θ-notation)
Theta notation encloses the function from above and below.
Since it represents the upper and the lower bound of the
running time of an algorithm, it is used for analyzing the
average-case complexity of an algorithm.
Theta bounds the function within constants factors
For a function g(n), Θ(g(n)) is given by the relation:
Θ(g(n)) = { f(n): there exist positive constants c1, c2 and
n0
such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
Asymptotic Notation…
Thus, little o() means loose upper-bound of f(n). Little o is a rough estimate of the maximum order of
growth whereas Big-Ο may be the actual order of growth
Θ(g(n)) = { f(n): there exist positive constants c and n0
such that 0 ≤ f (n) <cg(n) for all n ≥ n0 }
Properties Asymptotic Notation…
As we have gone through the definition of these three notations let’s now discuss some important properties
of those notations.
1. General Properties:
If f(n) is O(g(n)) then a*f(n) is also O(g(n)) ; where a is a constant.
Example: f(n) = 2n²+5 is O(n²)
then 7*f(n) = 7(2n²+5) = 14n²+35 is also O(n²) .
Similarly, this property satisfies both Θ and Ω notation.
We can say
If f(n) is Θ(g(n)) then a*f(n) is also Θ(g(n)) ; where a is a constant.
If f(n) is Ω (g(n)) then a*f(n) is also Ω (g(n)) ; where a is a constant.
Properties Asymptotic Notation…
2. Transitive Properties:
If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) = O(h(n)) .
Example: if f(n) = n, g(n) = n² and h(n)=n³
n is O(n²) and n² is O(n³)
then n is O(n³)
Similarly, this property satisfies both Θ and Ω notation.
We can say
If f(n) is Θ(g(n)) and g(n) is Θ(h(n)) then f(n) = Θ(h(n)) .
If f(n) is Ω (g(n)) and g(n) is Ω (h(n)) then f(n) = Ω (h(n))
Properties Asymptotic Notation…
3. Reflexive Properties:
Reflexive properties are always easy to understand after transitive.
If f(n) is given then f(n) is O(f(n)). Since MAXIMUM VALUE OF f(n) will be f(n) ITSELF !
Hence x = f(n) and y = O(f(n) tie themselves in reflexive relation always.
Example: f(n) = n² ; O(n²) i.e O(f(n))
Similarly, this property satisfies both Θ and Ω notation.
We can say that:
If f(n) is given then f(n) is Θ(f(n)).
If f(n) is given then f(n) is Ω (f(n)).
Properties Asymptotic Notation…
4. Symmetric Properties :
If f(n) is Θ(g(n)) then g(n) is Θ(f(n)) .
Example: f(n) = n² and g(n) = n²
then f(n) = Θ(n²) and g(n) = Θ(n²)
This property only satisfies for Θ notation.
5. Transpose Symmetric Properties :
If f(n) is O(g(n)) then g(n) is Ω (f(n)).
Example: f(n) = n , g(n) = n²
then n is O(n²) and n² is Ω (n)
This property only satisfies O and Ω notations.
Probabilistic Analysis
In general, probabilistic analysis is used to analyze the running time of an algorithm. In simple words, probabilistic
analysis is the use of probability in the analysis of problems.
Sometimes, we can also use it to analyze quantities. One such example is the hiring cost in procedure Hire-Assistant.
Hiring Problem:
For example, you are using an employment agency to hire a new assistant for your company. The agency works in such a
way that it sends you one candidate per day. You then interview the candidate after which you must immediately decide
whether or not to hire that person. Also, if you hire that person, you must fire your current office assistant. Even if it is
someone you have hired recently you must fire them before you hire a new assistant.
For this, let us consider the cost to interview as ci per candidate and the cost to hire the candidate is ch.
Also, there are a few requirements to be fulfilled which are:
 At all times, you want to have the best candidate you have seen so far.
 Whenever you interview a candidate and feel that the candidate is better than your current assistant, you have to fire the
current assistant and then hire the candidate.
Probabilistic Analysis..
Let us have a look at the pseudo code to the model.
Hire-Assistant (n)
best<- 0 ;Candidate 0 is a least qualified candidate
fori<- 1 to n
do interview candidate i
if candidate i is better than candidate best
then best <- i
hire candidate i
Here are a few points to be considered for the Cost Model:
 We are not concerned about the running time of the Hire-Assistant.
 We must determine the total cost of hiring the best candidate.
 If n candidates are interviewed and m candidates are hired, then the cost would be nci+mch
 We have to pay the cost: nci to interview the candidates’irrespective oof how many candidates we hire.
 So, focus on analyzing the hiring cost mch
 m varies with the order of the candidates.
Probabilistic Analysis..
Best-Case Analysis for Hiring Problem
 We get the best case when the agency sends us the best applicant on the first day.
 The cost would be nci+ch
Worst-case Analysis for Hiring problem
 The worst case is when we hire all the n candidates.
 This happens only if each candidate that comes later is better than all those who came before. That is
when candidates come in increasing order of quality.
 The cost would be nci+nch
 Whenever this happens, we will fire the agency.
Probabilistic Analysis..
Average-case Analysis for Hiring problem
 For the average case, an input to the hiring problem is an ordering of the n applicants, there are n! different
inputs.
 To use probabilistic analysis, we assume that the candidates arrive in random order.
 Then we analyze our algorithm by computing an expected running time.
 This expectation is taken over the distribution of all the possible inputs.
 Thus, we are averaging the running time over all possible inputs.
 Now, we want to know the expected cost of our hiring algorithm in terms of the number of times we hired an
applicant.
 For this, Random variable X(s) is the number of applicants who are hired for a given input sequence s.
 Indicator random variable Xi for applicant i will be 1 if applicant i is hired, 0 otherwise.
Probabilistic Analysis..
What is Indicator Random Variable?
It is a powerful technique that is used for computing the expected value of a random variable. Also, it is a convenient method for converting between
probabilities and also expectations. Indicator Random Variable takes only 2 values, which are 1 and 0.
Indicator Random Variable XA=I{A} for an event A of a sample space is defined as:
I{A} = 1 if A occurs
0 if A does not occur
The expected value of an indicator Random Variable is associated with an event A is equal to the probability that A occurs.
Indicator RV – Example
Problem: Determine the expected number of heads in one toss.
Sample space is s{H, T}
Indicator random variable
XA= I{ coin coming up with heads}=1/2
The expected number of heads obtained in one flip of the coin is equal to the expected value of the indicator random variable.
Therefore, E[XA] = 1/2
This is all about probabilistic analysis, Hiring problem, and the Indicator random variable. Do check the next article on Disjoint sets.
Amortized Analysis
This analysis is used when the occasional operation is very slow, but most of the operations which are
executing very frequently are faster. Data structures we need amortized analysis for Hash Tables, Disjoint
Sets etc.
In the Hash-table, the most of the time the searching time complexity is O(1), but sometimes it executes
O(n) operations. When we want to search or insert an element in a hash table for most of the cases it is
constant time taking the task, but when a collision occurs, it needs O(n) times operations for collision
resolution.
Aggregate Method
The aggregate method is used to find the total cost. If we want to add a bunch of data, then we need to find
the amortized cost by this formula.
For a sequence of n operations, the cost i
Amortized Analysis..
Let us consider an example of a simple hash table insertion. How do we decide table size? There is a trade-
off between space and time, if we make hash-table size big, search time becomes low, but space required
becomes high.
Amortized Analysis..
For the dynamic array, let = cost of ith insertion.
The solution to this trade-off problem is to use Dynamic Table (or Arrays).
The idea is to increase size of table whenever it becomes full. Following are the steps to follow when table becomes full.
1) Allocate memory for a larger table of size, typically twice the old table.
2) Copy the contents of old table to new table.
3) Free the old table.
If the table has space available, we simply insert new item in available space.
What is the time complexity of n insertions using the above scheme?
If we use simple analysis, the worst case cost of an insertion is O(n). Therefore, worst case cost of n inserts is n * O(n) which is
O(n2). This analysis gives an upper bound, but not a tight upper bound for n insertions as all insertions don’t take Θ(n) time.

More Related Content

What's hot

Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
Radhakrishnan Chinnusamy
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
DataminingTools Inc
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
rajkumar1631010038
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
Abhimanyu Mishra
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Predicate logic
 Predicate logic Predicate logic
Predicate logic
Harini Balamurugan
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm propertiesLincoln School
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
Madhu Bala
 
Algorithm analysis and efficiency
Algorithm analysis and efficiencyAlgorithm analysis and efficiency
Algorithm analysis and efficiency
ppts123456
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
Akshaya Arunan
 
Paradigms
ParadigmsParadigms
Paradigms
Edward Blurock
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
Muhammad Amjad Rana
 
advanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelismadvanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelism
Pankaj Kumar Jain
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
Manash Kumar Mondal
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
sandeep54552
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
Mohamed Loey
 

What's hot (20)

Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 
Unit 1-problem solving with algorithm
Unit 1-problem solving with algorithmUnit 1-problem solving with algorithm
Unit 1-problem solving with algorithm
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Predicate logic
 Predicate logic Predicate logic
Predicate logic
 
03 algorithm properties
03 algorithm properties03 algorithm properties
03 algorithm properties
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
 
Algorithm analysis and efficiency
Algorithm analysis and efficiencyAlgorithm analysis and efficiency
Algorithm analysis and efficiency
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Paradigms
ParadigmsParadigms
Paradigms
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
advanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelismadvanced computer architesture-conditions of parallelism
advanced computer architesture-conditions of parallelism
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 

Similar to DAA Unit 1.pdf

classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codeshermiraguilar
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
NayanChandak1
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
Mani Kandan
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
ssuserb7c8b8
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
Michael Heron
 
Algorithms and flow charts
Algorithms and flow chartsAlgorithms and flow charts
Algorithms and flow charts
Chinnu Edwin
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
RathnaM16
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
MaaReddySanjiv
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
ssusere336f4
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)Yogesh Deshpande
 
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
ssuser586772
 
Program concep sequential statements
Program concep sequential statementsProgram concep sequential statements
Program concep sequential statements
ankurkhanna
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
Dr. Pankaj Agarwal
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
Appili Vamsi Krishna
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
Dr. Syed Hassan Amin
 
Unit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and FlowchartUnit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and Flowchart
Bom Khati
 
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
Praveen M Jigajinni
 

Similar to DAA Unit 1.pdf (20)

classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Algorithmic problem sloving
Algorithmic problem slovingAlgorithmic problem sloving
Algorithmic problem sloving
 
Design and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit oneDesign and Analysis of Algorithm ppt for unit one
Design and Analysis of Algorithm ppt for unit one
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
Algorithms and flow charts
Algorithms and flow chartsAlgorithms and flow charts
Algorithms and flow charts
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
 
FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)FPL -Part 2 ( Sem - I 2013)
FPL -Part 2 ( Sem - I 2013)
 
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
 
Program concep sequential statements
Program concep sequential statementsProgram concep sequential statements
Program concep sequential statements
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Unit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and FlowchartUnit 3.1 Algorithm and Flowchart
Unit 3.1 Algorithm and Flowchart
 
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
 

More from Nirmalavenkatachalam

Unit 2 DATABASE ESSENTIALS.pptx
Unit 2 DATABASE ESSENTIALS.pptxUnit 2 DATABASE ESSENTIALS.pptx
Unit 2 DATABASE ESSENTIALS.pptx
Nirmalavenkatachalam
 
Unit 3 ppt.pptx
Unit 3 ppt.pptxUnit 3 ppt.pptx
Unit 3 ppt.pptx
Nirmalavenkatachalam
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
Nirmalavenkatachalam
 
EDA-Unit 1.pdf
EDA-Unit 1.pdfEDA-Unit 1.pdf
EDA-Unit 1.pdf
Nirmalavenkatachalam
 
Bayes Theorem.pdf
Bayes Theorem.pdfBayes Theorem.pdf
Bayes Theorem.pdf
Nirmalavenkatachalam
 

More from Nirmalavenkatachalam (6)

Unit 2 DATABASE ESSENTIALS.pptx
Unit 2 DATABASE ESSENTIALS.pptxUnit 2 DATABASE ESSENTIALS.pptx
Unit 2 DATABASE ESSENTIALS.pptx
 
Unit 3 ppt.pptx
Unit 3 ppt.pptxUnit 3 ppt.pptx
Unit 3 ppt.pptx
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 
EDA-Unit 1.pdf
EDA-Unit 1.pdfEDA-Unit 1.pdf
EDA-Unit 1.pdf
 
Bayes Theorem.pdf
Bayes Theorem.pdfBayes Theorem.pdf
Bayes Theorem.pdf
 

Recently uploaded

Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 

Recently uploaded (20)

Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 

DAA Unit 1.pdf

  • 1. DESIGN AND ANALYSIS OFALGORITHM - INTRODUCTION DR. V. NIRMALA DEPARTMENT OF AI & DS EASWARI ENGINEERING COLLEGE
  • 2. Algorithm • An algorithm is a procedure to solve a particular problem in a finite number of steps for a finite sized input. • An algorithm can be implemented in more than one programming language so algorithm is a language independent. • Not all procedures can be called an algorithm. 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.
  • 3. Characteristics of Algorithm • Unambiguous: Algorithm should be clear and unambiguous. Each of its steps (or phases), one and their inputs/outputs should be clear and must lead to only meaning. • Input: An algorithm should have 0 or more well-defined inputs. • Output: An algorithm should have 1 or more well-defined outputs, and should match the desired output. • Finiteness: Algorithms must terminate after a finite number of steps. • Feasibility: Should be feasible with the available resources. • Independent: An algorithm should have step-by-step directions, which should be independent of any programming code.
  • 5. Properties of Algorithm… 1. Input: An algorithm should have some inputs. 2. Output: At least one output should be returned by the algorithm after the completion of the specific task based on the given inputs. 3. Definiteness: Every statement of the algorithm should be unambiguous. 4. Finiteness: No infinite loop should be allowed in an algorithm. Example: while(1<2) { number=number/2; } 5. Effectiveness: Writing an algorithm is a priori process of actual implementation of the algorithm. So, a person should analyze the algorithm in a finite amount of time with a pen and paper to judge the performance for giving the final version of the algorithm.
  • 6. Properties of Algorithm… 6. Non Ambiguity Each step in an algorithm should be non-ambiguous. That means each instruction should be clear and precise. The instruction in any algorithm should not denote any conflicting meaning. This property also indicates the effectiveness of algorithm. 7. Range of Input The range of input should be specified. This is because normally the algorithm is input driven and if the range of input is not being specified then algorithm can go in an infinite state. 8.Multiplicity The same algorithm can be represented into several different ways. That means we can write in simple English the sequence of instruction or we can write it in form of pseudo code. Similarly, for solving the same problem we can write several different algorithms. 9.Speed The algorithmis written using some specified ideas. Bus such algorithm should be efficient and should produce the output with fast speed. 10.Finiteness The algorithm should be finite. That means after performing required operations it should be terminate
  • 7. How to write an Algorithm? • There are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent. Algorithms are never written to support a particular programming code. • As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if- else), etc. • These common constructs can be used to write an algorithm. We write algorithms in a step-by-step manner, but it is not always the case. • Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the problem domain, for which we are designing a solution • Example: Two ways of writing an algorithm
  • 8. Pseudo code for expressing algorithms • Pseudo code, as the name suggests, is a false code or is a high-level description of an algorithm. • Pseudo code can be easily understood by someone who has basic knowledge of programming. • Pseudo code does not have a specific syntax unlike a program that is written using syntaxes of a particular language. • Hence, pseudo code cannot be executed on a computer; rather it eases the work of a programmer as it can be easily understood. • While writing pseudo code one can use control structures that include for, while, if-then-else, repeat until. • These control structures are common in almost all programming languages and hence can be used when required. • Once we are done with the pseudo code we can easily modify it into a program in our preferred language. • Another advantage of pseudo code is that the running time of the program can be estimated in a general manner using it.  A pseudo code gives us a count of fundamental operations
  • 9. Difference between Algorithm and Pseudo code • An algorithm is a well defined sequence of instructions that provide a solution to the given problem. • A pseudo code is a method which is used to represent an algorithm. • An algorithm has some specific characteristics that describe the process. • A pseudo code on the other hand is not restricted to something. It’s only objective is to represent an algorithm in a realistic manner. • An algorithm is written in plain English or general language. • A pseudo code is written with a hint of programming concepts such as control structures. • To understand the difference between an algorithm and pseudocode lets have a look into the following example:
  • 10. Advantages of pseudo code • Pseudo code improves the readability and is the best way to start implementing an algorithm • A pseudo code works as a rough documentation. It makes the work of a programmer easy as it helps them understand the code better. • It acts as a bridge between a program and an algorithm. • It makes the process of constructing a code easy
  • 11. How to write a Pseudo code As mentioned, a pseudo code is a method used to implement an algorithm. However there are a few points to be noted while writing a pseudo code. They are: 1. First, arrange the tasks in a sequence so that the pseudo code can be written by following the same sequence. This will make the process more clear and simple. 2. A pseudo code is something that can be understood by any basic programmer. However, in case of complex problems it could be difficult to understand the main goal of the problem if it is not specified in the pseudo code. So, do include a statement that established the main goal. 3. Follow the indentation and whitespaces as you would do while writing an actual program. Indentation helps greatly with readability.
  • 12. How to write a Pseudo code.. 4. Here, both the examples have the same meaning. However, the 2nd example is more clear and understandable compared to the 1st. This is the magic of Indentation and white spaces. 5. Following a naming convention is important. Sometimes, naming in the wrong way can lead to confusion. So, naming must be done in a simple and distinct way. Sentence casings will always help you differentiate between constants, variables, methods, and others. This will avoid any confusion that might be present in the algorithm. For methods use CamelCase For constants use UPPERCASE For variables use lowercase 6. A pseudo code is supposed to explain the code in detail. Do not keep the pseudo code abstract. 7. Use of control structures such as ‘for’, ‘while’, ‘if-then’ and ‘cases’ makes it easy while you develop a program using the pseudo code as reference. 8. A pseudo code must be perfect in order for the code to be perfect. So do check if the pseudo code consists of any infinite loops or any gaps. 9. A pseudo code must be must enough to be understood by a layman so do not write it in a programmatic way.
  • 13. Do’s and Don’ts while writing a Pseudo code: Do’s:  Make use of control structures.  Naming conventions must be followed properly.  Use indentation and white spaces wherever required as these are the key points for a pseudo code.  Keep the pseudo code simple and concise. Don’ts:  Do not generalize the pseudo code.  A pseudo code mustn’t be abstract.
  • 14. Performance analysis of an algorithm Performance analysis of an algorithm depends upon two factors i.e. amount of memory used and amount of compute time consumed on any CPU. Formally they are notified as complexities in terms of: 1. Space Complexity. 2. Time Complexity. Space Complexity of an algorithm Space Complexity of an algorithm is the amount of memory it needs to run to completion i.e. from start of execution to its termination.  Constant space complexity  Linear space complexity let’s have a look into a few examples to understand how to calculate the space complexity.
  • 15. Performance analysis of an algorithm .. Here, in this example, we have 4 variables which are a, b, c, and sum. All these variables are of type int hence, each of them require 4 bytes of memory.  Total requirement of memory would be : ((4*4)+4)  20 bytes.
  • 16. Performance analysis of an algorithm .. Here, we have considered 4 additional bytes of memory which is for the return value. Also, for this example as the space requirement is fixed it is known as constant space complexity. In this example, we have 3 variables i, k, n each of which will be assigned 4 bytes of memory. Also, for the return value 4 bytes of memory will be assigned. Here, we also have an array of size n, hence the memory required for that would be (4*n).  Hence the total memory requirement would be : (4n+12)  This value will vary based on the value of n. As the value of n increases the memory would also increase linearly. Therefore it is known as Linear Space Complexity.  Depending on the complexity of the algorithm, the space complexity can range upto quadratic or complex too. We should always try to keep the space complexity as minimum as possible.
  • 17. Performance analysis of an algorithm .. Time complexity is most commonly evaluated by considering the number of steps required to complete the execution of an algorithm. Also, many times we make use of the big O notation which is an asymptotic notation while representing the time complexity of an algorithm. We will be discussing asymptotic notations in the next article. Now, let’s dive into calculating the time complexity with the help of a few examples. Constant time complexity: Example: Statement – printf(“EASWARI”); In the above example, as it is a single statement the complexity of it would be constant. Its complexity wouldn’t change as it is not dependent on any variable. Linear time complexity: Example: for(i=0; i< N; i++) { statement; } Here, the complexity would be proportional to n. Hence, the time complexity would be linear.
  • 18. Performance analysis of an algorithm .. Quadratic time complexity: Example: for(i=0; i< N; i++) { for(j=0; j <N;j++) { statement; } } Here, in this example we have a loop inside another loop. In such a case, the complexity would be proportional to n square. Hence, it would be of type quadratic and therefore the complexity is quadratic complexity.
  • 19. Performance analysis of an algorithm .. Logarithmic time complexity: Example: while(low <= high) { mid = (low + high) / 2; if (target < list[mid]) high = mid - 1; else if (target > list[mid]) low = mid + 1; else break; }  In this example, we are working on dividing a set of numbers into two parts. Which means that the complexity would be proportional to the number of times n can be divided by 2, which is a logarithmic value. Hence we have a logarithmic time complexity.
  • 20. Performance analysis of an algorithm .. Example:1 Algorithm for Transpose of a Matrix 1. Create an auxiliary dummy_matrix that stores the transpose of the_matrix. 2. For a row in the first matrix, make it the first column in the newly constructed matrix. 3. Move to the next row and do it for all the rows. B[i][j] = A[j][i] , B is Transpose of A. 4. After completing all the rows, print the new_matrix, it is a Transpose of the given_matrix. Time Complexity O(n2)  where n is the maximum of r1 and c1.  Here we simply run two loops first loop run r1 times and the second loop runs c1 times.  And inside the second loop, we just change the location of the current element in the auxiliary matrix. Space Complexity O(m2)  where m is the maximum of r1 and c1.  Here we create extra space for storing the transpose of the given_matrix.  Here we also declared r1*c1 size for taking input from the given matrix.
  • 21. Performance analysis of an algorithm .. Example:2 How to find the time complexity of an algorithm Given the following algorithm: public Integer sum EvenNumbers(Integer N) { int sum = 0; for (int number = 1; number <= N; number++) if ((number % 2) == 0) sum = sum + number; return sum; } First, we split the code into individual operations and then compute how many times it is being executed as is shown in the following table Now, we need to sum how many times each operation is executing. Time complexity = 1 + 1 + N + N + N + N + 1 => 4N + 3 .
  • 22. Asymptotic Notation Asymptotic notations are the mathematical notations used to describe the run time complexity of an algorithm Types of asymptotic notations: 1. Big-O notation 2. Omega notation 3. Theta notation 4. Little oh notation
  • 23. Asymptotic Notation… Big-O Notation (O-notation)  Big O Notation is a mathematical notation that helps us to analyze how complex an algorithm is in terms of time and space.  Big-O notation represents the upper bound of the running time of an algorithm.  Thus, it gives the worst-case complexity of an algorithm O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }
  • 24. Asymptotic Notation… Omega Notation (Ω-notation) Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides the best case complexity of an algorithm. Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 } The above expression can be described as a function f(n) belongs to the set Ω(g(n)) if there exists a positive constant c such that it lies above cg(n), for sufficiently large n. For any value of n, the minimum time required by the algorithm is given by Omega Ω(g(n)).
  • 25. Asymptotic Notation… Theta Notation (Θ-notation) Theta notation encloses the function from above and below. Since it represents the upper and the lower bound of the running time of an algorithm, it is used for analyzing the average-case complexity of an algorithm. Theta bounds the function within constants factors For a function g(n), Θ(g(n)) is given by the relation: Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }
  • 26. Asymptotic Notation… Thus, little o() means loose upper-bound of f(n). Little o is a rough estimate of the maximum order of growth whereas Big-Ο may be the actual order of growth Θ(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f (n) <cg(n) for all n ≥ n0 }
  • 27. Properties Asymptotic Notation… As we have gone through the definition of these three notations let’s now discuss some important properties of those notations. 1. General Properties: If f(n) is O(g(n)) then a*f(n) is also O(g(n)) ; where a is a constant. Example: f(n) = 2n²+5 is O(n²) then 7*f(n) = 7(2n²+5) = 14n²+35 is also O(n²) . Similarly, this property satisfies both Θ and Ω notation. We can say If f(n) is Θ(g(n)) then a*f(n) is also Θ(g(n)) ; where a is a constant. If f(n) is Ω (g(n)) then a*f(n) is also Ω (g(n)) ; where a is a constant.
  • 28. Properties Asymptotic Notation… 2. Transitive Properties: If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) = O(h(n)) . Example: if f(n) = n, g(n) = n² and h(n)=n³ n is O(n²) and n² is O(n³) then n is O(n³) Similarly, this property satisfies both Θ and Ω notation. We can say If f(n) is Θ(g(n)) and g(n) is Θ(h(n)) then f(n) = Θ(h(n)) . If f(n) is Ω (g(n)) and g(n) is Ω (h(n)) then f(n) = Ω (h(n))
  • 29. Properties Asymptotic Notation… 3. Reflexive Properties: Reflexive properties are always easy to understand after transitive. If f(n) is given then f(n) is O(f(n)). Since MAXIMUM VALUE OF f(n) will be f(n) ITSELF ! Hence x = f(n) and y = O(f(n) tie themselves in reflexive relation always. Example: f(n) = n² ; O(n²) i.e O(f(n)) Similarly, this property satisfies both Θ and Ω notation. We can say that: If f(n) is given then f(n) is Θ(f(n)). If f(n) is given then f(n) is Ω (f(n)).
  • 30. Properties Asymptotic Notation… 4. Symmetric Properties : If f(n) is Θ(g(n)) then g(n) is Θ(f(n)) . Example: f(n) = n² and g(n) = n² then f(n) = Θ(n²) and g(n) = Θ(n²) This property only satisfies for Θ notation. 5. Transpose Symmetric Properties : If f(n) is O(g(n)) then g(n) is Ω (f(n)). Example: f(n) = n , g(n) = n² then n is O(n²) and n² is Ω (n) This property only satisfies O and Ω notations.
  • 31. Probabilistic Analysis In general, probabilistic analysis is used to analyze the running time of an algorithm. In simple words, probabilistic analysis is the use of probability in the analysis of problems. Sometimes, we can also use it to analyze quantities. One such example is the hiring cost in procedure Hire-Assistant. Hiring Problem: For example, you are using an employment agency to hire a new assistant for your company. The agency works in such a way that it sends you one candidate per day. You then interview the candidate after which you must immediately decide whether or not to hire that person. Also, if you hire that person, you must fire your current office assistant. Even if it is someone you have hired recently you must fire them before you hire a new assistant. For this, let us consider the cost to interview as ci per candidate and the cost to hire the candidate is ch. Also, there are a few requirements to be fulfilled which are:  At all times, you want to have the best candidate you have seen so far.  Whenever you interview a candidate and feel that the candidate is better than your current assistant, you have to fire the current assistant and then hire the candidate.
  • 32. Probabilistic Analysis.. Let us have a look at the pseudo code to the model. Hire-Assistant (n) best<- 0 ;Candidate 0 is a least qualified candidate fori<- 1 to n do interview candidate i if candidate i is better than candidate best then best <- i hire candidate i Here are a few points to be considered for the Cost Model:  We are not concerned about the running time of the Hire-Assistant.  We must determine the total cost of hiring the best candidate.  If n candidates are interviewed and m candidates are hired, then the cost would be nci+mch  We have to pay the cost: nci to interview the candidates’irrespective oof how many candidates we hire.  So, focus on analyzing the hiring cost mch  m varies with the order of the candidates.
  • 33. Probabilistic Analysis.. Best-Case Analysis for Hiring Problem  We get the best case when the agency sends us the best applicant on the first day.  The cost would be nci+ch Worst-case Analysis for Hiring problem  The worst case is when we hire all the n candidates.  This happens only if each candidate that comes later is better than all those who came before. That is when candidates come in increasing order of quality.  The cost would be nci+nch  Whenever this happens, we will fire the agency.
  • 34. Probabilistic Analysis.. Average-case Analysis for Hiring problem  For the average case, an input to the hiring problem is an ordering of the n applicants, there are n! different inputs.  To use probabilistic analysis, we assume that the candidates arrive in random order.  Then we analyze our algorithm by computing an expected running time.  This expectation is taken over the distribution of all the possible inputs.  Thus, we are averaging the running time over all possible inputs.  Now, we want to know the expected cost of our hiring algorithm in terms of the number of times we hired an applicant.  For this, Random variable X(s) is the number of applicants who are hired for a given input sequence s.  Indicator random variable Xi for applicant i will be 1 if applicant i is hired, 0 otherwise.
  • 35. Probabilistic Analysis.. What is Indicator Random Variable? It is a powerful technique that is used for computing the expected value of a random variable. Also, it is a convenient method for converting between probabilities and also expectations. Indicator Random Variable takes only 2 values, which are 1 and 0. Indicator Random Variable XA=I{A} for an event A of a sample space is defined as: I{A} = 1 if A occurs 0 if A does not occur The expected value of an indicator Random Variable is associated with an event A is equal to the probability that A occurs. Indicator RV – Example Problem: Determine the expected number of heads in one toss. Sample space is s{H, T} Indicator random variable XA= I{ coin coming up with heads}=1/2 The expected number of heads obtained in one flip of the coin is equal to the expected value of the indicator random variable. Therefore, E[XA] = 1/2 This is all about probabilistic analysis, Hiring problem, and the Indicator random variable. Do check the next article on Disjoint sets.
  • 36. Amortized Analysis This analysis is used when the occasional operation is very slow, but most of the operations which are executing very frequently are faster. Data structures we need amortized analysis for Hash Tables, Disjoint Sets etc. In the Hash-table, the most of the time the searching time complexity is O(1), but sometimes it executes O(n) operations. When we want to search or insert an element in a hash table for most of the cases it is constant time taking the task, but when a collision occurs, it needs O(n) times operations for collision resolution. Aggregate Method The aggregate method is used to find the total cost. If we want to add a bunch of data, then we need to find the amortized cost by this formula. For a sequence of n operations, the cost i
  • 37. Amortized Analysis.. Let us consider an example of a simple hash table insertion. How do we decide table size? There is a trade- off between space and time, if we make hash-table size big, search time becomes low, but space required becomes high.
  • 38. Amortized Analysis.. For the dynamic array, let = cost of ith insertion. The solution to this trade-off problem is to use Dynamic Table (or Arrays). The idea is to increase size of table whenever it becomes full. Following are the steps to follow when table becomes full. 1) Allocate memory for a larger table of size, typically twice the old table. 2) Copy the contents of old table to new table. 3) Free the old table. If the table has space available, we simply insert new item in available space. What is the time complexity of n insertions using the above scheme? If we use simple analysis, the worst case cost of an insertion is O(n). Therefore, worst case cost of n inserts is n * O(n) which is O(n2). This analysis gives an upper bound, but not a tight upper bound for n insertions as all insertions don’t take Θ(n) time.