0% found this document useful (0 votes)
19 views81 pages

Ds-Unit I

The document discusses the importance of data structures in computer science, emphasizing their role in storing, accessing, and manipulating data across various fields such as operating systems, databases, and artificial intelligence. It provides examples illustrating the necessity of organized data structures for efficient problem-solving, such as binary search and library organization. Additionally, it covers various algorithmic strategies, including divide and conquer, greedy methods, and dynamic programming, highlighting the significance of algorithm analysis for optimizing performance.

Uploaded by

abishekn156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views81 pages

Ds-Unit I

The document discusses the importance of data structures in computer science, emphasizing their role in storing, accessing, and manipulating data across various fields such as operating systems, databases, and artificial intelligence. It provides examples illustrating the necessity of organized data structures for efficient problem-solving, such as binary search and library organization. Additionally, it covers various algorithmic strategies, including divide and conquer, greedy methods, and dynamic programming, highlighting the significance of algorithm analysis for optimizing performance.

Uploaded by

abishekn156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

DATA S T RU C T U R E S

UNIT - I

1
IN T R OD U C T I O N
 T H E N E E D F O R DATA ST RU C T U R E :
 The major task done in any computers are based
on 3 actives.
 Storing
 Accessing

 Manipulating data in one form to another

 Field of computer science is divided into major


categories like
 Operating Systems
 Databases

 Compilers

 Artificial Intelligence
2
 Each filed addresses these tasks of Storing, Accessing and
Manipulating data.

 Data can be used by these fields on their interest.


 The study of Data Structures and algorithms that work on
them are common to almost all applications of computer
science.
 The beginners and experienced programmers are avoid
learning Data Structures and algorithms, because they
think

 It is complicated

 No use in real life


3
 Not useful in daily job
EX A M P L E S F O R WHY DATA STRUCTURE

IS N E E D E D ?

 Example : 1
 If you need to search your roll
number in 20000 pages of PDF
document (roll numbers are
arranged in increasing order) how
would you do that?

4
 If you will try to search it randomly or in a sequential
manner it will take too much time. You might get
frustrated after some time.
 You can try another solution which is given below…
⚫ Go to page no. 10000
⚫ If your roll no. is not there, but all other roll no. in that
page are lesser than your than
⚫ Go to page no. 15000
⚫ Still if your roll no. is not there. but this time all other
roll no. is greater than your.
⚫ Go to the page no. 12500
 Continue the same process and within 30-40 seconds
you will find your roll number.
 This is called Binary Search algorithm.
 This was just a simple example and you might have
understood a little bit that why learning data structure 5
and algorithm is important in real life.
E X A M P L E 2:
 This example gives a clear idea that how important it
is to arrange or structure the data in real life.
 Now take the example of a library.
 If you need to find a book on C Programming
Language from a library.
 You will go to the computer section first, then the
Programming Language section.
 If these books are not organized in this manner and
just distributed randomly then it will be frustrating
to find a specific book.
 So data structures refer to the way we organize
information on our computer.
 Computer scientists process and look for the best way
we can organize the data we have, so it can be better
processed based on input provided. 6
 Let’s consider some examples …
 In Facebook :Can you just imagine that your
friends on facebook, friends of friends, mutual
friends they all can be represented easily by
Graph? you can apply graph to represent friends
connection on facebook.

 If you need to keep a deck of cards and arrange it


properly how would you do that? You will throw it
randomly or you will arrange the cards one over
another and from a proper deck. You can use Stack
here to make a proper arrangement of cards one
over another.

7
 If you need to search a word in the dictionary, what
would be your approach? Do you go page by page
or you open some page and if the word is not
found you open a page prior/later to one opened
depending upon the order of word to the current
page (Binary Search).

 The first two were a good example of choosing the


right data structure for a real-world problem.

 The Binary Search is a good example of choosing


the right algorithm to solve a specific problem in
less amount of time.

8
D ATA S T R U C T U R E A D V A N T A G E S
 Efficient Memory use: With efficient use of data
structure memory usage can be optimized, for e.g we
can use linked list vs arrays when we are not sure
about the size of data. When there is no more use of
memory, it can be released.
 Reusability: Data structures can be reused, i.e. once
we have implemented a particular data structure, we
can use it at any other place. Implementation of data
structures can be compiled into libraries which can be
used by different clients.
 Abstraction: Data structure serves as the basis of
abstract data types, the data structure defines the
physical form of ADT(Abstract Data Type). ADT is
theoretical and Data structure gives physical form to
9
them.
 So,Data structures are the
basic building block of any
programming language,
complex computations.

10
Need for data structures :
The major tasks of any computer based
on 3 activities :
🞄Storing
🞄A ccessing
🞄Manipulating
⦿ All
the major categories of computer
science field like OS , Databases,
Compilers, AI etc., will address the above
activities.
⦿ The study of data structures and
algorithms that work on them are common
to almost all applications of computer
field.

