SlideShare uma empresa Scribd logo
1 de 18
Unit - II
Algorithm
1
Prepared By:
Dabbal Singh Mahara
Unit –II: Table of Contents
• Concept and definition
• Characteristic of algorithm
• Design of algorithm
• Big O notation
2
An algorithm, named after the ninth century scholar Abu Jafar
Muhammad Ibn Musu Al-Khowarizmi, is defined as follows:
• An algorithm is a finite step-by-step procedure to achieve a
required result.
• An algorithm is a sequence of computational steps that
transform the input into the output.
• An algorithm is a precise specification of a sequence of instructions
to be carried out in order to solve a given problem. Each instruction
tells what task is to be done. There should be a finite number of
instructions in an algorithm and each instruction should be executed in
a finite amount of time.
Algorithm
3
• Input:
A number of quantities are provided to an algorithm initially
before the algorithm begins. These quantities are inputs which are
processed by the algorithm.
• Definiteness:
Each step must be clear and unambiguous that leads to a specific action.
• Effectiveness:
Each step must be carried out in finite time.
• Finiteness:
Algorithm must terminate after finite time or steps
• Output:
An algorithm must have one or more output.
• Correctness:
Correct set of output values must be produced from
the each set of inputs. For the same input data, it must
always produce the same output.
Characteristics of Algorithms
4
• An algorithm can be written in a number of ways such
as natural language, pseudo-code, or flowcharts.
Pseudo-code is a popular way to express algorithm.
• Pseudo-code is a way of expressing algorithms that
uses a mixture of English phrases and indention to make
the steps in the solution explicit
• Pseudo-code cannot be compiled nor executed, and
there are no real formatting or syntax rules.
• The benefit of pseudo-code is that it enables the
programmer to concentrate on the algorithms without
worrying about all the syntactic details of a particular
programming language and easy to convert it into
programs in any language.
• There is not any standard rules for writing algorithm.
Writing Algorithm
5
Example:
Write an algorithm to find the largest among three
different numbers entered by user.
Step 1: Start
Step 2: Declare variables a, b and c.
Step 3: Read variables a, b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
6
1. The “design” pertain to
i. The description of algorithm at an abstract level by means of a
pseudo language, and
ii. Proof of correctness that is, the algorithm solves the given
problem in all cases.
2. The “analysis” deals with performance evaluation or complexity
analysis. The complexity of an algorithm is a function describing
the efficiency of the algorithm in terms of the amount of data
the algorithm must process. That is, algorithmic complexity is a
function of problem size that refers to the rate at which required
storage or consumed time grows as input size increases. There
are two main complexity measures of the efficiency of an
algorithm: Time Complexity and Space complexity. Analysis of
algorithm in terms of efficiency allows programmers to compare
the algorithms for selecting appropriate one.
Design and Analysis of Algorithm
7
Time Complexity
8
• Time complexity is a function describing the amount of time an algorithm takes in terms
of the amount of input to the algorithm.
• That is, it is a function that refers the rate at which consumed time increases as input
size increases.
• To simplify the analysis, the time for the operations that is constant independent of
input size is ignored. Simplified analysis can be based on:
• Number of arithmetic operation performed
• Number of comparisons made
• Number of times through a loop
• Number of array elements are accessed etc.
• Although the running time of an implementation of the algorithm would depend upon
the speed of the computer, programming language, compiler etc, these technical issues
are ignored. Only how running time is increasing with increasing input size is considered
in analysis.
• When analyzing algorithm it depends upon the input data, there are three cases:
• Best case:
• Average case:
• Worst case:
• best, worst, and average cases of a given algorithm express what the resource
usage is at least, at most and on average, respectively. Usually the resource being
considered is running time, i.e. time complexity, but it could also be memory or
other resources.
9
#include <stdio.h>
// Linearly search x in arr[].
// If x is present then return the index,
// otherwise return -1
int search(int arr[], int n, int x)
{
int i;
for (i=0; i<n; i++)
{
if (arr[i] == x)
return i;
}
return -1;
}
/* Driver program to test above functions*/
int main()
{
int arr[] = {1, 10, 30, 15};
int x = 30;
int n = sizeof(arr)/sizeof(arr[0]);
printf("%d is present at index %d", x, search(arr, n, x));
getchar();
return 0;
}
Let us consider the following implementation of Linear Search.
For Linear Search
• The worst case happens when the element to be searched
(x in the above code) is not present in the array. When x is
not present, the search() functions compares it with all
the elements of arr[] one by one.
• Let us assume that all cases are uniformly distributed
(including the case of x not being present in array). So we
sum all the cases and divide the sum by (n+1).
• The best case occurs when x is present at the first
location. The number of operations in the best case is
constant (not dependent on n).
Space Complexity
10
Analysis of space complexity of an algorithm is the amount of memory it
needs to run to completion. It is the rate at which required storage space
grows as a function of input size.
The space needed by a program consists of following components:
• Instruction Space: space for code
• Data Space: space for variables, constants
• Environment stack space: return address, local variables in
called function.Example:
Algorithm Sum(a,n)
{
s:=0.0;
for i:=1 to n do
s:=s+a[i];
return s;
}
The space needed by n is one word, since it is of type integer. The space
needed by a is the space needed by variables of type array of floating point
numbers. This is at least n words, since a must be large enough to hold the n
elements to be summed. So, Ssum(n) >= (n+3), n for a[], 1 for each n, I and s).
Asymptotic Analysis
11
Big Oh Notation
• Big Oh is a characteristic scheme that measures properties of algorithm complexity
performance and/or memory requirements by eliminating constant factors.
• Big Oh notation is the formal method of expressing the upper bound of an algorithm's
running time. It's a measure of the longest amount of time it could possibly take for the
algorithm to complete.
• With big O notation we express the runtime in terms of—how quickly it grows relative
to the input, as the input gets arbitrarily large.
• It's how we compare the efficiency of different algorithms to a problem.
• Properties of Big O:
− Measuring the amount of work as it varies with the size of the data affected
− Predicting the expected overall performance of an algorithm
− Generally measured relative to other algorithms to handle the same problem
• Big O has following limitations:
− It ignores potential of constant factors in the algorithm.
− It does not try to improve algorithm, only gives the complexity.
• The exact time of an algorithm will depend on the implementation of algorithm,
amount input data, programming language, CPU speed etc.
• There is a proportionality approach to measure the time complexity in terms of
relationship between input size and number of key operations of computational
process. This type of analysis is known as asymptotic analysis.
• For asymptotic analysis we use different notations:
12
Big Oh (O) notation
When we have only asymptotic upper bound then we use O notation.
Formal definition
Let f and g be any two functions defined over set of positive integers. A
function f(n) is big oh of g(n) ), denoted as f(n)=O(g(n)), if there exists two
positive constants c and n0 such that for all n >= n0, f(n) <= c*g(n).
• O(1) is used to denote constants.
• Example:
f(n)=5n3+3n2+4 and g(n) =12n3 Then, is f(n) = O(n3)?
n F(n) g(n)
1 12 12 True
2 56 96 True
3 166 324 True
4 372 768 True
Therefore, for all n >=n0=1, c = 12, f(n) <= c*g(n) => f(n) = O(g(n)).
• Example: 2 If f(x)= 5x2+4x + 2 find big oh(O) of f(x).
Here, f(x)= 5x2+4x + 3 <= 5x2+4x2+ 2x2 = 11x2 , for all x>=1
f(x) <= c * g(x), where c =11, g(x) = x2 and x0 = 1.
Therefore, f(x) = O(x2 ).
13
Big Omega ( Ω ) notation
Big omega notation gives asymptotic lower bound.
Formal definition
Let f and g be any two functions defined over set of positive integers. A function f(n)
is big Omega of g(n) ), denoted as f(n)= Ω (g(n)), if there exists two positive
constants c and n0 such that for all n >= n0, f(n) >= c*g(n).
The above relation says that g(x) is a lower bound of f(x).
Example:
If f(n)= 3n2 +4n + 7, find big omega of f(n).
Here,
f(n)= 3n2 +4n + 7 >= 3n2 for all n>= 1.
i.e. f(n) >= c * g(n), where c = 3, n0=1
and g(n) =n2 .
Therefore, f(n) = Ω (g(n).
14
Big Theta ( Θ ) notation
When we need asymptotically tight bound then we use notation.
Formal definition
Let f and g be any two functions defined over set of positive integers. A function
f(n) is big Theta of g(n) ), denoted as f(n)= Θ (g(n)), if there exists three positive
constants c1, c2 and n0 such that for all n >= n0, c1*g(n) <= f(n) <= c2*g(n).
The above relation says that f(x) is order of g(x)
Example: f(n) = 3n2 + 4n + 7 g(n) = n2 , then prove
that f(n) = (g(n)).
Proof:
let us choose c1, c2 and n0 values as 14, 1 and 1
respectively then we can have,
f(n) <= c1*g(n), n>=n0 as 3n2 + 4n + 7 <= 14*n2 ,
and
f(n) >= c2*g(n), n>=n0 as 3n2 + 4n + 7 >= 1*n2
for all n >= 1(in both cases).
So c2*g(n) <= f(n) <= c1*g(n) is trivial.
Hence f(n) = Θ(g(n)).
15
16
17
Thank You !
18

