DS - Lecture Week 2
DS - Lecture Week 2
Week 2 Lecture
Example:
Definition
Making a Delicious Tea
Step by step
procedures to solve
a problem
Input Output
Characteristics of Algorithm
• Input & Output: Zero or more input but at least one output
Algorithm Program
• A program is a set of instructions written in Programming
• An algorithm is a well-defined
Language (computer understandable language) to
sequence of steps (written in Natural
perform a certain task.
Language) to solve a given problem
• Program is an implementation of an algorithm
• Design Time
• Knowledge of Programming Language
• Domain Knowledge
• Testing
• Analyze
• Dependent on OS and H/W
• Independent from OS and H/W
How Algorithm is different from program
end function
Note: Pseudocode is not an actual programming language.
Role of Algorithm Designer
Algorithm
Can I do
Designer
better?
Analysis of Algorithms
• Given a particular problem of size n. The time required by any algorithm for solving, this problem
is denoted by a function such as f(n).
• Best-case − The minimum amount of time needed by the algorithm to complete a task
(best data or best input data)
• Average case − The average amount of time needed by the algorithm to complete a
task
Write Algorithms
• Swap Two Numbers
Write an algorithm to swap the values of two variables.
• Find the Largest of Three Numbers
Write an algorithm to find the largest number among three given inputs.
• Check if a Number is Even or Odd
Write an algorithm to determine whether a given integer is even or odd.
• Sum of First N Natural Numbers
Write an algorithm to calculate the sum of the first N natural numbers.
• Check if a Number is Prime
Write an algorithm to determine if a given number is prime.
• Find the Maximum Element in an Array
Write an algorithm to find the largest number in an array of integers.
Analysis of Algorithms- How?
Problem Statement
Searching a X Student in a Class
A A
Algorithm Algorithm
11 22
Given two algorithms for the above task,
how do we find out which one is
Analysis of Algorithms – How?
Problem Statement
Searching a X Student in a Class A A
Algorithm Algorithm
11 22
Method-1
Problem Statement
Searching a X Student in a Class
A A
Algorithm Algorithm
Problem 11 22
It might also be possible that for some
inputs, first algorithm perform better
on one machine and the second works
Machine 1 Machine 2
better on other machine.
Analysis of Algorithms – How?
We determine the time and space complexity of an algorithm by just looking at the
algorithm rather than running it on a particular system with a different memory,
processor, and compiler. (Theoretical Analysis)
It doesn’t require algorithms to be implemented and the time taken by programs to be compared.
Different types of asymptotic notations are used to represent the complexity of an algorithm.
~ 35 Lakh
Price: 35 Lakh Price: 10,000/-
• Given a particular problem of size n. The time required by any algorithm for solving this
problem is denoted by a function such as f(n).
Assume that f(n) = 2n2, and g(n) = n3 , f(n) = O(g(n)) ? a positive integer n 0 and a
Question -1
F(n) = 1000 n2 + 5n3 + 6000n,
What is the Order of Function ?
a) f(n)= O(n2)
Which one is the
b) f(n)= O(6000n)
correct answer?
c) f(n)= O(n3)
d) I am confused
Correct Answer is c
General Assessment Time
10
9
8
7
6
5
4
3
2
1
Question -2
F(n) = n3 D(n) = n4 E(n) = n6
T(n)= F(n) + D(n) + E(n)
What is the Order of Function O(T(n))= ?
a) T(n)= O(n3)
Which one is the
b) T(n)= O(n6)
correct answer?
c) T(n)= O(n4)
Log2n n n 2
n 3
2 n
n
n
n=1 0 1 1 1 2 1
n=2 1 2 4 8 4 4
n=4 2 4 16 64 16 256
n=8 3 8 64 512 256 1,67,77,216
Algorithm Swap
(a, b)
{ 1 unit of
temp=a; time
1 unit of
a=b; time
1 unit of
b=temp; time
} Total= 3 unit of time = O(1) = constant
*Assume that every statement take one unit of time for
Analyzing Time Complexity
Time Complexity: Time complexity defi nes the total amount of time an algorithm
needs to execute all its key statements and in generating the output. (running
time)
n=
Algorithm of Sum of n number Algorithm Sum (A, n) 5 4
A 6 3 8 1
{
= 0 1 2 3 4
S=0; 1
Sum instructions will be executed
for ( i=0; i<n; i++) n+ i=0
i=0 -> 1 i=1 Sum instructions will be executed
{ i++ -> n 1 Sum instructions will be executed
i<n -> n+1 i=2
S= S + A[i]; n Sum instructions will be executed
i=3
} Sum instructions will be executed
i=4
return S; 1 i<n Sum instructions will not execute
i=5
} condition checked 6
f(n) = timesi has changed 5
i++,
times
*Assume that every statement take one unit of time for execution 2n+3
Understanding Space
Complexity
Algorithm of Swapping Two Numbers
a=b; b 1 word
b=temp; temp
} Total= 3 words
Space Complexity= s(n)= constant =
*Assume that every variable takes one word space for storage
Understanding Space
Complexity
Sum of n
number
Algorithm Sum (A, n) Space Complexity
{
S=0; A ----->n
for ( i=0; i<n; i++) n------>1
{ S------>1
S= S + A[i];
i------>1
}
return S; S(n) = n+3 =
} O(n)
General Assessment Time
10
9
8
7
6
5
4
3
2
1
• Arrays can be easily looped through, which is useful for processing data.
• Arrays are stored in contiguous memory locations, which improves memory access
speed and performance, especially in lower-level languages like C/C++.
• Arrays can represent complex structures like matrices, images, and tables.
Array Initialization
double balance[10];
int arr[5];
base address
cin>>var[i]; }
}
General Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
using namespace std; Which one is the
int main() correct answer?
{
a) 25
int a[] = {1,2,3,4};
b) 52
int b[4] = {5,6,7,8};
c) 36
Correct Answer is c cout<< a[2] << b[1];
d) 63
return 0;
}
Operations on Array
#include <iostream>
using namespace std;
int main(){
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
for (int i = 0; i < n; i++)
Time Complexity
{
cout<< num[i]; Time Complexity for Traversal – O(n)
(in all cases)
}
return 0;
}
Insertion − Adding an element
int new_element = 9 2 8 7 6 0
int num[]={2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
2 8 7 6 0
2 8 7 6 0
Shifting last element to
the next available space
Beginning of the array
Insertion − Adding an element
n=n+1;
int num[new_size]; for (i = n-1; i <=1; i--) Time Complexity
num[5]=num[4];
{ Time Complexity– O(n)
num[4]=num[3];
num[3]=num[2]; num[i] = num[i-1];
num[2]=num[1]; }
num[1]=num[0];
num[i]=new_element;
num[0]=new_element;
Insertion − Adding an element
int updated[new_size];
8 7 6 0
num[0] num[1] num[2] num[3] num[4]
Element at Index 0 of the array
You cannot actually remove an element from a static array because its size is fixed. But
you can simulate deletion by shifting elements to the left and reducing the logical size.
2 8 5 6 0
num[0] num[1] num[2] num[3] num[4]
1D Array
Suppose an array, A[ ] having Base address (BA) = 999 and size of an element = 2 bytes, find the
location of A[1].
2D Array
Note: 2-D arrays exists only from the user point of view and created to
implement a relational database table look alike data structure. In
computer memory, the storage technique for 2D array is similar to that of
an one dimensional array.
Visualization/Representation of 2-D arrays in Memory
• All the rows of the 2D array are stored into the memory contiguously.
• First, the 1st row of the array is stored into the memory completely, then
the 2nd row of the array is stored into the memory completely and so on
till the last row.
Address(a[i][j]) = B. A. + (i * n + j) * size
or
Address(a[i][j]) = B. A. + ((i -lr)* n + (j-lc)) * size
lr - Lower limit of row or start limit of the row or assume 0 if it is not given
lc- lower limit of column o start limit of column or assume 0 if it is not given.
Accessing Element of in 2D Array
Address(a[i][j]) = B. A. + ((i-lr) * n + (j-lc)) * size
Example-
Given an array, arr[1………10][1………15] with base value 100 and the size of each element
is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major order.
lr - Lower limit of row or start limit of the row or assume 0 if it is not given
lc- lower limit of column o start limit of column or assume 0 if it is not given.
Any Queries?