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

introductiontoalgorithms-201019152415

Uploaded by

any.chowdhury
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

introductiontoalgorithms-201019152415

Uploaded by

any.chowdhury
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

P.

Prathibha
Lecturer
Topics
Introduction to Algorithms
Characteristics of an Algorithm
Analysis of Algorithms
Asymptotic Analysis
Introduction to Algorithms
What is Algorithm ?
 An algorithm is a finite set of instructions or logic, written in order, to accomplish a
certain predefined task.
 Algorithm is not the complete code or program, it is just the core logic(solution) of a
problem, which can be expressed either as an informal high level description
as pseudocode or using a flowchart.
 Algorithm is a step-by-step procedure, which defines a set of instructions to be executed
in a certain order to get the desired output.
 Algorithms are generally created independent of underlying languages, i.e. an algorithm
can be implemented in more than one programming language.
Characteristics of an Algorithm
An algorithm should have the following characteristics −

 Unambiguous − Algorithm should be clear and unambiguous. Each of its steps


(or phases), and their inputs/outputs should be clear and must lead to only one
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.
Characteristics of a good and correct Alg
orithm

 Has a set of inputs

 Steps are uniquely defined

 Has finite number of steps

 Produces desired output


Example of a real-life situation for creating algorithm
Algorithm is for going to the market to purchase a pen.
Algorithm to add 3 numbers and print their sum:

1. START
2. Declare 3 integer variables num1, num2 and num3.
3. Take the three numbers, to be added, as inputs in
variables num1, num2, and num3 respectively.
4. Declare an integer variable sum to store the resultant
sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable
sum.
6. Print the value of variable sum
7. END
Algorithm Analysis
• Efficiency of an algorithm can be analyzed at two different stages,
before implementation and after implementation. They are −
1. A Priori Analysis :
 “Priori” means “before”.
 Hence Priori analysis means checking the algorithm before its
implementation.
 In this, the algorithm is checked when it is written in the form of
theoretical steps. i.e., This is a theoretical analysis of an algorithm.
 Efficiency of an algorithm is measured by assuming that all other
factors,
 for example, processor speed, are constant and have no effect on the
implementation.
(contd..)

2. A Posterior Analysis :

 “Posterior” means “after”. Hence Posterior analysis means c


hecking the algorithm after its implementation.
 In this, the algorithm is checked by implementing it in any
programming language and executing it.i.e., This is an
empirical analysis of an algorithm.
 This analysis helps to get the actual and real analysis report
about correctness, space required, time consumed etc.
 A measure of the average execution time necessary for an
algorithm to complete work on a set of data.
 Algorithm efficiency is characterized by its order.
 The complexity of an algorithm is a function describing the e
fficiency of the algorithm in terms of the amount of data the al
gorithm must process.
 Usually there are natural units for the domain and range of this
function.
 There are two main measures of the efficiency of an algorithm.
1. Time Complexity
2. Space Complexity
Time Complexity
 Time complexity of an algorithm represents the amount
of time required by the algorithm to run to completion.
 The time complexity of an algorithm is also calculated
by determining following 2 components:
 Constant time part: Any instruction that is executed just o
nce comes in this part. For example, input, output, if-else,
switch, etc.
 Variable Time Part: Any instruction that is executed more t
han once, say n times, comes in this part. F
or example, loops, recursion, etc.
 Time requirements can be defined as a numerical functio
n T(n), where T(n) can be measured as the number of ste
ps, provided each step consumes constant time.
For example, addition of two n-bit integers takes n steps.
Consequently, the total computational time is T(n) = c ∗ n, wh
ere c is the time taken for the addition of two bits. Here, we o
bserve that T(n) grows linearly as the input size increases.
Space Complexity
 Space complexity of an algorithm refers to the amount of memory that this algorithm requires to
execute and get the result.
 This can be for inputs, temporary operations, or outputs.
 The space complexity of an algorithm is calculated by determining following 2 components:
 Fixed Part: This refers to the space that is definitely required by the algorithm.
