Fundamentals of Data Structures: Technical
Fundamentals of Data Structures: Technical
Fundamentals of
Data Structures
(For IN SEM Exam - 30 Marks)
® ®
TECHNICAL
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge
(i)
Fundamentals of Data Structures
(For IN SEM Exam - 30 Marks)
Published by :
® ®
Amit Residency, Office No.1, 412, Shaniwar Peth,
TECHNICAL Pune - 411030, M.S. INDIA, Ph.: +91-020-24495496/97
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge Email : [email protected] Website : www.technicalpublications.org
Printer :
Yogiraj Printers & Binders
Sr.No. 10/1A,
Ghule Industrial Estate, Nanded Village Road,
Tal. - Haveli, Dist. - Pune - 411041.
ISBN 978-93-90041-85-5
The book uses plain, lucid language to explain fundamentals of this subject. The
book provides logical method of explaining various complicated concepts and stepwise
methods to explain the important topics. Each chapter is well supported with necessary
illustrations, practical examples and solved problems. All the chapters in the book are
arranged in a proper sequence that permits each topic to build upon earlier studies. All
care has been taken to make students comfortable in understanding the basic concepts
of the subject.
Representative questions have been added at the end of each section to help the
students in picking important points from that section.
The book not only covers the entire scope of the subject but explains the philosophy
of the subject. This makes the understanding of this subject more clear and makes it
more interesting. The book will be very useful not only to the students but also to the
subject teachers. The students have to omit nothing and possibly have to cover nothing
more.
We wish to express our profound thanks to all those who helped in making this
book a reality. Much needed moral support and encouragement is provided on
numerous occasions by our whole family. We wish to thank the Publisher and the
entire team of Technical Publications who have taken immense pain to get this book
in time with quality printing.
Any suggestion for the improvement of the book will be acknowledged and well
appreciated.
Authors
A. A. Puntambekar
Dr. Priya Jeevan Pise
Dr. Prashant S. Dhotre
Dedicated to God.
(iii)
Syllabus
Fundamentals of Data Structures - (210242)
Credit Scheme Examination Scheme and Marks
03 Mid_Semester (TH) : 30 Marks
(v)
1.10.1 Space Complexity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 19
1.10.2 Time Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 20
1.11 Asymptotic Notations .............................................................................1 - 21
1.11.1 Big oh Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 21
1.11.2 Omega Notation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 23
1.11.3 Q Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 24
1.11.4 Properties of Order of Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 25
1.11.5 How to Choose the Best Algorithm ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 26
1.12 Analysis of Programming Constructs ......................................................1 - 28
1.13 Recurrence Relation................................................................................1 - 32
1.14 Solving Recurrence Relation ...................................................................1 - 32
1.14.1 Substitution Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 32
1.14.2 Master's Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 40
1.15 Best, Worst and Average Case Analysis ..................................................1 - 45
1.16 Introduction to Algorithm Design Strategies ..........................................1 - 47
1.16.1 Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 48
1.16.1.1 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 48
1.16.2 Greedy Strategy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 - 54
1.16.2.1 Example of Greedy Method . . . . . . . . . . . . . . . . . . . 1 - 55
Unit - II
(vi)
2.7 Multidimensional Arrays...........................................................................2 - 15
2.7.1 Two Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 15
2.7.2 Three Dimensional Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 21
2.8 Concept of Ordered List ............................................................................2 - 23
2.8.1 Ordered List Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 24
2.8.2 Built in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 26
2.8.3 List Comprehension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 27
2.9 Single Variable Polynomial........................................................................2 - 28
2.9.1 Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 28
2.9.2 Polynomial Addition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 28
2.9.3 Polynomial Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 37
2.9.4 Polynomial Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 38
2.10 Sparse Matrix ..........................................................................................2 - 42
2.10.1 Sparse Matrix Representation using Array . . . . . . . . . . . . . . . . . . . . . . . . 2 - 43
2.10.2 Sparse Matrix Addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 45
2.10.3 Transpose of Sparse Matrix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 51
2.10.4 Fast Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 - 56
2.11 Time and Space Tradeoff ........................................................................2 - 64
(vii)
(viii)
Unit - I
Introduction to Algorithm
1 and Data Structures
Syllabus
Introduction : From Problem to Program (Problem, Solution, Algorithm, Data Structure and
Program). 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.
Contents
1.1 From Problem to Data Structure (Problem, Logic, Algorithm, and Data Structure)
1.2 Data Structures : Data, Information, Knowledge, and Data Structure
1.3 Abstract Data Types (ADT). . . . . . . . . . . . . . . Dec.-10, 11,
. . . . . . . . . . . . . . . . . May-10, 11, 12, 13, 19, · · · · · · Marks 6
1.4 Data Structure Classification . . . . . . . . . . . . . May-17, Dec.-18, 19, · · · · · · · · Marks 4
1.5 Problem Solving . . . . . . . . . . . . . . . . . Dec.-09, 10, May-10, 11, · · · · Marks 12
1.6 Difficulties in Problem Solving . . . . . . . . . . . . Dec.-10, May-16, · · · · · · · · · · · Marks 4
1.7 Introduction to Algorithm. . . . . . . . . . . . . . . . . Dec.-16, May-18, 19, · · · · · · · · Marks 4
1.8 Algorithm Design Tools . . . . . . . . . . . . . . . . . May-10, 11, Dec.-10, · · · · · · · · Marks 8
1.9 Step Count Method
1.10 Complexity of Algorithm . . . . . . . . . . . . . . . . . Dec.-09, 10, 11, 19,
. . . . . . . . . . . . . . . . . May-12, 13, · · · · · · · · · · · · · · · Marks 8
1.11 Asymptotic Notations . . . . . . . . . . . . . . . . . Dec.-09, 10, 11, 16, 17, 19,
. . . . . . . . . . . . . . . . . May-10, 12, 13, 18, 19, · · · · · · Marks 8
1.12 Analysis of Programming Constructs . . . . . . . May-10, 13, 14, Dec.-11, 13, · · Marks 8
1.13 Recurrence Relation . . . . . . . . . . . . . . . . . Dec.-18, · · · · · · · · · · · · · · · · · · Marks 2
1.14 Solving Recurrence Relation
1.15 Best, Worst and Average Case Analysis
1.16 Introduction to Algorithm Design Strategies . . May-17, 18, Dec.-17, 19, · · · · · Marks 6
(1 - 1)
Fundamentals of Data Structures 1-2 Introduction to Algorithm and Data Structures
0 1 2 3 4
10 20 30 40 50
Any element in above array can be referred by an index. For instance the element 40
can be accessed as array[3].
AbstractDataType Set {
Instances : Set is a collection of integer type of elements.
Preconditions : none
Operations :
1. Store ( ) : This operation is for storing the integer element in a set.
2. Retrieve ( ) : This operation is for retrieving the desired element from the given
set.
3. Display ( ) : This operation is for displaying the contents of set.
}
There is a specific method using which an ADT can be written. We begin with
keyword AbstractDataType which is then followed by name of the data structure for
which we want to write an ADT. In above given example we have taken Set data
structure.
· Then inside a pair of curly brackets ADT must be written.
· We must first write instances in which the basic idea about the corresponding
data structure must be given. Generally in this section definition of corresponding
data structure is given.
· Using Preconditions or Postconditions we can mention specific conditions that
must be satisfied before or after execution of corresponding function.
· Then a listing of all the required operations must be given. In this section, we
must specify the purpose of the function. We can also specify the data types of
these functions.
Review Questions
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1-5 Introduction to Algorithm and Data Structures
3. Explain following terminologies : i) Data type ii) Data structure iii) Abstract data type.
SPPU : May-13, Marks 6
The data structures can be divided into two basic types primitive data structure and
non primitive data structure. The Fig. 1.4.1. shows various types of data structures.
Data structure
The dynamic data structure is a data structure in which one can allocate the memory
as per his requirement. If one does not want to use some block of memory, he can
deallocate it.
For example (Dynamic Data Structure (Dynamic)) :
Linked list, the linked list is a collection of many nodes. Each node consists of data
and pointer to next node. In linked list user can create as much nodes (memory) as he
wants, he can dellocate the memory which cannot be utilized further.
The advantage of dynamic data structure over the static data structure is that there
is no wastage of memory.
30
State 1 : 20 20
10 10 10
stack stack stack
(a) (b) (c)
30
we have pushed 10, 20 and 30 in the
State 2 : 20 stack
10
stack
The ephemeral data structures are the data structures in which we cannot retain its
previous state.
For example Queues
State 1 : In queues elements are inserted by one end and deleted by other end. Let us
insert 10, 20, 30 in the queue.
10 10 20 10 20 30
State 2 : Now if we delete the element, we can delete it only by other end. That means
10 can be deleted. And queue will be -
20 30
Now this state is not matching with any of the previous states. That means we can
not retain the previous state of the data structure even after performing certain
operations. Such a data structure is called ephemeral data structure.
Review Questions
1. Differentiate between linear and non linear data structure with example.
SPPU : May-17, Marks 3
2. Explain static and dynamic data structures with examples SPPU : Dec.-18, Marks 4
3. Write short note on Linear and Non-Linear data structure with example
SPPU : Dec.-19, Marks 4
Problem solving is based on the decisions that are taken. Following are the six steps
of problem solving -
1. Identify the problem : Identifying the problem is the first step in solving the
problem. Problem identification is very essential before solving any problem.
2. Understand the problem : Before solving any problem it is important to
understand it. There are three aspects based on which the problem can be
understood.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1-8 Introduction to Algorithm and Data Structures
Step 2 : Understand the problem : Here the age of the person must be known so that
the charge for the ticket can be calculated.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1-9 Introduction to Algorithm and Data Structures
Step 4 : Select the best way to solve the problem : The only way to solve this problem
is to calculate ticket charge based on the age of the person.
Review Questions
1. State a reason why each of the six problem solving steps is important in developing the best
solution for a problem. Give one reason for each step. SPPU : May-10, Marks 8
2. Consider any one problem and solve that problem using six steps of problem solving. Explain
each step in detail. SPPU : Dec.-10, Marks 8
3. Describe the six steps in problem solving with example. SPPU : May-11, Marks 8
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 10 Introduction to Algorithm and Data Structures
· When solving the problems on computer then writing the instructions for the
computer is most crucial task. With lack of knowledgebase one can not write the
proper instruction set for the computers.
Review Questions
1. State and explain any four difficulties with problem solving SPPU : Dec.-10, Marks 4
2. What are the difficulties in problem solving ? Explain any four steps in problem solving with
suitable example. SPPU : May-16, Marks 4
Example : Let us take a very simple example of an algorithm which adds the two
numbers and store the result in a third variable.
Step 1 : Start.
Step 2 : Read the first number is variable 'a'
Step 3 : Read the second number in variable 'b'
Step 4 : Perform the addition of both the numbers i.e. and store the result in variable 'c'.
Step 5 : Print the value of 'c' as a result of addition.
Step 6 : Stop.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 11 Introduction to Algorithm and Data Structures
Review Questions
1. Define algorithm and its characteristics. SPPU : Dec.-16, Marks 4, May-19, Marks 2
2. Define and explain following terms – (i) Data (ii) Data structure (iii) Algorithm.
SPPU : May-18, Marks 3
Algorithm
Flow chart
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 12 Introduction to Algorithm and Data Structures
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 13 Introduction to Algorithm and Data Structures
:
:
statement n
}
While the condition is true the block enclosed with { } gets executed otherwise
statement after } will be executed.
13. The general form for writing for loop is :
for variable ¬ value1 to valuen do
{
statement 1
statement 2
:
:
statement n
}
Here value1 is initialization condition and valuen is a terminating condition.
Sometime a keyword step is used to denote increament or decreament the value of
variable for example
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 14 Introduction to Algorithm and Data Structures
Function sum(a,b:integer):integer
{
//body of function
//return statement
}
Procedure sum(a,b:integer):integer
{
//body of procedure
}
The difference between function sub-algorithm and procedure sub-algorithm is that,
the function can return only one value where as the procedure can return more than one
value.
Solution :
Solution :
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 15 Introduction to Algorithm and Data Structures
Solution :
Solution :
Solution :
Algorithm Mul(A,B,n)
//Problem Description:This algorithm is for computing
//multiplication of two matrices
//Input:The two matrices A,B and order of them as n
//Output:The multiplication result will be in matrix C
for i ¬ 1 to n do
for j ¬ 1 to n do
C[i,j] ¬ 0
for k ¬ 1 to n do
C[i,j] ¬ C[i,j]+A[i,k]B[k,j]
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 16 Introduction to Algorithm and Data Structures
1.8.2 Flowchart
I/O
Decision
Process Module
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 17 Introduction to Algorithm and Data Structures
Example 1.8.6 What do you mean by flow chart ? Give the meaning of each symbol used in
flowchart. Draw flowchart to compute the sum of elements from a given integer array
SPPU : May-10, Marks 8
Solution : Refer section 1.8.2 for flowchart and symbols used in it.
Start
Read array
elements
Sum = 0
i=
1 1 n
Display Sum
Stop
Fig. 1.8.2
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 18 Introduction to Algorithm and Data Structures
Example 1.8.7 Design and explain an algorithm to find the sum of the digits of an integer
number. SPPU : Dec.-10, Marks 6
Solution :
Read N
Remainder=0
Sum=0
Repeat
Remainder=N mod 10
Sum=Sum+remainder
N=N/10
Until N<0
Display Sum
End
Example 1.8.8 What is a difference between flowchart and algorithm ? Convert the algorithm
for computing factorial of given number into flowchart. SPPU : May-11, Marks 8
Solution : Algorithm is a set of instructions written in natural language or pseudo code
and flowchart is a graphical representation of an algorithm.
Read N
Set i and F to 1
While i<=N
F=F * i
Increase the value of i by 1
Display F
End
Start
Read the
number 'n'
fact = 1
i=
1 1 n
fact = fact i
Stop
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 19 Introduction to Algorithm and Data Structures
1.10 Complexity of Algorithm SPPU : Dec.-09, 10, 11, 19, May-12, 13, Marks 8
S(p) = C + Sp
where C is a constant i.e. fixed part and it denotes the space of inputs and outputs.
This space is an amount of space taken by instruction, variables and identifiers. And Sp
is a space dependent upon instance characteristics. This is a variable part whose space
requirement depends on particular problem instance.
There are two types of components that contribute to the space complexity - Fixed
part and variable part.
The fixed part includes space for
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 20 Introduction to Algorithm and Data Structures
Example 1.10.1 Compute the space needed by the following algorithms justify your answer.
Algorithm Sum(a,n)
{
s : = 0.0 ;
For i : = 1 to n do
s : = s + a[i] ;
return s
}
In the given code we require space for
s:=0 ¬ O(1)
For i : = 1 to n ¬ O(n)
s : = s + a[i] ; ¬ O(n)
returns ; ¬ O(1)
Hence the space complexity of given algorithm can be denoted in terms of big-oh
notation. It is O(n). We will discuss big oh notation concept in section 1.11.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 21 Introduction to Algorithm and Data Structures
2. What do you mean by frequency count and its importance in analysis of an algorithm
SPPU : Dec.-09,10, May-12,13, Marks 6
To choose the best algorithm, we need to check efficiency of each algorithm. The
efficiency can be measured by computing time complexity of each algorithm. Asymptotic
notation is a shorthand way to represent the time complexity.
Using asymptotic notations we can give time complexity as “fastest possible”,
“slowest possible” or “average time”.
Various notations such as W, Q and O used are called asymptotic notations.
f(n) £ c*g(n)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 22 Introduction to Algorithm and Data Structures
then f(n) is big oh of g(n). It is also denoted as f(n) Î O (g(n)). In other words f(n) is
less than g(n) if g(n) is multiple of some constant c.
c * g(n)
f(n)
n0 n
f(n) Î O(g(n))
Fig. 1.11.1 Big oh notation
= (1) 2
g(n) = 1
i.e. f(n) > g(n)
If n = 2 then,
f(n) = 2(2) + 2
= 6
g(n) = ( 2) 2
g(n) = 4
i.e. f(n) > g(n)
If n = 3 then,
f(n) = 2(3) + 2
= 8
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 23 Introduction to Algorithm and Data Structures
g(n) = ( 3) 2
g(n) = 9
i.e. f(n) < g(n) is true.
Hence we can conclude that for n > 2, we obtain
f(n) < g(n)
Definition
A function f(n) is said to f(n)
be in W (g(n)) if f(n) is
bounded below by some c * g(n)
positive constant multiple of
g(n) such that
f(n) ³ c * g(n)
For all n ³ n 0
It is denoted as f(n) Î W
(g(n)). Following graph n0 n
illustrates the curve for W
notation. Fig. 1.11.2 Omega notation f(n) Î W (g(n))
Example :
Consider f(n) = 2n 2 + 5 and g(n) = 7n
Then if n = 0
f(n) = 2 ( 0) 2 + 5
= 5
g(n) = 7 (0)
= 0 i.e. f(n) > g(n)
But if n = 1
f(n) = 2 (1) 2 + 5
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 24 Introduction to Algorithm and Data Structures
= 7
g(n) = 7(1)
7 i.e. f(n) = g(n)
If n = 3 then,
f(n) = 2 ( 3) 2 + 5
= 18 + 5
= 23
g(n) = 7(3)
= 21
i.e. f(n) > g(n)
2n 2 + 5 Î W (n)
Similarly any
n 3 Î W (n) 2
1.11.3 Q Notation
The theta notation is denoted by Q. By this method the running time is between
upper bound and lower bound.
Definition
Let f(n) and g(n) be two
c2 * g(n)
non negative functions. There f(n)
are two positive constants
namely c 1 and c 2 such that
c1 * g(n)
c 1 £ g(n) £ c 2 g(n)
where n ³ 2
Fig. 1.11.3 Theta notation f(n) Î Q (g(n))
Similarly f(n) = 2n + 8
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 25 Introduction to Algorithm and Data Structures
g(n) = 7n
i.e. 5n < 2n + 8 < 7n For n ³ 2
Here c1 = 5 and c 2 = 7 with n 0 = 2.
The theta notation is more precise with both big oh and omega notation.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 26 Introduction to Algorithm and Data Structures
2. Polynomials of degree m Î Q (n m ) .
( )
second of O n 2 , then we will usually 2 1 2 4 8 4
prefer the first one. 4 2 8 16 64 16
The reason for this is that as n 8 3 24 64 512 256
increases the time required for the
16 4 64 256 4096 65536
execution of second algorithm will get
far more than the time required for 32 5 160 1024 32768 2147483648
the execution of first.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 27 Introduction to Algorithm and Data Structures
We will study various values for computing function for the constant values. The
graph given below will indicate the rate of growth of common computing time
functions.
Notice how the times O ( n) and O (n log n) grow much more slowly than the others.
For large data sets algorithms with a complexity greater than O (n log n) are often
impractical. The very slow algorithm will be the one having time complexity 2n .
n
2 3
65536 n
32768
16384
2
n
8192
4096
2048
1024
n log2 n
512
256
n
128
64
32
16 log2 n
1 2 4 8 16 32 64 128
Review Questions
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 28 Introduction to Algorithm and Data Structures
3. Explain asymptotic notations Big O, Theta and Omega with one example each.
SPPU : Dec.-16, 17, May-18, Marks 6, Dec.-19, Marks 3
Example 1.12.1 Obtain the frequency count for the following code
Solution :
void fun()
{
int a;
a=0; ………………………………………….1
for(i=0;i<n;i++) …………………………….n+1
{
a = a+i; …………………………………n
}
printf("%d",a); ……………………………….1
}
The frequency count of above code is 2n+3
The for loop in above given fragment of code is executed n times when the condition
is true and one more time when the condition becomes false. Hence for the for loop the
frequency count is n+1. The statement inside the for loop will be executed only when
the condition inside the for loop is true. Therefore this statement will be executed for n
times. The last printf statement will be executed for once.
Example 1.12.2 Obtain the frequency count for the following code
Solution :
void fun(int a[][],int b[][])
{
int c[3][3];
for(i=0;i<m;i++) .....................................m+1
{
for(j=0;j<n;j++) ...................................m(n+1)
{
c[i][j]=a[i][j]+b[i][j]; ...........................m.n
}
}
The frequency count =(m+1)+m(n+1)+mn=2m+2mn+1=2m(1+n)+1
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 29 Introduction to Algorithm and Data Structures
Example 1.12.3 Obtain the frequency count for the following code
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(k=1;k<=n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
} SPPU : May-13, Marks 8; Dec.-13, Marks 3
Solution : After counting the frequency
count, the constant terms can be Statement Frequency Count
neglected and only the order of
for(i=1;i<=n;i++) n+1
magnitude is considered. The time
complexity is denoted in terms of for(j=1;j<=n;j++) n.(n+1)
algorithmic notations. The Big oh
c[i][j]=0; n.(n)
notation is a most commonly used
algorithmic notation. For the above for(k=1;k<=n;k++) n.n(n+1)
frequency count all the constant terms c[i][j]=c[i][j]+a[i][k]*b[k][j n.n.n
are neglected and only the order of ];
magnitude of the polynomial is 3 2
Total 2n +3n +2n+1
considered. Hence the time complexity
for the above code can be O(n3). The
higher order is the polynomial is always considered.
Example 1.12.4 Obtain the frequency count for the following code i=1;
do
{
a++;
if(i==5)
break;
i++;
}while(i<=n)
Solution :
Statement Frequency Count
i=1; 1
a++; 5
if(i==5) 5
break; 1
i++; 5
while(i<=n) 5
Total 22
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 30 Introduction to Algorithm and Data Structures
Example 1.12.5 Obtain the frequency count for the following code
m=n/2;
for(i=0;i+m<m;i++)
{
a++;
k++;
}
Solution :
If we consider only the degree of the polynomial for this code then the time
complexity in terms of Big-oh notation can be specified as O(n/2).
Example 1.12.6 Find the frequency count (F.C.) of the given code :
Explain each step.
double IterPow(double X, int N)
{
double Result = 1;
while (N > 0)
{
Result = Result *X;
N--;
}
return Result;
} SPPU : May-10, Marks 6
Solution :
double Result=1 1
while(N>0) N+1
Result=Result*X N
N-- N
return Result 1
Total 3N+3
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 31 Introduction to Algorithm and Data Structures
Example 1.12.7 What is frequency count of a statement ? Analyze time complexity of the
following code :
i) for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
for(k = 1; k <= p; k++)
sum = sum + i;
ii) i = n;
while(i ³ 1)
{i– –;} SPPU : Dec.-11, Marks 6, May-14, Marks 3
Solution : i) ii)
Example 1.12.8 Determine the frequency counts for all the statements in the following
program segment
i=10;
for(i=10;i<=n;i++)
for(j=1;j<i;j++)
x=x+1; SPPU : May-13, Marks 6
Solution : Assume (n–10+2)=m.
Then the total frequency count is - Statement Frequency Count
The outer loop will execute for i=10; 1
m times. The inner loop is
for(i=10;i<=n;i++) m (we assume the count of
dependant upon the outer loop.
execution is m)
Hence it will be (m+1)/2. Hence
overall frequency count is for(j=1;j<i;j++) m((m+1)/2)
(1+m+m )(m(m+1)/2)
2
x=x+1; m(m)
If we consider only the order of
magnitude of this code then overall time complexity in terms of big oh notation is O(n2).
If an algorithm takes time O(logn) then it is faster than any other algorithm, for
larger value of n. Similarly O(nlogn) is better than O(n2). Various computing time can be
O(1), O(logn), O(n),O(nlogn), O(n2), O(n3) and O(2n).
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 32 Introduction to Algorithm and Data Structures
· Definition :
The recurrence equation is an equation that defines a sequences resursively. It is
normally in following form :
Recurrence relation
T(0) = 0 (2)
Initial condition
Review Question
For example -
Consider a recurrence relation
T(n) = T(n – 1) + n
If n = 1 then
T(1) = T(0) + 1
= 0+1 Q Initial condition
\ T(1) = 1 … (1.14.2)
If n = 2, then
T(2) = T(1) + 2
= 1+2 Q equation (1.14.2)
\ T(2) = 3 … (1.14.3)
If n = 3 then
T(3) = T(2) + 3
= 3+3 Q equation (1.14.4)
\ T(3) = 6 … (1.14.5)
But, in practice, it is difficult to guess the pattern from forward substitution. Hence
this method is not very often used.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 34 Introduction to Algorithm and Data Structures
Let
T(n – 2) = T (n – 2 – 1) + (n – 2) … (1.14.9)
Putting equation (1.14.9) in equation (1.14.8) we get.
T(n) = T(n – 3) + (n – 2) + (n – 1) + n
M
= T(n – k) + (n – k + 1) + (n – k + 2) + … + n
if k = n then
T(n) = T(0) + 1 + 2 + … n
T(n) = 0 + 1 + 2 + … + n Q T(0) = 0
n(n + 1) n2 n
T(n) = = +
2 2 2
T(n) = T(n – 1) + 1
By backward substitution,
T(n – 1) = T(n – 2) + 1
\ T(n) = T ( n – 1) + 1
= (T(n – 2) + 1) + 1
T(n) = T(n – 2) + 2
Again T(n – 2) = T(n – 2 – 1) + 1
= T(n – 3) + 1
\ T(n) = T(n – 2) + 2
= (T(n – 3) + 1) + 2
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 35 Introduction to Algorithm and Data Structures
T(n) = T(n – 3) + 3
M
T(n) = T(n – k) + k … (1)
é æ n ö n2 ù
= 4 ê4 × T ç ÷ + + n2
è 2ø 2ú
ë 3 3 û
æ n ö n2
= 4 2T ç ÷ + 4 × + n2
è 32 ø 32
æ n ö 13n 2
= 4 2T ç ÷ +
è 32 ø 32
2
n 13n 2 n n n
= 16 × T æç ö÷ + Q T æç ö÷ = 4T æç ö÷ + æç ö÷
è 9ø 9 è 9ø è 27 ø è 9 ø
é n n 2 ù 13n 2 n n n2
= 16 ê 4 × T æç ö÷ + + Q T æç ö÷ = 4T æç ö÷ +
è 27 ø 81 ú 9 è 9ø è 27 ø 81
ë û
n n 2 13n 2
= 64T æç ö÷ + 16 × +
è 27 ø 81 9
n 133n 2
= 64T æç ö÷ +
è 27 ø 81
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 36 Introduction to Algorithm and Data Structures
2
æ n ö æ n ö
= 4 3T ç ÷ + 133 ×ç ÷
è 33 ø è 32 ø
M
2
æ n ö æ n ö
= 4 kT ç ÷ + C ç k- 1 ÷ Q 133 + C 1 = C
è 3k ø è3 ø
n
Now if we assume = 1 then n = 3 k and k = log 3 n
3k
2
æ n ö n
= 4 k T(1) + C ç ÷ Q = 1
è 3k - 1 ø 3k
C
= 4 k + C1 × n 2 Q C1 =
k- 1 2
(3 )
= 4 log 3 n + C 1 n 2
= n 1.26 + C × n 2
T(n) » Q (n 2 )
n
T(n) = 2T æç ö÷ + C
è 2ø
æ n ö
= 2 ç 2T æç ö÷ + C÷ + C
è è 4 ø ø
n
= 4T æç ö÷ + 3C
è4ø
æ n ö
= 4 ç 2T æç ö÷ + C÷ + 3C
è è 8ø ø
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 37 Introduction to Algorithm and Data Structures
n
= 8T æç ö÷ + 7C
è 8ø
æ n ö
= 2 3Tç 3
÷ + (2 - 1) C
èn3ø
M
æ n ö
T(n) = 2 k T ç ÷ + (2 k - 1) C
è 2k ø
If we 2 k = n then
n
T(n) = nT æç ö÷ + (n - 1) C
è nø
= nT(1) + (n – 1) C
T(n) = n + (n – 1) C Q T(1) = 1
ii) Let,
n
T(n) = T æç ö÷ + C
è 3ø
æ n ö
= ç T æç ö÷ + C÷ + C
è è 9ø ø
n
= T æç ö÷ + 2C
è 9ø
é n ù
= êT æç ö÷ + Cú + 2C
ë è 27 ø û
n
= T æç ö÷ + 3C
è 27 ø
M
æ n ö
= T ç ÷ + kC
è 3k ø
If we put 3 k = n then
n
= T æç ö÷ + log 3 n × C
è nø
= T (1) + log 3 n × C
T(n) = C × log 3 n + 1 Q T(1) = 1
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 38 Introduction to Algorithm and Data Structures
Solution : Let
T(n) = T(n – 1) + n 4
= T(n - 2) + (n - 1) 4 + n 4
= [T(n - 3) + (n - 2) 4 ] + (n - 1) 4 + n 4
= T(n - 3) + (n - 2) 4 + (n - 1) 4 + n 4
= [T(n - 4) + (n - 3) 4 ] + (n - 2) 4 + (n - 1) 4 + n 4
= T(n - 4) + (n - 3) 4 + (n - 2) 4 + (n - 1) 4 + n 4
= T(n - k) + (n - k + 1) 4 + (n - k + 2) 4 + (n - k + 3) 4 + K n 4
If k = n then
= T(n - n) + (n - n + 1) 4 (n - n + 2) 4 + (n - n + 3) 4 + L + n 4
= T(0) + 1 4 + 2 4 + 3 4 + K + n 4
n
= T(0) + å n4
i= 1
n n
= T(0) + n(n 4 ) Q å n4 = n4 × å 1 = n4 ×n
i= 1 i= 1
\ T(n) » q(n 5 )
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 39 Introduction to Algorithm and Data Structures
Solution : In the above code the following statement gets executed at least twice
count = count + 1
In the else part, there is a recursive call to the function sum, by changing the value
of n by n – 1, each time.
Hence the recursive formula for above code will be
T(n) = T(n – 1) + 2
For count
For recursive
statement
call to function
sum in else
part.
T(0) = 2
= [T(n – 2) + 2] + 2
\ = T (n – 2)+2(2)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 40 Introduction to Algorithm and Data Structures
= T(0) + 2n
= 2+2×n Q T(0) = 2
T(n) = 2(n+1)
We can denote the time complexity in the form of big oh notation as O(n).
1. T(n) = Q (n d ) if a < b d
2. T(n) = Q (n d log n) if a = b
log ba
3. T(n) = Q (n ) if a > b d
= Q (n 2 ) Q log 2 4 = 2
For quick and easy calculations of logarithmic values to base 2 following table can
be memorized.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 41 Introduction to Algorithm and Data Structures
m k
1 0
2 1
4 2
8 3
16 4
32 5
64 6
128 7
256 8
512 9
1024 10
log ba - e
1. If f(n) is O (n ), then
log ba
T(n) = Q (n )
log ba
2. If f(n) is Q (n log k n), then
log ba
T(n) = Q (n log k+ 1 n)
log ba + e
3. If f(n) is W (n ), then
T(n) is = Q (f(n))
Example 1.14.7 Solve the following recurrence relation. T(n) = 2T(n/2) + n log n
Solution :
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 42 Introduction to Algorithm and Data Structures
= Q (n 1 log 2 n)
\ T(n) = Q (n log 2 n)
Solution :
Here f(n) = n 2
a = 8 and b = 2
\ log 2 8 = 3
T(n) = Q (n 3 )
Solution :
Here a = 9, b=3 and f(n) = n 3
And log 3 9 = 2
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 43 Introduction to Algorithm and Data Structures
and log 2 1 = 0
= Q (n 0 × 1)
= Q (1) Q n0 = 1
log ba
\ We get T(n) = Q (n log k+ 1 n)
log 2 1
= Q (n log 0+ 1 n)
= Q (n 0 × log 1 n)
T(n) = Q (log n) Q n0 = 1
Example 1.14.11 Find the complexity of the following recurrence relation. T(n) = 9T(n/3) + n
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 44 Introduction to Algorithm and Data Structures
log b a
T(n) = Q (n )
= Q (n log 3 9 )
T(n) = Q (n 2 )
n
T(n) = k × T æç ö÷ + n 2 be a recurrence relation. Let us use substiturion
è kø
method
é n n2 ù
= 2 ê 2 × T æç ö÷ + ú+n
2
ë è 4 ø 4 û
n n2
= 4T æç ö÷ + + n2
è4ø 2
2
n 3n 2 n n n
= 4T æç ö÷ + T æç ö÷ = 2T æç ö÷ + æç ö÷
è4ø 2 è4ø è 8ø è 4ø
é n n 2 ù 3n 2
= 4 ê 2 × T æç ö÷ + +
ë è 8 ø 16 úû 2
n n 2 3n 2
= 8T æç ö÷ + +
è 8ø 4 2
2
n 7n
= 8T æç ö÷ +
è 8ø 4
æ n ö 2k - 1 2
= 2kT ç ÷ + n
è 2k ø 2k - 1
æ n ö 2k - 1
= 2 k T ç ÷ + Cn 2 Q = C = Constant
è 2k ø 2 k- 1
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 45 Introduction to Algorithm and Data Structures
n
If we put = 1 then 2 k = n and k = log 2 n
2k
= nT(1) + Cn 2
= n + Cn 2 Q T(1) = 1
T(n) = Q (n) 2
Alternate Method
We can solve the given recurrence relation using Master's theorem also.
n
T(n) = k × T æç ö÷ + n 2
è kø
¯ ¯ ¯
a b f(n)
log b a
By Master's theorem n = n log k k = n 1
= n1+ 1 when e = 1
= f(n) i. e. n 2
\ T(n) = Q(f(n))
T(n) = Q(n 2 )
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 46 Introduction to Algorithm and Data Structures
The algorithm guarantees that for any instance of input which is of size n, the
running time will not exceed Cworst(n). Hence the worst case time complexity gives
important information about the efficiency of algorithm.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 47 Introduction to Algorithm and Data Structures
P n (n + 1)
= + n (1 - P)
n 2
P (n + 1)
C avg (n) = + n (1 - P)
2
Thus we can obtain the general formula for computing average case time complexity.
Suppose if P = 0 that means there is no successful search i.e. we have scanned the
entire list of n elements and still we do not found the desired element in the list then in
such a situation,
C avg (n) = 0(n + 1)/2 + n (1 – 0)
C avg (n) = n
Thus the average case running time complexity becomes equal to n.
Suppose if P = 1 i.e. we get a successful search then
C avg (n) = 1 (n + 1)/2 + n (1 – 1)
C avg (n) = (n + 1)/2
That means the algorithm scans about half of the elements from the list.
For calculating average case time complexity we have to consider probability of
getting success of the operation. And any operation in the algorithm is heavily
dependent on input elements. Thus computing average case time complexity is difficult
than computing worst case and best case time complexities.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 48 Introduction to Algorithm and Data Structures
¡ Greedy Technique : To solve the problem locally optimal decisions are made.
¡ Backtracking : In this method, we start with one possible move out from many
moves out and if the solution is not possible through the selected move then we
backtrack for another move.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 49 Introduction to Algorithm and Data Structures
70 20 30 40 10 50 60
Divide Divide
70 20 30 40 10 50 60
Divide Divide
70 20 30 40 10 50 60
Divide Divide
70 20 30 40 10 50 60
Merge Merge
20 70 30 10 40 50 60
Merge Merge
20 30 70 10 40 50 60
Merge Merge
10 20 30 40 50 60 70
Fig. 1.16.2
Pseudo Code
Algorithm MergeSort(int A[0…n-1],low,high)
//Problem Description: This algorithm is for sorting the
//elements using merge sort
//Input: Array A of unsorted elements, low as beginning
//pointer of array A and high as end pointer of array A
//Output: Sorted array A[0…n-1]
if(low < high)then
{
mid ¬ low+high)/2 //split the list at mid
MergeSort(A,low,mid) //first sublist
MergeSort(A,mid+1,high) //second sublist
Combine(A,low,mid,high) //merging of two sublists
}
Algorithm Combine(A[0…n-1],low, mid, high)
{
k ¬ low; //k as index for array temp
i ¬ low; //i as index for left sublist of array A
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 50 Introduction to Algorithm and Data Structures
Logic Explanation
To understand above algorithm consider a list of elements as
70 20 30 40 10 50 60
0 1 2 3 4 5 6
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 51 Introduction to Algorithm and Data Structures
low high
0 1 2 3 4 5 6
70 20 30 40 10 50 60 Merge sort (A, low, mid)
4
1 This list is further
subdivided This list can be
subdivided
0 1 2 3
70 20 30 40
2 This list is
3 This list
subdivided can be Merge sort (A, mid+1, high)
subdivided
70 20 30 40 10 50 60
This list
7 Combine 5 can be
Combine two
subdivide
As A[j] < A[i] sublists in
copy A[j] to temp array 6 10 50 10
temp Combine two
sublists
20 70 30 40 9 Combine two
sublists
8 Combine
these two 10 50
20 30 40 70 sublists
10 50 60
11 Combine two
sublists
10 20 30 40 50 60 70
Fig. 1.16.3
Let us see the combine operation more closely with the help of some example.
Consider that at some instance we have got two sublits 20, 30, 40, 70 and 10, 50, 60,
then
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 52 Introduction to Algorithm and Data Structures
i j
if (A[i]<= A[j])
Initially k = 0. Then k will be incremented {
temp[k] A[i]
temp i i+1
10 k k+1
}
0 el else
k get s e p a r t
s ex o {
ecute f a l g o r ith m
d temp[k] A[j]
j j+1
Note that A (left sublist) A (right sublist)
k k+1
i remains }
there and j is 20 30 40 70 10 50 60
incremented
i j
i j
k = 1. It is advanced later on
if (A[i]<= A[j])
temp {
10 20 temp [k] A[i]
if part of algorithm i i+1
0 1
gets executed k k+1
k
moves ahead }
else
{
Note that Array A (left sublist) Array A (right sublist) temp [k] A[j]
j remains there j j+1
and only i is 20 30 40 70 10 50 60
k k+1
incremented
}
i j
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 53 Introduction to Algorithm and Data Structures
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 54 Introduction to Algorithm and Data Structures
Finally we will copy all the elements of array temp to array A. Thus array A
contains sorted list.
A
10 20 30 40 50 60 70
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 55 Introduction to Algorithm and Data Structures
3. From the set of feasible solutions, particular solution that satisfies or nearly
satisfies the objective of the function. Such a solution is called optimal solution.
4. As Greedy method works in stages. At each stage only one input is considered
at each time. Based on this input it is decided whether particular input gives
the optimal solution or not.
Now we will consider each vertex as a source and will find the shortest distance
from this vertex to every other remaining vertex. Let us start with vertex A.
D D-E, path = 7 + 8 = 15
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 56 Introduction to Algorithm and Data Structures
Pseudo Code
Algorithm Dijkstra(int cost[1…n,1…n],int source,int dist[])
for i ¬ 0 to tot_nodes
{
dist[i] ¬ cost[source,i]//initially put the
s[i] ¬ 0 //distance from source vertex to i
//i is varied for each vertex
path[i] ¬ source//all the sources are put in path
}
s[source] ¬ 1 Start from each source node
for(i ¬ 1 to tot_nodes)
{
min_dist ¬ infinity;
v1 ¬ -1//reset previous value of v1
for(j ¬ 0 to tot_nodes-1)
{
Finding minimum
if(s[j]=0)then distance from
{ selected source
if(dist[j]<min_dist)then node.That is :
{ source-j represents
min_dist. edge
min_dist ¬ dist[j]
v1 ¬ j
}
}
}
s[v1] ¬ 1
for(v2 ¬ 0 to tot_nodes-1)
{
if(s[v2]=0)then
{
if(dist[v1]+cost[v1][v2]<dist[v2])then
{
dist[v2] ¬ dist[v1]+cost[v1][v2]
path[v2] ¬ v1
} v1 is next selected destination
} vertex with shortest distance.
All such vertices are
} accumulated in array
} path[ ]
}
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 57 Introduction to Algorithm and Data Structures
Review Questions
1. Explain divide and conquer strategy with example. Also comment on the time analysis.
SPPU : May-17, 18, Marks 6
2. Explain the greedy strategy with suitable example. Comment on its time complexity.
SPPU : Dec.-17, Marks 6
qqq
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 1 - 58 Introduction to Algorithm and Data Structures
Notes
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Unit - II
Syllabus
Concept of Sequential Organization, Overview of Array, Array as an Abstract Data Type, Operations
on Array, Merging of two arrays, Storage Representation and their Address Calculation : Row major
and Column Major, Multidimensional Arrays : Two-dimensional arrays, n-dimensional arrays.
Concept of Ordered List, Single Variable Polynomial : Representation using arrays, Polynomial as
array of structure, Polynomial addition, Polynomial multiplication. Sparse Matrix : Sparse matrix
representation using array, Sparse matrix addition, Transpose of sparse matrix- Simple and Fast
Transpose, Time and Space tradeoff.
Contents
2.1 Concept of Sequential Organization
2.2 Array as an Abstract Data Type
2.3 Array Overview
2.4 Operations on Array
2.5 Merging of Two Arrays
2.6 Storage Representation and their Address Calculation
. . . . . . . . . . . . . . . . . Dec.-06, 09, 16, 18, · · · · · · · · · Marks 6
2.7 Multidimensional Arrays
2.8 Concept of Ordered List
2.9 Single Variable Polynomial . . . . . . . . . . . . . . . May-17, 18, · · · · · · · · · · · · · · · Marks 3
2.10 Sparse Matrix . . . . . . . . . . . . . . . . . May-17, 19, Dec.-19, · · · · · · · · Marks 6
2.11 Time and Space Tradeoff
(2 - 1)
Fundamentals of Data Structures 2-2 Linear Data Structure using Sequential Organization
For example : If we want to store names of all the students in a class we can make use
of an array to store the names in sequential form.
Syntax
data_type name_of_array [size] ;
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2-3 Linear Data Structure using Sequential Organization
1 20
Index
used to find 2 30
the element
3 40
0 10 0 10
1 20 1 20
2 30 New element 2 30
3 40 44 at position 4 3 44
4 50 4 40
Before 5 50
insertion
After
insertion
· We can implement array using Python program. Python does not support the
concept of array, but we can implement the array using List
· List is a sequence of values.
· String is also sequence of values. But in string this sequence is of characters. On
the other hand, in case of list the values can be of any type.
· The values in the list are called elements or items. These elements are separated
by commas and enclosed within the square bracket.
For example
[10,20,30,40] # list of integers
[‘aaa’,’bbb’,’ccc’] #list of strings
· The list that contains no element is called empty list. The empty list is represented
by [].
Python Program
print(“\nHow many elements are there in Array?”)
n = int(input())
array = []
i=0
for i in range(n):
print(“\n Enter element in Array”)
item = int(input())
array.append(item)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2-5 Linear Data Structure using Sequential Organization
Logic Explanation
For insertion of any element in the array, we can make use of list slicing technique.
Here is an illustration.
Suppose the array is as follows -
0 1 2 3 4
10 20 30 40 50
array
Position = 4
Element to be inserted = 44
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2-6 Linear Data Structure using Sequential Organization
0 1 2 3 4 5
10 20 30 40 44 50
array
C++ Code
#include <iostream>
using namespace std;
int main()
{
int array[100], position, i, n, value;
cout<<"\n How many elements are there in array";
cin>>n;
cout<<"Enter the elements\n";
for (i = 0; i < n; i++)
cin>>array[i];
cout<<"\n Enter the location where you wish to insert an element: ";
cin>> position;
cout<<"\n Enter the value to insert: ";
cin>>value;
for (i = n - 1; i >= position - 1; i--)//creating space by shifting the element down
array[i + 1] = array[i];
array[position - 1] = value;//at the desired space inserting the element
printf("Resultant array is\n");
for (i = 0; i <= n; i++)
cout<<"\n"<< array[i]);
return 0;
}
(2) Traversing List
· The loop is used in list for traversing purpose. The for loop is used to traverse
the list elements.
Syntax
for VARIABLE in LIST :
BODY
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2-7 Linear Data Structure using Sequential Organization
Example
>>> a=[‘a’,’b’,’c’,’d’,’e’] # List a is created
>>> for i in a:
print(i)
will result into
a
b
c
d
e
>>>
· We can traverse the list using range() function. Using range() function we can
access each element of the list using index of a list.
· If we want to increment each element of the list by one, then we must pass index
as argument to for loop. This can be done using range() function as follows -
>>> a=[10,20,30,40]
>>> for i in range(len(a)):
a[i]=a[i]+1 #incremented each number by one
>>> a
[11, 21, 31, 41]
>>>
(3) Deleting an element from array
· Deleting an element from array is complex activity because have to shift the
elements to previous position.
· That means after deleting an element size gets decremented by one.
0 10 0 10
1 20 Element at 1 20
Position 4 is
2 30 deleted 2 30
3 40 3 50
4 50
Python Program
print(“\nHow many elements are there in Array?”)
n = int(input())
array = []
i=0
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2-8 Linear Data Structure using Sequential Organization
for i in range(n):
print(“\n Enter element in Array”)
item = int(input())
array.append(item)
Logic Explanation :
Suppose array is
0 1 2 3 4
10 20 30 40 50
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2-9 Linear Data Structure using Sequential Organization
0 1 3 4
10 20 + 40 50
0 1 2 3
= 10 20 40 50
C++ Code
#include <iostream>
using namespace std;
int main()
{
int array[100], position, i, n;
cout<<"\n How many elements are there in array ";
cin>>n;
cout<<"\nEnter the elements:\n";
for (i = 0; i < n; i++)
cin>>array[i];
cout<<"\n Enter the location of the element which is to be deleted ";
cin>>position;
if (position >= n + 1)
cout<<"Element can not be deleted\n";
else
{
for (i = position - 1; i < n - 1; i++)
array[i] = array[i + 1];
cout<<"\nArray is\n";
for (i = 0; i < n - 1; i++)
cout<<"\n"<< array[i]);
}
return 0;
}
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 10 Linear Data Structure using Sequential Organization
a3
Step 2 :
a1 a2
10 30 50 70 20 40 60 80 Compare a[i] and a[j].
The lesser element is
transferred to a3.
i j
a3
10
k
As a1[i] < a2[j], transfer element a1[i]to a3[k]. Then increment i and k pointer
Step 3 :
a1 a2
10 30 50 70 20 40 60 80 As A[j] is less
than A[i] transfer
i j
it to a3
10 20
a3
a3
10 20 30
k
Increment i and k pointers. In this way we can transfer the elements from two
arrays a1 and a2 to get merged array a3.
Finally we get
10 20 30 40 50 60 70 80
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 11 Linear Data Structure using Sequential Organization
Python Program
def mergeArr(a1,a2,n,m):
a3 = [None]*(n+m)
i=0
j=0
k=0
#traverse both arrays
#if element of first array is less then store it in third array
#if element of second array is less then store it in third array
while i < n and j < m:
if a1[i] < a2[j]:
a3[k] = a1[i]
k=k+1
i=i+1
else:
a3[k] = a2[j]
k=k+1
j=j+1
#if elements of first array are remaining
#then transfer them to third array
while i < n:
a3[k] = a1[i]
k=k+1
i=i+1
a2 = []
m = int(input(“Enter total number of elements in second array:”))
for i in range(0,m):
item = int(input(“Enter the element: ”))
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 12 Linear Data Structure using Sequential Organization
a2.append(item)
mergeArr(a1,a2,n,m)
Output
The array can be represented using i) Row Major ii) Column Major Representation.
Row Major Representation
If the elements are stored in rowwise manner then it is called row major
representation.
For example : If we want to store elements
10 20 30 40 50 60 then in a two dimensional array
0 1 2
0 10 20 30
The Þ elements will
be stored 1 40 50 60
horizontally
.
To access any element in two dimensional array we must specify both its row
number and column number. That is why we need two variables which act as row
index and column index.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 13 Linear Data Structure using Sequential Organization
0 1
0 10 40
1 20 50
2 30 60
Each element is occupied at successive locations if the element is of integer type then
2 bytes of memory will be allocated, if it is of floating type then 4 bytes of memory will
be allocated and so on.
For example :
int a [3] [2] = { {10, 20 }
{30, 40}
{50, 60} }
Then in row major matrix
a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1]
10 20 30 40 50 60 …
100 102 104 106 108 110
And in column major matrix
a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1]
10 30 50 20 40 60 …
100 102 104 106 108 110
Here each element occupies 2 bytes of memory base address will be 100.
Address calculation for any element will be as follows
In row major matrix, the element a[i] [j] will be at
base address + (row_index * total number of columns + column_index)
* element_size.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 14 Linear Data Structure using Sequential Organization
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 15 Linear Data Structure using Sequential Organization
Review Questions
1. Derive address calculation formula for one dimensional array with one example.
SPPU : Dec.-16, Marks 6
2. Explain two dimensional arrays with row and column major implementation. Explain address
calculation in both cases with example SPPU : Dec.-18, Marks 6
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 16 Linear Data Structure using Sequential Organization
Solution :
# This program stores and displays the elements of two dimensional Array
row_num = int(input(“Input number of rows: ”))
col_num = int(input(“Input number of columns: ”))
arr = [[0 for col in range(col_num)] for row in range(row_num)]
print(arr)
Output
C++ Code
// This program stores and displays the elements of two dimensional Array
cout<<"\nInput number of rows: ";
cin>>row_num;
cout<<"\nInput number of columns: ";
cin>>col_num;
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 17 Linear Data Structure using Sequential Organization
int arr[row_num][col_num];
for (row=0;row<row_num;row++)
{
for (col=0;col<col_num;col++)
{
cout<<"Enter the element: ";
cin>>arr[row][col];
}
}
for (row=0;row<row_num;row++)
{
for (col=0;col<col_num;col++)
{
cout<<"\t"arr[row][col];
}
cout<<"\n";
}
(1) Addition of Two Matrices
Consider matrix A and B as follows –
1 2 3 1 1 1 2 3 4
4 5 6 2 2 2 6 7 8
7 8 9 3 3 3 10 11 12
Example 2.7.2 Write a python program for performing addition of two matrices
Solution :
def add_matrix(arr1,arr2):
result = [[arr1[i][j] + arr2[i][j] for j in range(len(arr1[0]))] for i in range(len(arr1))]
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 18 Linear Data Structure using Sequential Organization
#Driver Code
add_matrix(arr1,arr2,)
Output
Input number of rows: 3
Input number of columns: 3
Enter the elements in first matrix: 1
Enter the elements in first matrix: 2
Enter the elements in first matrix: 3
Enter the elements in first matrix: 4
Enter the elements in first matrix: 5
Enter the elements in first matrix: 6
Enter the elements in first matrix: 7
Enter the elements in first matrix: 8
Enter the elements in first matrix: 9
The first matrix is...
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Enter the elements in second matrix: 1
Enter the elements in second matrix: 1
Enter the elements in second matrix: 1
Enter the elements in second matrix: 2
Enter the elements in second matrix: 2
Enter the elements in second matrix: 2
Enter the elements in second matrix: 3
Enter the elements in second matrix: 3
Enter the elements in second matrix: 3
The second matrix is...
[[1, 1, 1], [2, 2, 2], [3, 3, 3]]
The Addition of Two Matrices...
[[2, 3, 4], [6, 7, 8], [10, 11, 12]]
>>>
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 19 Linear Data Structure using Sequential Organization
1 2 3 1 1 1 14 14 17
4 5 6 2 2 2 32 32 38
7 8 9 3 3 4 50 50 59
Solution :
A = [[1,2,3],
[4,5,6],
[7,8,9]]
B = [[1,1,1],
[2,2,2],
[3,3,4]]
result= [[0,0,0],
[0,0,0],
[0,0,0]]
print(“Matrix A is ...”)
print(A)
print(“Matrix B is ...”)
print(B)
1 2 3 1 4 7
4 5 6 2 5 8
7 8 9 3 6 9
Solution :
A = [[1,2,3],
[4,5,6],
[7,8,9]]
result= [[0,0,0],
[0,0,0],
[0,0,0]]
1
4 2 st
3 1 dimension
5
7 6
15
rows 8 nd
14 2 dimension
9 13
10 18
11 rd
12 17 3 dimension
16
columns
Python Program
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 22 Linear Data Structure using Sequential Organization
Output
C++ Code
/*
This program is for storing and retrieving the elements in a 3-D array
*/
# include<iostream>
using namespace std;
int main()
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 23 Linear Data Structure using Sequential Organization
{
int a[3][2][3]
int i,j,k;
cout<<"\n Enter the elements";
for (i=0; i<3; i++)
{
for(j=0; j<2; j++)
{
for(k=0; k<3; k++)
{
cin>>a[i][j][k];
}
}
}
cout<<"\n Printing the elements \n";
for(i=0; i<3; i++)
{
for (j=0; j<2; j++)
{
for(k=0; k<3; k++)
{
cout<<"\t"<<a[i][j][k];
}
cout<<"\n";
}
cout<<"\n-----------------------------------";
}
return 0;
}
With this concept in mind let us formally define the ordered list.
Definition : An ordered list is set of elements where set may be empty or it can be
written as a collection of elements such as (a1, a2, a3 .................. an).
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 24 Linear Data Structure using Sequential Organization
(3) sort
The sort method arranges the elements in increasing order. For example
>>> a=[‘x’,’z’,’u’,’v’,’y’,’w’]
>>> a.sort()
>>> a
[‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’]
>>>
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 25 Linear Data Structure using Sequential Organization
The methods append, extend and sort does not return any value. These methods
simply modify the list. These are void methods.
(4) Insert
This method allows us to insert the data at desired position in the list. The syntax is
insert(index,element)
For example -
>>> a=[10,20,40]
>>> a.insert(2,30)
>>> print(a)
[10, 20, 30, 40]
>>>
(5) Delete
The deletion of any element from the list is carried out using various functions like
pop, remove, del.
If we know the index of the element to be deleted then just pass that index as an
argument to pop function.
For example
>>> a=[‘u’,’v’,’w’,’x’,’y’,’z’]
>>> val=a.pop(1) #the element at index 1 is v, it is deleted
>>> a
[‘u’, ‘w’, ‘x’, ‘y’, ‘z’] #list after deletion
>>> val #deleted element is present in variable val
‘v’
>>>
If we do not provide any argument to the pop function then the last element of the
list will be deleted.
For example -
>>> a =[‘u’,’v’,’w’,’x’,’y’,’z’]
>>> val=a.pop()
>>> a
[‘u’, ‘v’, ‘w’, ‘x’, ‘y’]
>>> val
‘z’
>>>
If we know the value of the element to be deleted then the remove function is used.
That means the parameter passed to the remove function is the actual value that is to be
removed from the list. Unlike, pop function the remove function does not return any
value. The execution of remove function is shown by following screenshot.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 26 Linear Data Structure using Sequential Organization
>>> a=[‘a’,’b’,’c’,’d’,’e’]
>>> a.remove(‘c’)
>>> a
[‘a’, ‘b’, ‘d’, ‘e’]
>>>
In python, it is possible to remove more than one element at a time using del
function.
For example
>>> a=[‘a’,’b’,’c’,’d’,’e’]
>>> del a[2:4]
>>> a
[‘a’, ‘b’, ‘e’]
>>>
· There are various built in functions in python for supporting the list operations.
Following table shows these functions
Built-in Purpose
function
all() If all the elements of the list are true or if the list is
empty then this function returns true.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 27 Linear Data Structure using Sequential Organization
· List comprehension is an elegant way to create and define new lists using existing
lists.
· This is mainly useful to make new list where each element is obtained by
applying some operations to each member of another sequence.
Syntax
List=[expression for item in the list]
Example 2.8.1 Write a python program to create a list of even numbers from 0 to 10.
Solution :
even = [] #creating empty list
for i in range(11):
if i % 2 ==0:
even.append(i)
print(“Even Numbers List: ”,even)
even = [] #creating empty list
for i in range(11):
if i % 2 ==0:
even.append(i)
print(“Even Numbers List: ”,even)
Output
Even Numbers List: [0, 2, 4, 6, 8, 10]
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 28 Linear Data Structure using Sequential Organization
Output
[(‘a’, ‘b’), (‘a’, ‘d’), (‘b’, ‘d’)]
Definition : Polynomial is the sum of terms where each term consists of variable,
coefficient and exponent.
Various operations on polynomial are – addition, subtraction, multiplication and
evaluation.
2.9.1 Representation
For representing a single variable 0 –19
polynomial one can make use of one 1 10
dimensional array. In single dimensional Coefficients
2 7
of the polynomial
array the index of an array will act as the 3 5
exponent and the coefficient can be stored 4 3
at that particular index which can be 5
represented as follows :
6
For e.g. : 3x4 + 5x3 + 7x2 + 10x – 19 7
This polynomial can be stored in
Index which acts
single dimensional array. as exponent of
respective coefficient
Algorithm :
Assume that the two polynomials say A and B and the resultant polynomial storing
the addition result is stored in one large array.
1. Set a pointer i to point to the first term in a polynomial A.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 29 Linear Data Structure using Sequential Organization
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 30 Linear Data Structure using Sequential Organization
6. While i < t1
{
Copy the coefficient at position i of into the
coefficient field at position k in C.
Copy the exponent pointed by i into the exponent field
at position k in C array.
Increment i and k to point to the next position in
arrays A and C.
}
7. While j < t2
{
Copy the coefficient at position j of B into the coefficient field at position
k in C.
Copy the exponent pointed by j into exponent field at
position k in C.
Increment j, k to point to the next position in B and
C arrays.
8. Display the complete array C as the addition of two given polynomials.
9. Stop.
3 2 x Polynomial A
3x 2x 1
i pointer
3 Polynomial B
5x 7x
j pointer
Polynomial C
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 31 Linear Data Structure using Sequential Organization
As the terms pointed by i and j shows that both the terms have equal exponent, we
can perform addition of these terms and we will store the result in polynomial C.
3 2 x Polynomial A
3x 2x 1
i pointer
3 Polynomial B
5x 7x
j pointer
3 Polynomial C
8x
k pointer
3 2 x Polynomial A
3x 2x 1
i pointer
3 Polynomial B
5x 7x
j pointer
3 Polynomial C
8x
k pointer
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 32 Linear Data Structure using Sequential Organization
Now pointer i points to 2x2 and pointer j points to 7x. As 2x2 > 7x. We will copy 2x2
in the polynomial C. And we will simply increment i and k pointers
3 2 x Polynomial A
3x 2x 1
i pointer
3 Polynomial B
5x 7x
j pointer
3 2 Polynomial C
8x 2x
k pointer
3 2 x Polynomial A
3x 2x 1
i pointer
3 Polynomial B
5x 7x
j pointer
3 2 Polynomial C
8x 2x 8x
k pointer
Now terms in polynomial B are over. Hence we will copy the remaining terms from
polynomial A to polynomial C.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 33 Linear Data Structure using Sequential Organization
3 2 x Polynomial A
3x 2x 1
3 Polynomial B
5x 7x
3 2 1 Polynomial C
8x 2x 8x
Python Program
# A[] represents coefficients of first polynomial
# B[] represents coefficients of second polynomial
# m and n are sizes of A[] and B[] respectively
def add(A, B, m, n):
size = max(m, n);
C = [0 for i in range(size)]
return C
# Driver Code
if __name__ == ‘__main__’:
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 34 Linear Data Structure using Sequential Organization
B = [0, 7,0, 5]
m = len(A)
n = len(B)
C = add(A, B, m, n)
size = max(m, n)
Output
First polynomial is
1 + 1x^ 1 + 2x^ 2 + 3x^ 3
Second polynomial is
0 + 7x^ 1 + 0x^ 2 + 5x^ 3
Addition of polynomial is
1 + 8x^ 1 + 2x^ 2 + 8x^ 3
>>>
C++ Code
/************************************************************
Program To Perform The Polynomial Addition And To Print
The Resultant Polynomial.
************************************************************/
#include<iostream>
using namespace std;
class APADD
{
private:
struct p
{
int coeff;
int expo;
};
public:
p p1[10],p2[10],p3[10];
int Read(p p1[10]);
int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);
void Print(p p2[10],int t2);
};
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 35 Linear Data Structure using Sequential Organization
/*
------------------------------------------------------------------------------
The Read Function Is For Reading The Two Polynomials
--------------------------------------------------------------------------------
/*
int APADD::Read(p p[10])
{
int t1,i;
cout<<"\n Enter The Total number Of Terms in The Polynomial: ";
cin>>t1;
cout<<"\n Enter The Coef and Exponent In Descending Order";
for(i=0;i<t1;i++)
{
cout<<"\n Enter Coefficient and exponent: ";
cin>>p[i].coeff;
cin>>p[i].expo;
}
return(t1);
}
/*
------------------------------------------------------------
The add Function Is For adding The Two Polynomials
----------------------------------------------------------*/
int APADD::add(p p1[10],p p2[10],int t1,int t2,p p3[10])
{
int i,j,k;
int t3;
i=0;
j=0;
k=0;
while(i<t1 &&j<t2)
{
if(p1[i].expo==p2[j].expo)
{
p3[k].coeff=p1[i].coeff+p2[j].coeff;
p3[k].expo =p1[i].expo;
i++;j++;k++;
}
else if(p1[i].expo>p2[j].expo)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo =p1[i].expo;
i++;k++;
}
else
{
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 36 Linear Data Structure using Sequential Organization
p3[k].coeff=p2[j].coeff;
p3[k].expo =p2[j].expo;
j++;k++;
}
}
while(i<t1)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo =p1[i].expo;
i++;k++;
}
while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo =p2[j].expo;
j++;k++;
}
t3=k;
return(t3);
}
/*
------------------------------------------------------------
The Print Function Is For Printing The Two Polynomials
------------------------------------------------------------
*/
void APADD::Print(p pp[10],int term)
{
int k;
cout<<"\n Printing The Polynomial";
for(k=0;k<term-1;k++)
cout<<" "<<pp[k].coeff<<"X^"<<pp[k].expo<<"+ ";
cout<<pp[k].coeff<<"X^"<<pp[k].expo;
}
/*
------------------------------------------------------------
The main function
------------------------------------------------------------
*/
int main()
{
APADD obj;
int t1,t2,t3;
cout<<"\n Enter The First Polynomial";
t1=obj.Read(obj.p1);
cout<<"\n The First Polynomial is: ";
obj.Print(obj.p1,t1);
cout<<"\n Enter The Second Polynomial";
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 37 Linear Data Structure using Sequential Organization
t2=obj.Read(obj.p2);
cout<<"\n The Second Polynomial is: ";
obj.Print(obj.p2,t2);
cout<<"\n The Addition is: ";
t3=obj.add(obj.p1,obj.p2,t1,t2,obj.p3);
obj.Print(obj.p3,t3);
return 0;
}
Python Program
# A[] represents coefficients of first polynomial
# B[] represents coefficients of second polynomial
# m and n are sizes of A[] and B[] respectively
def mul(A, B, m, n):
return C
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 38 Linear Data Structure using Sequential Organization
# Driver Code
if __name__ == ‘__main__’:
C = mul(A, B, m, n)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 39 Linear Data Structure using Sequential Organization
where x = 1,
= – 10 + 4 + 3 = – 6 + 3
= 3 is the result of polynomial evaluation.
Now, let us see the algorithm for polynomial.
Algorithm :
Step 4 : Then calculate coeff * pow (x, exp) of each term and add the result to sum.
Step 6 : Stop
Python Program
def evalPoly(A,n,x):
result = A[0]
for i in range(1,n):
result = result + A[i]*x**i
return result
# Driver Code
if __name__ == ‘__main__’:
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 40 Linear Data Structure using Sequential Organization
Output
Polynomial is:
1 + 1x^ 1 + 2x^ 2 + 3x^ 3
Enter the value of x: 2
The result of evaluation of polynomial is: 35
>>>
C++ Code
/*************************************************************
Program to evaluate a polynomial in single variable for a
given value of x.
*************************************************************/
#include <iostream>
#include <cmath>
using namespace std;
#define size 20
class APEVAL
{
private:
struct p
{
int coef;
int expo;
};
public:
p p1[size];
int get_poly(p p1[size]);
void display(p p1[size],int n1);
float eval(int n1, p p1[]);
};
/*----------------------------------------------------------
Function get_poly
------------------------------------------------------------*/
int APEVAL::get_poly( p p1[] )
{
int term,n;
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 41 Linear Data Structure using Sequential Organization
/*
------------------------------------------------------------
This Function is to display the polynomial
Parameter Passing: By reference and value
------------------------------------------------------------*/
void APEVAL::display(p p1[ ],int n)
{
int term;
for (term = 0; term < n-1 ; term ++ )
cout<<p1[term].coef<<"x^"<<p1[term].expo<<"+ ";
cout<<p1[term].coef<<"x^"<<p1[term].expo;
}
/*----------------------------------------------------------
Function eval
------------------------------------------------------------*/
float APEVAL::eval(int n1, p p1[])
{
int i,sum, x;
cout<<"\nEnter the value for x for evaluation : ";
cin>>x;
sum = 0;
for(i=0; i < n1;i++ )
sum= sum+p1[i].coef *pow( x, p1[i].expo);
return( sum );
}
/*
------------------------------------------------------------
Function main
------------------------------------------------------------
*/
int main()
{
int n1;
int value;
APEVAL obj;
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 42 Linear Data Structure using Sequential Organization
Review Question
· In a matrix, if there are m rows and n columns then the space required to store
the numbers will be m ´ n ´ s where s is the number of bytes required to store the
value. Suppose, there are 10 rows and 10 columns and we have to store the
integer values then the space complexity will be bytes.
10 ´ 10 ´ 2 = 200 bytes
1 2 3 1 0 0
4 5 6 0 0 0
7 8 9 0 1 0
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 43 Linear Data Structure using Sequential Organization
· The representation of sparse matrix will be a triplet only. That means it stores
rows, columns and values.
· The 0th row will store total rows of the matrix, total columns of the matrix, and
total non-zero values.
· For example – Suppose a matrix is 6 ´ 7 and number of non zero terms are say 8.
In our sparse matrix representation the matrix will be stored as
Index Row No. Col. No. Value
0 6 7 8
1 0 6 – 10
2 1 0 55
3 2 5 – 23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 – 28
8 5 0 99
Python Program
# function display a matrix
def display(matrix):
for row in matrix:
for element in row:
print(element, end =" “)
print()
# sublist
temp.append(i)
temp.append(j)
temp.append(matrix[i][j])
# Driver code
#Original Matrix
A =[ [10, 0, 0, 0],
[0, 20, 0, 0],
[0, 0, 30, 0],
[0, 0, 0, 40],
[0, 50, 0, 0]]
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 45 Linear Data Structure using Sequential Organization
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 46 Linear Data Structure using Sequential Organization
Python Program
def create(s,row_num,col_num,non_zero_values):
s[0][0]= row_num
s[0][1] = col_num
s[0][2] = non_zero_values
for k in range(1,non_zero_values+1):
row = int(input(“Enter row value: ”))
col = int(input(“Enter col value: ”))
element = int(input(“Enter the element: ”))
s[k][0]= row
s[k][1] = col
s[k][2] = element
def display(s):
print(“Row\tcol\t Non_Zero_values”)
for i in range(0,(s[0][2]+1)):
for j in range(0,3):
print(s[i][j], “\t”, end=’’)
print()
def add(s1,s2):
i=1
j=1
k=1
s3 = []
if ((s1[0][0] == s2[0][0]) and (s1[0][1] == s2[0][1])):
#traversing thru all the terms
while ((i <= s1[0][2]) and (j <= s2[0][2])):
if (s1[i][0] == s2[j][0]):
temp =[]
if (s1[i][1] == s2[j][1]):
temp.append(s1[i][0])
temp.append(s1[i][1])
temp.append(s1[i][2]+s2[j][2])
s3.append(temp)
i += 1
j += 1
k += 1
elif (s1[i][1]<s2[j][1]):
temp.append(s1[i][0])
temp.append(s1[i][1])
temp.append(s1[i][2])
s3.append(temp)
i += 1
k += 1
else:
temp.append(s2[j][0])
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 47 Linear Data Structure using Sequential Organization
temp.append(s2[j][1])
temp.append(s2[j][2])
s3.append(temp)
j += 1
k += 1
elif (s1[i][0]<s2[j][0]):
temp =[]
temp.append(s1[i][0])
temp.append(s1[i][1])
temp.append(s1[i][2])
s3.append(temp)
i +=1
k +=1
else:
temp =[]
temp.append(s1[j][0])
temp.append(s1[j][1])
temp.append(s1[j][2])
s3.append(temp)
j +=1
k +=1
#copying remaining terms
while (i <= s1[0][2]): #s1 is greater than s2
temp = []
temp.append(s1[i][0])
temp.append(s1[i][1])
temp.append(s1[i][2])
s3.append(temp)
i += 1
k += 1
while (j <= s2[0][2]): #s2 is greater than s1
temp = []
temp.append(s2[j][0])
temp.append(s2[j][1])
temp.append(s2[j][2])
s3.append(temp)
j += 1
k += 1
#assigning total rows, total columns
#and total non zero values as a first row
#in resultant matrix
s3.insert(0,[s1[0][0],s1[0][1],k-1])
else:
print(“\n Addition is not possible”)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 48 Linear Data Structure using Sequential Organization
Output
Input total number of rows for first matrix: 2
Input total number of columns for first matrix: 2
Input total number of non-zero values: 3
Enter row value: 1
Enter col value: 1
Enter the element: 10
Enter row value: 1
Enter col value: 2
Enter the element: 20
Enter row value: 2
Enter col value: 1
Enter the element: 30
First sparse matrix is
Row col Non_Zero_values
2 2 3
1 1 10
1 2 20
2 1 30
Input total number of rows for second matrix: 2
Input total number of columns for second matrix: 2
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 49 Linear Data Structure using Sequential Organization
{
s3[k][2] = s1[i][2];
s3[k][1] = s1[i][1];
s3[k][0] = s1[i][0];
i++;
k++;
}
else
{
s3[k][2] = s2[j][2];
s3[k][1] = s2[j][1];
s3[k][0] = s2[j][0];
j++;
k++;
}
}//end of 0 if
else if (s1[i][0]<s2[j][0])
{
s3[k][2] = s1[i][2];
s3[k][1] = s1[i][1];
s3[k][0] = s1[i][0];
i++;
k++;
}
else
{
s3[k][2] = s2[j][2];
s3[k][1] = s2[j][1];
s3[k][0] = s2[j][0];
j++;
k++;
}
}//end of while
//copying remaining terms
while (i <= s1[0][2])
{
s3[k][2] = s1[i][2];
s3[k][1] = s1[i][1];
s3[k][0] = s1[i][0];
i++;
k++;
}
while (j <= s2[0][2])
{
s3[k][2] = s2[j][2];
s3[k][1] = s2[j][1];
s3[k][0] = s2[j][0];
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 51 Linear Data Structure using Sequential Organization
j++;
k++;
}
s3[0][2] = k - 1;
}
else
cout<<"\n Addition is not possible";
}
1 0 6 – 10
2 1 0 55
3 2 5 – 23
4 3 1 67
5 3 6 88
6 4 3 14
7 4 4 – 28
8 5 0 99
will become
Index Row No. Col. No. Value
0 7 6 8
1 0 1 55
2 0 5 99
3 1 3 67
4 3 4 14
5 4 4 – 28
6 5 2 – 23
7 6 0 – 10
8 6 3 88
During the transpose of matrix (i) Interchange rows and columns (ii) Also maintain
the rows in sorted order.
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 52 Linear Data Structure using Sequential Organization
Python Program
row_num = int(input(“Input number of rows: ”))
col_num = int(input(“Input number of columns: ”))
non_zero_values = int(input(“Input total number of non-zero values: ”))
cols =3
s1 = [[0 for col in range(cols)] for row in range(non_zero_values+1)]
s2 = [[0 for col in range(cols)] for row in range(non_zero_values+1)]
def create(s1,row_num,col_num,cols,non_zero_values):
s1[0][0]= row_num
s1[0][1] = col_num
s1[0][2] = non_zero_values
for k in range(1,non_zero_values+1):
row = int(input(“Enter row value: ”))
col = int(input(“Enter col value: ”))
element = int(input(“Enter the element: ”))
s1[k][0]= row
s1[k][1] = col
s1[k][2] = element
def display(s,cols,non_zero_values):
print(“Row\tcol\t Non_Zero_values”)
for i in range(0,non_zero_values+1):
for j in range(0,cols):
print(s[i][j], “\t”, end=’’)
print()
def transpose(s1,row_num,col_num,s2,cols,non_zero_values):
s2[0][0]= col_num
s2[0][1] = row_num
s2[0][2] = non_zero_values
nxt=1
for c in range(0,col_num):
# for each column scan all the terms for a ‘term’ in that column
for Term in range(1,non_zero_values+1):
if (s1[Term][1] == c):
# Interchange Row and Column
s2[nxt][0] = s1[Term][1]
s2[nxt][1] = s1[Term][0]
s2[nxt][2] = s1[Term][2]
nxt=nxt+1
#Driver Code
create(s1,row_num,col_num,cols,non_zero_values)
print(“Original sparse matrix is”)
display(s1,cols,non_zero_values)
transpose(s1,row_num,col_num,s2,cols,non_zero_values)
print(“Transposed sparse matrix is”)
display(s2,cols,non_zero_values)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 53 Linear Data Structure using Sequential Organization
Output
Logic Explanation :
In above program we look for column values of s1 array starting from 0 to total
number of column value and interchange row and column one by one. For instance –
Consider the output of above program
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 54 Linear Data Structure using Sequential Organization
s1[0][0] =10, the column value is 0 here, we swap the row and column and copy the
column, row and non zero value to s2 array
Locate zero in Col, swap row, col and copy it to s2. At the same time copy
corresponding non-zero-value to s2
s1 s2
0 0 10 0 0 10
0 2 20
1 1 30
2 1 40
Locate 1 in Col, swap row, col and copy it to s2. At the same time copy
corresponding non-zero-value to s2
0 0 10 0 0 10
0 2 20 1 1 30
1 1 30
2 1 40
0 0 10 0 0 10
0 2 20 1 1 30
1 1 30 1 2 40
2 1 40
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 55 Linear Data Structure using Sequential Organization
Locate 2 in Col, swap row, col and copy it to s2. At the same time copy
corresponding non-zero-value to s2
0 0 10 0 0 10
0 2 20 1 1 30
1 1 30 1 2 40
2 1 40 2 1 20
C++ Code
void transp(int s1[size + 1][3], int s2[size + 1][3])
{
int nxt, c, Term;
int n, m, terms;
/*Read Number of rows, columns and terms of given matrix */
n = s1[0][0];
m = s1[0][1];
terms = s1[0][2];
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 56 Linear Data Structure using Sequential Organization
{
/* Interchange Row and Column */
s2[nxt][0] = s1[Term][1];
s2[nxt][1] = s1[Term][0];
s2[nxt][2] = s1[Term][2];
nxt++;
}
}
}
}
}
Python Program
def create(s1,row_num,col_num,cols,non_zero_values):
s1[0][0]= row_num
s1[0][1] = col_num
s1[0][2] = non_zero_values
for k in range(1,non_zero_values+1):
row = int(input(“Enter row value: ”))
col = int(input(“Enter col value: ”))
element = int(input(“Enter the element: ”))
s1[k][0]= row
s1[k][1] = col
s1[k][2] = element
def display(s,cols,non_zero_values):
print(“Row\tcol\t Non_Zero_values”)
for i in range(0,non_zero_values+1):
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 57 Linear Data Structure using Sequential Organization
for j in range(0,cols):
print(s[i][j], “\t”, end=’’)
print()
def transpose(s1,row_num,col_num,s2,cols,non_zero_values):
s2[0][0]= col_num
s2[0][1] = row_num
s2[0][2] = non_zero_values
rterm = []
rpos = []
if non_zero_values > 0:
for i in range(col_num):
rterm.insert(i,0)
for i in range(1,non_zero_values+1):
#rterm[s1[i][1]]++
index = s1[i][1]
val = rterm.pop(index)
rterm.insert(index,val+1)
rpos.insert(0,1)
for i in range(1,col_num+1):
#rpos[i]=rpos[i-1]+ rterm[(i - 1)]
rpos_val=rpos[i-1]
rterm_val=rterm[i-1]
rpos.insert(i,(rpos_val+rterm_val))
for i in range(1,non_zero_values+1):
j = rpos[s1[i][1]]
s2[j][0] = s1[i][1]
s2[j][1] = s1[i][0]
s2[j][2] = s1[i][2]
rpos[s1[i][1]] = j + 1
#Driver Code
create(s1,row_num,col_num,cols,non_zero_values)
print(“Original sparse matrix is”)
display(s1,cols,non_zero_values)
transpose(s1,row_num,col_num,s2,cols,non_zero_values)
print(“Transposed sparse matrix is”)
display(s2,cols,non_zero_values)
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 58 Linear Data Structure using Sequential Organization
Output
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 59 Linear Data Structure using Sequential Organization
0 3 3 4
1 0 1 10
2 1 0 20
3 2 0 30
4 2 1 40
We will first consider one dimensional array, named rterm[]. In this array we will
store non zero terms present in each column.
At 0 th column there are two non zero rterm
terms. At 1 st column also there are two non 0 2
zero terms but there is non zero term in 2 nd 1 2
column.
2 0
Similarly we will take another one dimensional array named rpos[]. We initialize 0 th
location of rpos[] by 1. so,
rpos
rpos
Now we use following formula to fill up 0 1
the rpos array.
rpos[i] = rpos[i – 1] + rterm[i – 1]
= 1+ 2 =3+2 =5+0=5
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 60 Linear Data Structure using Sequential Organization
rpos
0 1
1 3
2 5
3 5
rpos[1] points to value 3. That means place the triplet (1, 0, 10) at index 3 in S2
array.
S2
3 1 0 10
rpos
0 1
1 34
2 5
3 5
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 61 Linear Data Structure using Sequential Organization
rpos[0] = 1
S2
1 0 1 20
3 1 0 10
rpos
0 12
1 4
2 5
3 5
rpos[0] = 2
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 62 Linear Data Structure using Sequential Organization
1 0 1 20
2 0 2 30
3 1 0 10
1 4
2 5
3 5
rpos[1] = 4
S2
1 0 1 20
2 0 2 30
3 1 0 10
4 1 2 40
Thus we get
Row Col Non-Zero values
0 3 3 4
1 0 1 20
2 0 2 30
3 1 0 10
4 1 2 40
C++ Code
void trans (int s1[max1][3],int s2[max1][3] )
{
int rterm[max1],rpos[max1];
int j,i ;
int row,col,num ;
row= s1[0][0];
col= s1[0][1];
num= s1[0][2];
s2[0][0] = col;
s2[0][1] = row;
s2[0][2] = num;
if ( num > 0 )
{
for ( i = 0; i <= col ; i ++ )
rterm[i] = 0;
for ( i = 1; i <= num ; i ++ )
rterm[s1[i][1]] ++;
rpos[0] = 1; /*setting the rowwise position*/
for ( i = 1; i <= col; i++ )
rpos[i]=rpos[i-1]+ rterm[(i - 1)];
for ( i = 1; i <= num ; i ++ )
{
j = rpos[s1[i][1]];
s2[j][0] = s1[i][1];
s2[j][1] = s1[i][0];
s2[j][2] = s1[i][2];
rpos[s1[i][1]] = j + 1;
}
}
}
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 64 Linear Data Structure using Sequential Organization
Review Questions
1. Explain fast transpose of sparse matrix with suitable example. Discuss time complexity of fast
transpose. SPPU : May-17, Marks 6
2. Write pseudo code to perform simple transpose of sparse matrix. SPPU : May-19, Marks 4
4. Write a pseudo code to perform the simple transpose of sparse matrix. Also discuss the time
complexity. SPPU : Dec.-19, Marks 6
C Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],n,mid,i,j,temp;
clrscr();
printf(“\n How many elements do you want? ”);
scanf(“%d”,&n);
printf(“\n Enter the elements in the ascending order ”);
for(i=0;i<n;i++)
{
printf(“\n Enter the element ”);
scanf(“%d”,&a[i]);
}
printf(“\nThe elements in descending order(By Method 1) are ...\n”);
j=0;
for(i=n–1;i>=0;i–)// reading the array in reverse direction
{
b[j]=a[i];//storing them in another array
j++;
}
for(j=0;j<n;j++) This method makes use of some
printf(“ %d”,b[j]); computations for storing the
elements in descending order. It
makes use of the same array. Hence
printf(“\nThe elements in descending
less amount of space is needed at
order(By Method 2) are ...\n”);
mid=n/2; the cost of addition computational
for(i=0;i<mid;i++) time.
{
j=(n–1)–i;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(i=0;i<n;i++)
printf(“ %d”,a[i]);
getch();
}
Output (Run 1)
How many elements do you want? 5
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
Fundamentals of Data Structures 2 - 66 Linear Data Structure using Sequential Organization
Output (Run 2)
How many elements do you want? 6
®
TECHNICAL PUBLICATIONS - An up thrust for knowledge
SUBJECT CODE : 210242
Fundamentals of
Data Structures
(For END SEM Exam - 70 Marks)
® ®
TECHNICAL
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge
(i)
Fundamentals of Data Structures
(For END SEM Exam - 70 Marks)
Published by :
® ®
Amit Residency, Office No.1, 412, Shaniwar Peth,
TECHNICAL Pune - 411030, M.S. INDIA, Ph.: +91-020-24495496/97
PUBLICATIONS
SINCE 1993 An Up-Thrust for Knowledge Email : [email protected] Website : www.technicalpublications.org
Printer :
Yogiraj Printers & Binders
Sr.No. 10/1A,
Ghule Industrial Estate, Nanded Village Road,
Tal. - Haveli, Dist. - Pune - 411041.
ISBN 978-93-332-2154-2
SPPU 19
9 789333 221542
Array
0 1 2 3 4 5 6 7
? – 40 11 33 37 42 45 99 190
?
Sub list 1 Sub list 2
Middle
element
Array
4 5 6 7
45 99 190
Sub Sub
list 1 list 2
?
Middle
? element
Array
4 5 6 7
45 99 190
7 th
Key = 99
[ 6] th
?
?
Left sublist Right sublist
Left
Right
sublist sublist
?
?
0 1 1 2 3 5 8
a= =b because this is
i.e. previous element the only element in
of b Fib. Series which is < n i.e. < 7
a b f
10 20 30 40 50 60 70
1 2 3 4 5 6 7
a b f
10 20 30 40 50 60 70
1 2 3 4 5 6 7
a f/b
10 20 30 40 50 60 70
1 2 3 4 5 6 7
m th
1 th
2 th
Index 1 Array
0 15
25
10
32
20
34
30
38
40
45
50 48
10,000 records
i.e. 100 blocks
500 records
Output i.e. 5 blocks Disk
can be sorted
Main memory
F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
G1 G2 G3 G4 G5 G6 G7 G8 G9 G10
H1 H2 H3 H4 H5
I1 I2 I3
J1 J2
Sorted
K1 record
a
0 45
1 –40
2 190
3 99
4 11
a
0 45
1 –40
2 190
3 99
4 11
a
0 –40
1 45
2 190
3 99
4 11
a
0 –40
1 45
2 190
3 99
4 11
a
0 –40
1 45
2 99
3 190
4 11
a
0 –40
1 45
2 99
3 11
4 190
a
0 –40
1 45
2 99
3 11
4 190
a
0 –40
1 45
2 99
3 11
4 190
a
0 –40
1 45
2 99
3 11
4 190
a
0 –40
1 45
2 11
3 99
4 190
a
0 –40
1 45
2 11
3 99
4 190
a
0 –40
1 11
2 45
3 99
4 190
a
0 –40
1 11
2 45
3 99
4 190
10 5 4 18 7 1 2
5 10 4 18 7 1 2
5 4 10 18 7 1 2
5 4 10 18 7 1 2
5 4 10 7 18 1 2
5 4 10 7 1 18 2
5 4 10 7 1 2 18
5 4 10 7 1 2 18
4 5 10 7 1 2 18
4 5 10 7 1 2 18
4 5 7 10 1 2 18
4 5 7 1 10 2 18
4 5 7 1 2 10 18
4 5 7 1 2 10 18
4 5 7 1 2 10 18
4 5 1 7 2 10 18
4 5 1 2 7 10 18
4 5 1 2 7 10 18
4 1 5 2 7 10 18
4 1 2 5 7 10 18
4 1 2 5 7 10 18
4 1 2 5 7 10 18
1 4 2 5 7 10 18
1 2 4 5 7 10 18
1 2 4 5 7 10 18
1 2 4 5 7 10 18
1 2 4 5 7 10 18
0 1 2 3 4 5 6
Compare 70
30 70 20 50 40 10 60
with 30 and
insert it at
Sorted Unsorted zone its position
zone
0 1 2 3 4 5 6
Compare 20
30 70 20 50 40 10 60 with the
elements in
Sorted Unsorted zone sorted zone
zone and insert it
in that zone
0 1 2 3 4 5 6 at appropriate
20 30 70 50 40 10 60 position
0 1 2 3 4 5 6
20 30 50 70 40 10 60
0 1 2 3 4 5 6
20 30 40 50 70 10 60
0 1 2 3 4 5 6
10 20 30 40 50 70 60
Sorted list of
elements
O(n 2 )
O(n 2 )
O (n 2 )
0 1 2 3 4
30 20 10 40 50 Initially it enters in
outer for loop
j i temp = A [i]
j=i–1
temp
20
0 1 2 3 4
30 30 10 40 50
j = –1 i A[ j + 1 ] = A[ j ]
temp
20
0 1 2 3 4
20 30 10 40 50
j = –1 i
temp it gets
copied at
20 A[ j + 1 ]
0 1 2 3 4
20 30 10 40 50 Again for loop gets
executed and
set i =2, temp = A[i]
This much and j = i –1
list gets
sorted.
20 30 10 40 50 It moves to while loop
As j > = 0 and
j i (A [ j] > temp) is true,
temp the while loop gets
executed.
10
i
20 30 30 40 50 A[ j +1] = A[ j ]
I
j j = j –1
j gets decremented
temp
10
0 1 2 3 4
II 20 20 30 40 50 A[ j +1] = A[ j ]
j j = j –1
temp
10
0 1 2 3 4
As j < 0, it comes
10 20 30 40 50
III out of while
j i loop
A[ j +1] = temp
temp gets
10 copied
Thus,
0 1 2 3 4
10 20 30 40 50 Again for loop gets
executed.
set i = 3,
This much temp = A [ i ]
list gets j = i –1
sorted
0 1 2 3 4
It moves to while loop
10 20 30 40 50
As A [ j] > temp
j i is false , while loop
will not get executed.
temp
40
0 1 2 3 4
10 20 30 40 50
j
temp gets
copied
10 (i.e. no change!)
Then, 0 1 2 3 4
Again for loop gets
10 20 30 40 50
executed
set i = 4
This much list temp = A [ i ]
gets sorted j = i –1
Then, 0 1 2 3 4
It moves to while loop
10 20 30 40 50
As A[ j ] > temp is
j i false, while loop
will not get
temp executed.
50
0 1 2 3 4
10 20 30 40 50
temp gets
copied
50
0 1 2 3 4
10 20 30 40 50
10 50 78 40 30 02 04 15 Compare 78 and
10 and 50. Insert
it at appropriate
Sorted Unsorted position
zone zone
10 50 78 40 30 02 04 15 Compare 40 with
elements in sorted
zone. Insert it
Sorted Unsorted at appropriate
zone zone position
10 40 50 78 30 02 04 15
Sorted Unsorted
zone zone
10 30 40 50 78 02 04 15
Sorted Unsorted
zone zone
02 10 30 40 50 78 04 15
Sorted Unsorted
zone zone
02 04 10 30 40 50 78 15
Sorted Unsorted
zone zone
02 04 10 15 30 40 50 78
Sorted
list
i th
n n 1 1
n 1 n 2... 2 1 2
2
n2 n
O n2
i th
n n 1 1
1 2 3 ... n 1
2
2
n2 n n2
Last n i elements
A[0] A[1] A[i 1] | A[i], , A[k], , A[n 1]
1st
Scan the array for finding smallest element
2 nd
Scan the array for finding
smallest element
3 rd
Smallest element is
searched in this list
4 th
Smallest element is
searched in this list
5 th
Search smallest
element in this list
6 th
34 9 78 65 12 –8
0 1 2 3 4 5
34 9 78 65 12 –8
0 1 2 3 4 5
–8 9 78 65 12 34
0 1 2 3 4 5
–8 9 78 65 12 34
Scan array
Min
for minimum
element
0 1 2 3 4 5
–8 9 78 65 12 34
Scan array
Min
for minimum
element
Swap A[2] and A[4]
0 1 2 3 4 5
–8 9 12 65 78 34
0 1 2 3 4 5
–8 9 12 65 78 34
0 1 2 3 4 5
–8 9 12 34 78 65
0 1 2 3 4 5
–8 9 12 34 78 65
Min
Swap A[4] and A[5]
0 1 2 3 4 5
–8 9 12 34 65 78
n items n
O(nlog 2 n)
2 n–2 items
3 n–3 items
1
O(n 2 )
O(nlog 2 n)
33 37 92 57
33 37 92 57
92 57
86 92
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
25 57 33 37 12 92 86 48
A 1 2 3 4
A 1 2 3 4
Count 2 0 2 1
A 1 2 3 4
Count 2 0 2 1
B 2
A 1 2 3 4
Count 2 0 2 1
+ + +
B 2 2 4 5
A 1 2 3 4
Element 4 is at position 5
B 2 2 4 5
Step2: Decrement
B 2 2 4 4 5 by 1 in
array B
A 1 2 3 4
B 2 2 4 4
Position 1 2 3 4 5
B 1 2 4 4
A 1 2 3 4
B 1 2 4 4
Position 1 2 3 4 5
Element 1 3 4
B 1 2 3 4
A 1 2 3 4
B 1 2 3 4
Position 1 2 3 4 5
Element 1 1 3 4
A 1 2 3 4
B 0 2 3 4
Position 1 2 3 4 5
Element 1 1 3 3 4
1 1 3 3 4
O n2
O n2
O n2
O n2
O n2
O n2
Operating system program and
program code
Node 10 20 30 40 50
a[0] a[1] a[2] a[3] a[4]
Data Next link
Index
Data members of list
Operations on list
10 NULL 20 NULL Gets created after
using operator new
head / temp New
10 20 30 40 NULL
head New/temp
10 20 30 40 50 NULL
head temp/New
10 20 30 40 50 NULL
head/
temp
1 temp data i.e.
10 will be displayed
as temp ! = NULL
10 20 30 40 50 NULL
head temp
2 temp data i.e.
3
set temp = temp next 20 will be displayed
as temp ! = NULL
10 20 30 40 50 NULL
head temp
4 5
temp data i.e.
set temp = temp next 30 will be displayed
as temp ! = NULL
10 20 30 40 50 NULL temp
head
set temp = temp next
There is no node in the
linked list. That means the
linked list is empty
10 20 30 40 50 NULL
head / temp
If want to insert this
9 NULL node as a head node
then
New
9 10 20 30 40 50 NULL
10 20 30 40 50 NULL
head/temp
If we want to insert 60 NULL
this node as a last node
then New
10 20 30 40 50 NULL
temp
while(temp next!=NULL)
temp = temp next;
/*traversing
thru'linked list*/
10 20 30 40 50 60 NULL
temp New
31 NULL
New
10 20 30 40 50 NULL
temp
31 NULL
New
10 20 30 40 50 NULL
temp
This can be
31 done using
these 'C+ +'
New statements
10 20 30 40 50 NULL
prev temp
10 20 30 40 50 NULL
prev temp
10 20 40 50 NULL
10 20 30 40 50 NULL
head / We want to
temp delete this
node.
10 20 30 40 50 NULL
temp head
If node containing desired data is
obtained in the linked list then set
found variable to TURE
10 20 30 40 50 NULL
head/temp
10 20 30 40 50 NULL
temp
?
10 20 30 40 50 NULL
temp
?
10 20 30 40 50 NULL
temp
?
10 20 30 40 NULL
First or
head
10 20 30 40
NULL 10 20 30 40 NULL
Head or
first
99
10 20 30 40
prev Data next
NULL 3 NULL
Single node
Head node
temp = n1;
last = temp;
n1/ temp/ last
last next = n1
20 n1 prev = last
temp/ last n1
last = n1
20
temp n1/ last
20 30
temp last n1
last next = n1
20 30 n1 prev = last
temp last n1
20 30
head / temp
If we
20 30 40 NULL want
to delete
head curr '30'
20 30 40 NULL
20 30 40 NULL
II tempnext) = curr next;
temp curr
II
20 30 40 NULL
20 30 40 NULL
temp
31
n1
III II
20 30 40 NULL
temp I
.... 30 31 40 NULL
Data Next link Previous Data Next
link field link field
10 20 30 40 50
head
node
–999 10 20 30 40 50
head
node
10
New/head
10 20 30 40 NULL
head temp New
10 20 30 40 NULL
head temp New
10 20 30 40
head temp New
10 20 30 40 50
head
9 NULL
New
10 20 30 40 9 NULL
head temp New
10 20 30 40 50
head temp New
10 20 30 40
As key = 30
head temp and temp data = 30
35 NULL
New
10 20 30 40 50
head/temp
Key = 30
10 20 30 40 50 and
head temp temp next data
= key, hence
10 20 30 40 50 temp next
head temp temp1 = temp1 next;
3x 2 5x 7
3 2 5 1 7 0 NULL
P1 3x 3 2x 2 1x
P2 5x5 3x 2 7
p node
p
1
p1 3 3 2 2 1 1 NULL
p2 5 5 3 2 7 0 NULL
p
3
p1 3 3 2 2 1 1 NULL
Read the
node
p2 5 5 3 2 7 0 NULL
Read
node If p2 exp > p1 exp
p3 p3 coef = p2 coef
5 5 NULL
p3 exp = p2 exp
temp p2 = p2 next
p1 3 3 2 2 1 1 NULL
If p1 exp = p2 exp
p3 coef = p1 coef
p3 exp = p1 exp
p2 5 5 3 2 7 0
p1 = p1 next
p3 5 5 3 3 NULL
p1 3 3 2 2 1 1 NULL
If p1 exp = p2 exp
p3 coef = p1 coef + p2 coef
p3 exp = p1 exp
p2 5 5 3 2 7 0 NULL Then
Increment both the pointers
p3 5 5 3 3 5 2 NULL
temp
p1 3 3 2 2 1 1 NULL
p2 5 5 3 2 7 0 NULL
p3 5 5 3 3 5 2 1 1 NULL
Transfer p1 node
to p3
p1 3 3 2 2 1 1 NULL NULL
p3 5 5 3 3 5 2 1 1 7 0 NULL
p1 3 3 2 2 1 1 NULL NULL
p2 5 5 3 2 7 0 NULL NULL
p3 5 5 3 3 5 2 1 1 7 0 NULL
temp return (temp)
p
3
a 1 , a 2 a 3 , ... , a n ,
a 1 , a 2 a 3 , ... , a n
head
0 a 1 0 d X
0 b 0 c X
0 a
0 b 0 c X
head node
0 p 0 q 1 0 x 0 y X
0 r 0 s 1 0 w X
0 t 0 u 0 v X
head node
1 1 X
0 a 0 b X
0 a 0 b 1 X
0 c 0 d 1 X
0 e X
Next
Flag Var Exp
pointer
9 x5 7 xy 4 10xz
0 z 2 10 1 X
temp 2 temp 3
0 y 2 7 1 X
coeff exp
0 x 2 9 5 } Indicates 9x5
9 x5
temp 1
dn
Flag ptr Exp Next
Attached 4
to x 1 4 7x y
variable
temp 2 temp 3
0 y 2 7 1 X
x4
7y 1
x4 7y 1
7x4 y 1
– 4 x 4 y 2 z 3 10x 2 yz 2 7 xyz 45
0 x 1 4 1 2 1 2 45 0 X
0 y 1 1 X
0 z 2 1 1 X
0 y 1 1 X
0 z 2 10 2 X
0 y 1 2 X
0 z 2 –4 3 X
0 th
top s
9 100
. .
. .
. .
top points
2 30 Value in the stack
here
1 20
0 10
st
0 th
1 st
top 9 90
Here
st.s[0] = 10
st.s[1] = 20 and so on
And
2 30
st.top = 9
1 20
0 10
st s[ ]
pushing
top = – 1 element 10
onto the
stack and
top 10 top = 0
stack is stack
empty
(a) (b)
(c) (d)
20 getting 10 getting
popped popped
set top = 0 and top = – 1
Means stack is empty
top 10
stack stack
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
30 40 20 50 60 10
0 1 2 3 4
top 1
( A + B ) * ( C – D ) $
none
reading
( A + B ) * ( C – D ) $ +
Push A
(
+
( A + B ) * ( C – D ) $ +
( AB
( A + B ) * ( C – D ) $
Push AB+
*
then '('
( A + B ) * ( C – D ) $ because
(
'(' has AB +
* more priority
than '*'
( A + B ) ( C – D ) $ –
* ( AB + C
*
popping
( A + B ) * ( C – D ) $ –
( AB + CD
AB + CD –
*
pop
( A + B ) * ( C – D ) $ and print
AB + CD – *
*
C DE FG $
A B C $DE
4$2 3 3 8 4 (1 1)
AB + C – BA + C$ –
AB + C – BA + C$ –
2
1
AB + C – BA + C$ –
1+2=3
3
Now, AB + C – BA + C$ –
3
3
AB + C – BA + C$ –
AB + C – BA + C$ –
2
0
AB + C – BA + C$ –
1
2
0
AB + C – BA + C$ –
2+1=3
0
3
0
AB + C – BA + C$ –
3
3
0
AB + C – BA + C$ –
27
0
AB + C – BA + C$ –
–27
AB + C – BA + C$ –
Stack
3 1 2 3 + *
2
1
Reading
Stack
Stack
5 1 2 3 + * $
1
Reading
Stack
5*1 1 2 3 + * $
Result = 5
Reading
top
40
node
30
20
10 NULL
10 top
30 NULL
NULL Make top = NULL
30 NULL
NULL
NULL
NULL
NULL
NULL
10 NULL top
New / top
20
top = New ;
10 NULL
10 NULL
30 temp
10 NULL
30
20 top
This will be new stack
with node '30' popped.
10 NULL
nth
3
4
2
3
4
1
2
3
4
2
3
4
3
4
4
Call to itself
This is tail recursive function as
after returning from this function
there is no pending operation.
fib(4)
= fib(3) + fib(2)
= [1+0] + 1 + 1
fib(4) = 3
3 fib(4)
2 fib(3) 1 fib(2)
fib(1) fib(0)
1 0
1st 1st
2 nd 3 rd
3 rd
2 nd
1
1 4
2 3
2 18 34 50
State
Problem
2 4 1 4 1 4
3 1 3
3 2 2
3 8 13 19 24 29 35 40 45 51 56 61
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 6 9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
4 3 4 2 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Answer State
Solution States
3 rd
Q Q Q Q
Q Q Q
Q Q
Q
2 18 34 50
2 4 1 4 1 4
3 1 3
3 2 2
3 8 13 19 24 29 35 40 45 51 56 61
3 4 2 4 2 3 3 4 1 4 1 3 2 4 1 4 1 2 2 3 1 3 1 2
4 6 9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
4 3 4 2 3 2 4 3 4 1 3 1 4 2 4 1 2 1 3 2 3 1 2 1
5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Solution
1 st 1 st
1
(1,1)
2 nd 3 rd
Q
Q
3
1 (2,3)
(1,1)
3 rd
2 nd
4 th
Q
Q
4
(2,4)
1
(1,1)
Q
Q
2 (3,2)
4 Q
(2,4)
1
(1,1)
Q
3 (4,3) Q
1 (3,1)
4 Q
(2,4)
2 Q
(1,2)
–1
Logically deleted
10 20 30 40 50
0 1 2 3 4
front rear front
Means queue empty
This will Then Q.front ++
be item 10 20 30 40 50
Q.front 10 20 30 40 50
item
Q.front Q.rear
Stack1 Stack2
10
Stack1 Stack2
20 10
Stack1 Stack2
10
20
Stack1 Stack2
20
10
Stack1 Stack2
20
30 10
Stack1 Stack2
10
20
30
Stack1 Stack2
30
20
10
Stack1 Stack2
30
20
40 10
Stack1 Stack2
10
20
30
40
Stack1 Stack2
front rear
Q1
front rear
10
Q2
Q1
10
Q2
20
Q2
10
Q1
20 10
Q2
Q1
20 10
Q1
Q2
20 10
Q1
30
Q2
Q1
30 20 10
Q2
Q2
30 20 10
Q1
40
Q2
30 20 10
Q1
40 30 20 10
Q2
Q1
40 30 20 10
Q1
Q2
Q2
50 40 30 20 10
Q1
10 20 30 40 50
front rear
3
front
40
4 50
30 2
rear
10 20
0 1
12 12
11 1 FRONT 11 1
2 2
10 10
REAR
3 Total 3
9 number of 9 Total
elements = 5 number of
elements = 6
REAR 8 4 FRONT 4
8
7 5 7 5
6 6
12 12
11 1 11 1
2 2
10 10
3 3
9 Total number 9
of elements = 2
4 4
8 8
7 5 7 5
6 6
FRONT
REAR
6 1
5 A 2 Front
D
4 C
3
Rear
6
6
1
1
Rear 5 5 F
F A 2 Front Rear
D A
2
D
C C
4 4
3 Front 3
6 6 Front
K K
5 F 5 F
L 1 L 1
D
M Rear
4 D 4 M
2 2
Front
3 3
Rear
6 Front 6
K K
5 5
L L
1 1
4 4
R M R M
3 3 2
2
S 4
1 S
4
Rear Front
R Rear 1
M R
3
2 3 M
2
Front
Finally
S
Front Rear
rear 3
front 1 rear 1 front 2 rear 2 front 3 front 4 rear 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Front Rear
node node
temp
temp data =10
10 NULL temp next = NULL
temp
front = temp;
10 NULL
rear = temp;
front/rear
temp
10 NULL 20 NULL
front/rear
10 20 NULL
front rear/temp
10 20 30 NULL Mark node 'front' as
'temp' node.
front/temp rear
50 40 10 20 30 .... 50 40 10 20 30 ....