Download - Mitra.ac.in

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

B-tree wikipedia , lookup

Quadtree wikipedia , lookup

Binary search tree wikipedia , lookup

Array data structure wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
Department of Information Technology
Data Structure
Semester IV (4IT01)
Question Bank
Prepared by Prof. S. P. Thakare
Data Structure Question Bank
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 1
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
UNIT I
1) What is a data structure? What are the types of data structures?
Ans: Data may be organized in many different ways; the logical or mathematical model of a
particular organization of data is called a data structure. The choice of a particular data model
depends on two considerations. First, it must be rich enough in structure to mirror the actual
relationships of the data in the real world. On the other hand, the structure should be simple
enough that one can effectively process the data when necessary.
Data structure have following types
Primitive and nonpremitive
2) What are list of areas in which data structures are applied extensively?
3) For each of the following patterns P and texts T, find the number C of comparisons to
find INDEX of P in T using the “Slow Algorithm:
a. P=abc, T=(ab)5 ii)P=abc, T=(ab)2n iii) P=aaa, T (aabb)3 .
4) Consider algorithm, which finds the location LOC and the value MAX of the largest
element in an Array DATA with n elements. Consider the complexity function C (n),
which measures the number of times LOC and MAX are updated in steps:
a. Describe and find C (n) for the worst case.
b. Describe and find C (n) for the best case.
c. Find C(n) for the average case when n=3, assuming all arrangements of the
elements in data are equally likely.
Ans: a) The worst case occurs when the elements of DATA are in increasing order, where
each comparison of MAX with DATA[K] forces LOC and MAX to be updated. In this case
C(n)=n-1.
b) The best case occurs when the largest element appears first and so when the comparision
of MAX with DATA[K] never forces LOC and MAX to be updated. Accordingly , in this
case, C(n)=0
c) Let 1,2 and 3 denote, respectively, the largest, second largest and smallest elements of
DATA. There are six possible ways the elements can appear in DATA, which correspond to
the 3!=6 permutations of 1,2,3. For each permutation p, let np denote the number of times
LOC and MAX are updated when the algorithm is executed with input p. The six
permutations p and the corresponding values np follow:
Permutation p: 123 132
213
231
312
321
Value of np:
0
0
1
1
1
2
Assuming all permutations p are equally likely
0+0+1+1+1+2 5
C(3)=
=6
6
5) Consider the pattern P=aba. Use the slow pattern matching algorithm to find the number
of comparisons C to find the index of P in each of the following text: i)a10 ii) (aba)10.
6) Linked storage of strings is more efficient than other forms of storage. Justify the
statement with an example.
7) The complexity of an algorithm depends not only on the size n of input data but also on
particular data. Justify the statement with an example.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 2
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
8) Consider the array A: 8 9 7 5 4 3 11. Write an efficient algorithm to find sum of all even
and odd numbers in A.
9) Explain the different structures for storing the strings.
10) Write an algorithm for linear search and obtain an expression for its complexity.
11) Consider a text string T= (abcde)5. Determine the number of comparisons C required to
find the index I of the following pattern strings P1 in the text string T:
a. P1-abcde ii)P2-cde iii) eabcd iv)ijkf
12) Write a procedure FIND (DATA, N, LOC1, LOC2) which finds the location LOC1 of
largest element and the location LOC2 of the element in an array DATA with N>1
elements.
13) Define the following
a. Space-time tradeoff of algorithm
b. Big O notation
c. Complexity of an algorithm
Ans: The analysis of algorithms is major task in computer science. In order to compare
algorithms, we must have some criteria to measure efficiency of our algorithms.
a) Space-time tradeoff of algorithm: Suppose M is an algorithm, and suppose n is the size of
the input data. The time and space used by the algorithm M are the two main measures for
the efficiency of M. The time is measured by the counting the number of key operations –
in sorting and searching algorithms, ex. Number of comparisons. That is because key
operations are so defined that the time for the other operations is much less than or at
most proportional to the time for the key operations. The space is measured by counting
the maximum of memory needed by the algorithm.
b) The complexity of an algorithm M is the function f(n) which gives the running time
and/or storage space requirement of the algorithm in terms of the size n of the input data.
Frequently, the storage space required by an algorithm is simply a multiple of the data
size n. accordingly, unless otherwise stated or implied, the term “complexity” shall refer
to the running time of the algorithm. The two cases one usually investigates in complexity
theory are as follows:
1. Worst case: the maximum value of f(n) for any possible input.
2. Average case: The expected value of f(n).
3. Best case: sometimes we also consider the minimum possible value of f(n).
The average case analysis assumes a certain probabilistic distribution for the input data; one
such assumption might be that all possible permutations of an input data set are equally likely
14) What are linear and non linear data structures?
15) Consider the pattern P=abc. Using the slow pattern matching algorithm, find the number
C comparisons to find the INDEX of P in each of the following texts T:
a. a10 ii) (aba)10 iii) d10 iv) (cbab)10 where n>3
16) For each of the following pattern P and text T, find the number of C comparisons to find
INDEX of P in T using slow pattern matching algorithm.
a. P=a b c, T=(ab)5 ii)P=ab c, T=(ab)20 iii)=a a a, T=(a a b b)3
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 3
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
17) Consider the following strings:
S=’JOHN PAUL JONES’
T=’A THING OF BEAUTY IS A JOY FOREVER’
Determine
1)INDEX(S,’JO’) 2)INDEX(S,’JOY’)
3)INDEX(S,’DJO’) 4)INDEX(T,’A’)
5)INDEX(T,’THE’) 6)SUBSTRING(S,4,8) 7)SUBSTRING(T,10,5)
18) Write a procedure which counts the number Num of paragraph in the short story.
19) Write the algorithm for linear search and explain its complexity?
20) Prove that complexity of first pattern matching algorithm is O(n2).
21) What are the various operations associated with word processing? How they are
implemented using basic string operations?
22) Describe in brief the meaning of static, semistatic and dynamic variables. Give the
advantages of each.
23) Consider the pattern P=aaabb. Construct the table and the corresponding labeled directed
graph used in the ‘fast’ pattern matching algorithm.
24) Find the table and corresponding graph for the second pattern matching algorithm where
the pattern P=ababab.
25) Describe in brief traversing, sorting and searching. Define in one line inserting and
deleting.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 4
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
UNIT II
1) Differentiate between linear search and binary search
2) Explain and write the algorithmic steps for linear search and find its complexity
3) Consider a linear array A with following elements A: 1 0 5 10 25 30 Name and explain
the efficient algorithm to search an item 10. Show all the steps.
4) Consider a linear array A with following elements A: 10 15 20 5 25 30 35 Name and
write the algorithm to search an item 5 in A.
5) What is bubble sort? And write steps of bubble sort algorithm for following example: 32
51 27 85 66 23 13 57
6) Write an algorithm Consider the string S=” AMRAVATI”. Apply bubble sort to arrange
the characters in S in alphabetic order. Show all the passes. Also find the number of
comparisons and number of exchanges.
7) for binary search. Let DATA be 10 elements stored array: 10 13 15 18 20 25 28 30 35 40
8) Apply binary search to DATA for searching item 10. Show all the step.
9) Explain representation of variable length records in memory.
10) Using bubble sort find the number of comparisons and the number of interchanges which
alphabetize the letters in NAGPUR. Show all intermediate passes.
11) Consider the following multidimensional arrays: X (-5:5, 3:33) Y (3:10, 1:15, 10:20)
a. Find the length of each dimension and the number of elements in X & Y.
b. Suppose Base (Y) =400 and there are W=4 words per memory location, find the
effective indices E1, E2, E3 and the address of Y [5, 10, 15] assuming
i. Y is stored in row major order
ii. Y is stored in column major order.
12) Prove the following identity
𝑛(𝑛+1)
1+2+3+-------+n=
2
13) Using bubble sort algorithm, find the number C of comparisons and the number D of
interchanges which alphabetize the n=6 letters in PEOPLE.
14) An array contains 25 positive integers. Write a module which
a. Find all pairs of elements whose sum is 25.
b. Finds the number EVNUM of elements of A which are even, and the number
ODNUM of elements of A which are odd.
15) Suppose multidimensional array A and B are declared using A (-2:2, 2:22) and B (1:8, 5:5, -10:5)
i. Find the length of each dimension and the number of elements in A and B.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 5
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
ii. Consider the element B [3, 3, 3] in B. Find the address of the element, assuming Base
(B) =400 and w=4 words/memory location.
16) Explain binary search algorithm with example in detail.
17) Consider two matrices A and B of the order m x p and p x n respectively give and explain
the algorithm for matrix multiplication of A and B.
18) What is a pointer array? What is the need of pointer array? Explain with example.
19) Explain sparse matrices and give its memory representation.
20) Modify the binary search algorithm, so that it becomes a search and insertion algorithm.
21) Suppose a hospital wants to keep record of 40 new born babies which contains the
following data items: Name, Sex, Birthday, Father, Mother. Suppose father Birthday is
group item with subitems Month, Day and Year and Father and Mother are group items
with subitems Name and Age. Give the record structure for the above example.
22) Explain why a 2D array is not suitable representation for sparse matrix? Suggest and
explain an efficient representation for a sparse matrix.
23) Name data structure which efficiently stores the contents of two dimensional arrays when
all the elements above the main diagonal are zero. Obtain an expression for L such that
B[L]=Ajk
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 6
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
UNIT III
1) Describe the garbage collection and their memory allocation.
2) Discuss the advantages, if any, of a two-way list over a one-way list for each of the
following operations:
i.
Traversing the list to process each node.
ii.
Deleting a node whose location LOC is given.
iii.
Searching an unsorted list for a given element ITEM
iv.
Searching a sorted list for a given element ITEM.
v.
Inserting a node before the node with a given location LOC.
vi.
Inserting a node after the node with a given location LOC.
3) Explain in detail traversing linked list with algorithm.
4) Explain linked list and insert a node into middle of linked list with example.
5) Write an algorithm to reverser a linked list.
6) Let LIST be a linked list in memory. Write an algorithm to count the number of nodes in
the list.
7) Consider a polynomial p(x,y,z)=8x2y2z-6yz+3x3yz+2xy7z-5x2y3-4xy7z3. Rewrite the
terms of p so that the terms are Lexicographically ordered. Suppose the terms are sorted
in the order shown in original p in linear arrays COEF, XEXP, YEXP, and ZEXP. Assign
values to the array LINK so that the linked list contains the ordered sequence of terms.
Draw the desired data structure.
8) Let LIST be a linked list containing integers. Write an algorithm to find the sum of
elements on the LIST.
9) Write an algorithm for traversing a circular header list.
10) Write an algorithm for insertion into a sorted linked list.
11) Write the advantages of a two-way list over a one way list for the following operations:
i.
Deleting a node whose location LOC is given.
ii. Inserting a node before the node with a given location LOC.
12) Write a procedure SORT (INFO, LINK, START) which sorts a list without changing
values in INFO.
13) Write an algorithm to insert ITEM in singly linked list so that ITEM follows the node
with location LOC or inserts ITEM as the first node when LOC=NULL.
14) Write an algorithm to delete a node from doubly linked list.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 7
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
15) Consider the alphabetized list of patients:
START
5
AVAIL
10
1
2
3
4
5
6
7
8
9
10
11
12
Kiran
Deepak
Manoj
Arun
Lalit
Gouri
Sahil
Fatima
Nancy
7
6
11
12
3
0
4
1
0
2
8
9
Determine the changes in the data structure if:
i. Walter is added to the list and then
ii. Kiran is deleted and then
iii. Rohit is added and then
iv.
Sahil is deleted.
Draw the diagrammatic representation after every change.
16) Text P(x) denote the following polynomial P(x)=9x3+7x2-3x+8. Give the diagram to
represent P(x) by header list. Draw an array representation of this header list.
17) Write the algorithm for traversing in one way list.
18) What is meant by garbage collection? Explain the overflow and underflow condition.
19) Following is the list in memory:
START
4
AVAIL
3
i.
ii.
iii.
1
2
3
4
5
6
7
8
A
B
C
D
E
F
2
8
6
7
0
0
1
5
Find sequence of characters in linked list.
Suppose F and then C are deleted from list and then G is inserted at the beginning
of the list. Find final structure
Suppose G is inserted at the beginning of list and then F and and then C are
deleted from structure. Find the final structure.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 8
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
20) Suppose LIST is header circular list in memory. Write an algorithm which deletes the last
node from LIST.
21) Let P(x)=2x8-5x7-3x2+4 Give a diagram to represent p(x) by header list. Draw an array
representation of this header list.
22) What is header linked list? Explain with example different kinds of header linked lists.
23) Suppose NAME1 is a list in memory. Write an algorithm which copies NAME1 into a list
NAME2.
24) Consider the following polynomial
P(x,y,z)=2xy2z3+3x2yz2+4xy3z+5x2y2+6y3z+7x3z+8xy2z5+9
i.
Rewrite the polynomial so that the terms are ordered lexicographically.
ii. Suppose the terms are stored in parallel arrays COEF, XEXP, YEXP, and ZEXP
with the header node first. Assign values to an array LINK so that the linked list
contains the ordered sequence of terms.
25) Explain a two way list is a linear collection of data elements, draw schematic diagram of
such list. Write advantages of two-way header lists over one way header lists.
UNIT IV
1) Describe the algorithm for TOWERS of HANOI with their steps.
2) Define the following:
i.
DEQUES
ii.
PRIORITY QUEUES
iii.
STACK
3) Consider following arithmetic expression P in postfix notation: P: 5, 6, 2, +,*, 12, 4, 1, Evaluate the expression and hence write the algorithm evaluation of postfix expression.
4) Write algorithm for linked representation of stack (PUSH and POP)
5) Consider the postfix expression AB+CDF/-*. Let A=1, B=2, C=3, D=4, and F=5.
Evaluate the expression and hence write the algorithm for evaluation of postfix
expression.
6) What is queue? Assuming array representation writes an algorithm for insertion and
deletion from a queue.
7) Consider the infix expression (A+B)*C-D/F. Covert the expression to reverse the polish
notation. Show all the steps. Hence write the algorithm for the same.
8) What is stack? Write an algorithm for push and pop operations on stack.
9) Consider the following arithmetic expression Q:A+(B*C-(D/E+F)*G)*H. Using the
algorithm for the transforming infix to postfix transform Q to postfix form. Show the
symbol scanned; stack contents and intermediate expression at each step.
10) Write procedure for inserting into a linked queue.
11) What is priority queue? Give array representation of priority queue.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 9
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
12) What is priority queue? Give one way list representation of a priority queue.
13) Consider following stack where STACK is allocated N=4 memory cells:
STACK : aaa bbb
ccc
TOP
:1 2
3
Describe the stack as the following operations take place:
i.
Push(STACK, ddd)
ii.
Pop(STACK, item)
iii.
Push(STACK, eee)
iv.
Push(STACK, fff)
14) Translate by inspection and hand, each infix expression into its equivalent postfix
expression:
i.
( A-B)*(D/E)
ii.
(A+B↑D)/(E-F)+G
15) Consider following stack where STACK is allocated N=6 memory cells:
STACK: AA DD
EE
FF,-------,-------Describe the stack as the following operations take place:
i.
Push(STACK, GG)
ii.
Push(STACK, KK)
iii.
Pop(STACK, item)
iv.
Push(STACK, LL)
v.
Pop(STACK, item)
vi.
Push(STACK, TT)
vii.
Push(STACK, RR)
16) Suppose S is the following list of 14 alphabetic characters: DATASTRUCTURES.
Suppose the characters in S are to be sorted alphabetically. Use quick sort algorithm to
find the final position of first character D. Show intermediate steps.
17) Write an algorithm to evaluate postfix expression and solve following with the help of
algorithm P:3,6,2,+,*,12,3,1,18) Suppose S consists of the following n=5 letters: A B C D E. Find the number of
comparisons to sort S using quick sort.
1.
2.
3.
4.
5.
6.
Unit VI
Explain merge sort and sort the following 14 elements:
66, 33, 40, 22, 55, 88, 60, 11, 80, 20, 50, 44, 77, 30.
Explain the algorithm for topological sort and find a topological sort T of a graph S
without cycles.
Sort the following elements by using Radix sort algorithm and find its complexity: 348,
143, 361, 423, 538, 128, 321, 543, 366.
Assume that an array A contains 8 elements. A: 80, 20, 25, 10, 90, 100, 70, 60. Apply
selection sort to sort the elements of A in ascending order. What will be the best case for
this algorithm?
Assume that array A contains 8 elements :
77, 88, 99, 100, 10, 20, 25, 18. Apply selection sort to sort the numbers in ascending
order.
Assume that an array A contains 8 elements :
A: 90, 80, 70, 20, 15, 5, 7, 8. Apply insertion sort to A so that all the elements of A are
arranged in descending order.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 10
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
7. Sort the following 8 elements by using insertion sort. Also explain the complexity of
insertion sort algorithm:
77, 33, 44, 11, 99, 22, 66, 55.
8. Write an algorithm for breadth-first search.
9. Write an algorithm for depth-first search.
10. Mention the search technique in which the search time is independent of the number of
elements in the collection. Explain the different challenges for this technique.
11. What is collision? What are the different collision resolution techniques? Explain any two
of them.
12. Draw and explain the linked representation of the following graph:
A
D
B
C
13. Suppose the following graph G represents the daily flights between coties of some airline,
find minimum path P from A to J.
14. Find the adjacency matrix A of the following graph G. Also explain whether G is strongly
connected or not?
X
Y
Z
W
15. What is meant by Hash function? Give the three methods for calculating Hash function.
16. Suppose the table T has 11 memory locations. T[1], T[2], . . . . .T[11] and suppose the file
F consists of 8 records, A, B, C, D, E, X, Y and Z : with following hash addresses:
Record: A, B, C, D, E, X, Y, Z
H(K) : 4, 8, 2, 11, 4, 11, 5, 1
Calculate the average number S of probes for successful search and average number U of
probes for an unsuccessful search.
17. Explain the method for traversing through the graph: with example.
18. Give and explain Warshall’s algorithm with example.
19. Explain Warshall’s algorithm for finding the shortest path with suitable example.
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 11
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
20.
Assignment Questions
Unit II
Que.1: Let A be an n x n square matrix array. Write a module which
a) Finds the number NUM of nonzero elements in A
b) Finds the SUM of the elements above the diagonal, i.e., elements A[I,J] where
I<J
c) Finds the product PROD of the diagonal elements (a11, a22,…..,ann)
Solution:
a)
1. Set NUM:=0
2. Repeat for I=1 to N:
3. Repeat for J=1 to N:
If A[I,J]!=0, then: Set NUM=NUM+1
[End of inner loop]
[End of outer loop]
4. Return.
b)
1. Set SUM=0
2. Repeat for J=2 to N:
3. Repeat for I=1 to J-1:
Set SUM=SUM+A[I,J]
[End of inner Step 3 loop]
4. Return
c)
1. Set PROD=1
2. Repeat for K=1 to N:
SEt PROD= PROD*A[K*K]
[End of loop]
3. Return
Que.2: Consider the following multidimensional arrays: X (-5:5, 3:33) Y (3:10, 1:15,
10:20)
a. Find the length of each dimension and the number of elements in X & Y.
b. Suppose Base (Y) =400 and there are W=4 words per memory location, find the
effective indices E1, E2, E3 and the address of Y [5, 10, 15] assuming
i) Y is stored in row major order
ii) Y is stored in column major order.
Solution:
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 12
Prof. Ram Meghe Institute of Technology & Research, Badnera-Amravati (M.S.)
a) The length of a dimension is obtained by:
Length = upper bound – lower bound + 1
Hence find Li of the dimensions of A, L1, L2, L3
b) Find effective index Ei is obtained form Ei=ki-LB, Where Ki is the given index and LB is
the lower bound. Hence found E1, E2, E3
c) The address depends on whether the programming language stores B in row-major order or
column-major order.
Formulas:
For Column major order
Base(C) +w [(((…(ENLN-1 + EN-1)LN-2) +… + E3)L2 + E2) L1 + E1]
For row major order
Base(C) + w [(…((E1L2 + E2)L3 + E3)L4 +…+ EN-1)LN+EN]
Base(C) Denotes the address of the first element of C, and w denotes the number of words
per memory location.
Que3. : Prove the following identity, Which is used in the analysis of various sorting and
searching algorithms:
𝒏(𝒏+𝟏)
1+2+3+-------+n=
𝟐
Solution: Writing the sum S forward and backward, we obtain :
S=1+2+3+. . . .+ (n-1) + n
S=n+ (n-1) + (n-2) +. . . +2+1
We find the sum of the two values of S by adding pairs as follows:
2S=(n+1)+(n+1)+(n+1)+. . . +(n+1)+(n+1)
There are n Such sums, so 2S=n(n+1). Dividing by 2 gives us our result
Prepared by Prof. Shailesh P. Thakare (Department of Information Technology)
Page 13