For example, input variables, output variables, program size, etc.
 Variable Part: This refers to the space that can be different based on the implementation
of the algorithm. For example, temporary variables, dynamic memory allocation, recursion
stack space, etc.
Algorithm Design Tools

The two popular tools used in the representation of algorithms a


re the following:
1. Pseudocode
2. Flowchart
Pseudocode
 An algorithm can be written in any of the natural languages such as English, German,
French, etc.
 One of the commonly used tools to define algorithms is the pseudocode.
 Pseudocode is one of the tools that can be used to write a preliminary plan that can be
developed into a computer program.
 A pseudocode is an English-like presentation of the code required for an algorithm.
 Pseudocode is a generic way of describing an algorithm without use of any specific p
rogramming language syntax.
 It is, as the name suggests, pseudo code —it cannot be executed on a real computer, but it
models and resembles real programming code, and is written at roughly the same level
of detail.
Flowchart
 A very effective tool to show the logic flow of a program is the flowchart.
 A flowchart is a pictorial representation of an algorithm.
 It hides all the details of an algorithm by giving a picture; it shows how the algorithm f
lows from beginning to end.
 A flowchart is a schematic representation of an algorithm or a stepwise process, showing
the steps as boxes of various kinds, and their order by connecting these with arrows.
 Flowcharts are used in designing or documenting a process or program.
 Flowcharts are usually drawn using some standard symbols; however, some special
symbols can also be developed when required.
Flowchart Symbols
Symbol Symbol Name Purpose
Start / Stop Used at the beginning and end of the algorithm to
Flowchart Symbols
show start and end of the program.
Process Indicates processes like mathematical operations.

Input/ Output Used for denoting program inputs and outputs.

Decision Stands for decision statements in a program,


where answer is usually Yes or No.
Arrow Shows relationships between different shapes.

On-page Connector Connects two or more parts of a flowchart, which


are on the same page.
Off-page Connector Connects two parts of a flowchart which are s
pread over different pages.
Flowchart to calculate the average of two numbers
Asymptotic Analysis
 Asymptotic analysis of an algorithm refers to defining the mathematical boundation/ framing
of its run-time performance.
 Using asymptotic analysis, we can very well conclude the best case, average case, and worst case
scenario of an algorithm.
 Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is concluded to w
ork in a constant time.
 Other than the "input" all other factors are considered constant.\
 Asymptotic analysis refers to computing the running time of any operation in mathematical
units of computation.
 The time required by an algorithm falls under three types −
 Best Case − Minimum time required for program execution.
 Average Case − Average time required for program execution.
 Worst Case − Maximum time required for program execution.
Asymptotic Notations

Commonly used asymptotic notations to calculate the running time complexity


of an algorithm.
1. Ο Notation
2. Ω Notation
3. θ Notation
Big Oh Notation, Ο

• The notation Ο(n) is the formal way to express the


upper bound of an algorithm's running time.
• It measures the worst case time complexity or the
longest amount of time an algorithm can possibly
take to complete.

For example, for a function f(n)


Ο(f(n)) = { g(n) : there exists c > 0 and n0
such that f(n) ≤ c.g(n) for all n > n0. }
Omega Notation, Ω
 The notation Ω(n) is the formal way to express the l
ower bound of an algorithm's running time.
 It measures the best case time complexity or the
best amount of time an algorithm can possibly take
to complete.

For example, for a function f(n)


Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that
g(n) ≤ c.f(n) for all n > n0. }
Theta Notation, θ
• The notation θ(n) is the formal way to express b
oth the lower bound and the upper bound of
an algorithm's running time.

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n


))
for all n > n0. }
Summary
 Introduction to Algorithms
 Characteristics of an Algorithm
 Algorithms Analysis
 Algorithms Efficiency
 Algorithms Design Tools
 Asymptotic Analysis

You might also like