Docsity

Data Structures and Algorithms: A Comprehensive Guide with Examples - Prof. Kumar, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Question Bank with solution-Data Structure and Algorithm
Q.No. – 1. Write an an algorithm for insertion in linear Array.
Q.No.-2. Write an algorithm for deletion in linear Array.
Sol. 1.
Insertion in Array-
Insert(A,N,k,Value)(Inserting at kth location)
1. Set j:=N
2. Repeat steps 3 & 4 while j >=k
3. Set A[j+1]=A[j].
4. Set j:=j-1
5. Set A[k]:= value
6. Set N:=N+1
7. Exit.
Sol. 2.
Deletion in Array-
Delete(A,N,k,Value)(deletion at kth location)
1. Set value:= A[k].
2. Set j:=k.
3. Repeat 3 & 4 while j<=N-1.
4. Set A[j]:=A[j+1].
5. Set J:=j+1.
Q. No. -3. Calculate address of A(4) when base address is 1000 and each element occupies 4 memory cells.
Sol.
LOC(A[k]) = Base(A) + w(k-LB) where w is number of memory cell per element, Base(A) is base address i.e.
address of first element of Array, LB is smallest index used in Array.
For ex. If Base address is 1000 and each element occupies 4 memory cells then address of 4th element is
1000+4(4-1)=1012.
Q./No.- 4. Consider a 25 * 4 matrix array A. Suppose Base(A) = 200 and w=4 memory cells per word. Find
address of A(12,3).
Sol.
Address Calculation:-
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Data Structures and Algorithms: A Comprehensive Guide with Examples - Prof. Kumar and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Question Bank with solution-Data Structure and Algorithm

Q.No. – 1. Write an an algorithm for insertion in linear Array. Q.No.-2. Write an algorithm for deletion in linear Array. Sol. 1. Insertion in Array- Insert(A,N,k,Value)(Inserting at kth location)

  1. Set j:=N
  2. Repeat steps 3 & 4 while j >=k
  3. Set A[j+1]=A[j].
  4. Set j:=j-
  5. Set A[k]:= value
  6. Set N:=N+
  7. Exit. Sol. 2. Deletion in Array- Delete(A,N,k,Value)(deletion at kth location)
  8. Set value:= A[k].
  9. Set j:=k.
  10. Repeat 3 & 4 while j<=N-1.
  11. Set A[j]:=A[j+1].
  12. Set J:=j+1. Q. No. -3. Calculate address of A(4) when base address is 1000 and each element occupies 4 memory cells. Sol. LOC(A[k]) = Base(A) + w(k-LB) where w is number of memory cell per element, Base(A) is base address i.e. address of first element of Array, LB is smallest index used in Array. For ex. If Base address is 1000 and each element occupies 4 memory cells then address of 4th^ element is 1000+4(4-1)=1012. Q./No.- 4. Consider a 25 * 4 matrix array A. Suppose Base(A) = 200 and w=4 memory cells per word. Find address of A(12,3). Sol. Address Calculation:-

