Sie sind auf Seite 1von 15

Question Bank

1. What is data structure?

A data structure is a way of organizing data that considers not only the items stored,
but also their relationship to each other. Advance knowledge about the relationship
between data items allows designing of efficient algorithms for the manipulation of
data.

2. List out the areas in which data structures are applied extensively?

1. Compiler Design,
2. Operating System,
3. Database Management System,
4. Statistical analysis package,
5. Numerical Analysis,
6. Graphics,
7. Artificial Intelligence,
8. Simulation

3. What are the major data structures used in the following areas : RDBMS, Network
data model and Hierarchical data model.

1. RDBMS = Array (i.e. Array of structures)


2. Network data model = Graph
3. Hierarchical data model = Trees

4. If you are using C language to implement the heterogeneous linked list, what
pointer type will you use?

The heterogeneous linked list contains different data types in its nodes and we need a
link, pointer to connect them. It is not possible to use ordinary pointers for this. So we
go for void pointer. Void pointer is capable of storing pointer to any type as it is a
generic pointer type.

5. Minimum number of queues needed to implement the priority queue?

Two. One queue is used for actual storing of data and another for storing priorities.

6. What is the data structures used to perform recursion?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so
knows whom to return when the function has to return. Recursion makes use of
system stack for storing the return addresses of the function calls.

Every recursive function has its equivalent iterative (non-recursive) function. Even
when such equivalent iterative procedures are written, explicit stack is to be used.
7. What are the notations used in Evaluation of Arithmetic Expressions using prefix
and postfix forms?

Polish and Reverse Polish notations.

8. Convert the expression ((A + B) * C - (D - E) ^ (F + G)) to equivalent Prefix and


Postfix notations.

1. Prefix Notation: - * +ABC ^ - DE + FG


2. Postfix Notation: AB + C * DE - FG + ^ -

9. Sorting is not possible by using which of the following methods? (Insertion,


Selection, Exchange, Deletion)

Sorting is not possible in Deletion. Using insertion we can perform insertion sort,
using selection we can perform selection sort, using exchange we can perform the
bubble sort (and other similar sorting methods). But no sorting method can be done
just using deletion.

10. What are the methods available in storing sequential files ?

1. Straight merging,
2. Natural merging,
3. Polyphase sort,
4. Distribution of Initial runs.

11. List out few of the Application of tree data-structure?

1. The manipulation of Arithmetic expression,


2. Symbol Table construction,
3. Syntax analysis.

12. List out few of the applications that make use of Multilinked Structures?

1. Sparse matrix,
2. Index generation.

13. In tree construction which is the suitable efficient data structure? (Array, Linked
list, Stack, Queue)

Linked list is the suitable efficient data structure.

14. What is the type of the algorithm used in solving the 8 Queens problem?

Backtracking.

15. In an AVL tree, at what condition the balancing is to be done?

If the 'pivotal value' (or the 'Height factor') is greater than 1 or less than -1.
16. What is the bucket size, when the overlapping and collision occur at same time?

One. If there is only one entry possible in the bucket, when the collision occurs, there
is no way to accommodate the colliding value. This results in the overlapping of
values.

17. Classify the Hashing Functions based on the various methods by which the key
value is found.

1. Direct method,
2. Subtraction method,
3. Modulo-Division method,
4. Digit-Extraction method,
5. Mid-Square method,
6. Folding method,
7. Pseudo-random method.

18. What are the types of Collision Resolution Techniques and the methods used in
each of the type?

1. Open addressing (closed hashing), The methods used include: Overflow


block.
2. Closed addressing (open hashing), The methods used include: Linked list,
Binary tree.

19. In RDBMS, what is the efficient data structure used in the internal storage
representation?

B+ tree. Because in B+ tree, all the data is stored only in leaf nodes, that makes
searching easier. This corresponds to the records that shall be stored in leaf nodes.

20. What is a spanning Tree?

A spanning tree is a tree associated with a network. All the nodes of the graph appear
on the tree once. A minimum spanning tree is a spanning tree organized so that the
total edge weight between nodes is minimized.

21. Does the minimum spanning tree of a graph give the shortest distance between
any 2 specified nodes?

