UNIVERSITY OF THE PUNJAB
B.S. in Computer Science: Fourth Semester – Spring 2022
Paper: Data Structure & Algorithm Course Code:CC-212 Time: 3 Hrs. Marks: 60
THE ANSWERS MUST BE ATTEMPTED ON THE ANSWER SHEET PROVIDED
[4 marks] Question 1
The following array is to be sorted using Insertion Sort:
(18, 10, 8, 35, 9, 29, 5)
Which of the following shows the contents of the array at the end of one of the iterations
of the algorithm?
1. (5, 10, 8, 35, 9, 29, 18)
2. (5, 8, 9, 35, 10, 29, 18)
3. (8, 9, 10, 35, 18, 29, 5)
4. (8, 10, 18, 35, 9, 29, 5)
5. (10, 8, 18, 9, 29, 5, 35)
[3x2-6 marks] Question 2 Algorithmic Complexity:
For each algorithm in the table, write its worst-case Theta complexity.
Algorithm or Problem Worst-case Theta()
Print all values appearing at least twice in a O(n)
sorted stack of size N
Duplicate a queue of N elements O(n)
Print all diagonal values of a given N-by-N matrix O(n)
[5 Marks] Question 3
The following is a recursive version of InsertionSort. Write down the recurrence relation
that describes the number of write accesses to the array (i.e., array[...] = ...)
made in the worst case.
public static void sort(int[] array, int n) {
// Sorts the first n elements of array
if (n <= 1) return;
int top = array[n-1];
sort(array, n-1);
for (int i = n-2; i >= 0; i--) {
if (array[i] > top) {
array[i+1] = array[i];
} else {
array[i+1] = top;
return;
}
}
array[0] = top;
}
E.g: [5, 4, 3, 2, 1]
Recurrence Relation:
W(n)=W(n−1)+n, W(1)=0
[5 Marks] Question 4
Which of the following comparison sorts are in-place sorts? Circle all that apply.
● SelectionSort
● InsertionSort
● HeapSort
● BucketSort
● Sequential QuickSort
● Sequential MergeSort
● Parallel QuickSort
● Parallel MergeSort
In-place sorts:
1. InsertionSort
2. Selection Sort
3. HeapSort
4. Sequential QuickSort
5. Parallel QuickSort
Reasons:
SelectionSort: In-place. It swaps elements directly in the original list and does not require additional
storage.
InsertionSort: In-place. It sorts elements by shifting them around within the same array, using only a
small amount of extra space for temporary variables.
HeapSort: In-place. It builds a heap within the original array and performs sorting by swapping
elements directly in place.
BucketSort: Not in-place. It requires extra space to store the individual buckets where elements are
placed before sorting within them.
Sequential QuickSort: In-place. It performs partitioning within the same array and only requires
additional space for recursive calls (logarithmic space).
Sequential MergeSort: Not in-place. It requires additional space for the temporary arrays used
during the merging process.
Parallel QuickSort: In-place. Like Sequential QuickSort, it rearranges elements within the array;
parallelism is used for dividing tasks, but it still doesn't require extra space beyond what's needed for
recursion.
Parallel MergeSort: Not in-place. It uses additional space for the merging process, similar to
sequential MergeSort, but the parallel nature doesn't change the space requirement for merging.
[10 Marks] Question 5
Here is an adjacency list representation of a directed graph, where there are no
weights assigned to the edges:
A. Draw a picture of the directed graph that has the above adjacency list representation.
B. Another way to represent a graph is an adjacency matrix. Draw the adjacency matrix for
this graph.
A B C D
A 0 1 1 1
B 1 0 0 0
C 0 1 0 1
D 0 0 0 0
[8 marks] Question 6
In the graph below, use your algorithm from above to
compute whether there is a path from node A to node E
that has a cost of at most 4. In particular, whenever
BFS expands a new node, show the content of the main
data structure that BFS maintains. Break ties arbitrarily.
Answer: A TO B TO C TO E
[2x6 - 12 Marks] Question 7
A. Use Prim's algorithm starting at node A to compute the Minimum Spanning Tree
(MST) of the following graph. In particular, write down the edges of the MST in the
order in which Prim's algorithm adds them to the MST. Use the format (node1, node2) to
denote an edge.
B.Write down the adjacency matrix A of the following undirected graph. Note that each
undirected edge corresponds to two directed edges of weight 1.
1 2 3 4
1 0 1 0 0
2 1 0 1 1
3 0 1 0 1
4 0 1 1 0
[2x5 - 10 marks] Question 8
What is the running time of the following code fragment, as a function of n? Assume n ≥
1. Express the running time using big-O notation.
int l = n;
int p = 0;
while (p * p < n) {
l = l / 2;
p++;
}
p²<n
p<√n
O(n^(1/2))
What is the running time of the following code fragment, as a function of n? Assume n ≥ 1.
Express the running time using big-O notation.
int total, i,j;
total = 1;
i=n;
while (i>0) do{
j=n;
while(j>0) do{
total = total*10;
j=j/2;
i=i-10;}
return total;
Outer Loop:
I-10 implies but we can Ignore constants so time complexity is O(n)
Inner loop
We can ignore constants that is 2