Mais conteúdo relacionado

Mais procurados

Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsMohamed Loey
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2Kumar
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computingjayavignesh86
 
Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final Dgech
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and AlgorithmDhaval Kaneria
 
Algorithm Analyzing
Algorithm AnalyzingAlgorithm Analyzing
Algorithm AnalyzingHaluan Irsad
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Muhammad Hammad Waseem
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteDesign and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteZeal Education Society, Pune
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithmDevaKumari Vijay
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithmPratik Mota
 

Mais procurados (20)

chapter 1
chapter 1chapter 1
chapter 1
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computing
 
Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final
 
Introduction to data structures and Algorithm
Introduction to data structures and AlgorithmIntroduction to data structures and Algorithm
Introduction to data structures and Algorithm
 
Algorithm Analyzing
Algorithm AnalyzingAlgorithm Analyzing
Algorithm Analyzing
 
01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]
 
Design and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. MohiteDesign and analysis of Algorithm By Dr. B. J. Mohite
Design and analysis of Algorithm By Dr. B. J. Mohite
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Introduction to design and analysis of algorithm
Introduction to design and analysis of algorithmIntroduction to design and analysis of algorithm
Introduction to design and analysis of algorithm
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithm
 

Semelhante a Unit ii algorithm

DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptxNishaS88
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity CalculationAkhil Kaushik
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notationirdginfo
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...AntareepMajumder
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxskilljiolms
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingMvenkatarao
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 