No. The Minimal spanning tree assures that the total weight of the tree is kept at its
minimum. But it doesn't mean that the distance between any two nodes involved in
the minimum-spanning tree is minimum.
22. Which is the simplest file structure? (Sequential, Indexed, Random)

Sequential is the simplest file structure.

23. Whether Linked List is linear or Non-linear data structure?

According to Access strategies Linked list is a linear one.


According to Storage Linked List is a Non-linear one.

Q.1. (a) List out the differences between sequential allocation and dynamic
allocation
Schema: Any valid eight differences. Each difference carries half mark.

(b) Develop an algorithm to delete an element from a circular Queue.


Algorithm: Delete_CQ Input: CQ, Circular Queue; F, Front index; R, Rear Index;
Output: Updated CQ; Updated F and R. Method: If(F== R== -1) { Printf(Deletion
not possible, queue empty); else
Element_deleted = CQ[F]; If(R == F) { R=F=-1; else
F = F+1 mod n /*if array index starts from 0 */
Sequential
Dynamic Allocation
1. Logically Adjacent Elements are also Physically Adjacent
Need not be the case
2. Works on the basis of Address computation mechanism
Works on the basis of address stored in pointers
3. Best when memory required for the task is known prior hand
Best when memory required is not known prior hand
4. No such Problem
Over head of pointers
5. Inserting an element or deleting an element requires data movement
Insertion or deletion of elements into the list is straight forward
6. Splitting a list into two requires additional storage
Splitting of a list into two is straight forward
7. No such problem
Loss of pointer results in inaccessible data in the list
8. It is necessary to have contiguous memory free
No such necessity
}
}

(c) Develop an algorithm to Insert an element into a circular Queue Algorithm:


Insert_CQ
Input: CQ, Circular Queue; F, Front index; R, Rear Index; e, element to be inserted;
Output: Updated CQ; Updated F and R. Method: If (R+1 mod n == F)
{ else
}
Q.2.
Printf(Insertion not Possible Queue is full);
{ R=R+1modn CQ[R] = e;
If(F == -1) { F = R; /* F=0 or F= F+1 mod n are also correct*/ }
}

(a) Define ADT for Stacks and Queues.


4+3+3
Schema: Minimum Four functions and four axioms are expected for both Stacks and
Queues. ADT for Stack: Domain = { Application Dependent } + Boolean Functions =
{Create(Stack)= Stack; Isempty(Stack)=Boolean; Isfull(Stack)=Boolean; Top(Stack)=
top element e; Push(Stack, e) = updated Stack; Pop(Stack) = top element e; }
Axioms = {Isfull(create(stack))= No; Isempty(create(stack))=yes;
pop(create(stack))=not possible; pop(push(stack, e))=e; } Note: The students can
frame many axioms, marks should be given if they are valid axioms.
ADT for Queue:
Domain = { Application Dependent } + Boolean Functions = {Create(Queue)=
Queue; Isempty(Queue)=Boolean; Isfull(Queue)=Boolean; Front(Queue)= front
element e; Insert(Queue, e) = updated Queue; Delete(Queue) = Rear element e; }
Axioms = {Isfull(create(Queue))= No; Isempty(create(Queue))=yes;
Delete(create(Queue))=not possible; If Isempty(Queue) == yes then Delete(Queue)=
Not Possible; If Isfull(Queue) == yes Insert(Queue, e) = Not possible }
Note: The students can frame many axioms, marks should be given if they are valid
axioms.

(b) List atleast 5 real life examples where stacks and queues are being used.
Schema: Minimum five examples are expected for both Stacks and Queues.
Examples for Stacks:
1. Stack of CDS in a CD box 2. Stack of plates in a Buffet 3. Bangle of same size
4. Rings of same size in a needle 5. Layers in an onion
Examples for Queues

1. Queues of articles to be printed by a computer 2. Queues in a ticket counter


3. Cars exiting from a parking lot

4. Bogies of a train 5. Piles of answer scripts to be corrected