⦿ It is very essential that the computer


science field users should study a formal
course data structures.
What is Data Structure?
Data structure means way of storing ,
accessing , retrieving and manipulating
the data in the memory.
(or)
A data structure is a specialized format
for organizing, processing, retrieving and
storing data. While there are several
basic and advanced structure types, any
data structure is designed to arrange
data to suit a specific purpose so that it
can be accessed and worked with in
appropriate ways.
Definition :
⦿ An algorithm is a finite number of steps
to be followed to solve a problem.
⦿ The steps when accomplished in order,
starting from the initial state , will lead to
the target state.
⦿ Algorithms define a general procedure
which can be implemented in any
programming language and solved using
any type of computers .
⦿ Hence, algorithms are given in pseudo
code format.

⦿ Pseudo code is a generic way of


describing the algorithm using the
conventions of programming languages
but not using any programming
language.
⦿ The efficiency of the algorithm plays a
major role in determining the efficiency of
the program when implemented.

⦿ The choice of the data structures selected


for the implementation of the algorithm also
adds to the efficiency of the program.
⦿ Each step should be precisely defined.
⦿ There must be a flow of control from the
start to the end point of the algorithm.
⦿ All possible instance of the problem
should be considered.
• i:e there must be conditions to check
all cases of inputs
⦿ Problem solving is part of the thinking
process.
⦿ It describes the steps to proceed from a
given state to a desired goal state.
⦿ Four basic steps to solve a problem :
1. Look at the problem
2.Select the strategy
3.Solve
4.Final Check
1. Look at the problem :
Check whether we have seen similar problems
earlier. If yes, find out how this problem is similar to
or different from earlier one.

2. Select the strategy :


Based on the earlier experience and on the knowledge
of the existing strategies, select one which we think can
solve the problem. Later we can even improve or
modify the strategy.

3. Solve :
Using the strate gy selected, solve the problem.
4. Final Check :
Check whether we have a reasonable
answer.
⦿ Problem solving strategies fall into any one
of the following categories:
1.Compute
2.Use formula
3.Make a model
4.Tabularize
5.Guess
6.Reduce to a simpler problem
7.Eliminate
8.Find out the pattern
1. Compute:
 Straightforward & involves the application for
arithmetic rules.
 Eg : given 7X-3=25, find X.

2. Use Formula:
 Common category where formulas are available
for solving problem.
 Eg : find area of a Square, Circle, etc.,

3. Make a Model:
 A problem can be solved by making a model in
order to visualize it.
 Model can be described using equations, which
can be then solved.
4. Tabularize:
 A Table or a list can be prepared with the available
solutions.
 The solution for the instance can be then found from
the table.
5. Guess:
 Makes a guess at the result. After making the guess
we have to check it , the solution is correct.
 If not, we have to revise the guess and repeat till we
get the correct solution.
6. Reduce to simpler problem:
 A problem can be simply solved if its size is
smaller.
7. Eliminate:
 Solve the problem by eliminating all in correct
solutions.
 Thus obtain the correct solution.

8. Find out the Pattern:


 A pattern is a regular, systematic repetition.
 A problem can be solved by determining the
pattern in the data.
 This strategy is useful in application like
weather forecasting, stock forecasting.
⦁ The commonly used problem solving
strategies are
 Divide and Conquer
 Greedy Method
 Backtracking
 Dynamic Programming
 Branch and Bound
⦁ A Problem is divided into smaller units.
⦁ Each unit is solved independently .
⦁ The results are combined to give the overall result
of the problem.

⦁ Applications:
• Quick sort

• Merge sort

• Binary Search
⦁ A greedy algorithm is an algorithmic strategy
that makes the best optimal choice at each
small stage with the goal of this eventually
leading to a globally optimum solution.
⦁ It is based on heuristics (not focused on
efficient run times or optimal solutions)
⦁ This means that the algorithm picks the best
solution at the moment without regard for
consequences.
⦁ Greedy methods do not guarantee an optimal
solution.

⦁ Applications
◦ Prim’s algorithm
◦ Kruskal’s algorithm
◦ Minimum cost spanning tree
⦁ Backtracking is an algorithm technique and it is
used to find solutions for the problems in which
• ⦁ This algorithm terminates
certain constraints have to be satisfied.
when there more solutions to the
arefirst
no
sub problem.
• ⦁ Applications :
◦ n-Queens problem
◦ Hamiltonian Cycle problem
◦ Subset problem
⦁ This problem is to place n queens on an n*n
chessboard such that no two queens are in
the same row or columns or diagonal.

⦁ The following illustrates how this problem is


