# Data Structures and Algorithms | Set 33

• Difficulty Level : Easy
• Last Updated : 03 Dec, 2015

Following questions have been asked in GATE CS 2014 exam.

1) Consider the tree arcs of a BFS traversal from a source node W in an unweighted, connected, undirected graph. The tree T formed by the tree arcs is a data structure for computing.
(A) the shortest path between every pair of vertices.
(B) the shortest path from W to every vertex in the graph.
(C) the shortest paths from W to only those nodes that are leaves of T.
(D) the longest path in the graph

BFS always produces shortest paths from source to all other vertices in an unweighted graph. The reason is simple, in BFS, we first explore all vertices which are 1 edge away from source, then we explore all vertices which are 2 edges away from the source and so on. This property of BFS makes it useful in many algorithms like Edmonds–Karp algorithm.

2) Consider the following pseudo code. What is the total number of multiplications to be performed?

```D = 2
for i = 1 to n do
for j = i to n do
for k = j + 1 to n do
D = D * 3 ```

(A) Half of the product of the 3 consecutive integers.
(B) One-third of the product of the 3 consecutive integers.
(C) One-sixth of the product of the 3 consecutive integers.
(D) None of the above.
The statement “D = D * 3” is executed n*(n+1)*(n-1)/6 times. Let us see how.

For i = 1, the multiplication statement is executed (n-1) + (n-2) + .. 2 + 1 times.
For i = 2, the statement is executed (n-2) + (n-3) + .. 2 + 1 times
………………………..
……………………….
For i = n-1, the statement is executed once.
For i = n, the statement is not executed at all

So overall the statement is executed following times
[(n-1) + (n-2) + .. 2 + 1] + [(n-2) + (n-3) + .. 2 + 1] + … + 1 + 0

The above series can be written as
S = [n*(n-1)/2 + (n-1)*(n-2)/2 + ….. + 1]

The sum of above series can be obtained by trick of subtraction the series from standard Series S1 = n2 + (n-1)2 + .. 12. The sum of this standard series is n*(n+1)*(2n+1)/6

S1 – 2S = n + (n-1) + … 1 = n*(n+1)/2
2S = n*(n+1)*(2n+1)/6 – n*(n+1)/2
S = n*(n+1)*(n-1)/6

3) Consider a hash table with 9 slots. The hash function is h(k) = k mod 9. The collisions are resolved by chaining. The following 9 keys are inserted in the order: 5, 28, 19, 15, 20, 33, 12, 17, 10. The maximum, minimum, and average chain lengths in the hash table, respectively, are
(A) 3, 0, and 1
(B) 3, 3, and 3
(C) 4, 0, and 1
(D) 3, 0, and 2

Following are values of hash function for all keys

``` 5 --> 5
28 --> 1
19 --> 1  [Chained with 28]
15 --> 6
20 --> 2
33 --> 6  [Chained with 15]
12 --> 3
17 --> 8
10 --> 1 [Chained with 28 and 19]```

The maximum chain length is 3. The keys 28, 19 and 10 go to same slot 1, and form a chain of length 3.
The minimum chain length 0, there are empty slots (0, 4 and 7).
Average chain length is (0 + 3 + 1 + 1 + 0 + 1 + 2 + 0 + 1)/9 = 1

4) A priority queue is implemented as a Max-Heap. Initially, it has 5 elements. The level-order traversal of the heap is: 10, 8, 5, 3, 2. Two new elements 1 and 7 are inserted into the heap in that order. The level-order traversal of the heap after the insertion of the elements is:
(A) 10, 8, 7, 3, 2, 1, 5
(B) 10, 8, 7, 2, 3, 1, 5
(C) 10, 8, 7, 1, 2, 3, 5
(D) 10, 8, 7, 5, 3, 2, 1

```Initially heap has 10, 8, 5, 3, 2
10
/  \
8    5
/ \
3   2

After insertion of 1
10
/   \
8     5
/ \   /
3   2 1
No need to heapify as 5 is greater than 1.

After insertion of 7
10
/   \
8     5
/ \   / \
3   2 1   7
Heapify 5 as 7 is greater than 5
10
/   \
8     7
/ \   / \
3   2 1   5
No need to heapify any further as 10 is
greater than 7 ```

5) Which one of the following correctly determines the solution of the recurrence relation with T(1) = 1?

`T(n) = 2T(n/2) + Logn `

(A) Θ(n)
(B) Θ(nLogn)
(C) Θ(n*n)
(D) Θ(log n)

This can be solved using Master Method. It falls in case 1.

7) Suppose implementation supports an instruction REVERSE, which reverses the order of elements on the stack, in addition to the PUSH and POP instructions. Which one of the following statements is TRUE with respect to this modified stack?
(A) A queue cannot be implemented using this stack.
(B) A queue can be implemented where ENQUEUE takes a single instruction and DEQUEUE takes a sequence of two instructions.
(C) A queue can be implemented where ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction.
(D) A queue can be implemented where both ENQUEUE and DEQUEUE take a single instruction each.

To DEQUEUE an item, simply POP.
To ENQUEUE an item, we can do following 3 operations
1) REVERSE
2) PUSH
3) REVERSE

See GATE Corner for all previous year paper/solutions/explanations, syllabus, important dates, notes, etc.