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

UNIT - I Introduction

Uploaded by

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

UNIT - I Introduction

Uploaded by

apurva3296
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

PROBLEM SOLVING TECHNIQUES

UNIT - I
Introduction
UNIT – I: Introduction
❖ Introduction: The Role of Algorithms in Computing,
Algorithms as a technology, Analyzing algorithms,
Designing algorithms, Growth of Functions, Asymptotic
notation, Standard notations and common functions.
❖ Fundamental Algorithms: Exchanging the values of two
variables, Counting, Summation of a set of numbers,
Factorial Computation, Generating of the Fibonacci
sequence, Reversing the digits of an integer, Character to
number conversion.
UNIT – I: Introduction
❖ Introduction: The Role of Algorithms in Computing,
Algorithms as a technology, Analyzing algorithms,
Designing algorithms, Growth of Functions, Asymptotic
notation, Standard notations and common functions.
❖ Fundamental Algorithms: Exchanging the values of two
variables, Counting, Summation of a set of numbers,
Factorial Computation, Generating of the Fibonacci
sequence, Reversing the digits of an integer, Character to
number conversion.
What is an Algorithm?
❖ An algorithm is a process or a set of rules required to perform
calculations or some other problem-solving operations especially by
a computer.

❖ The formal definition of an algorithm is that it contains the finite


set of instructions which are being carried in a specific order to
perform the specific task.

❖ It is not the complete program or code; it is just a solution (logic) of


a problem, which can be represented either as an informal
description using a Flowchart or Pseudocode.
What is an Algorithm?

Qualities of a Good Algorithm


❖ Input and output should be defined precisely.
❖ Each step in the algorithm should be clear and unambiguous.
❖ Algorithms should be most effective among many different ways to
solve a problem.
❖ An algorithm shouldn't include computer code. Instead, the
algorithm should be written in such a way that it can be used in
different programming languages.
Algorithm- Examples
Algorithm 1: Add two numbers entered by the user
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Algorithm- Examples
Algorithm 2: Find the largest number among three numbers
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
Algorithm- Examples
Algorithm 3: Find Roots of a Quadratic Equation ax2 + bx + c = 0
Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop
Algorithm- Examples
Algorithm 4: Find the factorial of a number
Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i←1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
Algorithm- Examples
Algorithm 5: Check whether a number is prime or not
Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag ← 1
i←2
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
5.1 If remainder of n÷i equals 0
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
Display n is not prime
else
Display n is prime
Step 7: Stop
Algorithm- Examples
Algorithm 6: Find the Fibonacci series till the term less than 1000
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop
Characteristics of Algorithm
The following are the five important characteristics (features) of
algorithm.
1. Finiteness: An algorithm must always terminate after a number of
steps.
If we trace out the instructions of an algorithm, then for all cases,
the algorithm terminates after a finite number of steps.

2. Definiteness: Each operation must be definite meaning that it must


