0% found this document useful (0 votes)
6 views13 pages

Algorithm

The document provides a comprehensive overview of algorithms, defining them as finite sets of precise instructions for computations or problem-solving, and discusses their characteristics such as input, output, definiteness, correctness, effectiveness, and generality. It also covers the complexity of algorithms, focusing on time and space complexity, and introduces Big O notation for analyzing algorithm efficiency. Additionally, it categorizes algorithms based on their time complexity and explains the concepts of worst, best, and average case complexities.

Uploaded by

dipeshcrestra7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views13 pages

Algorithm

The document provides a comprehensive overview of algorithms, defining them as finite sets of precise instructions for computations or problem-solving, and discusses their characteristics such as input, output, definiteness, correctness, effectiveness, and generality. It also covers the complexity of algorithms, focusing on time and space complexity, and introduces Big O notation for analyzing algorithm efficiency. Additionally, it categorizes algorithms based on their time complexity and explains the concepts of worst, best, and average case complexities.

Uploaded by

dipeshcrestra7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

a.

Concept and Definition


b. Design of algorithm
c. Characteristic of algorithm
d. Big O notation

1
Concept and definition
 “An algorithm is a finite set of precise instructions
executed in finite time for performing a computation or
for solving a problem.”
 To represent an algorithm we use pseudocode.
 Pseudocode represents an algorithm in a clear
understandable manner as like English language and
gives the implementation view as like programming
language.

2
Example: write an algorithm for
finding the factorial of a given number.
fact(n)
{
fact=1;
for(i=1;i<=n; i++)
fact=fact*i;
return fact;
}
This algorithm is for getting factorial of n. The
algorithm first assigns value 1 to the variable fact and
then until n is reached, fact is assigned a value fact*i
where value of i is from 1 to n. At last the value fact is
returned. The pseudocode used here is loosely based on
the programming language C.
3
Model question (2008)
 What are the major characteristics of algorithms?

4
Characteristics (properties) of algorithm
 Input: An algorithm has input values from a specified set.
 Output: From each set of input values, an algorithm produces
output values from a specified set. The output values are the
solution to the problem.
 Definiteness: The steps of an algorithm must be defined
precisely.
 Correctness: An algorithm should produce the desired output
after a finite (but perhaps large) number of steps for any input
in the set.
 Effectiveness: It must be possible to perform each step of an
algorithm exactly and in a finite amount of time.
 Generality: The devised algorithm should be capable of
solving the problem of similar kind for all possible inputs.
5
 The above algorithm for factorial satisfies all the properties
of algorithm as follows:
 The input for the algorithm is any positive integer and output is
the factorial of the given number.
 Each step is clear since assignments, finite loop, the arithmetic
operations and jump statements are defined precisely.
 Each input gives its corresponding factorial value after a finite
number of steps → Correctness.
 When the return statement is reached, the algorithm
terminates and each step in the algorithm terminates in finite
time. All other steps, other than the loop are simple and require
no explanation. In case of loop, when the value of i reaches n+1,
then the loop terminates.
 The algorithm is general since it can work out for every positive
integer.
6
Complexity of algorithms
 When an algorithm is designed, it must be analyzed
for its efficiency i.e. complexity.
 The complexity of an algorithm is defined in terms of
computational resource needed by the algorithm.
 There are two kinds of computational resources
used by an algorithm: CPU’s processing power (TIME
COMPLEXITY) and computer’s primary memory
(SPACE COMPLEXITY).

7
Complexity of algorithms…
 The measure of time required by an algorithm to
run is given by time complexity and the measure
of space (computer memory) required by an
algorithm is given by space complexity.
 We focus on time complexity.
 Since actual time required may vary from computers
to computers (e.g. on a supercomputer it may be
million times faster than on a PC), we have to use
the number of steps required by the algorithm for a
given set of inputs to measure the time
complexity.

8
 Example (Time complexity):
➢ We can find the time complexity of the above
algorithm by enumerating the number of steps
required for the algorithm to obtain the factorial of n.
➢ We can clearly see that the assignment of the variable
fact is done once, the for loop executes n+1 times, the
statement inside the for loop executes n times and the
last operation that is the return statement executes
single time.
➢ Thus the total time in terms of number of steps
required for finding the factorial of n can be written
as:
f(n) = 1 + (n+1) + n + 1 = 2n+3.

9
Big-Oh (O) notation
 The complexity of an algorithm is analyzed in terms of a
mathematical function of the size of the input.
 The complexity analysis (TIME) of an algorithm is very
hard if we try to analyze exact. So we need the concept of
asymptotic notations.
 Big-O notation is one of the asymptotic notation used for
complexity analysis of an algorithm.
 Definition: Let f and g be functions from the set of integers
or the set of real numbers to the set of real numbers. We
say that f(n) is O(g(n)) (read as f(n) is big-oh of g(n)) if
there are constants C and k such that
|f(n)| ≤ C*|g(n)|
whenever n > k.
10
 For example: In the above algorithm
f(n) = 2n+3 ≤ 6.n
so that we can infer that f(n) = O(g(n)) with
g(n) = n, C=6 and n > 1.
Hence the time complexity of the factorial
algorithm is O(n) i.e. linear complexity.

11
Categories of algorithms
 Based on Big-O notation, the algorithms can be
categorized as follows:
 Constant time algorithms → O(1)
 Logarithmic time algorithms → O(log n)
 Linear time algorithms → O(n)
 Linearithmetic time algorithms → O(n log n)
 Polynomial time algorithms → O(nk) for k>1
 Exponential time algorithms → O(kn) for k>1

12
Worst, Best and Average Case Complexity
 The worst-case complexity of an algorithm is defined as the
maximum number of steps taken on any instance of size n.
 The best-case complexity of an algorithm is defined as the
minimum number of steps taken on any instance of size n.
 The average-case complexity of an algorithm is defined as
the average number of steps taken on any random instance
of size n.

 Note: In case of running time, worst-case time complexity indicates the


longest running time performed by an algorithm for any input of size n,
and thus this guarantees that the algorithm finishes on time. Hence,
algorithms are analyzed for their efficiency mostly in terms of its worst-
case complexity.

13

You might also like