(c) Develop an algorithm to delete an element e from a Linear Single Linked List
Schema: Each box below representing various conditions to be checked carries one
mark
Algorithm: Delete_e_LSLL Input: F, the address of the first node in the list; e,
element to be deleted Output: F, List updated; Method: If (F == Null)
Printf(Deletion Not possible, list is empty) {
Else
If end
Else
} Algorithm ends
Else
If end
}
If (F.link == Null)
{
}
If (F.data == e) F=null;
Else If end
Printf(Deletion Not possible, element not present)
If (F.data == e) { F= F.link}
{ V= F;
While(V.link != null && V.link.data != e) { V=V.link; }
If(V.link == null) Printf(Deletion Not possible, element not present)
Else
/* If (V.link.data == e) */ V.link = V.link.link;
If end
2.5+2.5+5

Q.3 Write a recursive function to solve the Tower of Hanoi Problem. Show the status
of the stack using state space diagram, when the function, Tower of Hanoi is called
with n=3 rings.
Schema: Correct function carries 5 marks and the state space diagram carries 5 marks
Tower_of_Hanoi(n, S, D, T) {
If (n > 0) { Tower_of_Hanoi(n-1,S,T,D); Printf(Move disk from %c to %c, S, D);
Tower_of_Hanoi(n-1,T,S,D); }

Calling Tower_of_Hanoi with n=3, S= A, T=B, D=C


n=3, S=A, T=B, D=C
1
Move Disk from A to C
n=2, S=A, T=C, D=B
n=2, S=B, T=A, D=C
2
8
Move Disk from A to B
9
Move disk from B to C
n=1, S=A, T=B, D=C
n=0, S=A, n=0, S=B, T=C, D=B T=A, D=C
n=1, S=C, T=A, D=B
n=0, S=A, n=0, S=C, T=C, D=B T=A, D=B
n=1, S=B, T=C, D=A
n=2, S=A, T=B, D=C
5
Move Disk from A to C
3
4
6
7
Move Disk from C to B
Move Disk from B to A
Move Disk from A to C
n=0, S=B, T=A, D=C
n=0, S=C, T=B, D=A
n=0, S=A, T=C, D=B
n=0, S=B, T=A, D=C
5+5
5x2=10 10

Q.4 Obtain the equivalent prefix and postfix expressions for the following infix
expressions

Q.5. Design an algorithm to convert infix expression to its equivalent postfix


expression
Schema: Statements in each box contains 1 mark each (9 boxes = 9marks). Proper
sequencing of statements and the correctness of the algorithm carries 1 mark
Algorithm: Infix_to_postfix_equivalent Input: IE, Infix Expression Output: PostEx,
postfix Expression Method:
Infix
Prefix
Postfix
1
a/b-c+d*e-a*c
- + -/abc*de*ac
ab/c-de*+ac*-
2
(a/(b-c+d))*(e-a)*c
**/a+-bcd-eac
abc-d+/ea-*c*
3
a^2^b+b^2^a+2*a*b
++^a^2b^b^2a**2ab
a2b^^b2a^^+2a*b*+
4
(a*b^(c+d)^e)-f-g
- -*a^b^+cdefg
abcd+e^^*f-g-
5
(a-b/c*d+e*f/g)^ (a- b/c*d+e*f/g)
^+ - a*/bcd/*efg+ - a*/bcd/*efg
abc/d*- ef*g/+abc/d*- ef*g/+^
Concatenate(IE, $); PostEx = null;
symbol=getsymbol(IE); Initialise(Stack); /*optional */ While(symbol != $) {
If(symbol is an Operand) Concatenate(PostElse
Switch(Operator) {
Case +,-,*,/ ::
while (Priority(top) > Priority(symbol)) {
Concatenate(PostEx, pop(Stack));
}
Q.6. Write a recursive algorithm to implement binary search technique and hand
simulate the algorithm on
the following list of elements [22 29 37 45 67 88 89 90 95] with search key = 22 and
key = 100.
4+3+3 Schema: 4 marks for correct recursive algorithm. 3 marks for tracing the
developed algorithm with key=22
and 3 mark for tracing the developed algorithm with key =10. Tracing can be in any
form.
Algorithm: Binary_Search_recursive Input: A, array of elements; low, Lower limit;
high, upper limit; key, search element Output: true/false, element present or not
Method:
If(low<=high) Mid = (low+high)/2; If(A[mid] == key)
Push(symbol, Stack);
Case ^ :: Case ( ::
Push(symbol, Stack); Push(symbol, Stack);
Pop(); /*pop and discard ( */
Case ) ::
while (Top(Stack) != ( ) {
Concatenate(PostEx, pop(Stack));
}
}
Symbol=getsymbol(IE);
}
While(Isempty(Stack) != yes) {
Concatenate(PostEx, pop(Stack));
}
Algorithm Ends
Else
Return(true)
If(key < A[mid]) Binary_Search_recursive(A,low,mid-1,key)
Else

If(key > A[mid])Ex, symbol);

Binary_Search_recursive(A,mid+1,high,key) If end
If end
If end Return(false) Algorithm ends
Tracing for Key = 22
A = [22 29 37 45 67 88 89 90 95]
Low=0 High=8 Key=22
Tracing for Key = 100
A = [22 29 37 45 67 88 89 90 95]
Low=0 High=8 Key = 100
Binary_Search_recursive(A, 0, 8, 22) Low <= high 0 <=8 is true Mid = (0+8)/2 = 4
If(key == A[mid]) 22 ==67 is false If(key < A[mid]) 22<67 is true
Binary_Search_recursive(A, 0, 3, 22)
Binary_Search_recursive(A, 0, 3, 22) Low <= high 0 <=3 is true Mid = (0+3)/2 = 2
If(key == A[mid]) 22 ==37 is false If(key < A[mid]) 22<37 is true
Binary_Search_recursive(A, 0, 2, 22)
Binary_Search_recursive(A, 0, 2, 22) Low <= high 0 <=2 is true Mid = (0+2)/2 = 1
If(key == A[mid]) 22 ==29 is false If(key < A[mid]) 22<29 is true
Binary_Search_recursive(A, 0, 0, 22)

Binary_Search_recursive(A, 0, 0, 22) Low <= high 0 <= 0 is true Mid = (0+0)/2 = 0


If(key == A[mid]) 22 ==22 is true Return(true)

Binary_Search_recursive(A, 0, 8, 100) Low <= high 0 <=8 is true Mid = (0+8)/2 = 4


If(key == A[mid]) 100 ==67 is false If(key < A[mid]) 100 <67 is false
If(key>A[mid]) 100 >67istrue Binary_Search_recursive(A, 5, 8, 100)
Binary_Search_recursive(A, 5, 8, 100) Low <= high 5 <=8 is true Mid = (5+8)/2 = 7
If(key == A[mid]) 100 ==90 is false If(key < A[mid]) 100 <90 is false
If(key>A[mid]) 100 >90istrue Binary_Search_recursive(A, 8, 8, 100)
Binary_Search_recursive(A, 8, 8, 100) Low <= high 8 <=8 is true Mid = (8+8)/2 = 8
If(key == A[mid]) 100 ==95 is false If(key < A[mid]) 100 <95 is false
If(key>A[mid]) 100 >95istrue Binary_Search_recursive(A, 9, 8, 100)
Binary_Search_recursive(A, 9, 8, 100) Low <= high 9 <=8 is false Return(false)

Q.7.
(a) Devise algorithms to realize stack operations Push and Pop using linear single
linked lists To realize Push operation of stack using linear single linked list we need
to implement the algorithm
of inserting an element e as the first node into the LSLL. To realize Pop operation of
stack using linear single linked list we need to implement the algorithm
of deleting the first node from the LSLL
Algorithm: Stack_Push_LSLL Input: F, first node address; e, element being pushed
into the stack; Output: F, Updated Method:
N = getnode(); N.data = e; n.link = F; If (F == null)
Else
F = n;

n.link = F; F = n;

If end Algorithm ends


Algorithm: Stack_Pop_LSLL Input: F, first node address; Output: F, Updated;
E, poped element /*optional */ Method:
If (F == null) Print(List empty);
Else
If end Algorithm ends
e = F.data; F = F.link;

(b) Devise algorithms to realize queue operations Insert and Delete using circular
single linked lists.
To realize Insert operation of Queue using linear single linked list we need to
implement the algorithm of inserting an element e as the last node into the LSLL.
To realize Delete operation of Queue using linear single linked list we need to
implement the algorithm of deleting the first node from the LSLL
Algorithm: Queue_Insert_LSLL Input: F, first node address; e, element being Inserted
into the Queue; Output: F, Updated Method:
N = getnode(); N.data = e; n.link = null;
If (F == null) F = n;
Else
If end Algorithm ends
V=F; While(V.link != null) V=V.link; While end
V.link=n;
Algorithm: Queue_Delete_LSLL Input: F, first node address; Output: F, Updated;
E, Deleted element /*optional */ Method:
If (F == null) Print(Queue empty);
Else

e = F.data;

If end Algorithm ends


F = F.link;

Q.8. Design an algorithm to Insert an element e in between the elements x and y


in a Linear double linked list. 10 Algorithm: Insert_ebtwXY_LDLL Input: F,
address of the first node; e, element to be inserted; X, Y, elements between which X
and Y has to be inserted.
Output: Updated F Method:
N = getnode(); n.data = e; If(F == null)
Else
If end Algorithm ends
Print(list empty);
if(F.rlink == null) Print(List has only one element);
Else
If end
V = F; Position_X = 0; While(V != null) && (V.data != X)
V = V.rlink;
Position_X ++; While end
V1 = F; Position_Y = 0; While (V1!= null) && (V1.data != Y)
V1 = V1.rlink;
Position_Y++; While end
If(V == null) Printf(X is not present in the list)
Else
If end
If(V1 == null) Printf(Y is not present in the list)
Else
If(Position_X < Position_Y) n.rlink = V.rlink;
else
If end
n.llink = v; v.rlink = n;
if(Position_X > Position_Y) n.rlink = V1.rlink;
If end
n.llink = v1; v1.rlink = n;

4*2.5= 10
Q.1.
(a) Is n=2 the largest value of n for which there exist positive integer x, y and z such
that xn+yn=zn has a solution?
2+2+2+4 Ans: Requires to check if the equation xn+yn=zn gets satisfied for any value
greater than 2. Since there are
infinitely many numbers that are greater than n, it enters an infinite loop. Therefore
the statement does not satisfy the finiteness property.

(b) Store 5 divided by zero into x and go to statement 10


Ans: 5/0 is indeterminant or indefinite. Therefore the statement does not satisfy the
definiteness property.

(c) If n<10 assign the value of A or the value of B in to the variable C All the above
three statements fail to satisfy one of the five basic properties of an algorithm. Which
criteria do they violate?
Ans: if n<10 C=A or C=B. OR in this statement has a dilemma or ambiguous.
Therefore violates the property of defineteness.

(d) Write a C program that prints out the integer values X, Y, Z in ascending order
and prove the correctness of your program.
Ans:
#include<stdio.h> #include<conio.h> void Sort_Three_One(int a, int b, int c); void
main() { int x,y,z; printf(Enter the values for x, y and z\n);
scanf(%d%d%d,&x,&y,&z);
Sort_Three_One(x,y,z);
/* or Sort_Three_Two(x,y,z); Or any sorting technique such as Sort_Three_Three(x,
y, z); */
}
void Sort_Three_One(int a, int b, int c) if(a<=b)&& (a<=c) && (b<=c)
Void Sort_Three_Two(A, B, C) { if(a <= b) {
if(a<=c) {
if(b <= c) printf(%d%d%d,a, b, c);
else
printf(%d%d%d,a, c,b); else if(a>c)
printf(%d%d%d, c, a, b);
}
} else if (b<a)

{ if(b<=c)

{printf(%d%d%d,a, b, c);} else if (a<=c)&&(a<=b) && (c<=b)


{
printf(%d%d%d,a, c, b);} else if (b<=a)&&(b<=c) && (a<=c)
{printf(%d%d%d,b, a, c);} else if (b<=c)&&(b<=a) && (c<=a)
{printf(%d%d%d,b, c, a);} else if (c<=a)&&(c<=b) && (a<=b)
{ printf(%d%d%d,c, a, b);} else if (c<=b)&&(c<=a) && (b<=a) {
printf(%d%d%d,c, b, a); }
{ if (a <=c)
printf(%d%d%d, b, a, c); else
printf(%d%d%d, b, c, a);
}
else
}
}
printf(%d%d%d, c, b, a);
void Sort_Three_Three(int a, int b, int c) { int array[3], temp,i,j; array[0]=a;
array[1]=b; array[2]=c; for (i=0; i<2;i++)
{
for( j = i+1; j<3; j++) {
if(array[i] > array[j]) {
temp = array[i]; array[i]=array[j]; array[j]=temp;
}}
} for (i=0; i<3;i++)
printf(%d\t,array[i]);
}

Q.2.
(a) Define Data, Data Object, Data Storage, Data type and an abstract data type. Ans:
Data:.
Data type: Abstract data type:
Data: Data is a collection of facts which can be served as input to any processing unit
such as a program, equation etc
Data Object: Data object is an instance of data. It is application dependent. Any
natural number in a set of natural numbers is a data object. A bangle in a hand is a
data object.
Data Storage: A structure which holds the data.
Data Type: A data type is a collection of objects speaks the characteristics of the data
in the collections and the operations that can be performed on that.
ADT: An abstract data type is a data type that is organized in such a way that the
specification of the objects and the specification of the operations on the objects is
separated from the representation of the objects and implementation of the operation.
Formally described by Domain, Functions and Axioms.
Ans:

(b) Create and abstract data type Bag. Bag is similar to a set except that a bag may
contain duplicate elements.
ADT for BAG: Domain: Application Dependent items + Boolean Eg: Bag of books,
Bag of vegetables, Bag of Electronic Items, School Bag etc
Functions:
Create(Bag)= null; Isfull(Bag) = Boolean; Isempty(Bag)=Boolean; Insert(item,
Bag)=Bag; Pick(item, Bag)=item Count(Bag) Search(Bag,item) Count_duplicate(Bag
Axioms:
Isfull(create(Bag))=false Isempty(insert(item, Bag))=false If (isfull(bag)==yes), Insert
(item,Bag)=Not possible If(isempty(bag)==yes), Pick(item,Bag)=Not possible
Q.3
(a) Define asymptotic notations O, and .
ASYMPTOTIC NOTATION
Time complexity can be denoted by the following notations. 1. Big (oh) notation -O
[Worst case time complexity] 2. Omega notation - *Best case time
complexity+ 3. Theta notation *Average case time complexity]
1. Big (oh) notation O
Big oh can be expressed as f(n)=O(g(n)) iff there exists c, n0 such that f(n)<=c g(n)
For all n>=n0.
2. Omega notation -
Omega can be expressed as f(n)=(g(n)) iff there exists c, n0 such that f(n)>=c g(n)
For all n>=n0.
3. Theta notation
Theta can be expressed as f(n)=(g(n)) iff there exists c1,c2, n0 such that
c1g(n)<=f(n)<=c2 g(n) For all n>=n0.

(b) Write an algorithm to search if an element is present in a list of n elements using


linear search. Workout O, and complexities for the same. Ans. Input: n, number
of elements.

list[], an array of n elements.


key, search element. Output: Boolean (True/False) Method: flag=0;
for(i=0;i<n;i++) if(key==list[i])
flag=1;
break; End if;
End for; Step count= 2n+2
O(n) where c=3 and n0=2 (n) where c=1 and n0=1 (n) where c1=1, c2=5 and n0=1
Q.4 The factorial of a number n denoted by n! has value 1 when n1 and n*(n-1)!
When n>1. Write both a recursive and an iterative C function to compute n!. Compute
their worst time and space complexities.
10
Ans:
Q.5. The Fibonacci numbers are defined as: f0=0, f1=1, and fi=fi-1+fi-2 for i>1.
Write both recursive and an iterative C function to compute the first n Fibonacci
numbers. Compute their worst time and space complexities. 10
Ans:
void factorial_iterative(int num) { Int fact = 1; for (i=1; i<=num; i++)
{ fact = fact*i; } Printf(Factorial of %d is %d\n,num,fact); } Worst case time
complexity: f(n)=2n+1; O(n) where c=3, n0=1 Worst case space complexity: O(3)
[i.e., the number of variables used]
void factorial_recursion(int num) { if (n<=1)
return 1;
else
return (num * factorial_recursion(num-1)); }
Worst case time complexity : f(n)=2n; O(n) where c=2 and n0=1 Worst case space
complexity : 3n-1 [involves stack operations]
void Fibonacci_Iterative(int n) { Int f0=0, f1=1; printf(%d\t%d\t,f0,f1);
for (int i = 3; i<=n; i++)
{
}}
f2=f0+f1; f0=f1; f1=f2; printf(%d\t,f2);
Worst case time complexity : f(n)=3n-4; O(n) Worst case time complexity : O(5)
void Fibonacci_Recursive(f0,f1,n) {if(n>2)
{
}}
f2=f0+f1; disp(f2); f0=f1; f1=f2; Fibonacci_Recursive(f0,f1,n-1)
Worst case time complexity: f(n)=5n-8; O(n) Worst case space complexity: O(4n+n)
[invokes stack operations]
/* computes ith Fibonacci term */ int FibonacciTerm_Recursive(int i)n i3 (n4)
i 0
{
34 r i e t u rO n ( (n 0 ) ;
if(i==1)
n
e l si e0
if(i==2) return(1);
else
return(FibonacciTerm_Recursive(i-1) + FibonacciTerm_Recursive(i-2));
}

Q.6. Show that the following equalities are correct


ANS:

(a) 3n = O(2n); (b)n32n+6n23n = O(n22n) ; (d)2n2+nlogn = (n2); (e)


6*2n+n2=(1)

(a) f(n)= 3n has higher order term than g(n)= 2n, the
(b) re cannot exist a C and a n0 which satisfy the inequality f(n)<=c g(n)
for n>=n0.
(b)f(n)= n32n+6n23n has higher order term 3n than g(n)= n22n there cannot exist a
C and a n0 which satisfy the inequality f(n)<=c g(n) for n>=n0.

(c)f(n)= where i is a variable interms of n, g(n)=n4 can satisfy the inequality


f(n)<=c2 g(n) for positive constants c2 and n0 for all n>=n0 but there exits no positive
constant c1 that satisfy the inequality f(n)>=c1 g(n). Therefore is incorrect and
should be

(c) Correct C1=1 , C2=3 , n0=1 (e) : Correct C1=1 ,n0=1


(d)
Q.7. Devise an algorithm to compute the address of A(i1,i2,i3,i4,...,in) in an n
dimensional array where l1i1u1, l2i2u2 , l3i3u3, ..., lninun. Assume base
address as B and wordsize as w.
10
Ans. Algorithm : Address computation of N dimensional array. Input : l1, l2, l3.....ln
The lower bounds of each index
u1, u2, u3.....un The upper bounds of each index i1,i2,i3,...in The indices at which
address has to be calculated. W Word size. B- Base address. Size - Size of each
element of an array.
Output: Address of the A(i1,i2,i3...in) Method :
t=0; for(k=1;k<=n;k++) {
p=1; for(j=k+1;j<=n;j++) {
p*=u[j]-l[j]+1; t+=(i[k]-l[k])*p;
Address=B+(t*Size);
}

Q.8. Consider an array A of dimension 3, the subscripts of which are defined as -


3i8, 0j7, -2k4. Assuming that the base address is 2000 and word size as 3
compute address of A[0,0,0], if A has (a) Row major ordering (b) Column major
ordering. Ans.
Given, li=-3, ui=8, lj=0, uj=7, lk=-2, uk=4 i=0, j=0, k=0.
(a) Rowmajorordering: A[I, j, k]=B+[(i-li)(uj-lj+1)(uk-lk+1)+(j-lj)(uk-lk+1)+(k-
lk)]*W
=2000+[(0+3)(7-0+1)(4+2+1) + (0-0)(4+2+1) + (0+2)] *3
=2000+[(3*8*7)+(0)+(2)]*3 =2000+[168+0+2]*3
=2000+170*3 =2000+510 =2510
(b) Columnmajorordering: A[I, j, k]=B+[(k-lk)(uj-lj+1)(ui-li+1)+(j-lj)(ui-li+1)+(i-
li)]*W
=2000+[(0+2)(7-0+1)(8+3+1) + (0-0)(-3-8+1)+(0+3)] * 3 =2000+[(2*8*12)+3]*3
=2000+[(2*96)+3]*3 =2000+195*3
=2000+585 =2585

Das könnte Ihnen auch gefallen