be perfectly clear.
Each step of an algorithm must be precisely defined.
The actions to be carried out must be rigorously and
unambiguously specified for each case.
Characteristics of Algorithm
3. Input: An algorithm has zero or more "inputs" quantities that are
given to it initially before the algorithm begins, or dynamically as
the algorithm runs. These inputs are taken from specified set of
objects. These inputs are extremely supplied to the algorithm.
4. Output: An algorithm has one or more "output" quantities that have
a specified relation to the inputs. An algorithm produces at least
one or more outputs.
5. Effectiveness: Each operation should be effective i.e.,the operation
must be able to carry out in finite amount of time. An algorithm is
generally expected to be "effective",in the sense that its operations
must all be sufficiently basic that they can in principle be done
exactly and in a finite length of time by some one using pencil and
paper.
Characteristics of Algorithm
❖ Important Note:
❖ The Algorithm designed must be language-independent, i.e. it must
be just plain instructions that can be implemented in any language,
and yet the output will be same, as expected.
❖ Just like it is an important plan before working. It is important to
define the algorithm before coding.
The Role of Algorithms in Computing
❖ An algorithm is a well-defined procedure that allows a computer to
solve a problem. Another way to describe an algorithm is a
sequence of unambiguous instructions. In fact, it is difficult to
think of a task performed by a computer that does not use
algorithms.
❖ We can also view an algorithm as a tool for solving a well-specified
computational problem. The statement of the problem specifies in
general terms the desired input/output relationship. The algorithm
describes a specific computational procedure for achieving that
input/output relationship.
❖ Let us consider an example to sort the numbers in ascending order.
Here is how we formally define the sorting problem.
The Role of Algorithms in Computing
❖ Sorting Problem
❖ Problem: Sorting:
❖ Input: A sequence of n keys a1, a2, a3,••,•.an
❖ Output: The reordering of the input sequence
such that a1<= a2 <= .. <= an-1<=an
❖ For example, given the input sequence {30, 50,40,20, lO}, a sorting
algorithm returns as output the sequence {10, 20, 30, 40, 50}, Such
an input sequence is called an instance of the sorting problem.
❖ An instance of sorting might be an array of names, like
{Rama,Anitha, Indu, Srikanth, Sita},or a list of numbers like {30,
50, 40, 10, 20}. Determining that we are dealing with a general
problem is our first step towards solving it.
The Role of Algorithms in Computing
❖ What is an instance of a problem?
❖ In general, an instance of a problem consists of the input (satisfying
whatever constraint are imposed in the problem statement) needed
to compute a solution to the problem.
❖ Example: The input values like {30, 50, 40, 20, lO} in a-sorting
algorithm.
The Role of Algorithms in Computing
How to decide which algorithm is best suited?
1. It depends on how efficient the algorithm when higher values of
input is given.
For example, sorting algorithm might be efficient for smaller values
of n such as 10 or 100 or 1000 numbers but many not be efficient
for large values of n such as 1 million or 10 million numbers.
2. The possible restrictions/constraints on the input values.
3. The architecture of the computer and the kind of storage devices to
be used,
4. The correctness of the algorithm
The Role of Algorithms in Computing
❖ What is correctness of an algorithm?
❖ An algorithm is said to be correct if, for every instance, it halts with
the correct output.
❖ We say that a correct algorithm solves the given computational
problem. An incorrect algorithm might not halt at all on some input
instances, or it might halt with an incorrect answer.
Practical Applications of Algorithm.
❖ Algorithms are heart of computing, We can find several algorithms
working to solve our daily life problems like internet, social media
networks, GPS applications, Google Maps, e-commerce platforms
like amazon and flipkart, youtube recommendations and so on.
There are many applications of algorithms in various domains. Lets
look at few of the applications of algorithms,

1. Internet: The Internet without which it is difficult to imagine a day


is the result of clever and efficient algorithms. With the help of
these algorithms, various sites on the Internet are able to manage
and manipulate this large volume of data,
Finding good routes on which the data will travel and using search
engine to find pages on which particular information is present.
Practical Applications of Algorithm.
2. The Human Genome Project: Another great milestone is the
Human Genome Project which has great progress towards the goal
of identification of the 100000 genes in human DNA, determining
the sequences of the 3 billion chemical base pairs that make up the
human DNA, storing this huge amount of information in databases,
and developing tools for data analysis. Each of these steps required
sophisticated and efficient algorithms.
3. e-Commerce: The day-to-day electronic commerce activities are
hugely dependent on our personal information such as credit/debit
card numbers, passwords, OTPs and so on. The core technologies
used include public-key cryptocurrency and digital signatures
which are based on numerical algorithms and number theory.
Practical Applications of Algorithm.
4. Page Rank: Google's search engine uses a highly effective algorithm
called PageRank to find the best matches for search terms.
PageRank decides which pages are listed first when you search for
something. This algorithm is highly sophisticated and played an
important role in Google Search success.

5. Weather Forecasting: Weather forecasting algorithms to model


