0% found this document useful (0 votes)
44 views6 pages

CSC3100 Final

The document is a final exam paper for CUHK(SZ)-CSC3100 covering various topics in algorithms and data structures. It consists of multiple-choice questions, true/false questions, and problem-solving tasks related to graph theory, binary trees, and linked lists. Students are required to answer all questions within a time limit of 150 minutes without any external aids.

Uploaded by

3154482093
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)
44 views6 pages

CSC3100 Final

The document is a final exam paper for CUHK(SZ)-CSC3100 covering various topics in algorithms and data structures. It consists of multiple-choice questions, true/false questions, and problem-solving tasks related to graph theory, binary trees, and linked lists. Students are required to answer all questions within a time limit of 150 minutes without any external aids.

Uploaded by

3154482093
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/ 6

CUHK(SZ)-CSC3100 Final Exam

1st Semester, 2021-2022


Note:
a. No notes or calculators are allowed in the exam.
b. This exam paper has four pages, in double-sided printing.
c. Answer all questions within 150 minutes in an answer book.

1. (10 points) Choose ONE solution that best suits each question. (2 points each)

B (1) The typical complexity of searching an element in a balanced binary search tree is:
A. O (1)
B. O (log n)
C. O (n)
D. O (n log n)
(2) For input sequence: {2, 3, 5, 6, 9, 11, 15}, which algorithm needs the largest number
of comparisons?
CID A. Bubble Sort
B. Insertion Sort
C. Merge Sort
D. Quick Sort

m
00 (3) For input sequence: {2, 3, 5, 6, 9, 11, 15}, which algorithm runs in O(N ) time? Note:
throughout the paper, N (or n) denotes the number of input elements.
A. Bubble Sort

neri
B. Selection Sort
C. Insertion Sort
D. Quick Sort
(4) Which algorithm typically has the most space requirement on the same input?

C A. Selection Sort
B. Insertion Sort
C. Merge Sort
D. Quick Sort
(5) Which of the following typically runs the fastest?

B A. Searching for an element in a singly linked list


B. Computing the hash value for a given input
0 1
0 N
C. Searching for a value in a binary search tree

O N
D. Emptying all elements from a stack
IT
CUHK(SZ)-CSC3100 Final Examination - Page 2 of 4 Dec. 2021

2. (10 points) Answer the following questions with either true or false (2 points each).

(1) One can implement a stack based on a linked list so that each individual push/pop
operation is in time O(1).
有 pointer

(2) One can implement a stack (of unlimited size) based on a linear array so that each
individual push/pop operation is in time O(1).
1
57 3
_y ㄕ
0
(3) The core data structure used in Depth First Search is a queue.
五 ⺟
皆品
(4) One can reverse the order of the elements in a linked list in time O(n).
(5) It is possible to append two linked lists in time O(1).

3. (15 points) Consider the graph in Figure 1 with nodes A to K and weights shown on
edges. Answer the following questions (5 points each).

(1) In what order are vertices visited for Depth First Search starting at node A.
(2) In what order are vertices visited for Breadth First Search starting at node A.
(3) Show the minimum spanning tree (MST) derived by Kruskal’s or Prim’s algorithm.

Note: If a node has multiple adjacent nodes to visit, always visit in alphabetic order!

Figure 1: A Weighted Graph.

4. (10 points) For the directed graph shown in Figure 2, answer the following questions.

(1) Draw both the adjacency matrix and adjacency list representations of this graph.
(2) Give two valid topological orderings of the nodes in the graph.
1 DFS

ABDEGIHFCK MBFSiABCFDJHKEG Pinia.co


t

ii ˇ ˋi
ㄨ ㄨ
x ㄨ 337 4C
17 6 3
3
3 6 卡 幾
79 10 10
kn
Prim s v E w
EG 华
428
器品
品 EI
發品
Decrease
iii
J6
wniieig.ie
㬫 dunwu
tn
giiai
and
Kruskal A B 4FDG I H E D不
aguia.Tiiu.in 了


1 年
kruskal
soiiiE
viw.siE
edgeinGraph 哭ÌH 1k
⼈⼯

蠿 丁

䲜䲜
else
for win setarragiu
点器器装
品以
6) [16 points total] Graph Manipulations:

Use the following graph


CUHK(SZ)-CSC3100 Finalfor this problem. Where needed
Examination and not
- Page 3determined
of 4 by the Dec. 2021
algorithm, assume that any algorithm begins at node A.

73
D
B
10 0
5 C
42
A
6 E

a) (4 pts) Draw both the adjacency matrix and adjacency list representations of this
graph. Be sure to specify which is which.
Figure 2: A Directed Graph.

5. (15 points) Suppose the class java.util.LinkedList is implemented by a doubly linked


list, maintaining a reference to the first and last node in the list, along with its size.
public class LinkedList<Item> {
private Node first; // the first node in the linked list
private Node last; // the last node in the linked list
private int b)N;(2 pts) Give two//
valid topological orderings of the nodes in the graph.
number of items in the linked list
private class Node {
private Item item; // the item
private Node next, prev; // next and previous nodes
}
...
}

