Professional Documents
Culture Documents
FDS IN SEM Decode
FDS IN SEM Decode
DataStructures
SPPU-CBCs SCHEME-2019/2020 SYLLABUS-S.E. (COMP/AI&DS) SEM I -
DECODE
AGuide for
Price:105/-
ISBN 978-93-558-101-7
Engineering Students
www.e wise
Unit
Introduction to Algorithm
and Data Structures
follows
0 1 2 4
10 20 30 40 50
Ans.
Data Information.
Sr. No.
1. Data is in raw form. Information is processed form of
data.
(1 - 1)
Data Structures
Sructures
D'ata
Fundamenalsof
Data Structures
Abstract
1.2:
for an
an arrays.
arrays,
Write
ADT for
?
What is
ADT
Dec.-11,-11, Marke
Marks g8.
6, May-12, Marks
Q.3 May-10,11,
4
[SPPU:
of D
inle of D SSet ofdow
data type
is triple
a
et
of domains, F Set of
.
abstract
what is to be
only wha
only done is mentioned
Ans.:
The
axioms in which
which but
functions,
A mentioned.
not
done is
how is to be hidden.
details are
implementation
the
In ADT, all
AbstractData1ype Array
Operations
stores the desired elements at each successive
Store ()- This operation
location.
elements of the array.
display ()
-
Q.5 Write short note on Linear and Non-Linear data structure with
example.
0SPPU : Dec.-19, Marks 4
OR Differentiate between linear and non linear data structure with
example.
SPPU May-17, Marks 3]
Ans.:Linear data structures are the data structures in which data s
arranged in a list or in a
straight sequence.
For example: arrays, list.
Non linear data
structures are the data structures in
arranged in hierarchical which data may
manner. For
example : trees, grapns
ICOD
A Gulde for Emgineertng Students
orithm
ctures Introducthon to Algorithm
and Data Structures
Fumdamentals of Data Structures 1-3
1.4:Problem Solving
Q.7 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]
OR Consider any one problem and solve that problem using six
steps of problem solving. Explain each step in detail.
K[SPPU: Dec.-10, Marks 81
OR Describe the six steps in problem solving with example.
[SPPU May-11, Marks 8]
Ans.: Problem solving is based on the decisions that are taken.
Following the six steps of problem solving
are -
Fande
Before
olving any
solving any problemit is important
problem
:Uderstand
the
problem
aspects
three aspects
based on which the problem
ere are
are
There
understand
it.
can e understood
While solving
he problem
the problem the
prohleBEDase knowledgebase
.Knowiedgebase machine.I
or a
Iff the problem is to be
to a person
be related what the to know
to
then it is
can necessary
the person solved for the machin
machine then
soved for is to be solved
knows. Ifthe
problem
person known. Along with
this the problem
problem
set must be
instruction
its own instruction
set.
make use of his/her
solver can
the subject on which the
.Subject:
Before solving the problem
knoWn. For instance To solve the
is based must be
problem knoW about the
t is necessary to
problem involving Laplace,
Laplace transform.
steps inadequately.
People do not define the problem statements correctly.
They do not generate the sufficient list of alternatives. Sometimes
good
alternatives eliminated. Sometimes the merits and
might get
demerits of the alternatives is defined hastily.
The sequence of solution is not defined logically, or the focus of the
design is sometimes on detailed work before the framework solution.
.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.
Start
Read array
elements
Sum = 0
i= 1 1 n
Display Sum
Stop
Fig. a.10.1
riue Sudenrs
Introduction to Algorthm
Fundamentals of Data Structures 1-7 and Data Structures
a.11 Design and explain an algorithm to find the sum of the digits
of an integer number. [SPPU: Dec.-10, Marks 6]
Ans.
Read N
Remainder=0
Sum=0
Repeat
Remainder=N mod 10
Sum=Sum+remainder
N=N/10
Until N<0
Display Sum
End
Q.12 What is a difference between flowchart and algorithm ?
Convert the algorithm for computing factorial of given number into
flowchart. SPPU May-11, Marks 8]
Ans. Algorithm is a set of Start
instructions written in natural
language or pseudo code and
Read the
flowchart is a graphical
number'n
representation of an algorithm.
Read N
fact 1
Set i andF to 1
While i< =N i 1 1 n
F=F *i
Increase the value of i by 1
Display F fact fact» i
End
Print the value
of fact
Stop
Fig. Q.12.1 Flowchart for factorial
int a,
..1
a=10;.
printf(%d",a); . .
1.9:Complexity of Algorithm
Q.14 What is time complexity ? How is time complexity of an
algorithm computed ?
[SPPU : Dec.-11, Marks 6|
Ans.: The amount of time required by an algorithm to execute is called
the time complexity of that algorithm.
For determining the time complexity of particular algorithm following
steps are carried out
1. Identify the basic
operation of the algorithm.
2. Obtain the frequency count for this
basic operation.
3. Consider the order
of magnitude of the frequency count and express
in terms of big oh notation.
Q.15 Write an
algorithm to find smallest element in
arTAY a
integers and analyze its time complexity. p
ISPPU: May-13, Mar ks
Ans.
Algorithm MinValue(int ain])
min_element=a[0];
QECODD rdens
for(i-0;i<n;1+ +)
if (alil<min_element) then
min_element=a[i);
Write(mín_element);
We will first obtain the frequency count for the above code
1
min_element=a[0]
for(i-0;i<n;i++) n+1
if(a[i]<min_element) then n
1
Write(min_element);
Total 2n+3
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.
Definition
can write
f(n)sc'g(n)
then fn) is big oh of g(n). It is also denoted as fn) e O (g(n)). In other
words fn) is less than g(n) if g(n) is multiple of some constant c.
Cg(n)
f(n)
n
no
fn) e O(g(n))
For all n 2 no
It is denoted as f(n) e 2 (g(n)). Following graph illustrates the curve for
2 notation.
f(n)
C9(n)
n
no
Fig. Q.18.2 Omega notation f(n) e 2 (g(n))
C1 g(n) s c2 g(n)
Then we can say that
fn) E (g(n))
C2 *g(n)
f(n)
Cg(n)
n
no
Flg. Q.18.3 Theta notation f(n) e (g{n)
return Result;
double Result=1
while(N>0) N+1
Result-Result*X N
N N
return Result
Total 3N+3
Q.20 What is frequency count of a statement ? Analyze time
complexity of the following code:
i) for(i 1; i<=n; i++)
for 1; j <= m; i++)
for(k = 1; k <= p; k++)
sum=sum + i;
while(i 2 1)
SPPU Dec.-11, Marks 6, May-14, Marks 3
Solution: i)
Statement Frequency Count
for(i-1;is=n;it+) n+1
for(j-1j<=mj++) n(m+1)
fordk-1;k=p:kt++) n.m.(pt+1)
sum=sum+i | n.m.p
Total 2(n+nm+nmp)+1
ii)
while(i>=1) nt1
Total 2(n+1)
Q.21 Determine the frequency counts for all the statements in the
following program segment.
i=10;
fori=10;i<=nzi++)
forg-1:j<i:j++)
x=x+1;
SPPU : May-13, Marks 6)
Ans. Assume (n-10+2)=m. Then the total frequency coúnt is -
The outer loop will execute for m times.
The inner loop is dependant
upon the outer loop. Hence it will be
(m+1/2. Hence overall frequency
count is (1+m+m*)(m(m+1)/2)
If consider only the order of
we
magnitude of this code then overall time
complexity in terms of big oh notation is O(n^).
DECQREY
Introduction to Algorithm
Fundamentals of Data Structures 1 - 15 and Data Structures
i-10;
for(i-10;i<=n;it+) m (we assume the count of execution is m)
forj1j<ij+) m(m+1)/2)
x=X+1; mm)
Recurrence relation
T(0) = 0
(2)
Initial condition
Data
Fundamentals ef
Relation
Recurrence
1.13 Solving
T(n) = T ( n _- 1)1
the
following
r e c u r r e n c e
relation
with T0) = 0 ss
=
+7C
Ans.:Let 1)+ I1
Tn)= T(n -
By hackward s u b s t i t u t i o n .
1
-
23 n
a3 -1)C
Tn 1)= T(n -2)
+1
T(n)
=
T (n - 1)
1
(Tn 2) +1) Tn) = 2k
2) + 2
T(n) =
T(n -
2 1) 1 If we 2 = n theen
T(n
-
2)
-
Again Tn
-
2) +2
= nT(1) +(n - 1) C
(T(n -3)+ 1)+2 T(1) = 1
T(n) =
T(n -
3) +3 T(n) =
n + (n -
1) C
ii) Let,
(1)
T(n) =
T(n -
k) + k
T(n) T(3C
If k = n then equation (1) becomes
T(n)= T(O) + n
= 0+n T(0) 0 ).c-c
T(n)= n -
2C
We can denote T(n) in terms of big oh notation as
T(n) = O(n)
T| c T)-1.
3C
Ans.: i) Let
T(n) C
T Ta -a)-n
T
a
Ta 2-1)
1 a'n] Ta) =
-[T(n-2) 2) 2
T (-2r2)
IT-3) 2) 2
T(1-k T n - 2 ) - T(n -3) - 3 (2
runt =
C 1
2 2 T9)-2
Tn) 2n+1)
1 We can derane the tine umpizity in e tn d n h urasn a
ot= cunt +
return 0; 0n)
a24 What is Master's method ?
eise ie vive reitsn uin a f r l a deru vy
Ans. can reorens
Write
r e u r r e n e relation Tn) n ) ifa b
zet yets executed a leas if a b
in e avve u e the fullwing
Ans. :
1w Tn) Gn P ifa b
e a s i v e call v the functin sum, by changins relatíon.
In the else pen, thre is a Q27 Soive the following
recurrence
th vahue i n by rn 1, e h time
-
Tn) 2T(n/2) + n og n
Fundamentals ofData
Fundamentals of Data Struetures Introduction to Algorithm
1-21 and Date Structures
-( n 2 log*n)
- e(n 'log*n) T(n)- e(n*)
Hence the complexity of given recurrence relation is n).
T(n) (n log*n) Q.30 Solve recurrence relation T(n)
recurrence
relation T(n) =
9T (n/3) + 3
-
k T (mi k) +m*when T(1) -
1,
a.28 Solve the following and k is any constant.
Tn) kTn2
=
Here a =
And log 9 =2
According to case 3 in above Master theorem be a recurrence relation. Let us use substitution method
If we assume k = 2 then the equation becomes
AS fn) is =
Q (n°839+
have fn) =ns. T) = 2 7 n 2
i.e. Q(n2E) and we
Then to have f(n) =2(n). We must put e =1.
T(n) = O(f(n)
Then
Then Tn) e(fn))
Tn) =e(n
a b f(n) L
.We get a = 9, b =3 and f(n) =
n.
= 8T
%3n2
Now fn) n
a-
Tn) n)-e(n ) =@n2-)
When&-1. That means case 1 is
applicable. According to
time complexity of such
equatíons is
case 1, the
2*T +Cn2 C Constant
Tn) n " )
Ifwe put=1 then 2 =n andk- log2n
-9n')
-nT(1)+Cn
A Gulde (r Eaetacerduu Studetnt
pucoD A Gulde for Engineertng Studens
Fundamentale of Date Sttueture Introduction to Algorithm
T(1) and Data Structures
Tn)A (n)* The time complexity that we get for certain set of
same. Then for corresponding input such a time inputs as a isaverage
is
T(n) nloskh+
e
Applying case 3 (X[i=key)then
return i
ntI when e= 1
Best case time complexity
f(n) i.e. n
Best case time complexity is a time complexity when an
algorithm runs
T(n) = e(f(n)) for short time. In above searching algorithm the element key is searched
from the list of n elements. If the key element is present at first location
T(n) n 2 ) in the list(X[0...n-1]) then algorithm runs for a very short time and
thereby we will get the best case time complexity. We can denote the best
1.14 Best, Worst and Average Case Analysis case time complexity as
best1
Q.31 Explain Best case, worst case and average case analysis. Worst case time complexity
Ans.: If an algorithm takes minimum amount of time to run to Worst case time complexity is a time complexity when algorithm nns for
completion for a specific set of input then it is called best case time a longest time. In above searching algorithm the element key is searched
complexity. from the list of n elements. If the key element is present at n location
then clearly the algorithm will run for longest time and thereby we will
For example : While searching a particular element by using sequential get the worst case time complexity. We can denote the worst case time
search we get the desired element at first place itself then it is called best complexity as
case time complexity. worst n
The algorithm guarantees
that for any instance of input which is of size n,
If algorithm takes maximum amount of time to run
an
tocompletion for a the nunning time will not exceed Cwos(n). Hence the worst case time
specific set of input then it is called worst case time complexity gives important information about the efficiency of algorithm.
complexity.
For Average case time complexity
example : While searching an element by using linear
method if desired element is searching This type of complexity gives information about the behaviour of an
placed at the end of the list then we get algorithm on specific or random input. Let us understand some
worst time complexity. that for computing average case time
terminologies are required
Complexity.
CIcODD A Guidefor Engineering Students
A Gulde for
Engineering Students OIcODD
Introduction to Algori
1-24
and Data Structure
Structures
Fundamenials
of .
Data Introduction to Algorithm
search and
Fundamentals ofData Structures 1-25 and Data Structures
is for sequential
Let the algorithm search.
successfül
1.15: Introduction to AlgorlthmicDesign Strategles
P be a probability of getting
of elements in the list.
number
n is the total nro a.32 What is algorithmie design statregy ?
at i location. Hence
The first match
of the element
for every
will occur
i" element.
ability Ans.: Algorithm design strategy is a general approach by which many
first match is P/n These problems may belong to
of occurring
problems canbe solved algorithmically.
unsuccessful search is (1 -
P).
The probability of getting different areas of aputing. Algorithmic strategies are also called as
Now, we can find average case time complexity Cavg n) as algorithmic techniques or algorithmic paradigm.
with suitable
Probability of successful
search Q.33 What is divide and conquer strategy ? Explain it
Cavg (n)
=
[1+2+. +i..n]+
n
n (1-P) are possible. Hence|
If the sub problems are large enough, then divide and conquer is
n.(1-P) reapplied.
nP
n
nn+1)
2
+n (1-P) The divide and conquer technique is as shown in Fig. Q.33.1.
Thus we can obtain the general formula for computing average case time
complexity. Sub problem 1
Suppose if P = 0 that means there is no successful search i.e. we have Sub problem 1
of size n/2
of size n/2
scanned the entire list of n elements and still we do not found the desired
element in the list then in such a
situation,
Cavg (n) = On + 1/2 + n (1 - 0)
Solution to
Cavg(n) n Solutionto sub problem 2
Thus the average case sub problem 1
running time complexity becomes equal to n.
Suppose ifP I i.e. =
we successful search then
get a
F u n d a m e n t a l s
70 20 3040 10 50 60
Example of Greedy Method
Divide shortest path
Divide .Dijkstra's Algorithm is a popular algorithm for finding
source shortest
40 10 50 60 using Greedy method. This algorithm is called single
70 20 30 path algoithm.
Divide Divide
I n this algorithm, for a given vertex called source the shortest path to
Divide Divide In this algorithm the main focus is not to find only one single path but
to find the shortest paths from any vertex to all other remaining
40 50 60 vertices.
Merge Merge This algorithm applicable to graphs with non-negative weights only.
2070 30| 10 40 50 60 Consider a weighted connected graph as given below.
Merge Merge
20 30 70 10 40 50 60 7
Merge Merge 8 3
|10 20 30 40 50 60 70
Fig. Q.33.2
A Guidefor Engincering Students
OacODD A QIcODD
Gulde for Engineering Students
ntroducflu
duction to Algoriu
and Data St
1-28
dametals of
Data
Structures
ture
un
as a source and will find
wit
with other
vertices|
2
Distance
Source
Linear Data Structure
vertex
3
path-4
using Sequential Organization
A-B,
path 8
A-C,
A-D,
path oo
8 3
2.1 Concept of Sequential Organization
A-E path
3
B B-C, path =4+1 Q.1 What is arrays ?
B-D path =
4 + 3 8 Ans.: Defintion of Arays: Aray is a set of consecutive memory
path= o
locations which contains similar data elements.
B-E 3
Array is basically a set of pair-index and the value.
C 5 + 7 12 3 Syntax
C-D, path
path 5+ 3 8 data_type name_of _array [size]
C-E, 8
For example, int a [10]; double b[10] [10];
3
Here a' is the name of the array inside the square bracket size of the
array is given. This array is of integer type i.e. all the elements are of
D D-E, path 7+ 8 15
integer type in array 'a'.
But we have one shortest distance obtained from A to E and that is Q.2 Give the advantages and disadvantages of sequential
A B C E with path length 4 + 1 +3 8. Similarly other organization of data structure.
shortest paths can be obtained by choosing appropriate source and Ans.: Advantages of sequential organization of data structure
destination.
1. Elements can be retrieved stored very
or efficiently in sequential
Dijkstra's Shortest path algorithm take oElogV+VlogV) time, where organization with the help of index or memory location.
E is number of edges and V is number of vertices of a
graph. 2. All the elements are stored at continuous
memory locations. Hence
searching of element from sequential organization is easy.
Disadvantages of sequential organization of data structure
END..
1. Insertion and deletion of elements becomes
complicated due to
sequential nature.
2. For storing the data large continuous free block of memory is required.
3. Memory fragmentation occurs if we remove the elements randomly.
pIcoD A
Guide for Engineering Students
(2-1)
Linear Data Structu
2-2
using Sequentlal Organlzaton
Structures
Array as
an
2.2:
Then it will look like this-
ADT for
Arrays. Colurmn
Write an
Q.3
Ans
AbstractDataType Array
row 10 20 30
index 1 and total number of
of some size,
Instances An array A 40 50 60
n.
elements in the array
Operations:
creates an array.
1. Create () This operation
the element in an array
2. Insert) operation is for inserting
This
the elements from the
3. Delete() This operation is for deleting
-
2.4:Operations on Array
2.3: Array Overview
Q.5 Write a Python program to insert an element in an array.
Q.4 Explain the types of arrays.
Ans.
Ans. One dimensional array
dimensional array 'a' is declared as int a[10]; Python Program
The one
elements are there in Aray?")
print("\nHow many
Value stored in array
n int(input())
10 array= l
20
Index i=0
used to find
the element 2 30 for i in range(n):
40 print("n Enter element in Array")
item int(input()
array.append(item)
want to insert an element")
print("Enter the location where you
position= int(input())
Twodimensional array print("Enter the value to insert")
If we declare a two dimensional array aS
value int(input())
int a[10] [3); array=arrayl:position]+|value]+araylposition:
array.
print(array)
traversal of an
Step 2
a a2
a3
a.7 Write a Python program to delete an element from an array.
array.append(item)
print("Enter the index from where you want to delete
position int{input(0)
an element") 10 20
a3
array=arrayl:position] +arraylposition+1:] As a20] is transferred to a3 aray increment j and k pointer.