weather patterns and make predictions.
Practical Applications of Algorithm.
6. Linear Programing: An oil company may wish to know where to
place it's wells in order to maximize its expected profit. An airline
may wish to assign crews to flights in the least expensive way
possible, making sure that each flight is covered and that
government regulations regarding crew scheduling are met. An
Internet service provider may wish to determine where to place
additional resources in order to serve its customers more
effectively.All of these are examples of problems that can be solved
using linear programming.
6. Shortest Path Algorithm: Transportation companies may have
financial interest in finding shortest path through a road or rail
network because taking shortest path result in lower labour or fuel
costs.
Practical Applications of Algorithm.
8. Other Important Applications of Algorithms: Speech recognition,
image processing, assigning fastest pick up to cab drivers using
Hungarian algorithm, Facebook's friend suggestion algorithm,
Resource allocation in operating system, product recommendation
in e-commerce sites like amazon, duckworth Lewis method in
cricket, machine learning algorithms, solving puzzles like
crosswords and sudoku.
ALGORITHM IS A TECHNOLOGY
● We must understand that computer programs adopt different
algorithms that run on computer hardware that has a processor &
memory, and these components have limitations.
● A processor is not infinitely fast, and the memory we have is not
free. They are bounded resources. They must be used wisely, and a
good algorithm that is efficient in terms of time complexities and
space complexities.
● Despite the fact that modern processors are incredibly fast and a
memory is cheap, we still have to study algorithms, design them so
as to see if the solution terminates and does so with a correct
result.
Efficiency of an Algorithm
● Efficiency considerations for algorithms are inherently tied in with
the design, implementation, and analysis of algorithms. Every
algorithm must use up some of a computer's resources to complete
its task.
● The resources most relevant in relation to efficiency are central
processor time ( CPU time) and internal memory. Because of the
high cost of computing resources it is always desirable to design
algorithms that are economical in the use of CPU time and memory.
● Algorithms that solve the same problem can differ enormously in
their efficiency.
● Generally speaking, we would like to select the most efficient
algorithm for solving a given problem.
Why Analysing Efficiency is Important?
● Suppose we would like to run two different sorting algorithms on
two different computers A and B, where computer B is 1000 times
slower than computer A.
● For comparing the performance, let us consider of running the
slower sorting algorithm Insertion Sort on faster computer A and
running the faster sorting algorithm Merge Sort on slower computer
B.
Efficiency Comparison of Two Computers

Computer A Computer B

Execute 10 billion Instruction/Second Execute 10 million Instruction/Second

Running Insertion Sort to Sort an array Running Merge Sort to Sort an array

Running Time of the Insertion sort = n2 Running Time of the Merge sort = n logn

Input Size = 10 million Numbers

Time taken by Computer A - 6 Hours Time taken by Computer B - 25 minutes

Input Size = 100 million Numbers

Time taken by Computer A - 23 Days Time taken by computer B - 4 Hours


Efficiency Comparison of Two Computers
● What difference do we observe? Computer B is taking much less time
than computer A, if the input size is large. This gap will increase
further if we increase the input size. This would be one of the reason for
analysing the efficiency of an algorithm.

● It means that using efficient algorithms can be even more important


than building faster computers: more efficient thinking beats more
efficient hardware! And this means that algorithms are definitely worth
studying.
Steps in Problem Solving
● A computer cannot solve a problem on its
own. One has to provide step by step
solutions of the problem to the computer.
● In fact, the task of problem solving is not
that of the computer. It is the programmer
who has to write down the solution to the
problem in terms of simple operations
which the computer can understand and
execute.
● The following figure shows the steps
involved in problem solving.
Steps in Problem Solving
● Analysing the Problem: It is important to clearly understand a problem
before we begin to find the solution for it. If we are not clear as to what is
to be solved, we may end up developing a program which may not solve
our purpose.
● Thus, we need to read and analyse the problem statement carefully in
order to list the principal components of the problem and decide the core
functionalities that our solution should have.
● By analysing a problem, we would be able to figure out what are the
inputs that our program should accept and the outputs that it should
produce.
● We should also understand the constraints on input values and output
values.
Steps in Problem Solving
● Designing Algorithm: It is essential to device a solution before writing a
program code for a given problem. The output of this step is an algorithm.
We start with a tentative solution plan and keep on refining the algorithm
until the algorithm is able to capture all the aspects of the desired
solution. The design of an algorithm depends mainly on the problem and
chosen design technique. There can be more than one algorithm to solve
the same problem, and the choice between them will be decided by their
effectiveness.
● Analysis of Algorithm: An essential task in algorithm development is
proving its correctness. One possible method is to run a number of data
sets as inputs and compare the results against expected output. Another
important task of analysing algorithms is to find out the time complexity
and space complexity of an algorithm to prove that an algorithm is
efficient.
Steps in Problem Solving
● Implementation or Coding: After finalising the algorithm, we need to
convert the algorithm into the format which can be understood by the
computer to generate the desired solution. Different programming
languages can be used for writing a program. We must convert each step of
the algorithm into one or more statements in a programming language
such as C,C++, and Java etc.
● Testing and Debugging: The program created should be tested on various
parameters. The program should meet the requirements of the user. It
must respond within the expected time. It should generate correct output
for all possible inputs. In the presence of syntactical errors, no output will
be obtained. In case the output generated is incorrect, then the program
should be checked for logical errors by debugging the program. Debugging
is the process of identifying and correcting or removing the Bugs (errors)
Designing Algorithms
● There are many ways to design algorithms for a given problem, The
following are some of the popular design approaches.

