Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

NIGER STATE POLYTECHNIC, ZUNGERU

DEPT OF COMPUTER SCIENCE


SECOND SEMESTER EXAMINATIONS 2018/2019 SESSION
COURSE: COM 124 – ALGORITHM AND DATA STRUCTURE
PROGRAMME: NATIONAL DIPLOMA IN COMPUTER SCIENCE
INSTRUCTION: ANSWER QUESTION 1 AND ANY OTHER FOUR QUESTIONS.

MARKING SCHEME

1. MULTIPLE CHOICE
i. A
ii. C
iii. B
iv. A
v. C
vi. A
vii. A
viii. D
ix. B
x. C
xi. D
xii. C
xiii. A
xiv. B
xv. B
xvi. C
xvii. C
xviii. C
xix. C
xx. C
(1 mark for each correct option, Total = 20 marks)
2(a) Differentiate between algorithm and data structure (2 marks)

(b) Given the binary tree below, answer the following questions:

14
/ \
2 11
/ \ / \
1 3 10 30
/ /
7 40

1
i. How many leaves does it have? List them (2½ marks)
ii. How many of the nodes have at least one sibling? List them. (3½ marks)
iii. What is the value stored in the parent node of the node containing 30? (½ mark)
iv. How many descendants does the root have? (½ mark)
v. What is the depth of the tree? (½ mark)
vi. How many children does the root have? List them. (1 mark)

Answer.
2(a)
 Algorithm only specifies the steps for processing data on the computer while data structure
specifies the arrangement of data on the memory as wells as operations that can be performed
on the data.
 While algorithms does not determine the data structure to be used for keeping the data, choice
of data structure will determine the operations ( or algorithms ) that can be used in processing
the data.
 Efficiency of algorithms is measured using time complexity while efficiency of data structure
is measured using space complexity.
(2 marks for clearly differentiating between the two terms)
(b)
i. How many leaves does it have?
Number of leaves = 4. (½ mark)
The leaves nodes have the following keys: 1, 3, 7, 40 (½ mark each, Total = 2 marks)

ii. How many of the nodes have at least one sibling?


Nodes with at least one sibling are 6 (½ mark).
Nodes with at least one sibling have the following keys: 2, 11, 10, 30, 1,3
(½ mark each, Total = 3 marks)

iii. What is the value stored in the parent node of the node containing 30?
Value stored in the parent node of the node containing 30 = 11. (½ mark)

iv. How many descendants does the root have?


Descendants of the root node = 8 (½ mark)
v. What is the depth of the tree?
Depth of the tree = 3. (½ mark)

vi. How many children does the root have? Children of the root node are 2, namely: 2 ,11

(½ mark each, 1 mark total)

2
3(a) Given an array and a singly linked list, which of these two data structures uses more memory space
to store the same number of elements? Explain your answer. (2 marks)
(b) Consider the Java program below. Remember, a program is like an interesting poem, every word
and every line have their meanings. Then, answer the following questions:
public void some_function (int[] a)
{
Stack C =new Stack(a.length);
int[] b=new int[a.length];
for (int i=0;i<a.length;i++)
{
C.push(a[i]);
}
For (int i=0;i<a.length;i++)
{
b[i]=(int)(C.pop());
}
System.out.println("output :");
For (int i=0;i<b.length;i++)
{
System.out.println(b[i]);
}
}
i. Identify the data structures in the program. Name them, and state the size of each of the data
structures. (3 marks)
ii. Explain briefly, what you observe the program is doing. (3 marks)
iii. What is the worst-case time complexity for this algorithm? Explain briefly why. (2 marks)

Answer:

3(a) A singly linked list, because it also needs to maintain the pointers to the other nodes, which
takes up space. (2 marks)

(b)
i. The data structures in the program are: 1 stack, 2 arrays and theirs names are C, a, b
respectively. (½ mark each, Total = 1½ marks)
Size of the stack is given by the size of array a = a.length
Arrays a and b are of the same size (½ mark each, Total = 1½ marks)

ii. The program creates a stack C and fills it repeatedly- using the first for loop- with data items
in array a. Then, the data items in the stack are removed repeatedly and inserted into array b. At
the end the original items in array a are now arranged in reverse order in array b. Simply, the
program displays an output from array b that shows a reversal of original data in array a by
using stack C. (3 marks)

