0% found this document useful (0 votes)
18 views

CACSC02 Question Paper Data Structure Open Book Solution

Uploaded by

donotreply
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CACSC02 Question Paper Data Structure Open Book Solution

Uploaded by

donotreply
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Q. No.

Question Marks

1 void bubblesort(int a[], int n) {


2 int swapped = 1;
3 while(swapped) {
4 swapped = 0;
5 for(i=0; i<= n-1; i++) {
6 if(a[i+1] <= a[i]) {
7 // assume that swap function is available
8 swap(a[i], a[i+1]);
9 swapped = 1;
10 }
11 }
12 }
1.a 13 } 3

Above function is a variant of Bubble Sort. The input array to this function may
contain an element multiple time. The function has been written to sort the array
in ascending order in time O(n2). However, there is a bug in it………..
a) Which line has the bug and how can it be fixed?
b) If this bug is not removed, how will this function behave? Explain your answer
by taking suitable input of 8 elements

Answer:
a) Line 6 has a bug. a[i+1] ≤ a[i] should be a[i+1] < a[i] .
b) The function never terminates. Once two equal elements are adjacent, a[i] = a[i
+ 1], then the function will swap them forever.

What is the time complexity of running the following nested loop? Show your
work.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j = j+i) {
printf(“Hello\n”);
1.b } 2
}

In the i-th iteration, the inner loop runs O(n/i) times, and therefore, the overall
complexity is O(n + n/1 + n/2 +...+ 1), and this is O(nlogn).
Suppose we want to encode a text having characters [ a, b, c, d, e, f, g ] occurring
with the following frequencies

Character a b c d e f g
Frequency 36 22 29 15 33 18 7

(i) Assign each of the character a different fixed length code of minimum size.
Compute the number of bits required to represent the text in binary.
(ii) Now, compute the variable length code using Huffman Coding technique for
each of the character. Compute the saving in terms of number of bits required
to represent the text in binary.

Character a b c D e f g
Frequency 36 22 29 15 33 18 7
Fixed Length
Code 000 001 010 011 100 101 110
Variable
Length Code 01 110 111 1011 00 100 1010

Total size is: (36+22+29+15+33+18+7) x 3 = 480 bits

Variable Length Code number of bits = 36x2 + 22x3 + 29x3 + 15x4 + 33x2 +
18x3 + 7x4 = 72+66+87+60+66+54+28 = 433 bits
2 5
Saving = 47 bits or 9.79%
Draw an Expression Tree for the following expression:
( ( a + b ) ^ 3 ) – 3 * a * b ) / ( ( a + b ) ( a – b ) ). From the expression tree, derive
the (i) Prefix Expression and (ii) Postfix Expression

3.a 3

Prefix : / - ^ + a b 3 * 3 * a b * + a b – a b

Porstfix : a b + 3 ^ 3 a b * * - a b + a b - /

What is the average number of comparisons needed in a search to determine the


position of an element in an array of 128 elements, if the elements are
(i) ordered from smallest to largest
(ii) unordered

When the list is ordered, we will binary search to determine the position of an
3.b 2
element in the array. The average case complexity of binary search is log n. Hence
it will take log 128 = 7 comparison.

When list is unordered, we perform linear search. Hence number of comparisons


required in this case will be
(1 + 2 +3 + … + 128) / 128 = ( (128 x 129) / 2) / 128 = 64.5
(i) A programmer is writing program for developing a text editor very similar to
Microsoft Word. Which data structure should he opt to implement an "undo"
feature? Justify.

Stack

(ii) Indigo Airline has developed a booking system that maintains a wait-list of
passengers on a flight who are waiting for an upgrade to first class. To give incentive
to frequent flyers, passengers are upgraded to a first-class seat when it becomes
available based on the number of flights a passenger has taken. Which data structure
Airline must have implemented in their system? Justify.

Priority Queue
4.a 3
(iii) What is the minimum number of comparisons needed to find the maximum
element in a binary min-heap having 1023 elements assuming heap is implemented
in an array? Justify your answer.

In a Min-heap having n elements, there are ceil(n/2) leaf nodes.