● Brute Force Algorithm: This is the most basic and simplest type of
algorithm. A Brute Force Algorithm is the straightforward approach to a
problem.
● It is just like iterating every possibility available to solve that problem.
● This type of algorithms are moreover used to locate the ideal or best
solution as it checks all the potential solutions.

● Example: If there is a lock of 4-digit PIN. The digits to be chosen from 0-9
then the brute force will be trying all possible combinations one by one like
0001, 0002, 0003, 0004, and so on until we get the right PIN.
● In the worst case, it will take 10,000 tries to find the right combination.
Designing Algorithms
● Recursive Algorithms: This type of algorithm is based on recursion.
● In recursion, a problem is solved by breaking it into sub-problems of the
same type and calling own self again and again until the problem is solved
with the help of a base condition.

● Example: Some common problem that is solved using recursive algorithms


are Factorial of a Number, Fibonacci Series, Tower of Hanoi, tree
traversals, depth first search for Graph, etc.
Designing Algorithms
● Divide and Conquer Technique: The "divide and conquer" technique involves
solving particular problem by dividing it into one (or) more sub problems of
smaller size, recursively solving each sub problem and then "merging" the
solutions to the sub-problem to produce a solution to the original problem. The
divide and conquer paradigm involves three steps at each level of recursion.
1. Divide: Divide the problem into a number of sub problems.
2. Conquer: Conquer the sub problems by solving them recursively. If the
sub problem sizes are small enough, then solve the sub problem in a
straightforward manner.
3. Combine: Combine the solutions to the sub problems in to the solution
for the original problem.
● Example: This technique is the basis of efficient algorithms for all kinds of
problems, such as sorting (Quick Sort and Merge Sort), finding maximum and
minimum numbers, multiplying large numbers etc.,
Designing Algorithms
● Greedy Approach: A greedy algorithm is a type of algorithm that is
typically used for solving optimization problems.
● So whenever one wishes to extract the maximum in minimum time or with
minimum resources, such an algorithm is employed.
● This is an algorithm paradigm that makes the best choice possible on each
iteration in the hopes of choosing the best solution. It is simple to set up
and has a shorter execution time.
● The result is a good solution but not necessarily the best one. The greedy
algorithm does not always guarantee the optimal solution however it
generally produces solutions that are very close to the optimal solution.

● Example: Some common problems that can be solved through the Greedy
Algorithm are Prim's Algorithm, Kruskal's Algorithm, Huffman Coding, etc.
Designing Algorithms
● Dynamic Programming: A dynamic programming algorithm works by
remembering the results of a previous run and using them to arrive at new
results.
● Such an algorithm solves complex problems by breaking it into multiple
simple subproblems, solving them one by one and storing them for future
reference and use.

● Example: The following problems can be solved using Dynamic


Programming algorithm Knapsack Problem, Weighted Job Scheduling,
Floyd Warshall Algorithm, Dijkstra Shortest Path Algorithm, etc.
Designing Algorithms
● Backtracking Algorithms: Backtracking algorithm is one that entails
finding a solution in an incremental manner. There is often recursion/
repetition involved and attempts are made to solve the problem one part at
a time. At any point, if one is unsuccessful at moving forward, one
backtracks and comes back to start over and find another way of reaching
the solution.
● So backtracking algorithm solves a subproblem and if and when it fails to
solve the problem, the last step is undone and one starts looking for the
solution again from the previous point.