3
iii The worst case time complexity is determined by repetitions performed in each of the three
loops. The number of repetitions in each loop is determine by the size of the array a, since the
stack and array b are of the same size. Assuming the size of array a is n, then the number
repetitions in each of the three loops is n. Total time = 3n. As the coefficient 3 is not significant,
we can approximate the number of repetition to n. The time complexity for the algorithm clearly
is linear time. The bigger the size of array a (i.e., n), the longer the execution time of the
program. In Big O notation, this is O(n). (2 marks)

4(a) Mention four (4) types of linear and two (2) types of nonlinear data structures that you know (3
marks)

(b) Consider the algorithm below, and answer the following questions:
INPUT: A sequence of natural numbers of length n such that n >0
OUPUT: The largest and smallest element of S
1. max = S[0]; min = S[0]
2. for i = 1 to n do
3. if S[i] > max then
4. max = S[i]
5. else
6. if S[i] < min then
7. min = S[i]
8. end if
9. end if
10. end for
11. return max, min

(i) What type of data structure is S? (1 mark)


(ii) Explain briefly, what the algorithm does and how it works. (3 marks).
(iii) What is the worst-case time complexity for this algorithm? Explain briefly why. (3marks)
Answers:
4(a)
 Four (4) types of linear data structures: array, linked list, stack, queue.
(½ mark each, Total = 2 marks)
 Two (2) types of nonlinear data structures – trees, graph (½ mark each, Total = 1 mark)
(b) i. S is an array. (1 mark)
ii. What the algorithm does? The algorithm finds the largest and smallest numbers in an array of number.
(1 mark)

4
How? It assumes that the largest and smallest element in an array is the element in the first position (i.e.,
S[0] ) and it then compares largest and smallest with all other values in the array. Largest changes to a new
value where such value is greater than Largest. The same logic applies in determining the smallest. (2 marks)
iii. The worst-case time complexity of the algorithm is determined by the n – the number of times the
loop will be executed. The bigger the value of n, the longer the execution time. Thus, this is a linear time
algorithm. In other words, the worst case time complexity of the algorithm is O(n).

(1½ mark for correct time complexity, 1½ mark for correct and brief explanation)

5(a) Given a 5 element stack S (from top to bottom: 6, 4, 2, 10, 8), and an empty queue Q, remove the
elements one-by-one from S and insert them into Q, then remove them one-by-one from Q and re-
insert them into S.

i. How does S look like (from top to bottom)? With the aid of well-labelled diagrams of
the stack and the queue, briefly show how you arrived at your answer. (3 marks)
ii. What is the time complexity for:
 removing an element from the stack (1 mark)
 inserting an element to the queue (1 mark)
(b) Fill in the following table the time complexities of these operations on an array of n items.
Use only big-O notation, and do not have any extraneous constants in your expressions.

Operations Worst Case Average Case


Binary search of a sorted array
Access
deletion
Insertion
Heap sort

Answer
i.
Original state of Stack Original state of Queue, Q
6
4 pop
2
10
8

6
4
2
10
8

5
6 4

2
10
8

6 4 2

10
8

6 4 2 10

6 4 2 10 8

(2 marks)

By deleting elements from the queue using FIFO, the stack is repopulated with the value now reversed
from the way it was originally.

So the final states of both data structures are as shown below:

S Q
8
10
2
4
6
(1 mark)
ii the time complexity for :
removing an element from the stack - O(1) (1 mark)
inserting an element to the queue – O(1) (1 mark)

6
b.
Operations Worst Case Average Case
Binary search of a sorted array O(log n) O(log n)
Access O(1) O(1)
deletion O(n) O(n)
Insertion O(n) O(n)
Heap sort O(n log n) O(n log n)

( ½ mark for each correct time complexity, total = 5 marks)

6(a) Explain briefly, how each of these sorting algorithms works:

i. Bubble sort (2 marks)


