You are on page 1of 16

Version : 1st Draft

CHAPTER 1
Introduction to Data Structure &
Algorithms

Prepared by
Santosh Kumar Behera,
Learning Outcomes

1. Student will learn the fundamental of data structure and Algorithm.

2. Student can perform various operation on Data Structure.

3. Student can compare and analyse algorithms.

4. Student can calculate the running time of an Algorithm.


Contents

1 Introduction to Data Structure & Algorithms 1


1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Why Organization or arrangement of Data is needed . . . . . 1
1.3 Classification of Data Structure . . . . . . . . . . . . . . . . . . 3
1.4 Data Structure operations . . . . . . . . . . . . . . . . . . . . . 4
1.5 Algorithm Comparision . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Algorithm Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 Looping Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7.1 Linear Loops . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7.2 Logarithmic loops . . . . . . . . . . . . . . . . . . . . . 9
1.7.3 Quadratic Loop . . . . . . . . . . . . . . . . . . . . . . . 10
1.7.4 Linear Logarithmic Loop . . . . . . . . . . . . . . . . . 10
1.7.5 Depndent Quadratic Loop . . . . . . . . . . . . . . . . . 10
1.8 Growth-rate Functions . . . . . . . . . . . . . . . . . . . . . . . 11
1.9 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

i
CHAPTER 1

Introduction to Data Structure & Algorithms

1.1 Introduction
Data Structure is a way of collecting and organising data in such a way that
we can perform operations on these data in an effective way. Data Structures
is about rendering data elements in terms of some relationship, for better
organization and storage.
For example, we have data player’s name ”Virat” and age 26. Here ”Virat”
is of String data type and 26 is of integer data type.
We can organize this data as a record like Player record. Now we can collect
and store player’s records in a file or database as a data structure.
For example: ”Dhoni” 30, ”Gambhir” 31, ”Sehwag” 33 In simple language,
Data Structures are structures programmed to store ordered data, so that
various operations can be performed on it easily.

- Algorithm:-Step-by-step mthod or procedure to solve a problem.

- Program:-Implementation of algorithm in some programming language.


Program is a set of instruction.

- Data Structure:-

Arrangement of data.

Way to Organize your data.

1.2 Why Organization or arrangement of Data is


needed
Particular way to organize the data so that it can be used efficiently.

1
Data Structure

To be Organized Some accessing function or


Large Collection of
+ or arranged as per operation to be performed
Data elements +
some rules/policy on the Data elements

Problem Statement:-

Let us consider a collection of 7 integer data elements which are given below:

42,29,33,38,54,76,49

Objective:-To store the data in such a way that it can be used efficiently.
Procedure 1:

int p=42; // Variable declared is of type integer


int x=29;
int y=33;
int z=38;
int n=54;
int c=76;
int m=49;

Difficulties in produre 1:

- Difficult to keep track of or remember variable names.

- Searching is also require to write lot of condition to compare each


variable.
e.g:-Let we want to search for 76 then our condition will be:

if(p==76)
if(x==76)
if(y==76)
.
.
.
.
.
When no of variable is 1000 then 1000 times we need to write the condition.

Procedure 2:

- Now we arrange the data elements in a particular way.

- Now see how th data elment can be used efficiently.

- So the above arrangement is a single variable name which can store


multiple value of similar type.

Search the data item 76

for i=0 to 6
{
if(N[i]==76)
}

So from the above code it indicate that lot of condition are not required.
So in Procdure 2 what structure we have used is nothing but a Data Struc-
ture called Array.

Example of Data Structure:

- Array

- Linked List

- Stack

- Queue

- Tree

- Graph

1.3 Classification of Data Structure


Data structure are normally divided into two broad categories:

i Primitive Data Structure

ii Non-Primitive Data Structure


Data Structure

Primitive Non-Primitive

integer float character Array Lists Files

Linear List Non-Linear List

Stack Queue Tree Graph

(i)Primitive Data Structure:


There are the basic structure and ar dirctly operated upon by th machine.
Ex-int,float,char etc.
(ii)Non-Primitive Data Structure:
These data structure are derived from the primitive data structure.
Ex-Array,Lists etc.

1.4 Data Structure operations


Some of the important operations on data structure are given below.
- Creation:A data structure cab be created from the data elements. This
operation reserves memory for data.Ex-Array

A 42 29 33 38 54

0 1 2 3 4

- Insertion:New data items can be added to the array.

A 42 29 33 38 54

0 1 2 3 4 5

insert item 76 at index 2

