Ds-Unit I
Ds-Unit I
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
Compilers
Artificial Intelligence
2
Each filed addresses these tasks of Storing, Accessing and
Manipulating data.
It is complicated
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.
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).
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.
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.
⦁ 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.
⦁ Applications :
• Warshall’s Algorithm
• Floyd’s Algorithm
⦁ 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
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.
3
⦿ The abstract operations count is used to
analyze the algorithm.
4
Print the first element of the array
Statement :
printf(“% d” , data[0]);
for(i=0;i<n;i++)
printf(“%d” , data [i]);
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.
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.
10
Print the first even number in the array:
for(i=0;i<n;i++)
if (data[i]%2==0)
return data[i];
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.
memory.
2
F E A T U R E S OF AN ARRAY
of the collection. 5
Example :
int data[10];
Array of 10 integers values
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
8
ADDRESSING FUNCTION
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
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
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.
Tridiagonal Matrices
Sparse Matrices
2
LO W E R T R I A N G U L A R MATRICES
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