What are the best estimates of the worst-case running time of the following operations in
big-O notation? (3 points each, choose among O(1), O(log N ), O(N ), O(N log N ), O(N 2 ))
Page 8 of 13

(1) addFirst(item): Add the item to the beginning of the list.


(2) get(i): Return the item a position i of the list.
(3) set (i, item): Replace position i of the list with item.
(4) removeLast(): Delete and return the item at the end of the list.
Final Examination Page 6/10 E&CE 250 W’01
(5) contains(item): Is the item in the list?
PROBLEM 4 [16]
6. (10 points) Consider a binaryA.tree
Trees
shown in Figure 3. For each of the preorder, inorder
and postorder traversals, give the order in which the nodes are visited.
1.Consider the binary tree shown below. For each of the traversals listed, give the order in which the
nodes are visited.
FBAECDHJL
preorder
F

inorder ABEDCFJHL
B H
preorder

order ADCEBJLHF
inorder
A E J L

post
postorder
breadth-first
C

2. As discussed in the class, preorder, postorder and inorder traversals are special cases of the more
general depthfirst traversal. Rather than implementing individual traversals separately, we made use of a
Figure 3: A Binary Tree.
design pattern, called adapter, which allowed a single depthFirstTraversal method to provide most of the
needed traversal functionality. This point is illustrated in the following for the postorder case:
Visitor v = new PrintingVisitor ( );
Tree t = new SomeTree ( );
// …
t.depthFirstTraversal (new PostOrder (v));
Consider an expression tree. Complete the following PostOrder adapter class and the visit method of the
PrintingVisitor. The PrintingVisitor prints the contents of the tree in the postfix notation. It is permissible
that the postfix format such as ab/cd-e*+ be printed with one operator/variable per line.
5 170 1

7
niini.in

写 0
遍 of

8
华篇筣
不同label数

eeoc
CUHK(SZ)-CSC3100 Final Examination - Page 4 of 4 Dec. 2021

7. (10 points) The height of a binary search tree (BST) depends on the order in which
the keys are inserted into a tree if no balancing operation is performed. Given an
initially empty BST, in what order will you insert the keys A, B, C, D, E, F, G so
that the height of the BST is minimal. Note: the keys are in alphabetic order, i.e.,
A < B < C < D < E < F < G.
Task 4 (10 pts). A connected component of a graph is a set of vertices where each node can reac
8. (10 points) A connected component of a graph is a set of nodes where each node can
very other node in the
reach component
every other node in along the given
the component alongedges, and
the given which
edges, is connected
and which totono additiona
is connected
ertices. For example, the graph
no additional below
nodes. For hasthe
example, 3 connected components.
graph in Figure 4 has three connected components.

ICS 161 — Algorithms — Spring 2005 — Final Exam

Please answer the following eight questions on the answer sheets provided. Answers written on
other pages or on the wrong sheet will not be scored. Be sure to write your name and student
ID on allFigure 4: Ansheets.
three answer Undirected
You may Graph with
continue Three
your Connected
answers Components.
on the back of the same answer
Explain, in words, how to use Kruskal’s algorithm to compute
sheet. No books, notes, or calculators may be used during the exam.
the number of connected com
ponents in a graph.
Explain, in words,
1. (10 points) how toMethod
Use the Master use Kruskal’s algorithm
to solve the to compute
following two theWrite
recurrences. number of connected
your answers
components in an undirected graph.
using O notation.
(a) Q( f ) = 2Q( f /2) + log2 ( f )
9. (10 (b)
points)
H(m) = The algorithm
3H(m/2) + m3 (pseudo code) in Figure 5 sorts an array (given as parameter
seq) of n numbers. Estimate the time complexity of the algorithm as a function of input
2. (15
size n.points)
BrieflyTheshow
following
yourrecursive algorithm
calculation. sortsThe
Note: a sequence of nparameter
function numbers. Write
seq down a
is passed by
recurrence describing the running time of the algorithm as a function of
reference, and 2n/3 will be rounded up to the nearest integer in execution. n. You do not need to
solve your recurrence.
def triplesort(seq):
if n <= 1: return
if n == 2:
replace seq by [min(seq),max(seq)]
return
triplesort(first 2n/3 positions in seq)
triplesort(last 2n/3 positions in seq)
triplesort(first 2n/3 positions in seq)

3. (15 points) Suppose you have Figure 5: A Triple


an undirected graphSort
withAlgorithm.
weighted edges, and perform a depth-
first search, such that the edges going out of each vertex are always explored in order by weight,
smallest first. Is the depth first search tree resulting from this process guaranteed to be a minimum
spanning tree? Explain why, if it is, or, if it isn’t, provide a counterexample (specifying the start
vertex for the DFS and showing both trees).

4. (15 points) We are given as input a sequence L of n numbers, and must partition it into as
few contiguous subsequences as possible such that the numbers within each subsequence add
to at most 100. For instance, for the input [80,-40,30,60,20,30] the optimal partition has two
subsequences, [80] and [-40,30,60,20,30]. Let C[i] denote the number of subsequences in the

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