● Example: Some common problems that can be solved through the


Backtracking Algorithm are Hamiltonian Cycle,M-Coloring Problem, N
Queen Problem, Rat in Maze Problem, ete.
Qualities of Good Algorithm
● There are usually many ways to solve any given problem. In computing, we
are generally concerned with "good" solutions to problems. Good
algorithms usually possess the following qualities and capabilities:
1. They are simple but powerful and general solutions.
2. They can be easily understood by others, that is, the implementation is clear and
concise without being "tricky"
3. They can be easily modified if necessary.
4. They are correct for clearly defined situations.
5. They are able to be understood on a number of levels.
6. They are economical in the use of computer time, computer storage and peripherals.
7. They are documented to be used by others who do not have a detailed knowledge of
their inner workings.
8. They are not dependent on being run on a particular computer or particular
programming language.
9. They are able to be used as a sub-procedure or functions for other problems.
Analysis of Algorithm
● Algorithm analysis refers to the task of determining the computing time
and storage space requirement of an algorithm. It is also known as
performance analysis or efficiency of an algorithm which enables us
to select an efficient algorithm.
● The general idea is to take a particular algorithm and to determine its
quantitative behaviour, occasionally we also study whether or not
an-algorithm is optional in some sense.
● When we have a problem to solve, there may be many algorithms
available. We would obviously like to choose the best.
● The selection of best algorithm is possible by analysing the algorithms in
proper manner.
Analysis of Algorithm
● Analysis of algorithms or performance analyse is refers to the task of
determining how much computing time and storage an algorithm
requires.
● This is a challenging area which sometimes require great mathematical
skills.
● An important result of this study is that it allows to make quantitative
judgments about the value of one algorithm over another.
● Another result is that it allows to predict whether the software will meet
any efficiency constraints that exist.
● Analysis of an algorithm is a process of making evaluative judgement
about algorithms.
● And also Performance of an algorithm means predicting the resources
which are required to an algorithm to perfo rm its task.
Analysis of Algorithm
● Generally, the performance of an algorithm depends on the following
elements.
1. Whether that algorithm is providing the exact solution for the problem?
2. Whether it is easy to understand?
3. Whether it is easy to implement?
4. How much space (memory) it requires to solve the problem?
5. How much time it takes to solve the problem? Etc.,
● When we want to analyse an algorithm, we consider only the space and
time required by that particular algorithm and we ignore all remaining
elements.
● Based on this information, performance analysis of an algorithm can also
be defined as follows.
"Performance analysis of an algorithm is the process of calculating space
required by that algorithm and time required by that algorithm".
Analysis of Algorithm
● We can analyse an algorithm by two ways.
○ By checking the correctness of an algorithm
○ By measuring time and space complexity of an algorithm
● Time Factor: Time is measured by counting the number of key
operations such as comparisons in the sorting algorithm.
● Space Factor: Space is measured by counting the maximum memory
space required by the algorithm.
Priori Analysis and Posteriori Analysis
● To compute the analysis of algorithm, two phases are required
○ Priori Analysis
○ Posteriori Analysis

● 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. This is done usually by the algorithm designer.
● Algorithm complexity is determined in this phase.
● In this we obtain a function which bounds the algorithms computing
time. Suppose there is some statement and we wish to determine the
total time that statement will spend for the execution, given some initial
state of input data.
Priori Analysis and Posteriori Analysis
● This requires essentially two items of information.

1) The statements frequency count.


