0% found this document useful (0 votes)
18 views14 pages

CC 212 Data Structure & Algorithm

The document outlines the syllabus and exam structure for the Data Structure & Algorithm course (CC-212) at the University of the Punjab for the B.S. in Computer Science program. It includes various questions related to sorting algorithms, graph theory, algorithmic complexity, and data structures, along with specific tasks such as writing code and evaluating expressions. The exam is structured to assess students' understanding of theoretical concepts and practical applications in data structures and algorithms.

Uploaded by

tayyabamaryam602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views14 pages

CC 212 Data Structure & Algorithm

The document outlines the syllabus and exam structure for the Data Structure & Algorithm course (CC-212) at the University of the Punjab for the B.S. in Computer Science program. It includes various questions related to sorting algorithms, graph theory, algorithmic complexity, and data structures, along with specific tasks such as writing code and evaluating expressions. The exam is structured to assess students' understanding of theoretical concepts and practical applications in data structures and algorithms.

Uploaded by

tayyabamaryam602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

B.S.

Computer Science
CC-212

Data Structure & Algorithm


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 sorted


stack of size N

Duplicate a queue of N elements


Print all diagonal values of a given N-by-N matrix

[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;
}

[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

[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.

[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.
[26-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.
[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++;
}

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;
UNIVERSITY OF THE PUNJAB
B.S. in Computer Science: Fourth Semester – Fall 2023
Paper: Data Structure & Algorithm Course Code:CC-212 Time: 3 Hrs. Marks: 60

THE ANSWERS MUST BE ATTEMPTED ON THE ANSWER SHEET PROVIDED

Q.1. Answer the following short questions:​ ​ ​ ​ (5x6=30)

1.​ 3A three-dimensional array (having lower bound zero) A[u1][u2][u3] stored


in a row-major-order with base address 100. The dimensions of the array are:​

○​ u1 = 5, u2 = 3, and u3 = 10​
You are required to calculate the address of location A[i][j][k], where
i = 3, j = 1, and k = 6.​
Also, suppose that each cell occupies 2 bytes in memory.
2.​ Obtain the step count of the following code segment:​
for (i = 0; i < n; i++)

temp;

3.​ The running time of a program is 4N³ + 5N² + 6N + 7, where N is the input size.​
What are the upper and lower bounds for the growth rate of the program? Do not
forget to mention the constants (c and n₀).​

4.​ An algorithm takes 10 seconds for an input size of 1000.​


How long will it take for input size of 100 if the running time is 5N?​

5.​ Convert A * ((B - C) - D) into its equivalent pre fix form.

Q.2. Answer the following questions:​ ​ ​ ​ ​ ​ (3x10=30)

1.​ Sort the following array into increasing order using Bubble Sort.​
Clearly show the contents of the array after each iteration of the outer loop.​
You will get NO credit if it is not clear that you used Bubble Sort to sort the given
array.​
Array:​
39, 37, 38, 36, 30 ,32, 24, 35,31,33​

2.​ Draw a Binary Search Tree (BST) when the following values are inserted
one-by-one (in the given order from left to right) into an initially empty BST:​
Values:​
55, 53, 58, 57, 54, 52, 40, 50, 59, 51​

3.​ Given the following directed graph, G,

write the sequence of vertices after performing Breadth-First Search (BFS) starting from
the source vertex A. Also, show the resulting BFS tree.If there is more than one choice of
vertices at any given point, traverse them in alphabetical order.
B.S. Information Technology
CC-212

Data Structure & Algorithm


UNIVERSITY OF THE PUNJAB
B.S. 4 Year Program/ Fourth Semester – Spring 2023
Paper: Data Structure & Algorithm Course Code:CC-212 Time: 3 Hrs. Marks: 60

THE ANSWERS MUST BE ATTEMPTED ON THE ANSWER SHEET PROVIDED

Q.1. Answer the following short questions:​ ​ ​ ​ (5x6=30)

6.​ 3A three-dimensional array (having lower bound zero) A[u1][u2][u3] stored


in a row-major-order with base address 100. The dimensions of the array are:​

○​ u1 = 10, u2 = 5, and u3 = 3​
You are required to calculate the address of location A[i][j][k], where
i = 6, j = 3, and k = 1.​
Also, suppose that each cell occupies 2 bytes in memory.
7.​ Obtain the step count of the following code segment:​
for (i = 0; i < n; i++)

temp;

8.​ The running time of a program is 4N³ + 5N² + 6N + 7, where N is the input size.​
What are the upper and lower bounds for the growth rate of the program? Do not
forget to mention the constants (c and n₀).​

9.​ An algorithm takes 12 sec for input size 12000. How large a problem can be
solved in one minute if the running time is 10N? ​

10.​Convert A +((B - C) * D) into its equivalent post-fix form.

Q.2. Answer the following questions:​ ​ ​ ​ ​ ​ (3x10=30)

4.​ Sort the following array into increasing order using Insertion Sort.​
Clearly show the contents of the array after each iteration of the outer loop.​
You will get NO credit if it is not clear that you used Insertion Sort to sort the
given array.​
Array:​
3, 5, 7, 2, 9 ,8, 0, 6,4,1​

5.​ Draw a Binary Search Tree (BST) when the following values are inserted
one-by-one (in the given order from left to right) into an initially empty BST:​
Values:​
15, 13, 18, 17, 14, 12, 20, 10, 19, 11​

6.​ Given the following directed graph, G,

There can be many possible weighted acyclic graphs exist in G, you are required to find and
draw the one, connected with the least total weight.
UNIVERSITY OF THE PUNJAB
B.S. 4 Year Program/ Fourth Semester – Spring 2024
Paper: Data Structure & Algorithm Course Code:CC-212 Time: 3 Hrs. Marks: 60

THE ANSWERS MUST BE ATTEMPTED ON THE ANSWER SHEET PROVIDED

Q.1. Answer the following short questions:​ ​ ​ ​ (5x6=30)

1.​ Write a function to find the middle node of a singly linked list. If the list contains
an even number of elements, return the second middle node.​

2.​ Given the postfix expression: "482+", evaluate the expression using a stack.​

3.​ Run the Merge Sort algorithm on the following array of integers, showing the
step-by-step process of dividing the array into subarrays and merging them:​
38, 27, 43, 3, 9, 82, 10​

4.​ Give at least two examples of real-world applications where hashing is used.
Briefly explain each application to gain marks.​

5.​ Differentiate between Dijkstra's Algorithm and Floyd-Warshall Algorithm in terms


of edge weight requirements.

Q.2. Answer the following questions:​ ​ ​ (3x10=30)

●​ Consider the following sorting algorithm, which performs a modified


Bubble Sort on an array of n elements:​
def modified_bubble_sort(arr):

n = len(arr)

for i in range(n):

swapped = False
for j in range(0, n-i-1):

if arr[j] > arr[j+1]:

arr[j], arr[j+1] = arr[j+1], arr[j]

swapped = True

if not swapped:

break

return arr

Given an array of n = 10 elements in reverse order, calculate the number of


comparisons and swaps required to sort the array using the modified_bubble_sort
algorithm.

●​ Consider a graph represented as an adjacency list:

0: 1, 2

1: 0, 2, 3

2: 0, 1, 4

3: 1, 4

4: 2, 3

This graph has 5 nodes numbered from 0 to 4, and the adjacency list shows the neighbors
of each node.​
Perform BFS and DFS on it and MUST mention the steps involved.​

○​ Which of the following comparison sorts are in-place sorts? Circle all that apply.
2.​ SelectionSort
3.​ InsertionSort
4.​ HeapSort
5.​ BucketSort
6.​ Sequential QuickSort
7.​ Sequential MergeSort
8.​ Parallel QuickSort
9.​ Parallel MergeSort

Recompiled by:
Mr Shah;+92 340 5424517

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy