You are on page 1of 3

Course Code: CSCI 2100B Final Examination

Page 1 of 3

~ ~ ~ .:t *- ~
The Chinese University of Hong Kong

1I!l.;j;jl1'lT:fi

;r:;flfit1~J'

Copyright Reserved

Course Examinations First Term, 2013-2014

noLt

<;:-1- t uit u. r.( ~

CSCI 2100B Final Examination

Course Code & Title

Time allowed

....:....... ~............. hours

Student I.D. No.

.........................................

............. ~.~.......... minutes

Seat No. : .......................... .

1. Deque (10)
Consider a double-ended queue data structure named, deque, which supports the following operations:
PUSH(x )-inserts item x to the front of the deque data structure.
POPO-removes the item at the front of the deque data structure.
INJECT(x)-inserts item x to the back of the deque data structure.
EJECTO-removes the item at the back of the deque data structure.
Answer the following questions.
(a) (3) Could the deque emulate a queue data structure? Justify your answer.
(b) (3) Could the deque emulate a stack data structure? Justify your answer.
(c) (4) Explain how you would implement the deque data structure using two stacks. Justify your
answer.
2. Heaps (7)

(a) (5) We would like to write a procedure to merge two heaps, A and B, each with m and n ele
ments respectively into a single heap A'. A simple algorithm is to remove elements from heap
B and then insert them one by one into A which will result in A' after completion. Which
implementation of the heap data structure is more suitable for this operation. Justify your an
swer, write a short pseudo code, and illustrate how you can perform the heap merge operation
efficiently.
(b) (2) Give an expression of the time complexity of your merge operation in the last question in
terms of m and n. Justify your answer.
3. Hashing (16)
Given the input {48, 96, 84, 38,12,16,60,108,36, 72} and a hash function h(x)
x mod 10, show
what happens when each element in the list is inserted into the hash table of size lOusing the following
collision policy.
(a) (4) Open hash table. Explain what method of insertion you are using.
(b) (4) Closed hash table using linear probing (assuming that the increment is I).

Course Code: CSCI 2100B Final Examination

Page 2 of 3

(c) (4) What are the advantages and disadvantages of the various collision strategies in the first two
questions? Justify your answer.
(d) (4) Which hash function is a better hash function for the above sequence: (1) hI (x) = x mod 11
or (2) h2(x)
x mod 12? Justify your answer. Note that the size of hash table will change
according to hash function.
4. Graph (28)

(a) (3) Draw the corresponding adjacency matrix for the graph.
(b) (2) If one node is removed, the graph will no longer be strongly connected. Which node is it?
(c) (3) Please list the breath-first search sequence started from vertex D.
(d) (8) Find the shortest weighed path from vertex E to all other vertices for the graph above.
Illustrate intermediate steps with a table or figures.
(e) (8) Use Prim algorithm to find the minimum cost spanning tree of the graph. Show the interme
diate steps of the algorithm and the final result of the minimum cost spanning tree that you have
obtained.
(f) (4) Find the maximum flow in the network above assuming that the source is node D and the
sink is node G . No intermediate steps are required.

5. Sorting (21)
(a) (3) Show all the inversion pairs in the list (4, 1,2,5, 3)? List them out in an organized manner,
i.e., sort them in an ascending order by the first element and then by the second element.
(b) (5) Given the sequence (4,6, 1,8,3,7,2,5), sort the sequence using the Bubble Sort algorithm.
Illustrate the result after each pass. How many comparisons and swap have you performed?
Justify your answer by using a table with a column for comparison and swap performed in each
pass.
(c) (5) Given the sequence (5,2,8,1,4,7,6,3), sort the sequence using Mergesort. Illustrate the
result after each pass. How many comparisons have you performed?
(d) (4) Using the median of the first, middle, and last element of the array as pivot, demonstrate
ONLY the process of partition the sequence, (5, 7,8,4, 1, 2, 3, 6), into two parts of the Quicksort
(do not show the whole Quicksort algorithm!). Illustrate intermediate steps clearly.
(e) (4) while doing partition in the Quicksort, the loop invariant (a property that holds before each
iteration) can be illustrated as follows, where'?, represents unsorted elements.

Now you have an array which consists only R, G and B to be sorted in alphabetical order from
left to right. You can use an algorithm similar with the partition process to solve it by using
only one pass (i.e., one for-loop). What is the loop invariant of this algorithm? You can either
describe it or draw it.

Course Code: CSCI 2100B Final Examination

Page 3 of 3

6. Short Answers (18) (Please give only concise and short answers!)
(a) (6) The followings are the partial results of a binary trees traversals in pre-order, in-order and
post-order (X represents UNKNOWN). Please draw the corresponding tree. Note that the tree
has 11 nodes in total, which are from A to K.
Pre-order: XFXBXIHECXG In-order: DXKBIAXEJHX Post-order: XKXBFCJXGXA
(b) (12) What is the Average- and Worst-case runtimes of a single FindMax operation on the fol
lowing types of data structures? The answers should be expressed in the big-Oh notation in
terms of, n, the number of elements stored in the data structure.

I Average-Case I Worst-Case I
I

-End

Ascending-Sorted Single Linked List


Unsorted Array
Decending Sorted Array

Binary Search Tree

AVL Tree

Max-Heap

You might also like