- Deletion:Data item can be deleted from the array.

A 42 29 33 38 54

0 1 2 3 4

Delete item 33 from index 2


- Traverse:Each data item in the array is visited for displaying purpose.

A 42 29 33 38 54

0 1 2 3 4

for(int i=0;i<=4;i++)
cout<<A[i];

- Searching:We can search an item from the array.

A 42 29 33 38 54

0 1 2 3 4

Search or find the item 38 in the array A.


- Sorting:Sort the element in the array in ascending or decensing order.

A 42 29 33 38 54

0 1 2 3 4

After sorting:-

A 29 33 38 45 54

0 1 2 3 4

1.5 Algorithm Comparision


Comparing algorithm to check which algorithm is better then other one.

Problem Statement 1:-Sum of N natural numbers.


Algorithm 1.1
Step1:-Read n
Step2:-Initialize S=0
Step3:-for i=1 to n
do
s=s+i
end loop
Step4:-Print s
Step5.Exit
Algorithm 1.2

Step1: Read n
Step2: Initialize s=0
Step3: s=n*(n+1)/2
Step4: Print s
Step5: Exit

Both the algorithm will fullfill the objective but the Algorthm 1 looks muh
better then the Algorithm 2, but how we can say that Algorithm 1 is much
better than Algotihm 2. For this we need to analyse the algorithms.

1.6 Algorithm Analysis


To analyse an algorithm means determining the amount of resource(such as
time and space) need to execute it.
Tim Complexity: Time complexity of an algorithm is basically the running
time as a function of input size
Algorithm 1.1 No.of times of execution

Step1:-Read N--------------------------1
Step2:-Initialize S=0------------------1
Step3:-for i=1 to n--------------------n+1(including false condition)
do
s=s+i------------------------n
end loop
Step4:-Print s-------------------------1
Step5.Exit-----------------------------1
___________
2n+5

Algorithm 1.2 No.of times of execution

Step1:-Read n--------------------------1
Step2:-Initialize S=0------------------1
Step3:-s=n*(n+1)/2---------------------1
Step4:-Print s-------------------------1
Step5.Exit-----------------------------1
___________
5

if we represent it as a function then:

Algorithm 1 → f(n)=2n+5
Algorithm 2 → f(n)=constant
Lets analyse another algorithm “Sum of array elements”
Algorithm 2.1 No.of times of execution

Step1: Initialize s=0-----------------1


Step2: for i= 0 to n-1---------------n+1
do
s=s+a[i]-------------------n
end loop
Step3:Exit---------------------------1
___________
f1(n)=2n+3

Algorithm 2.2 No.of times of execution

Step1:Initialize s=0----------------1
Step2: for i= 0 to n-1-------------n+1
do
s=s+a[i]-----------------n
end loop
Step3: Display s-------------------1
Step4:Exit-------------------------1
___________
f2(n)=2n+4

The f1(n) and f2(n) have two different value but the growth rate of these two
functions are same.

How to prove that these two function f1(n) and f2(n) are same. For this thing
we need Asymptotic Notation.

Asymptotic Notation:
We are usually interesting in the order of growth of the running time of
an algorithm, not in the exact running time. This is also referred to as the
asymptotic running time.

Big-oh(O) Notation:-

f(n)=O(g(n)) iff ∃ two +ve constants c and n0 such that f(n)≤ c(g(n)) ∀n ≥ n0
f(n)=5n+5
5n+5 ≤ 5n+n
5n+5 ≤ 6n(c=6,g(n)=n)

Let n=1, 5X1+56X1


n=2, 5X2+56X2
n=3, 5X3+56X3
n=4, 5X4+56X4
n=5, 5X5+5≤6X5

So n0 =5, c=6

With c=6 at n0=5 f(n)≤c(g(n))

So f(n)=O(n)

Algorithm 2.1 Time Complexity

f1(n)=2n+3
2n+3≤2n+n
2n+3≤3n

Let, n=1, 2X1+33X1


n=2, 2X2+33X2
n=3, 2X3+3≤3X3
So n0 =3, c=3
With c=3 at n0 =3 f1(n)≤c(g(n))
So f1(n)=O(n)

Algorithm 2.2 Time Complexity


f2(n)=2n+4
2n+4leq2n+n
2n+4≤3n
Let, n=1, 2X1+43X1
n=2, 2X2+43X2
n=3, 2X3+43X3
n=4, 2X4+4≤3X4
So n0 =4,c=3
With c=3 at n0 =4 f2(n)≤c(g(n))
So f2(n)=O(n)