Semelhante a Unit ii algorithm (20)

Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Algorithms & Complexity Calculation
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
 
DSA
DSADSA
DSA
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...Algorithm Class at KPHB  (C, C++ Course Training Institute in KPHB, Kukatpall...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpall...
 
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
Algorithm Class at KPHB (C, C++ Course Training Institute in KPHB, Kukatpally...
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 

Último

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
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call 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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
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
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
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
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
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
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Último (20)

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...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(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...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
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...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
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 )
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
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...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Unit ii algorithm

  • 1. Unit - II Algorithm 1 Prepared By: Dabbal Singh Mahara
  • 2. Unit –II: Table of Contents • Concept and definition • Characteristic of algorithm • Design of algorithm • Big O notation 2
  • 3. An algorithm, named after the ninth century scholar Abu Jafar Muhammad Ibn Musu Al-Khowarizmi, is defined as follows: • An algorithm is a finite step-by-step procedure to achieve a required result. • An algorithm is a sequence of computational steps that transform the input into the output. • An algorithm is a precise specification of a sequence of instructions to be carried out in order to solve a given problem. Each instruction tells what task is to be done. There should be a finite number of instructions in an algorithm and each instruction should be executed in a finite amount of time. Algorithm 3
  • 4. • Input: A number of quantities are provided to an algorithm initially before the algorithm begins. These quantities are inputs which are processed by the algorithm. • Definiteness: Each step must be clear and unambiguous that leads to a specific action. • Effectiveness: Each step must be carried out in finite time. • Finiteness: Algorithm must terminate after finite time or steps • Output: An algorithm must have one or more output. • Correctness: Correct set of output values must be produced from the each set of inputs. For the same input data, it must always produce the same output. Characteristics of Algorithms 4
  • 5. • An algorithm can be written in a number of ways such as natural language, pseudo-code, or flowcharts. Pseudo-code is a popular way to express algorithm. • Pseudo-code is a way of expressing algorithms that uses a mixture of English phrases and indention to make the steps in the solution explicit • Pseudo-code cannot be compiled nor executed, and there are no real formatting or syntax rules. • The benefit of pseudo-code is that it enables the programmer to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language and easy to convert it into programs in any language. • There is not any standard rules for writing algorithm. Writing Algorithm 5
  • 6. Example: Write an algorithm to find the largest among three different numbers entered by user. Step 1: Start Step 2: Declare variables a, b and c. Step 3: Read variables a, b and c. Step 4: If a>b If a>c Display a is the largest number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the greatest number. Step 5: Stop 6
  • 7. 1. The “design” pertain to i. The description of algorithm at an abstract level by means of a pseudo language, and ii. Proof of correctness that is, the algorithm solves the given problem in all cases. 2. The “analysis” deals with performance evaluation or complexity analysis. The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. That is, algorithmic complexity is a function of problem size that refers to the rate at which required storage or consumed time grows as input size increases. There are two main complexity measures of the efficiency of an algorithm: Time Complexity and Space complexity. Analysis of algorithm in terms of efficiency allows programmers to compare the algorithms for selecting appropriate one. Design and Analysis of Algorithm 7
  • 8. Time Complexity 8 • Time complexity is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm. • That is, it is a function that refers the rate at which consumed time increases as input size increases. • To simplify the analysis, the time for the operations that is constant independent of input size is ignored. Simplified analysis can be based on: • Number of arithmetic operation performed • Number of comparisons made • Number of times through a loop • Number of array elements are accessed etc. • Although the running time of an implementation of the algorithm would depend upon the speed of the computer, programming language, compiler etc, these technical issues are ignored. Only how running time is increasing with increasing input size is considered in analysis. • When analyzing algorithm it depends upon the input data, there are three cases: • Best case: • Average case: • Worst case: • best, worst, and average cases of a given algorithm express what the resource usage is at least, at most and on average, respectively. Usually the resource being considered is running time, i.e. time complexity, but it could also be memory or other resources.
  • 9. 9 #include <stdio.h> // Linearly search x in arr[]. // If x is present then return the index, // otherwise return -1 int search(int arr[], int n, int x) { int i; for (i=0; i<n; i++) { if (arr[i] == x) return i; } return -1; } /* Driver program to test above functions*/ int main() { int arr[] = {1, 10, 30, 15}; int x = 30; int n = sizeof(arr)/sizeof(arr[0]); printf("%d is present at index %d", x, search(arr, n, x)); getchar(); return 0; } Let us consider the following implementation of Linear Search. For Linear Search • The worst case happens when the element to be searched (x in the above code) is not present in the array. When x is not present, the search() functions compares it with all the elements of arr[] one by one. • Let us assume that all cases are uniformly distributed (including the case of x not being present in array). So we sum all the cases and divide the sum by (n+1). • The best case occurs when x is present at the first location. The number of operations in the best case is constant (not dependent on n).
  • 10. Space Complexity 10 Analysis of space complexity of an algorithm is the amount of memory it needs to run to completion. It is the rate at which required storage space grows as a function of input size. The space needed by a program consists of following components: • Instruction Space: space for code • Data Space: space for variables, constants • Environment stack space: return address, local variables in called function.Example: Algorithm Sum(a,n) { s:=0.0; for i:=1 to n do s:=s+a[i]; return s; } The space needed by n is one word, since it is of type integer. The space needed by a is the space needed by variables of type array of floating point numbers. This is at least n words, since a must be large enough to hold the n elements to be summed. So, Ssum(n) >= (n+3), n for a[], 1 for each n, I and s).
  • 11. Asymptotic Analysis 11 Big Oh Notation • Big Oh is a characteristic scheme that measures properties of algorithm complexity performance and/or memory requirements by eliminating constant factors. • Big Oh notation is the formal method of expressing the upper bound of an algorithm's running time. It's a measure of the longest amount of time it could possibly take for the algorithm to complete. • With big O notation we express the runtime in terms of—how quickly it grows relative to the input, as the input gets arbitrarily large. • It's how we compare the efficiency of different algorithms to a problem. • Properties of Big O: − Measuring the amount of work as it varies with the size of the data affected − Predicting the expected overall performance of an algorithm − Generally measured relative to other algorithms to handle the same problem • Big O has following limitations: − It ignores potential of constant factors in the algorithm. − It does not try to improve algorithm, only gives the complexity. • The exact time of an algorithm will depend on the implementation of algorithm, amount input data, programming language, CPU speed etc. • There is a proportionality approach to measure the time complexity in terms of relationship between input size and number of key operations of computational process. This type of analysis is known as asymptotic analysis. • For asymptotic analysis we use different notations:
  • 12. 12 Big Oh (O) notation When we have only asymptotic upper bound then we use O notation. Formal definition Let f and g be any two functions defined over set of positive integers. A function f(n) is big oh of g(n) ), denoted as f(n)=O(g(n)), if there exists two positive constants c and n0 such that for all n >= n0, f(n) <= c*g(n). • O(1) is used to denote constants. • Example: f(n)=5n3+3n2+4 and g(n) =12n3 Then, is f(n) = O(n3)? n F(n) g(n) 1 12 12 True 2 56 96 True 3 166 324 True 4 372 768 True Therefore, for all n >=n0=1, c = 12, f(n) <= c*g(n) => f(n) = O(g(n)). • Example: 2 If f(x)= 5x2+4x + 2 find big oh(O) of f(x). Here, f(x)= 5x2+4x + 3 <= 5x2+4x2+ 2x2 = 11x2 , for all x>=1 f(x) <= c * g(x), where c =11, g(x) = x2 and x0 = 1. Therefore, f(x) = O(x2 ).
  • 13. 13 Big Omega ( Ω ) notation Big omega notation gives asymptotic lower bound. Formal definition Let f and g be any two functions defined over set of positive integers. A function f(n) is big Omega of g(n) ), denoted as f(n)= Ω (g(n)), if there exists two positive constants c and n0 such that for all n >= n0, f(n) >= c*g(n). The above relation says that g(x) is a lower bound of f(x). Example: If f(n)= 3n2 +4n + 7, find big omega of f(n). Here, f(n)= 3n2 +4n + 7 >= 3n2 for all n>= 1. i.e. f(n) >= c * g(n), where c = 3, n0=1 and g(n) =n2 . Therefore, f(n) = Ω (g(n).
  • 14. 14 Big Theta ( Θ ) notation When we need asymptotically tight bound then we use notation. Formal definition Let f and g be any two functions defined over set of positive integers. A function f(n) is big Theta of g(n) ), denoted as f(n)= Θ (g(n)), if there exists three positive constants c1, c2 and n0 such that for all n >= n0, c1*g(n) <= f(n) <= c2*g(n). The above relation says that f(x) is order of g(x) Example: f(n) = 3n2 + 4n + 7 g(n) = n2 , then prove that f(n) = (g(n)). Proof: let us choose c1, c2 and n0 values as 14, 1 and 1 respectively then we can have, f(n) <= c1*g(n), n>=n0 as 3n2 + 4n + 7 <= 14*n2 , and f(n) >= c2*g(n), n>=n0 as 3n2 + 4n + 7 >= 1*n2 for all n >= 1(in both cases). So c2*g(n) <= f(n) <= c1*g(n) is trivial. Hence f(n) = Θ(g(n)).
  • 15. 15
  • 16. 16
  • 17. 17