Unit I
Unit I
Introduction to Algorithm
and Data Structures
BY MRS. MANISHA DESAI
Introduction to Algorithm and Data Structures
Introduction: From Problem to Data Structure (Problem, Logic, Algorithm, and Data Structure).
Data Structures: Data, Information, Knowledge, and Data structure, Abstract Data Types (ADT), Data
Structure Classification (Linear and Non-linear, Static and Dynamic, Persistent and Ephemeral data
structures)
Algorithms: Problem Solving, Introduction to algorithm, Characteristics of algorithm, Algorithm design
tools: Pseudo-code and flowchart
Complexity of algorithm: Space complexity, Time complexity, Asymptotic notation- Big-O, Theta and
Omega, Finding complexity using step count method, Analysis of programming constructs-Linear,
Quadratic, Cubic, Logarithmic.
Algorithmic Strategies- Introduction to algorithm design strategies- Divide and Conquer, and Greedy
strategy.
Case Study: Multiplication technique by the mathematician Carl Friedrich Gauss and Karatsuba
algorithm for fast multiplication.
Introduction: From Problem to Data Structure (Problem, Logic,
Algorithm, and Data Structure).
Data Structures: Data, Information, Knowledge, and Data structure,
Problem
Problem :
“Problem is defined as situation or condition which needs to solve to achieve
goal”
Steps in Problem Solving :
1.Define the problem
2.Data gathering
3.Decide effective solution
4.Implement and evaluate the solution
5.Review the result.
Logic
Logic
A method of human thoughts that involves thinking in linear step by step
manner about how a problem can be solved
Logic is a language for reasoning. It is a collection of rules we use when
doing reasoning.
Example: Mary’s mom has four children
◦ The First child is called April
◦ The second May
◦ The Third June
What is the name of the fourth child..?
Algorithm
Algorithms is set of instructions which are written in simple English language.
An algorithm is defined as a step-by-step procedure or method for solving a
problem by a computer in a finite number of steps.”
From the data structure point of view, following are some important categories
of algorithms −
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
Characteristics of Algorithm
1. 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.
2. Input − An algorithm should have 0 or more well-defined inputs.
3. Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
4. Finiteness − Algorithms must terminate after a finite number of steps.
5. Feasibility − Should be feasible with the available resources.
6. Independent − An algorithm should have step-by-step directions, which should
be independent of any programming code.
Example of Algorithm
Data Object :
“Data object is a region of storage that contains a value or group of
value”
Stages of Processing
Knowledge
100 miles
Information
100
Data
NEED OF DATA STRUCTURE
Non-
Primitives
Primitives
data structure
data structure
Files
Integer Float Char Pointer Arrays Lists
Problem :
“Problem is defined as situation or condition which needs to solve to achive goal”
DEFINITION :
“An algorithm is defined as a step-by-step procedure or method for solving a problem by a computer
in a finite number of steps.”
From the data structure point of view, following are some important categories of algorithms −
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
CHARACTRISTICS OF ALGORITHM
1.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.
2. Input − An algorithm should have 0 or more well-defined inputs.
3. Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.
4. Finiteness − Algorithms must terminate after a finite number of steps.
5. Feasibility − Should be feasible with the available resources.
6. Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
ALGORITHM DESIGN TOOLS
There can be two tools :
1. Flowchart
2. Pseudo Code
Flowchart :
“ Flowchart is graphical representation of the algorithms”
Pseudo Code :
“It is simply an implementation of an algorithm in the form of annotations and informative text
written in plain English.
Symbols used in flowchart
Example : flowchart & Pseudocode
Complexity of algorithm: Space complexity, Time
complexity, Asymptotic notation- Big oh, Theta and Omega
Performance Analysis
Is it giving the exact solution for a problem?
How much space it takes?
How much time it takes? Is my
Is it easy to understand algorithm
Best..?
Is it easy to implement
Etc.……..
Complexity of algorithm
There are two important complexity measures:
Time complexity :
Total time which is required for an algorithm to complete its process.
Independent of programming language or implementation details
It allows comparing the algorithms to check which one is the efficient one.
Space complexity :
The amount of computer memory required for an algorithm to solve the Problem(successful
execution)
Memory space is generally considered as space of primary memory.
Performance Evolution done in two measure phases
A Priori Estimates (referred as performance Analysis)
Posteriori testing (referred as performance measurement)
Space complexity
Total space
Fixed Part Variable Part required
(depends on
10 A,B,C data type of
A,B,C)
Asymptotic notations
Asymptotic Notations
It is a mathematical representation of algorithm’s complexity.
In asymptotic notation, when we want to represent the complexity of an algorithm, we use only the
most significant terms in the complexity of that algorithm and ignore least significant terms in the
complexity of that algorithm (Here complexity can be Space Complexity or Time Complexity).
Following are the commonly used asymptotic notations to calculate the running time complexity of
an algorithm.
1. Ο (Big Oh) Notation upper bound
2. Ω (Big Omega) Notation lower bound of a function
3. θ (Theta) Notation average bound of a function
Graphical Representation
Time complexity of any algorithm can be represented with any one of the above.
If it is not among these , it will be multiple of these.
And if not then we require lower bound or upper bound
Big - Oh Notation (O)
Big - Oh notation is used to define the upper bound of an algorithm in terms of Time
Complexity.
That means Big - Oh notation always indicates the maximum time required by an
algorithm for all input values.
That means Big - Oh notation describes the worst case of an algorithm time complexity.
The function f(n) = O(g(n)) if & only if there exist positive constant C , n0
Such that f(n) <= C * g(n) for all n >= n0 where C > 0 and n0 >= 1.
Ex:f(n)=2n+3
Consider g(n)=n Consider g(n)=𝑛2
i.e. 2n+3<= 5 n n>=1 2n+3<= 5 𝑛2 n>=1
f(n)=O(n) f(n)=O(𝑛2 )
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) = Ω(g(n)) then it must satisfy f(n) >= C g(n) for all values of C > 0 and n0>= 1
By using Big - Omega notation we can represent the time complexity as follows...
3n + 2 = Ω(n)
Big - Theta Notation (Θ)
Big - Theta notation is used to define the average bound of an algorithm in terms of Time
Complexity.
That means Big - Theta notation always indicates the average time required by an algorithm for
all input values.
That means Big - Theta notation describes the average case of an algorithm time complexity.
The function f(n) = Θ(g(n)) if & only if there exist positive constant C1,C2 , n0
Such that C1 * g(n) <= f(n) <= C2 * g(n) for all n >= n0
Ex:f(n)=2n+3
Consider g(n)=n
i.e. 1*n <= 2n+3 <= 5*n n>=1 f(n)= Θ(n)
Example
Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) = Θ(g(n)) then it must satisfy C1 g(n) <= f(n) <= C2 g(n) for all values
of C1 > 0, C2 > 0 and n0>= 1
By using Big - Theta notation we can represent the time compexity as follows...
3n + 2 = Θ(n)
Finding complexity using step count method, Analysis of
programming constructs-Linear, Quadratic, Cubic, Logarithmic.
Syntax of for loop in CPP
for (statement 1; statement 2; statement 3)
Example
{ for (int i = 0; i < 5; i++)
{
// code block to be executed cout << i << "\n";
}
}
statement 1 is executed (one time) before the execution of the code block.
statement 2 defines the condition for executing the code block.
statement 3 is executed (every time) after the code block has been executed.
Step Count Method
The step-count method count for all the time spent in all parts of the
program
A program step is loosely defined to be a syntactically or
semantically meaningful segment of a program for which the
execution time is independent of the instance characteristics.
Step count Method
First define steps:
1.int mean(int a[], size_t n)
2.{
3.int sum = 0; // 1 step
4.for (int i = 0; i < n; i++) // 1 step
5.sum += a[i]; // 1 step
6.return sum; // 1 step
7.}
Next determine the frequency of the
steps based on N:
1.int mean(int a[], size_t n)
2.{ Space Complexity
3.int sum = 0; // 1 step * 1 Variables Used are
a=n
4.for (int i = 0; i < n; i++) // 1 step * (N+1)
Sum=1
5.sum += a[i]; // 1 step * N n=1
6.return sum; // 1 step * 1 i=1
-----------------------
7.}
=n+3
Space Complexity
=O(n)
Add up the steps: 1 + (N+1) + N + 1
Reduce: 2N + 3
Throw away factors that don't grow with N and you're done: O(N)--(order of polynomial is 1 ie of degree 1)
Example
1. seqSearch(arr, n, key) Step Frequency
2. i := 0 1 1
3. while i < n, do 1 n+1
4. if arr[i] = key, then 1 n Space Complexity
5. break 1 0/1 Variables Used are
6. end if
7. done Arr=n
8. return i 1 1 Key=1
n=1
Time complexity =1*1+1*(n+1)+1*n+1*1+1*1 i=1
=n+n+1+3 -----------------------
=n+3
=2n+4 Space Complexity
=O(n) =O(n)
Algorithm for adding two n*n Matrix
}
}
F(n) = O(𝑛2 )
Cubic
In this case of algorithms there are three nested loops
Multiply(A,B,n)
{
for(i=o; i<n ; i++)
{
for(j=o; j<n; j++) F(n) = O(𝑛3 )
{
C[i,j]=0;
for(k=o; k<n; k++)
{
C[i,j]= C[i,j]+A[i,k]*B[k,j];
}
}
}
}
Logarithmic
In this case of algorithm controlling factor is either
For(i=1; i<n; i=i*2)
multiplication (i*2) or division (i/2). {
Print(“hello”);
F(n) = O(log 𝑛) }