So the Algorithm 2.1 f1(n)=O(n)


Algorithm 2.2 f2(n)=O(n)

So Asympototically these two algorithm are same because the growth rate of
these two function are same.

1.7 Looping Analysis


In Looping analysis we will discuss asympototic calculation of variety of
loop.

1.7.1 Linear Loops


To calculate the efficiency of an algorithm that has single loop, we ned to first
determine the number of times the statment in the loop will be executed.This
is because the number of iteration is directly proportionate to the loop factor.
Higher the loop factor, more is th number of iteration.

Ex-for(i=0;i<n;i++)
statement block;

if n=100 then the statement block will execute 100 times as it is directly
proportional to number of iteration.

So f(n)=O(n)

1.7.2 Logarithmic loops


In logarithmic loop the loop control variable is either multiplied or divided
during each iteration of the loop.

Ex-for(i=0;i<n;i=i*2)
statement block;

for(i=n;i>0;i=i/2)
statement block;

Consider the first for loop in which the loop control variable i is multiplied
by 2. For n=100 after each iteration of the loop, the loop will be xcuted 10
times and not 100 times. Therefore putting the analysis in general terms,
efficiency of the loop in which iterations divide/multiply the loop control
variable can be given as:-

So f(n)=O(log n)

1.7.3 Quadratic Loop


In a quadratic loop, the number of iteration in the inner loop is equal to
number of iteration in the outer loop. Consider the blow given code.

Ex-for(i=0;i<n;i++)
for(j=0;j<n;j++)
statement block;

For n=10 the outer loop will execute 10 times, and for each iteration of outer
loop the inner loop will excute 10 times.

So f(n)=O(n2 )

1.7.4 Linear Logarithmic Loop


Consider the following code below in which loop controlling variable of the
inner loop is multiplied after each iteration.

Ex-for(i=0;i<n;i++)
for(j=0;j<n;j=j*2)
statement block;

The number of iteration in the inner loop is log n. The inner loop is controlled
by outer loop which iterates n times.
So f(n)=O(n log n)

1.7.5 Depndent Quadratic Loop


In a dependent quadratic loop the nunber of iteration in the inner loop is
dependent on the outer loop. Consider the code given below.

Ex-for(i=0;i<n;i++)
for(j=0;j<=i;j++)
statement block;

In the above code the inner loop will execute just once in the first iteration,
twice in the second iteration, thrice in the second iteration, and so on.For
n=10 the number of iteration can be calculated as:-

1 + 2 + 3 + 4 + 5 + . . . + 9 + 10 = 55

If we calculate the average of this loop(55/10=5.5), we will observe that it


is equal to the number of iteration in the outr loop(n)+1 divided by 2. In
general terms, the inner loop iterates (n+1)/2 times.

So f(n)=n ( n + 1)/2

The function can be expanded as 1/2n2 +1/2n

Step1: Set the coefficient of each term to 1, so now we have n2 + n.


Step2: Keep the largest term and discard the rest, so discard n and the Big-oh
notation can be given as:-

f(n)=O(n2 )

1.8 Growth-rate Functions


- O(1):-Constant time.
- O(log n):-Logarithmic time.
- O(n):-Linear time.
- O(n2 ):-Quadratic time.
- O(nk ):-Polynomial time.
- O(2n ):-Exponential time.

A comparision of growth-rate functions


So the order of the growth of common functions is:
O(1) < O(log n) < O(n) < O(n log n) < O(n2 ) < O(n3 ) < O(2n )
1.9 Exercise
1. For the below given code find the running time complexity(Big-Oh
Notation).

[a] sum=0;
for(i=0;i<n;i++)
sum++;

[b] sum=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sum++;

[c] sum=0;
for(i=0;i<n;i++)
for(j=0;j<i;j++)
sum++;

[d] sum=0;
for(i=0;i<n;i++)
for(j=0;j<i*i;j++)
for(k=0;k<j;k++)
sum++;

[e] sum=0;
for(i=1;i<n;i++)
for(j=1;j<i*i;j++)
if(j%i==0)
for(k=0;k<j;k++)
sum++;

2. For the given f(n) find Big-oh(O) Notation.


[a]f(n)=45
[b]f(n)=n2 +n
[c]f(n)=2n3 + 3n2 + 16
[d]f(n)=13n + 40

3. Arrange the following growth rates in the increasing order.


O(n3 ), O(1), O(n2 ), O(n log n), O(n2 log n), O(2n )

You might also like