i.e.,the number of times the statement will be executed.
2) The time taken for one execution.
Priori Analysis and Posteriori Analysis
● The product of two numbers is the total time.
● Since the time per execution depends on both i.e., the machine being
used and the programming
● languages used together with its compiler, a priori analysis limits itself to
determine the frequency
● count of each statement.
● Priori analysis of computing time ignores all of the factors, which are
machine or programming
● language dependent and only concentrates on determining the order of
the magnitude of the
● frequency of execution of the statements.
● The notation used in the priori analysis are Big-oh (0). Omega (fi). Theta
(8) and Small-oh(o).
Priori Analysis and Posteriori Analysis
● Posterior analysis
● "Posterior" means "after". Hence Posterior analysis means checking the
algorithm after its implementation.
● In this, the algorithm is checked by implementing it in any programming
language and executing it.
● This analysis helps to get the actual and real analysis report about
correctness, space required, time consumed etc.
● In this we will collect the actual statistics about the algorithm,
conjunction of the time and space while executing.
● Once the algorithm is written it has to be tested. Testing a program
consists of two major phases.
Priori Analysis and Posteriori Analysis
● Debugging : It is the process of executing programs on sample data sets
that determine whether we get proper results. If faulty results occurs it
has to be corrected.
● Profiling: It is the process of executing a correct program on actual data
sets and measuring the time and space it takes to compute the results
during execution. The actual time taken by the algorithm to process the
data is called profiling.
Profiling
{
1. Read data
2. Time (t1)
3. Process (data)
4. Time (t1)
5. Write (time = t2 - t1)
}
Differences between Priori Analysis and
Posteriori Analysis
Priori Analysis Posteriori Analysis
Analysis is the process of determining Profiling is the process of executing
how much computing time and the correct program on data sets and
storage an algorithm will require. measuring the time and space it
takes to compute the results.
This is independent of machine This is dependent on machine,
programming language and won't programming language and the
involve the execution of program. compiler used.
It will give approximate answer. It will give exact answer.
It uses the asymptotic notations to It doesn't use asymptotic notations to
represent how much time the represent the time complexity of an
algorithm will take in order to algorithm.
Complexity of Algorithms
● Complexity of an algorithm is a measure of the amount of time and/or
space required by an algorithm for an input of a given size (n).
● Algorithm complexity can be further divided into two types: time
complexity and space complexity.
○ Space required to complete the task of that algorithm (Space
Complexity).
○ Time required to complete the task of that algorithm (Time
Complexity)
Space Complexity
● It indicates the amount of temporary storage required for running the
algorithm. i.e., "the amount of memory needed by the algorithm to run to
completion".
● When we design an algorithm to solve a problem, it needs some
computer memory to complete its execution. For any algorithm, memory
is required for the following purposes.

1. Memory required to store program instructions


2. Memory required to store constant values
3. Memory required to store variable values
4. And for few other things
Definition: Total amount of computer memory required by an algorithm to
complete its execution is called as space complexity of that algorithm.
Space Complexity
Generally, when a program is under execution it uses the computer memory
for three reasons. They are as follows.
1. Instruction Space: It is the amount of memory used to store
compiled version of instructions.
2. Environmental Stack: It is the amount of memory used to store
information of partially executed functions at the time of function
call.
3. Data Space: It is the amount of memory used to store all the
variables and constants.
Note:
We consider only Data Space and ignore Instruction Space and
Environmental Stack while calculating space complexity. It means that we
calculate only the memory required to store Variables, Constants, Structures,
etc.,
Time Complexity
● The amount of time needed to run the program is termed as time
efficiency or time complexity.
● The total time taken by a program is the sum of the compile time and
runtime. The compile time does not depend on the instance
characteristics and it can be assumed as a constant factor so we
concentrate on the runtime of a program.
● Let this runtime is denoted by tp (instance characteristic), then
tp(n) = ta ADD(n) + ts SUB(n) + tm MUL(M) +- - - -

● Where n indicates the instance characteristics and ta, ts, tm- - - denote
the time needed for an addition, subtraction, multiplication, and so on.
● ADD, SUB, MUL- - - represent the functions and they are performed
when the code for the program is used on an instance characteristic 'n'.
Time Complexity
● Obtaining such an exact formula is itself an impossible task, since the
time needed for an addition, subtraction, multiplication and so on, often
depends on the numbers being added, subtracted, multiplied and so on.
● The value of t(n) for any given ‘n' can be obtained only experimentally.

● Definition of Time Complexity


