CPT212-Test2-2023 Solution
CPT212-Test2-2023 Solution
CPT212-Test2-2023 Solution
CPT212 -
Design & Analysis of
Algorithms
(Reka Bentuk & Analisis
Algoritma)
VENUE: DKG31/AUDITORIUM DATE: 5th July 2023 TIME: 1.55 pm-2.55pm (1 hour)
SIGNATURE:___________________________________
NO parts of this paper should be taken out of the test venue. Any incomplete papers submitted will not be
checked and appropriate action will be taken.
You may answer in English or Malay.
Answer space for Questions 1-20: Shade the letter to indicate the chosen answer.
1) = A = = B = = C = = D = = E = 11) = A = = B = = C = = D = = E =
2) = A = = B = = C = = D = = E = 12) = A = = B = = C = = D = = E =
3) = A = = B = = C = = D = = E = 13) = A = = B = = C = = D = = E =
4) = A = = B = = C = = D = = E = 14) = A = = B = = C = = D = = E =
5) = A = = B = = C = = D = = E = 15) = A = = B = = C = = D = = E =
6) = A = = B = = C = = D = = E = 16) = A = = B = = C = = D = = E =
7) = A = = B = = C = = D = = E = 17) = A = = B = = C = = D = = E =
8) = A = = B = = C = = D = = E = 18) = A = = B = = C = = D = = E =
9) = A = = B = = C = = D = = E = 19) = A = = B = = C = = D = = E =
10) = A = = B = = C = = D = = E = 20) = A = = B = = C = = D = = E =
(50 marks)
1
Matric No. :…………..
Questions 1-24: Answer in the space given at the front page of this test paper (50 marks).
1. What are the characteristics or attributes of a graph whose adjacency lists are as follows?
A: C → Bﬧ
A B
B: ﬧ
C: B →Dﬧ
D: ﬧ C D
I. Acyclic, II. Connected, III. Has a cycle, IV. Undirected
(a) I only (b) I and II only (c) II and III only
(d) II, III and IV only (e) None of the above
5. Find the minimum spanning tree using Prim-Jarnik algorithm to the following graph starting from
vertex A. The total minimum cost for the spanning tree is:
A
3
7
D B C
1
7
5 6 2
3
E F G
6
3
H I
4 6
1
K J
(a) 41
(b) 34
(c) 29
(d) 26
(e) 31
2
Matric No. :…………..
6. Shortest path algorithm is applied to a weighted graph with the following adjacency matrix. The
origin for the algorithm is vertex 3. This is not an appropriate question as it was not
covered in class.
1 2 3 4 5
1| 0 8 9 4
2| 0 1
3| 2 0 3
4| 2 0 7
5| 1 8 0
Which of the following is TRUE?
(a) Shortest path from vertex 3 to vertex 5 has a length of 10.
(b) Shortest path from vertex 3 to vertex 4 has a length of 2.
(c) Shortest path from vertex 3 to vertex 2 has a length of 2.
(d) There is a path from vertex 3 to vertex 1.
(e) All of the above are false.
7. Which of the following properties of Bellman- Ford’s and Dijkstra’s shortest path algorithm is
WRONG?
(a) Ford’s gives all-to-all shortest paths while Dijkstra’s gives only one-to-all shortest paths.
(b) Bellman-Ford’s is more powerful than Dijkstra’s in that it can process graphs with negative
weights.
(c) Dijkstra’s algorithm is based on the greedy method while Bellman-Ford’s is not.
(d) Bellman-Ford’s is more efficient than Dijkstra’s.
(e) None of the above.
13. A hash table is indexed 0..1011, with a hash function h(key) = key % 11. How many
collisions will occur when key 7, 0, 4, 8, 1 are inserted into the hash table?
(a) 0 (b) 1 (c) 3 (d) 4 (e) 5 7 % 11= 4, 0 % 11= 11, 4 % 11= 7, 8% 11= 3, 1 % 11 = 10
14. A hash table contains 8 slots and is indexed 0..7. The hash function used on the hash table is
(Key+1) % 8. Separate Chaining is used to resolve collision. The following keys are inserted into
the hash table: 23, 34, 8, 6, 7, 14, 28. The longest chain of the hash table is of length:
(a) 1 (b) 2 (c) 3 (d) 4 (e) 5
0: 23,7/- 1: 8,6 /- 2: /- 3: 34/- 4:/- 5: 28:/- 6: /- 7: 14/-
3
Matric No. :…………..
4
Matric No. :…………..
Questions 21-23: Answer in the space provided in each question. If the space is insufficient use
other blank spaces or please get additional papers.
21 A function is required to determine the number of isolated vertices (vertices with no incident
edges) in a directed graph.
(a) How can we determine it if we are given the graph in adjacency lists representation? Give
also the pseudocode for the purpose.
(10 marks)
Scan each chain, and count the no. of empty chains.
isolated :=0
For i:=0; I < n; i++ {
If adjlist [i] = null;
isolated++;
}
This is not required in the answer, just for illustration no. of isolated vertices = 2
A: C → Bﬧ
B: ﬧ
C: B →Dﬧ
D: ﬧ
Note: The question is a bit unclear, i.e., the intended meaning of “isolated vertices”, I will consider your
own interpretation in your answer, but it must be sensible. Here it means “no outgoing edges”. To
count the no. of incoming edges, there is no direct info. Same for Question 21 (c).
(b) What is the complexity of the algorithm in (i) (a) above? Justify your answer.
(5 marks)
(c) What would happen to the complexity of the algorithm in 21 1(a)/(b) above if we are given
the graph in adjacency matrix representation? Justify your answer.
(5 marks)
O(n2)
For each vertex (n vertices) we need to check whether there is an edge to other
vertices (n vertices), if there is none then we increment the no. of isolated vertices.
This is not expected in the answer, just to illustrate the algorithm, no. of isolated vertices = 1
0 1 0 1
0 0 0 0
1 0 0 0
0 0 0 1
5
Matric No. :…………..
22. (a) A hash table uses a hash function hash(key) = (key+3) % 13 and the table size is 14 12 13. Is
the hash function a good hash function? Why?
(5 marks)
Good
Fast computation,
Modulo base is a prime number
(b) Show the configuration of the hash table using the above hash function in (a) above and
quadratic probing technique to resolve collisions after the insertion of the keys 13, 14, 11, 40,
22, 30 into an initially empty table.
(10 marks)
0 1 2 3 4 5 6 7 8 9 10 11 12
11 13 14 40 30 22
Improves the brute-force algorithm by adding two potentially time-saving heuristics, i.e.,
Looking-Glass Heuristic and Character-Jump Heuristic and they have to work together.
occur = 0;
occur++;
return occur;
(b) Given below is the pseudocode for Brute-Force algorithm for pattern matching.
The above algorithm reports only one occurence of the pattern which is the location of the first
ocurrence of the pattern in the text. A pattern may occur several times in a text. Modify the
above pseudocode so the algorithm reports the number of occurrences of the pattern in the
text.
(10 marks)