# Manual 8473071

```Family Name: . . . . . . . . . . . . . . . . . . . . . . . . . .
Other Names: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Signature: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
ID Number: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Model Solutions
COMP261: Test
10 Sept 2012
Instructions
• Time allowed: 45 minutes .
• Answer all the questions. There are 45 marks in total.
• Write your answers in the boxes in this test paper and hand in all sheets.
• If you think some question is unclear, ask for clarification.
• You may use paper translation dictionaries, and non-programmable calculators.
• You may write notes and working on this paper, but make sure your answers are clear.
Questions
Marks
1. Shortest Paths
[15]
2. Graphics
[8]
3. Union Find
[11]
4. Tries and String Searching
[11]
TOTAL:
Cross out rough working that you do not want marked.
Specify the question number for work that you do want marked.
COMP 261 (Terms Test)
2
continued...
Student ID: . . . . . . . . . . . . . . . . . . . . . . .
Question 1. Shortest Paths
[15 marks]
(a) [8 marks] Suppose you are using A* to search for the shortest path from A to G in the weighted
undirected graph below, where the heuristic estimate of the cost from each node to the G is shown
in the node. Show how A* finds the shortest path, making clear the order in which nodes are added
and removed from the queue, and the final path found.
Make sure you show the priorities of the nodes on the queue, for example, by writing F←D/156 to
show that node F was added from node D with a priority of 156.
K/19
16
5
H/16
24
4
F/34
8
B/31
10
C/23
6
A
/28
18
6
10
D
/20
25
G/0
action, new value of queue (bold are newly added items)
[initialise:] [A/28]
[visit A:] [C←A/29 D←A/30 H←/40]
[visit C←A:] [K←C/29, B←C/47, D←A/30, H←A/40]
[visit K←C:] [H←K/31, F←K/60, D←A/30, H←A/40, B←C/47]
[visit D←A:] [H←D/32, G←D/35, H←K/31, H←A/40, B/47, F/60]
[visit H←K:] [G←H/33, H←D/32, G←D/35, H←A/40, B/47, F/60]
[H←D:] already visited [G←H/33, G←D/35, H←A/40, B/47, F/60]
[visit G←H:] goal node
The Shortest Path Found:
A C K H G
(Question 1 continued on next page)
COMP 261 (Terms Test)
3
continued...
(Question 1 continued)
(b) [4 marks] The choice of the heuristic estimate is important for A*. Ideally, it should be
• admissable (underestimate the the remaining cost) and
• consistent ( the estimate never becomes less accurate along a path)
Explain the consequences for A* search of having heuristics that are
(i) [2 marks] Not admissible (ie, may sometimes overestimate)
If the heuristic is not admissable, A* may return a
path that is not optimal -- has a higher cost than the
optimal one.
(ii) [2 marks] Admissable but not consistent
If the heuristic is admissable but not consistent,
you need to use the more expensive version of A* to
guarantee finding the optimal/lowest cost path. If
you use the simple version, it may find a non-optimal
path.
(Question 1 continued on next page)
COMP 261 (Terms Test)
4
continued...
Student ID: . . . . . . . . . . . . . . . . . . . . . . .
(Question 1 continued)
(c) [3 marks]
Europe has an extensive and complicated railway system with a range of trains, from long distance
high speed trains through to local trains connecting small towns. A complete timetable for all the
trains is available. Suppose you are building a trip planner for the train system that will find the
fastest train route between any two places in Europe, and you are using A* to search for the route.
Suggest a good heuristic that you might use for the search and justify your choice.
If we are minimising time, then the heuristic must
measure time. The obvious measure for an admissable
heuristic is straight-line distance between the two
places, divided by the speed of the fastest train on
the network. But this will be very optimistic. It
doesn’t take into account the varying speeds of the
trains on different routes, the amount of time for
transfering between trains, or the varying timetables
at different times of the day (or days of the week).
Since the heuristic is estimating the cost over an
unknown route, it is hard to take these into account.
I would consider precomputing a table of minimum times
between each pair of stations based on the fastest
train on each route, assuming instant transfers.
COMP 261 (Terms Test)
5
continued...
Cross out rough working that you do not want marked.
Specify the question number for work that you do want marked.
COMP 261 (Terms Test)
6
continued...
Student ID: . . . . . . . . . . . . . . . . . . . . . . .
Question 2. Computer Graphics
[8 marks]
Show the values in the edgelists that would be constructed for the following polygon by the z-buffer
rendering algorithm described in the lectures. The (x,y,z) coordinates of the vertices are shown.
State the order that you processed the edges, explain how you handled the horizontal edge, and
(13.0, 20.0, 40.0)
x
9
y
10
11
12
13
14
Edge-Lists
15
x left
19
20
20
21
21
22
22
23
zleft
x right
zright
23
24
24
25
(14.0, 24.0, 60.0)
(10.0, 24.0, 56.0)
Order edges processed: left, right, bottom
Horizontal edge: There is no change in y, so there will be no iteration to interpolate
along the edge. We need to be careful to enter the two end points (though in this
case, the values were already there from the other edges).
Assumptions, if any
COMP 261 (Terms Test)
7
continued...
Question 3. Union Find
[11 marks]
(a) [7 marks] Consider the following diagram of a collection of sets of letters, represented using the
union-find data structure. The numbers are the ranks.
allSets:
A
B
0
F
3
C
J
2
K
G
L
H
D
2
E
Show the changes that will be made to the structure (including changes to the ranks of the top four
nodes) if the following three operations are applied, using the union-find algorithm on the facing
page:
Union(A, C),
Union(F, L)
Union(E, H)
allSets:
A
B
0
F
3
C
2
J
2
K
G
H
D
L
E
(Question 3 continued on next page)
COMP 261 (Terms Test)
8
continued...
Student ID: . . . . . . . . . . . . . . . . . . . . . . .
(Question 3 continued)
MakeSet ( x ) :
x . parent ← x
x . rank ← 0
add x t o a l l S e t s
Union ( x , y ) :
x r o o t ← Find ( x )
y r o o t ← Find ( y )
I F x r o o t . rank < y r o o t . rank THEN
xroot . parent ← yroot
Find ( x ) :
remove x r o o t from a l l S e t s .
I F x . p a r e n t = x THEN
ELSE I F x r o o t 6= y r o o t THEN
return x
yroot . parent ← xroot
ELSE
remove y r o o t from a l l S e t s .
x . p a r e n t ← Find ( x . p a r e n t )
I F x r o o t . rank = y r o o t . rank THEN
return x . parent
x r o o t . rank++
(b) [4 marks] The version of the Union-Find algorithm above has two separate improvements over
the basic algorithm (using ranks, and modifying the trees in Find). Discuss the effects of the two
improvements and give a reason why using ranks might not be worthwhile.
Using ranks means that when joining two trees, the algorithm
joins the shorter tree to the longer tree, which will make the
resulting trees shorter.
Modifying the trees in Find makes trees much shorter and bushier
- every time Find is called on an item, all the nodes on the tree
from item to the root will be made into direct descendents of the
root. This keeps the trees very short.
It also means that the ranks are mostly overestimates, so that
the use of rank in Union is typically wrong, but it doesn’t
matter since the trees are so short anyway -- it doesn’t make
much difference if a short tree is added to an even shorter tree,
and Find will shorten it again soon.
COMP 261 (Terms Test)
9
continued...
Question 4. Tries and String Searching
[11 marks]
(a) [5 marks] Show a Trie containing the following set of strings.
(Note: label the links of the Trie, not the nodes.)
policy
polite
politics olive
polls
oppose
police
op
oliver
pop
(Question 4 continued on next page)
COMP 261 (Terms Test)
10
continued...
Student ID: . . . . . . . . . . . . . . . . . . . . . . .
(Question 4 continued)
(b) [3 marks] Briefly explain why it is more efficient to use a trie to search for any of a set of phrases
in a large piece of text than to search for each phrase separately (even using the KMP algorithm).
When searching with a trie, you are searching for all the
phrases at once. For each position in the text, you will
(at most) go down one path of the trie. That means the
cost is at worst O(n × m), where n is the length of the text
and m is the length of the longest phrase, and typically
faster than this since the search will usually exit before
reaching the end of a path, and won’t always go down the
longest path.
When searching with naive search, you have to search for
each phrase at each position of the text, so the cost is
at worst O(n × m0 × p) where m0 is the average length of the
phrases and p is the number of phrases.
When searching with KMP, you again have to search for each
phrase, but it is more efficient: the cost is at worst
O(n × p).
So using a trie will generally be more efficient if the
number of phrases is larger than the average length of the
phrases.
(c) [3 marks] Using your answer to part (a) (or another trie, if you wish), explain how searching for
any of set of phrases using a trie can be improved by adding “backlinks” into the trie, connecting
some nodes to earlier nodes in other branches of the trie.
Suppose we are searching down the tree on the facing page,
and have matched p o l i (reaching the node labeled ∗),
but then mismatch on the next character. We don’t need to
start searching again from the top of the tree because we
know we have just seen o l i, which corresponds to taking
the first three steps down the rightmost branch. We could
therefore put a link from the nodel labeled ∗ to the node
labeled +, and keep searching, in the same way that KMP
on the tree that matches the prefix of a path, then we
could search a piece of text without ever having to step
backwards in the text, which would be more efficient.
********************************
COMP 261 (Terms Test)
11
```