● The time complexity of an algorithm is the total amount of time required
by an algorithm to complete its execution.
Time Complexity- Measuring an Input size
● The input size is denoted by 'n' and we use 'n' in most of the algorithms. In
searching and sorting n indicates the number of array elements, in matrix
manipulation n indicates the matrix order, in polynomials n indicates the
degree and in travelling salesman problem ‘n’ indicates the number of
cities.
● Therefore the input size n is very much important in analyzing the
algorithm.
● The analysis of an algorithm will be focussed on input size 'n'. Some
algorithms require more than one parameter to indicate the size of their
inputs.
● For example, the number of vertices and the number of edges for
algorithms on graphs represented by adjacency linked lists.
● Finally, we can say that it is logical to investigate an algorithm's efficiency
as a function of some parameter n indicating the algorithm's input size.
Time Complexity- Units for measuring Runing Time
● We cannot measure the running time by seconds, milliseconds, and so on
because such a measurement depends on the type of computer, compiler
and the program. We would like to have a metric that does not depend on
these extraneous factors.
● Following are the some of the methods of computing the time efficiency of
algorithms.
○ Operation Counts.
○ Step Counts
○ Asymptotic notations (Mathematical Analysis)
Time Complexity- Operation Counts.
● Consider an algorithm 'A' with 'n' size of the input data. The time and
space are the two main measures for the efficiency of the algorithm. In
operation counts, the time is measured by counting the number of basic
operations or key operations.
● The basic operations are defined that the time for the other operations is
much less than or almost proportional to the time for the basic
operations.
Time Complexity- Operation Counts.

Code Description
A = a * b; This code takes 1 unit of time

for (i = 0; i < n; i ++) This code takes 'n' units of time


a = a + i; because, it executes for n times.

for (i = 0; i < n; i ++)


for (j = 0; j < n; j ++) This code takes 'n" units of time
printf (" Hello");

● The operation count method concentrates on certain important basic


operations like multiplications, for loop or while loop where ittakes
considerably more time than any other operations in an algorithm.
Time Complexity- Steps Counts
● In step counts method, we attempt to find the time spent in all parts of
the program. A step is any computational unit that is independent of the
selected characteristics.
● Example 1
○ x=a+b Step Count 1

○ for (i = 1; i < n; i++) Step Count n


x=a+b

○ for (i = 1; i < = n; i++)


for (j = 1; j < = n; j++)
x=a+b Step Count = n2
Time Complexity- Steps Counts
Example 2 Finding the
The step count for 'sum' =1
Sum of all Array Elements
The step count for 'for' statement =n+1
int sum(int a[], int n){
The step count for 'assignment' =n
int i, sum = 0;
The step count for 'return' =1
sum_count++;
Total steps = 2n + 3
for (i = 0; i < n; i++)
{
Here we consider the every statement rather
For_count++;
than basic operations. The sum initialization
sum = sum + a [i];
takes 1 unit of time, the for loop executes
Assign_count++;
n+1 times, the assignment statement
}
executes n times and the return statement
for_count ++;
takes 1 unit of time
return_count ++;
Return sum;
}
Orders of Growth
● The time complexity of an algorithm is generally some function of the
instance characteristics. This function is very useful in determining how
the time requirements vary as the instance characteristics change.
● Let T(n) be the complexity function with input size 'n',
● The values of T(n) increases when 'n' value increases and T(n) value
decreases when 'n' value decreases.
● Therefore, the complexity function is directly proportional to the instance
characteristics 'n',
● Assume that algorithm P has complexity O(n) and algorithm Q has
complexity 0(n^2). We can assert that algorithm P is faster than
algorithm Q for sufficiently large n.
● Since here (n < n-) and we can say that algorithm P is faster than
algorithm Q.
Orders of Growth
● The most standard common
Function Name
computing time functions are
1 constant
shown below.
log n logarithmic

n linear

n log n linearithmic

n^2 quadratic

n^3 cubic

2^n exponential

n! Factorial
The order of growth is
0(1) < O(log n) < O(n) < O(n log n) < O(n^2) < 0(n^3) < 0(2^n) < o(n!)
Orders of Growth
● To know how the various functions grow with "0", it is advised to study the
following table.
Orders of Growth
● It is evident from the above table that the function 2^n grows very rapidly
with n.
● In fact, if an algorithm needs 2^n steps for execution, then when n = 32,
the number of steps needed is approximately 4.2 x 10^9
● Therefore we may conclude that the utility of algorithms with exponential
complexity is limited to small n.

You might also like