LOC(A[j,k])=Base(A) + w[(j-LB)+M(k-LB)] - CMO LOC(A[j,k])=Base(A) + w[N(j-LB)+(k-LB)] - RMO M – Total No. of Rows N – Total No. of Columns LOC(A[12,3])= 200 + 4[(12-1)+25(3-1)] CMO = 200 + 4[4(12-1)+3-1)] RMO Q. No. -5. Three dimensional Array- Suppose Array A(2:8, -4:1, 6:10), Base(A) - 200, w- 4 Find address of A(5, -1, 8) Solu:LOC(A[i,j,k)] = Base(A) + w[(E1L2 + E2)L3 + E3] RMO = Base(A) + w[(E3L2+E2)L1 + E1] CMO L1=8-2+1= L2=1-(-4)+1= L3=10-6+1= E1=5-2= E2=-1-(-4)= E3=8-6= LOC(A[5,-1,8]) = 200 +4[(18+3)5+2] RMO = 200+4[(12+3)7+3] CMO Q. No. -6.. Find relation between index of one and two dimensional array when two dimensional sparse matrix is stored in one dimensional Array. Sol. Matrices with a relatively high proportion of Zero entries are called sparse matrices. Lower Triangular Matrix- All entries above the main diagonal are zero or where nonzero entries can occur on or below the main diagonal. Upper Triangular matrix- All entries below the main diagonal are zero or where nonzero entries can occur on or above the main diagonal. Tridiagonal Matrix- Non zero entries can only occur on the diagonal or on elements immediately above or below the diagonal If we store these matrix into one dimensional array B then to access an element index will be calculated as L=j(j-1)/2 +k for LTM(1 in I row, 2 in II row , 3 in III ro. So upto j-1 row no. of elements = j(j-1)/ and upto jth row and kth col j(j-1)/2 + k L=k(k-1)/2 + j for UTM L=[3(j-2)+2] + [k-j+1] +1= 2j+k-2 for tridiagonal ( 3 elements in each row except first and last)

Question - 9. Suppose STACK is allocated N=6 memory cells and initially stack is empty, or in other words TOP=0. Find the output of following module.

  1. Set AAA:=2 and BBB:=
  2. Call PUSH(STACK,AAA). Call PUSH(STACK,4). Call PUSH(STACK,BBB+2). Call PUSH(STACK,9). Call PUSH(STACK, AAA+BBB).
  3. Repeat while TOP<> Call POP(STACK, value). Write: value.
  4. Return. Sol. Step 1. Set AAA=2 and BBB = 5. Step 2. Push AAA=2, 4, BBB+2= 7, 9 and AAA+BBB=7 onto STACK yielding STACK: 2, 4, 7, 9, 7, _ Step 3. Pop and print the elements of STACK until STACK is empty. Since top element is always popped, Output consists of the following sequence: 7, 9, 7, 4, 2 Question :- 10. Suppose a given space S of N contiguous memory cells is allocated to K=6 stacks. Describe ways that the stacks may be maintained in S. Sol. Suppose no prior data indicates that one stack will grow more rapidly than any of the other stacks. Then one may reserve N/K cells for each stack.Alternatively, one can partition the stacks into pairs and reserve 2N/K cells for each pair if stacks. The second method may decrease the number of times overflow will occur. Problem11- Consider following Arithmetic expression. Convert it into equivalent postfix expression P. Q: A+(BC-(D/E^F)G)H Solution: A+(BC-(D/E^F)G)H) Symbol Scanned Stack P ( A ( A
  • ( + A ( ( + ( A B ( + ( A B
  • ( + ( * A B

C ( + ( * A B C

- ( + ( - A B C *

( ( + ( - ( A B C *

D ( + ( - ( A B C * D

/ ( + ( - ( / A B C * D

E ( + ( - ( / A B C * D E

^ ( + ( - ( / ^ A B C * D E

F ( + ( - ( / ^ A B C * D E F

) ( + ( - A B C * D E F ^ /

* ( +( - * A B C * D E F ^ /

G ( + ( - * A B C * D E F ^ / G

) ( + A B C * D E F ^ / G * -

* ( + * A B C * D E F ^ / G * -

H ( + * A B C * D E F ^ / G * - H

) A B C * D E F ^ / G * - H * +

Question:-1 2. Translate by inspection and hand each infix expression into its post fix expression. a) (A-B)(D/E) = [AB-][DE/]=AB-DE/* b) (A+B^D)/(E-F)+G=(A+[BD^])/[EF-]+G=[ABD^+]/[EF-]+G=[ABD^+EF-/]+G=ABD^+EF-/G+ c) A(B+D)/E-F(G+H/K)=A[BD+]/E-F(G+[HK/])=[ABD+]/E-F[GHK/+]=[ABD+E/]-[FGHK/ +]=ABD+E/FGHK/+-

Q.No. – 13. Find A(1,3) using Ackermann Function.

Sol.

1. A(1,3)=A(0,A(1,2))

2. A(1,2)=A(0,A(!,1))

3. A(1,1)=A(0,A(1,0))

4. A(1,0)=A(0,1)

5. A(0,1)=1+1=

6. A(1,0)=

7. A(1,1)=A(0,2)

8. A(0,2)=2+1=

9. A(1,1)=

10. A(1,2)=A(0,3)

11. A(0,3)=3+1=

12. A(1,2)=

13. A(1,3))=A(0,4)

14. A(0,4)=4+1=

15. A(1,3)=

Q.No. – 14. Write an algorithm to delete an element in a Queue.

Question:16 Suppose each data structure is stored in a circular array with N memory cells. a) Find no. of elements NUM in a queue in terms of FRONT and REAR. b) Find the no. of elements NUM in deque in terms of LEFT & RIGHT. c) When will the array be filled? Sol. a) If FRONT <= REAR, then NUM = REAR – FRONT + 1 If REAR < FRONT, then FRONT – REAR + 1 is no. of empty cells so NUM=N-(FRONT-REAR-1)= N + REAR-FRONT + 1 Using arithmetic modulo N, we need only one foremula NUM=REAR-FRONT+1(mod N) b) NUM=RIGHT-LEFT+1(mod N) c) With a queue when FRONT =1 and REAR = N or FRONT = REAR + With a dequeue when LEFT = 1 and RIGHT = N or LEFT + RIGHT + Each of these conditions implies NUM = N Q.No. 17. Algorithm for insertion in sorted L.L. Sol. Ins(INFO,LINK,START,AVAIL,LOC,value)

  1. if AVAIL = NULL, then: Write: OVERFLOW and Exit.
  2. Set NEW:= AVAIL and AVAIL:= LINK[AVAIL].
  3. Set INFO[NEW]:= value.
  4. if LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW.
  5. Exit Finda(INFO,LINK,START,value,LOC)
    1. if START = NULL, then: Set LOC:= NULL and Return
    2. if value < INFO[START, thenm: Set LOC:=NULL and Return.
    3. Sewt SAVE:=START and ptr:=LINK[START].
    4. Repeat steps 5 & 6while ptr<>NULL.
    5. if value< INFO[ptr], then: Set LOC:=SAVE and Return.
    6. Set SAVE:=ptr and ptr:=LINK[ptr].
    7. Set LOC:=SAVE.
    8. Return INSERT(INFO,LINK,START,AVAIL,value)
    9. Call Finda(INFO,LINK,START,value,LOC)
    10. Call Ins(INFO,LINK,START,AVAIL,LOC,value)
    11. Exit.

Q. No.18. Algo for deletion. Deletion in LinkedList- Deleting a node following a giving node- Del(INFO,LINK,START,AVAIL,loc,locp)

  1. If locp = NULL, then: Set START:=LINK[START]. Else: Set LINK[locp]:=LINK[loc].
  2. Set LINK[loc]:=AVAIL and AVAIL:= loc.
  3. Exit.

Q.No. -19. Write an Algorithm for preorder Traversal of Binary Tree.

Sol. Pre(INFO,LEFT,RIGHT,ROOT)

  1. Set Top:=1, STACK[1]:= NULL, ptr := ROOT.
  2. Repeat steps 3 to 5 while ptr <> NULL
  3. Apply process to INFO[ptr].
  4. if RIGHT[ptr] <> NULL, then: Set Top:= Top + 1, Stack[Top]:= RIGHT[ptr].
  5. if LEFT[ptr] <> NULL, then: Set ptr:= LEFT[ptr] Else : Set ptr:= STACK[Top], Top:= Top – 1.
  6. Exit. Q. No. -20. Writa an algo for inorder traversal of Binary Tree.. In(INFO,LEFT,RIGHT,ROOT)
  7. Set Top:=1, STACK[1]:= NULL, ptr:= ROOT
  8. Repeat while ptr <> NULL: a) Set Top:= Top + 1, STACK[Top]:=ptr. b) Set ptr:= LEFT[ptr].
  9. Set ptr:= STACK[Top], Top:=Top-
  10. Repeat steps 5 to 7while ptr <> NULL:
  11. Apply process to INFO[ptr].
  12. if RIGHT[ptr] <> NULL, then: a) Set ptr:=RIGHT[ptr]. b) Goto step 2.
  13. Set ptr:= STACK[Top], Top:= Top + 1.
  14. Exit.

b) If deleted from left then LEFT is increased by 1(mod N).if deleted from right, RIGHT is decreased by 1(mod N). in the case LEFT=RIGHT before deletion both LEFT & RIGHT are assigned NULL to indicate deque is empty. Q. No.-24. Suppose S is the following list of 14 alphabetic characters: D A T A S T R U C T U R E S Use Quicksort algorithm to find final position of first character D. Follow Algorithm

  1. C A T A S T R U D T U R E S
  2. C A D A S T R U T T U R E S
  3. C A A D S T R U T T U R E S
  4. C A A D S T R U T T U R E S Sol. a) Since order in which subsets are sorted does not matter, LOWER and UPPER can be implemented as queues or even deques rather the stacks Q. No. -25. Suppose S consists of the following n=5 letters: A B C D E Find no. of comparisons to sort S using Quicksort. Draw general Conclusion. Beginning with E it takes n-1=4 comparisons to recognize that A is already in its correct position. Sorting S is now reduced to sorting the following sublist with n-1=4 letters. A B C D E Beginning with Eit takes n-2 = 3 comparisons to recognize that B in sublist is already in its correct position. Sorting is now reduced to sorting the following sublist with n-2=3 letters: A B C D E Consequently we have 4 + 3 + 2 + 1 = So using Quicksort it takes C=(n-1) + (n-2) + )n-3) + …. + 2 + 1 = n(n-1)/2 = O(n2) Q. No. -26. Consider Quick sort Algo. a) Can arrays LOWER and UPPER be implemented as queues rather than stacks? b) what is space complexity? . Sol. b) Quick sort Algorithm is an in-space algorithm that is elements remain in their places except for interchanges. The extra space required mainly for stacks LOWER and UPPER. On the average extra space required for algo is proportional to log n, n is no. of element to be sorted.

Q27)Explain Basic terminology of Data Structure. Ans.Basic Terminology:- Data - values or set of values Data Item /datum – single unit of value Group item – which are divided into sub items like employee name may be divided into sub items like first name, middle name, last name Elementary data item – that can’t be divided into subparts Entity – something that has certain attributes. Attributes- are properties that can be assigned some values. Entity set – Entities with similar attributes. Information- meaningful or processed data Q28) What is Data structure and also Criteria that must be taken into consideration while chosing a Data Structur Ans: Study of data structure includes 1)Logical or mathematical model description of the structure

  1. Implementation of the structure on a computer 3)Quantitative analysis of the structure which includes determining the amount of memory needed to store the structure and the time required to process the structure. In subject data structure and algorithm, data in primary memory are considered while DBMS deals with data in secondary memory Data Structure – Logical or mathematical model of a Particular organization of data is called data structure. Criteria that must be taken into consideration while chosing a Data Structure - The choice of a Particular data structure depends on following considerations. 1. First it must be rich enough in structure to mirror the actual relationships of the data in real world.
  1. Structure should be simple enough that one can effectively process the data when necessary.

INSERT(‘ABCDEFG’, 3, ‘XYZ’)=’ABXYZCDEFG’ Deletion- DELETE(‘ABCDEFG’, 4,2)=’ABCFG’ Replacement- REPLACE(‘XABYABZ’, ‘AB’, ‘C’)=’XCYABZ’ Question31- Discuss how strings are stored and various operations that are applied on strings. Sol. Refer Lipschutz page 3.2-3. Q32.what is array. Array SOLU: Data structure(DS) are classified as linear or non linear. A DS is said to be linear If its elements form a sequence or linear list or they are at same level like Arrays, Queues, Linked List. A DS is said to be non linear when its elements do not form a sequence or when they are at different levels like Trees and Graphs. One way to represent linear relationship through sequential memory Locations other way by means of pointer. Various Operations that are applied on any data structure- a)Traversal- Visiting each element exactly once. b) Search-Finding the Location of element with a given value or the record with a given key. c)Insertion- Adding a new element to the list. Various conditions may be associated with it. d) Deletion- Removing an element from the list. Various conditions may be associated with it. e)Sorting-Arranging elements in some order. f) Merging-Combining two lists into a single list. Linear Array :- Linear Array is a list of finite number n of homogeneous data elements. such that a)Elements are referenced respectively by an index set consisting of consecutive numbers. b) Elements are stored respectively in successive memory Locations. Length = UB-LB+ Linear Array are called one dimensional arrays because here each element is referenced by single subscript. Sixe(3*5) is read as 3 by 5

Representation of Array in Memory- Elements are stored in successive memory Locations in memory Computer Memory Address calculation for single dimensional array - LOC(A[k]) = Base(A) + w(k-LB) where w is number of memory cell per element, Base(A) is base address i.e. address of first element of Array, LB is smallest index used in Array. For ex. If Base address is 1000 and each element occupies 4 memory cells then address of 4 th^ element is 1000+4(4-1)=1012. Note:- A collection A of elements is said to be indexed If any element of A, which we will cal A[k] can be Located and processed in a time that is independent of K. Linear Array can be indexed. Linked List doesn’t have this property. Q33. Write an algorithm for traversing of Linear Array. Ans Traversing Linear Array- Traverse(A,LB,UB) 1.Set k:=LB. 2.Repeat steps 3 & 4 while k <= UB. 3.Apply process to A[k]. 4.Set k:=k+ 5.Exit.-- Insertion in Array-

Bubble(A,N) 1.Repeat steps 2 -4 for K=1 to N-1. 2.Set PTR:=1 and flag = 1. 3.Repeat while PTR<=N-K a)If A[PTR] > A[PTR+1], then: Interchange A[PTR] and A[PTR+1] and Set flag:= b) Set PTR:=PTR+1. 4.If flag=1, then: goto step 5. 5.Exit Q35. Write an algorithm for binary search. and linear search? Ans. Searching:- Linear Search-Linear(A,N,value,LOC) 1.Set LOC := 2.Repeat step 3 while LOC <=N and A[LOC]<> Value 3.Set LOC:=LOC+1. 4.If LOC = N+1, then: write: value does not exist. Else: Write: value exist at Location LOC 5.Exit. Complexity:- Best case Complexity = Worst case - n Average case:- Suppose Pk is probability that value appear in A[k] and suppose q is probability that value does not appear. Since algorithm uses k comparisons when value appear in A[k], the average number of comparisons is given by F(n)= 1.p 1 + 2.p 2 + …… +n.pn+(n+1).q In Particular suppose q is very small and value appear with equal probability in each Location then q=0 and pi=1/n. So F(n) = 1. 1/n + 2. 1/n + ……+ n.1/n + (n+1).0=(1+2+3+4…+n).1/n=n(n+1)/2 * 1/n=(n+1)/ Binary Search – Prerequisites are data should be sorted and one must have direct access to mid element before search begins. Binary(A,LB,UB,Value,LOC) 1.Set Beg:=LB, End:=UB, Mid:=INT((Beg+End)/2) 2.Repeat steps 3 & 4 while Beg<=End and A[Mid]<>Value 3.If Value < A[Mid], then: Set End:=Mid-1. Else: Set Beg:=Mid + 1 4.Set Mid:=INT((Beg+End)/2)

5.If A[Mid] = Value, then: Set LOC:=Mid. Else: Set LOC:=NULL. 6.Exit. Complexity – Log2 N(Log N with base 2) Limitations:- It may be difficult to keep data sorted and one may not have access to mid element. Q36)Explain Address calculation Equation for two dimenstional array. Ans. Multidimensional Array- Representation of two dimensional Array in memory 1.Column- Major Order :- Elements are stored column wise 2.Row-Major Order:- Elements are stored row wise Address Calculation:- LOC(A[j,k])=Base(A) + w[(j-LB)+M(k-LB)] - CMO LOC(A[j,k])=Base(A) + w[N(j-LB)+(k-LB)] - RMO M – Total No. of Rows N – Total No. of Columns EX. --Consider a 25 * 4 matrix array A. Suppose Base(A) = 200 and w=4 memory cells per word. Then LOC(A[12,3])= 200 + 4[(12-1)+25(3-1)] CMO = 200 + 4[4(12-1)+3-1)] RMO Three dimensional Array- LOC(A[i,j,k)] = Base(A) + w[(E 1 L 2 + E 2 )L 3 + E 3 ] RMO = Base(A) + w[(E 3 L 2 +E 2 )L 1 + E 1 ] CMO Suppose Array A(2:8, -4:1, 6:10), Base(A) - 200, w- 4 L1=8-2+1= L2=1-(-4)+1= L3=10-6+1= E1=5-2= E2=-1-(-4)= E3=8-6= LOC(A[5,-1,8]) = 200 +4[(18+3)5+2] RMO = 200+4[(12+3)7+3] CMO