solved using backtracking.
⦁ This strategy is used to solve multistage
decision problems.
⦁ The Dynamic programming approach is
similar to that of divide and conquer rule.
⦁ The problem should be able to be divided
into smaller overlapping sub-problem.
⦁ An optimum solution can be achieved by
using an optimum solution of smaller sub-
problems.
⦁ Dynamic algorithms use memoization
(I:e recording partial results)

⦁ Applications :
• Warshall’s Algorithm

• Floyd’s Algorithm

• Optimal Binary Search Trees


⦁ Branch and bound is a systematic method for
solving optimization problems.
⦁ Branch and bound is a rather general
optimization technique that applies where the
greedy method and dynamic programming fail.
⦁ It also finds the optimal solution by keeping the
best solution found up to that point.
⦁ The partial solutions which cannot improve the
best solution are ignored.
⦁ In branching step, it finds the possible
solution of the sub problem.
⦁ In bounding step, it discards the solution of
the sub problem if it is worse than the best
solution found so far.

⦁ Applications:
◦ Knapsack problem
◦ Traveling salesman problem
◦ Assignment Problems
⦿ Analysis is what we do before coding.
⦿ Analysis of algorithms is more important
for the following reasons :
1. Analysis is more reliable
than experimentation or
testing.
2. Analysis helps to select better
algorithms.
3. Analysis predicts performance.
4. Analysis identifies scope of
improvement of algorithm. 1
1. Analysis is more Reliable than
Experimentation or Testing :
Analysis gives the performance of the
algorithm for all cases of inputs, whereas
testing is done only on specific cases of
input.

2. Analysis Helps to Select Better Algorithms :


When there are different algorithms for
the same task, analysis helps to pick out
the most efficient algorithm.

2
3. Analysis Predicts Performance :
Analysis can be used to predict the run
time of the algorithms. If the algorithm is
analyzed to be very slow, then it need not be
implemented at all.

4.Analysis Identifies Scope of Improvement of


Algorithm :
Analysis of an algorithm can find out
which portion of the algorithm is faster and
which is slower. The slower part may then be
modified, if possible, to increase execution
time.

3
⦿ The abstract operations count is used to
analyze the algorithm.

⦿ Following the a few examples of


operations count.

⦿ Inthe examples, we shall consider only


the major looping constructs and not the
complete program.

4
Print the first element of the array
Statement :
printf(“% d” , data[0]);

⦿ This statement is executed once


irrespective of the size of the input.

⦿ Hence, the abstract operation count is a


constant T(n) = 1.
5
Print the elements of an array Statement :

for(i=0;i<n;i++)
printf(“%d” , data [i]);

⦿ This statement is executed n times.


⦿ Hence, the abstract operation count is
T(n) = n .

6
Print the elements of the n*n array
Statement :
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf(“%d”,data[i][j]);
⦿ This statement is executed n times for
inner loop and n times for outer loop.
⦿ Hence, the abstract operation count is
T(n) = n2 .

7
Multiplication of Two Matrices Statement :
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
sum=0;
for (k=0;k<n;k++)
{
sum=sum+mat1[i][k] * mat2[k][j]);
}
result[i][j]=sum;
}
}
8
⦿ Abstract operation count in innermost
loop is n.

⦿ Abstract operation count in middle loop


is n(1+n+1)=n(n+2) = n2 +2n.

⦿ Abstract operation count in outer loop is


n*(n2 +2n) = n3 + 2n2 .

⦿ Hence T(n) = n3 + 2n2 .

