MidSem17 Soln
MidSem17 Soln
Semester 2
Fifty minutes
Answer all four questions
Total marks 60
ANSWERS
Name:________________________________
You have access to a large string (more than 100000 characters) describing the inputs of an
experienced player who you assume knows and frequently uses the cheat codes. You would
like to use this string to identify candidate sequences of inputs that could be cheat codes (the
sequences of 20 inputs that occur most frequently).
is a
String question
a Solution i A variant of Rabin Karp
string Hash every 20
matching algorithm
characters to an integer and then check
for the most commonly occurring integers
Each one then has to be checked f
spurious matches
b Identical strings will have identical hashes
so a well chosen hash factor ill identify
me occur
my strings
c This will have an expected case of line time
In the worst case there will be many spurious
matches heuh would give 04pl.lu
CITS3001 Mid-semester Test 2017 3
CITS3001 Mid-semester Test 2017 4
Question 2: Optimisation (15 marks)
a) Define the term Greedy Algorithm and describe one problem where greedy algorithms
always give a correct answer, and one problem where greedy algorithms sometimes
give the wrong answer.
7 marks
b) Demonstrate the dynamic programming algorithm for the 0-1 knapsack problem, with
the items with weight and value given in the following table.
Item A B C
Error The D
knapsack
Weight 5 7 9 11 has size
Value 3 4 5 6 12
8 marks
b O i z 3 4 5 67 8 9 10 11 12
O 3 3 3 3 3 3 3 3
A O O O O
4 4 4
B O O O O O 3 3 44
5 5 5
Iq
C O O O O O 3 3 44 74
O O O 3 3 44 55 6
D O O
Items A and B f
The tower of Hanoi is a puzzle that consists of three rods and a number of disks of different
sizes, which can slide onto any rod. The puzzle starts with the disks in a neat stack in
ascending order of size on leftmost rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to the rightmost rod, obeying the
following simple rules:
Consider the problem of using the A* algorithm to find the fastest solution to this problem.
a) Explain the terms admissible, monotonic and dominates in the context of heuristics
for A* algorithm?
6 marks
b) Consider the heuristic that estimates the number of moves remaining to be the total
number of discs minus the number of discs on the rightmost rod. Describe a new
heuristic and show that it is admissible, monotonic and dominates the old heuristic.
9 marks
a i A heuristic h admissible if
is hGaEh a
f all oc where h is the true cost
of reaching the goal from 0C
b see next
page
CITS3001 Mid-semester Test 2017 7
b A new heuristic could be
h s is masc n l diskn is not on the
rightmost rod
largest
I monotonic HCS
Clearly can
only
decrease
by 1 at most and
the step costs ane I s t
h I hcs.lt 1
The first child (the divider) divides the lollies into two non-empty piles. The second child (the
chooser) can either take the smallest pile, leaving the largest pile for the divider, or they can
give the smallest pile to the divider, and then they repeat the process with the largest pile,
except this time they swap roles. They repeat this process until all the lollies are given out.
For example, with a pile of 10 lollies, the first child could break the piles in 2 and 8 lollies.
The second child is not happy with 2, so he gives those to the first child, and then divides the
pile of 8 lollies into 2 and 6. The first child is not happy with 4 lollies, so he gives the pile of
2 lollies to the second child, and spilts the pile of 6 into a pile of 3 and 3. The second child
accepts the split and takes the 3 lollies, leaving 3 more for the first child and they both end up
with 5.
a) Demonstrate the minimax algorithm for this game assuming that the pile starts with 5
lollies.
10 marks
b) Suppose that we started with a pile of 100 lollies. What would you use as an
evaluation function in order to able to use alpha-beta pruning? Explain your choice.
5 marks
n
I
Io 2,3 0,0 I
4 I O
41,1 431,01
do 3,21 2,1 2,01
9 4,1
1 1 1 I
if
3
11
e et iii
I
iii
0,0 3,2 1142,1 0,012,3
CITS3001 Mid-semester Test 2017 9 0,0 3,27
I I th
i t l
0,0 3,2
11
b Evaluation of fu b c d
would be tat
what max
already has plus the
roughly fair