So, there will be ceil(1023/2) = ceil(511.5) = 512 elements as external nodes.
Now, in general, to find maximum of n elements you need (n-1) comparisons.
Just compare first two and then select the larger and compare with next one, select
the larger and compare with next one etc.
Therefore, we need 511 (=512 – 1) minimum number of comparisons required to find the
maximum in the given heap.

An array-based tree structure contains 74 nodes. Assume that the array index starts
at 0.
(i) What is the index of the first leaf node?
(ii) Which index node is the parent of a node at index 55?
(iii) Which index are the children of node at index 24?
(iv) How many leaf nodes are there in the tree?
4.b 2
N = 74
(i) Leaves N/2 to N-1 => 74 / 2 = 37
(ii) 55-1 / 2 = 27
(iii) 2x24 + 1 and 2x24+2 => 49, 50
(iv) From N/2 to N-1 => 37
Show the step by step construction of a Binary Tree structure whose Preorder
Traversal and Postorder Traversal are as given below: (No need to write code)
Preorder: 25, 35, 42, 38, 30, 15, 29, 16, 8, 47
Postoder: 42, 30, 38, 35, 16, 8, 29, 47, 15, 25

5.a 3
Draw the step by step Min Heap structure obtained from inserting the 24, 29, 23, 27,
22, 25, 28, 26 in this order and then calling deleteMin twice on the obtained Min
Heap. (Please note that Heap is to be drawn in form of a Binary Tree not as an Array)

2
5.b
Show step by step construction of an AVL Tree that is obtained from inserting the
following values: 13, 19, 12, 17, 14, 15, 18. If an insertion causes the tree to become
unbalanced, then perform and explain the type of rotation required to maintain the
balance.

6.a 3
Show the step by step construction of a Binary Search Tree structure for the given
Postorder traversal: 25, 20, 36, 40, 32, 62, 60, 74, 65, 52 (No need to write algorithm
/ code)

6.b 2
Name the two different approaches implement a priority queue. Compare these on
the basis of time required for Enqueue and Dequeue operations.

1st Approach Using Heaps:


Enqueue O(log N)
Dequeue O(log N)
7.a On an average O(log N) 1

2nd Approach Using Linked List:


Enqueue O(N)
Dequeue O(1)
On an average O(N)

Can a linear search be more efficient than binary search to find an element in an
array?
7.b 1
A linear search takes only one comparison to search for the first element whereas
with binary search it is at least 1 (1 when the size of an array is 1, > 1 otherwise).

There are two approaches for finding the nth Fibonacci number: Recursive Method
and Iterative Method. Comment on the Big-O time complexity of the two
7.c approaches. 1

Iterative Method is linear time while recursive approach is exponential time.

Quick Sort Algorithm is used to sort 8 numbers in ascending order using the first
element as pivot. When the input is (1, 2, 3, 4, 5, 6, 7, 8) it performs C1 comparisons.
However, when input is (4, 6, 1, 3, 8, 5, 2, 7) it might perform C2 comparisons.
7.d Comment on the relation between C1 and C2 with proper justification. 1

When first element or last element is chosen as pivot, Quick Sort‘s worst case occurs
for the sorted arrays. So, C1 > C2

A graph can be represented using two approaches. Compare these on the basis of
memory required to store graph information, connectivity density in the graph and
easiness to find adjacent vertices.

Matrix List
7.e 1
Memory: O (V2) Memory: O (V+E)
Better for Dense Graphs Better for Sparse Graph
Easier to Check if two vertices are Easier to find the vertices adjacent from
connected another vertex
Following is the Pseudocode of Depth First Traversal of Graph

//Initialization
for j = 0… N-1 {visited [j] = 0; parent [j] = -1 ; }

function DFS(i) // DFS staring from vertex i


{ //Mark I as visited
visited[i] = 1 ;
// Explore each neighbor of I recursively
for each (i, j) in E // Set E contains all edges of graph
{
if (visited[j] == 0)
{
parent[j] = I ;
DFS(j) ;
}
8 } 5
}

(a) Represent the above shown graph in the adjacency matrix form.
(b) Show the status of visited array, parent array when the above written DFS
function runs from vertex i = 0. Explain the functioning of this algorithm for each
edge (i, j) as derived from Adjacency Matrix.

----------X----------X---------

You might also like