9
Nested Loop Statement :
for(i=0;i<n;i=i*2)
for ((j=0;j<n;j++)
printf(“%d”,data [i][j]);

⦿ Abstract
operation count in inner loop is
n and outer loop is logn.

⦿ HenceAbstract operation count


T(n)=nlogn.

10
Print the first even number in the array:

for(i=0;i<n;i++)
if (data[i]%2==0)
return data[i];

Best case : First element is even then T(n)=1.


Worse case : Last element is even then T(n)=n.
Average case : Even element being anywhere
in the array. So T(n) is difficult to derive. 11
⦿ Time complexity is a concept in
computer science that deals with the
quantification of the amount of time
taken by a set of code or algorithm to
process or run as a function of the
amount of input.
⦿ In other words, time complexity is
essentially efficiency, or how long a
program function takes to process a
given input.
12
⦿ The time complexity of an algorithm
indicates the run time of the algorithm as
a function of the size of the input.

⦿ Some of the classes are:


1.Constant (1)
2.Logarithmic (log n)
3.Linear (n)
4.Linearithmic (n log n)
5.Quadratic
6.Exponential
13
1. Constant :
Algorithms that perform fixed number
of steps irrespective of the input size fall in
this category.
2. Logarithmic (log n) :
Algorithms that break the problem
into smaller ones belong to this class.
3. Linear (n) :
Algorithms that work with every
element of the input will have linear time.

14
4. Linearithmic (n log n):
Algorithms that break the problem into
smaller ones and combine their results
together will have linearithmic time.
5. Quadratic:
This class of algorithms works with
every pair of elements in the input.
6. Exponential:
Algorithm that are based on
brute-force search to find out solutions
satisfying a condition will have exponential
time.
15
ARRAYS

1
INTRODUCTION
 An array refers to a finite ordered collection of
homogeneous elements stored in contiguous
location in memory.

 The basic operations on arrays include extraction


and storing.

 An array is an example of static allocation of

memory.

2
F E A T U R E S OF AN ARRAY

⦿ A finite collection refers to the size of the array.


⦿ The elements of the array are same type.
⦿ Arrays are ordered collection because we have a distinct
ordering of the elements like first, second, third, etc.,
⦿ Each element of the array is stored adjacent to each
other.
⦿A block of memory is allocated for all the elements of the
collection at one time.
⦿ All elements of an array are referred by a common
name. 3
R A N G E OF AN A RRA Y
 Individual elements of an array referred with the
index.

 Index gives the relative position of the element (offset)


from the starting position of the array.

 First index – lower bound

 Last index – upper bound

 Range – number of elements in the array.


 First index – lower bound – it depends on the
programming languages. 4
RA N G E OF AN ARRAY C ONT …
 For example, languages like PASCAL are flexible
as they allow the programmers to fix up the
lower bound, i.e. an index starts from 0,10,-5 any
value convenient to the programmers.

 Last index – upper bound – fixed according to the


size of the collection to be stored in the array.

 Languages like C, C++, Java fix the lower bound


as 0 and upper bound as n-1 where n is the size

of the collection. 5
Example :
int data[10];
 Array of 10 integers values

 Lower bound 0, upper bound 9, range 10

6
PRIMITIVE OP E RAT I ONS
The primitive operations on array is,
1.Storing – A value is stored in an element of the
array.

data[i]=x;
2. Extracting – Getting the value of an element
stored in the array.

x=data[i];

7
ELEMENT ACCESS IN AN A R R AY

 Each element in an array accessed by using an


index.
 An index is only the logical numbering of the
data in the collection and not the byte address
from the beginning of the array.

8
ADDRESSING FUNCTION

 Addressing function is a function that gives the


starting byte address of an element.
 These addressing functions give a mapping
between the logical numbering of the elements in
the array and the physical byte address.

 Integer – 2 bytes

 Char – 1 byte

 Float – 4 bytes 9
ARRAY T Y P E S
 One dimensional array
 One-column array

 Eg : str[10];

10
11
T WO DIMEN SION AL ARRAY

• 2 values of array i:e Row value &Column value


• 2D arrays are logical representations.
• The memory storage is in single dimension.

12
• Storage Representation can be done in
• Row major order representation
• Column Major order representation

13
EXAMPLE F O R ROW & COLUMN MAJOR :

14
Addressing Function Of Both Representation

15
 Multi Dimensional Array

 These kind of array refers to higher dimensions.


 To find the number of elements in an array is,
Π (ubi-lbi+1), where i = 1 to n

16
18
S P E C I A L T Y P E S OF M A T R I C E S
 Special types of matrices have characteristic
positioning of the non-zero elements.

 There are 4 types

 Lower Triangular Matrices

 Upper Triangular Matrices

 Tridiagonal Matrices

 Sparse Matrices

2
LO W E R T R I A N G U L A R MATRICES

 A lower-triangular matrix is a matrix which only has


nonzero entries on the downwards-diagonal and
below it.

 An efficient usage of storage is, to store non-zero


elements.

3
 So the representation is, if i >= j then it is a non-zero
element.
U P P E R TR I A N G U L A R MA T R I C E S

 An upper triangular matrix is a square


matrix in which all entries below the main
diagonal are zero.

 The representation is, if i <= j then it is a non-


zero element. 4
T R I DI AGONA L M AT R I C E S

 A tridiagonal matrix is one which only the


elements in main diagonal, and the diagonals
immediately above and below it are nonzero
elements.
 For example, the following matrix is tridiagonal:

 The total number of non-zero elements is 3n-2.


 Here n= 4, so 3 * 4 – 2 =10 non-zero elements.
5
 So 3n-2 memory location is needed to store non-
zero elements.
 Example 2: Tridiagonal Matrix

 The total number of non-zero elements is 3n-2.


 Here n = 6. So the total number of non-zero
elements is 3 * 6 - 2 = 16.
 So 3n-2 memory location is needed to store non-
zero elements. 6
S PA R S E M AT R I C E S
 Sparse matrix is a matrix which contains very few
non-zero elements.
 ie., it contains the number of non-zero elements is
less compared to zero elements and also in random
position.

You might also like