ii. Selection sort (2 marks)
(b) Here is an array of ten integers: 5 6 8 0 1 7 9 2 3 4
Show the sorting of the numbers using bubble sort (6 marks)
Answer:
6(a)
i. Bubble sort algorithm works, as it name suggests, by comparing two adjacent elements in a list
and the bigger value “bubbles” to the rightmost position. At the end of the first iteration, the
biggest value in the list has “bubbled” up to its rightful positions. Successful iterations realise
the same “bubbling” up of values into their appropriate position, until the whole list is sorted
and no “bubbling” is necessary. (2 marks for clear, correct and complete description)
ii. Selection sort is one of the simplest sorting algorithms. It works by simply selecting the smallest
element and swapping its position with the element in the first position in an unsorted list. Now
that the first element is in its rightful position, the remaining elements are the unsorted list, and
the same logic is applied repeatedly to the unsorted list until all elements are sorted. (2 marks
for clear, correct and complete description)

Bubble sort of the ten integers:


5 6 8 0 1 7 9 2 3 4
5 6 0 8 1 7 9 2 3 4
5 6 0 1 8 7 9 2 3 4
5 6 0 1 7 8 9 2 3 4
5 6 0 1 7 8 2 9 3 4
5 6 0 1 7 8 2 3 9 4
5 6 0 1 7 8 2 3 4 9 End of first iteration.

(2 marks)

7
5 6 0 1 7 8 2 3 4 9
5 0 6 1 7 8 2 3 4 9
5 0 1 6 7 8 2 3 4 9
5 0 1 6 7 2 8 3 4 9
5 0 1 6 7 2 3 8 4 9
5 0 1 6 7 2 3 4 8 9 End of second iteration

(1marks)

5 0 1 6 7 2 3 4 8 9
0 5 1 6 7 2 3 4 8 9
0 1 5 6 7 2 3 4 8 9
0 1 5 6 2 7 3 4 8 9
0 1 5 6 2 3 7 4 8 9
0 1 5 6 2 3 4 7 8 9 End 3rd iteration
(1 marks)

0 1 5 6 2 3 4 7 8 9
0 1 5 2 6 3 4 7 8 9
0 1 5 2 3 6 4 7 8 9
0 1 5 2 3 4 6 7 8 9 End 4th Iteration
(1 mark)
0 1 5 2 3 4 6 7 8 9
0 1 2 5 3 4 6 7 8 9
0 1 2 3 5 4 6 7 8 9
0 1 2 3 4 5 6 7 8 9 End of 5th Iteration

(1 mark)

7(a) Draw a simple diagram to describe briefly each of following data structures.

i. Linked list
ii. Stack
iii. Queue (6 marks)

(b) Given an empty queue Q, what does it look like after the following operations?
1. Q.enqueue(10)
2. Q.enqueue(5)
3. Q.dequeue()
4. Q.enqueue(7)
5. Q.enqueue(20)
6. Q.dequeue() (3 marks)

(c) Mention one (1) real life application of the stack data structures (1 marks)

8
Answers:
7(a)

i. A linked list is a linear data structure made up of nodes containing information as well as link (or
address of the) next information on the list. Information can inserted at any point in linked list.

Link to
the First
Node
Link

Null

First Node Last Node

Data Item

(1 mark for brief description, 1 mark for diagram)

ii. Stack is a linear data structure in which the last item to be added to the stack is the first to be removed.
This implies that data items are added or removed from one point in the stack called TOP. The
operation for adding an item is a called a PUSH and to remove is POP.

PUSH 9 TOP

(1 mark for brief description, 1 mark for diagram)

9
iii. Queue:

The queue is a First-In-First-Out data structure. This means the first item added to the queue comes out first.
Data items are added at the REAR and they are removed at the FRONT. A queue can be implemented in
programs using an array or a linked list.

FRONT REAR

A B C D E

(1 mark for brief description, 1 mark for diagram)

(b) Given an empty queue Q, what does it look like after the following operations?
1. Q.enqueue(10)
2. Q.enqueue(5)
3. Q.dequeue()
4. Q.enqueue(7)
5. Q.enqueue(20)
6. Q.dequeue()

FRONT REAR

Empty Queue Q

10 5 Result of the first two enqueue operations (1 mark)

5 Result of the dequeue operation (½ mark)

5 7 20 Result of the next two enqueue operations (1 mark)

7 20 Final state of the Queue. (½ mark)

(c) Real life applications of stack:

- For realising recursions in programs


- Used in parsing of programming language source codes by compilers.
- Reversing words
- Expression conversions
(1 mark for any correct application)

10

You might also like