Sunteți pe pagina 1din 6

FIT1029: Tutorial 5 Solutions

Semester 1, 2014
Certain large problems are too big (and complex) to be solved in a single
algorithm. In those cases, it is worthwhile spending some time to break them
down in to simpler problems which can then be handled by a collection of
modules to solve the initial problem rather than relying on one algorithm
do to everything. In many of these tasks, you are required to break vari-
ous problems down into simpler problems and write modules to solve these
simpler problems.
Activity 1
In addition to the main module possible modules are:
createDataBase(): which returns new database
addRecord(database, record): which takes a database and record as
parameters and adds a record to the database
getFirstRecord(database): which takes a data as a parameter and re-
turns the rst record of the database
getNextRecord(database, currentRecord): which takes a database and
the current record, currentRecord, in that database as parameters and
returns the next record in the database
lastRecord(database, record): which takes a database and a record as
parameters and returns true if the record is the last one in the database,
and false otherwise.
sort(database): which takes a database as a parameter and sorts the
database
1
equals(record1, record2): which takes two records, record1 and record2,
as parameters and returns true if record1 and record2 are the same and
false otherwise
lessthan(record1, record2): which takes two records, record1 and record2,
as parameters and returns true if record1 sorts before record2, and
false otherwise.
One possible implementation for merging the two databases is given in
Algorithm 1.
2
Algorithm 1 MergeDatabases(DB1[0..N-1], DB2[0..M-1])
1: input: Two lists of records, DB1 and DB2
2: output: A third list of records, holding the union of DB1 and DB2
3: assumptions: The modules listed above have been dened.
4: assumptions: getNextRecord(database, currentRecord) returns -1
when it reaches the end of the database.
5: DBNew createDataBase()
6: sort(DB1)
7: sort(DB2)
8: current1 getFirstRecord(DB1)
9: current2 getFirstRecord(DB2)
10: while (current1 = -1 AND current2 = -1 ) do
11: if (lessthan(current1, current2)) then
12: addRecord(DBNew, current1)
13: current1 = getNextRecord(DB1, current1)
14: else
15: if (lessthan(current2, current1)) then
16: addRecord(DBNew, current2)
17: current2 = getNextRecord(DB2, current2)
18: else
19: addRecord(DBNew, current1) # If records are equal, only one is added.
20: current1 = getNextRecord(DB1, current1)
21: current2 = getNextRecord(DB2, current2)
22: end if
23: end if
24: end while
# If the two original databases were dierent lengths, one still has records that need to be added.
25: while (current1 = -1) do
26: addRecord(DBNew, current1)
27: current1 = getNextRecord(DB1, current1)
28: end while
29: while (current2 = -1) do
30: addRecord(DBNew, current2)
31: current2 = getNextRecord(DB2, current2)
32: end while
33: return DBNew
3
Task 1
The travelling salesman problem seeks to nd the shortest Hamiltonian Cir-
cuit (i.e., a circuit that includes each vertex in the graph exactly once) in
an edge-weighted graph. A brute-force approach to this problem could be to
generate all permutations of vertices in the graph (using Johnson-Trotter),
remove those permutations that do not represent valid circuits in the graph,
then of the remaining permutations, select the one with the shortest distance.
Useful modules would be:
generatePermutations(V[0..N-1]): takes a list V of N vertices, and re-
turns a list of all possible permutations of V
validCircuit(G, V[0..N-1]): takes a graph G, and an ordered list of
vertices V. Returns true if V is a circuit in G, false otherwise.
sumEdges(G, V[0..N-1]): Takes a graph G, and an ordered list of ver-
tices V. Assumes V is a valid circuit in G. Returns the sum of all edge
weights in that circuit.
Algorithm 2 TSP
B
rute(G(V, E))
1: input: An edge-weighted graph G with a set of vertices V, and a set of
edges E.
2: output: The distance of the shortest possible Hamiltonian Circuit in
G. If no Hamiltonian Circuit exists, returns -1.
3: assumptions: The modules generatePermutations, validCircuit,
sumEdges have been dened.
4: allPerms generatePermutations(V)
5: minCircuit -1
6: for (i = 0..length(generatePermutations)) do
7: nextPerm generatePermutations[i]
8: if (validCircuit(G, nextPerm) then
9: circuitLength sumEdges(G, nextPerm)
10: if (minCircuit == -1 OR circuitLength < minCircuit) then
11: minCircuit circuitLength
12: end if
13: end if
14: end for
15: return minCircuit
4
Task 2
This is a good problem where we can use the recently seen search method of
Binary Search. This is because the water is sorted: it starts at 20% at 6am
and reaches 80% at 12 noon, so it has to cross at 50% at some point (note:
if might cross 50% more than once if the water level is allowed to increase
and decrease, but we wont worry about that as long as we can nd 1 of the
times at which it will be 50%).
Algorithm 3 HalfFull()
1: input: -
2: output: The time (to the closest minute) at which the water tank was
50%0.0001% full
3: assumptions: percentageFull(t) returns the percentage of water in the
tank at time t.
4: min 6am
5: max 12noon
6: mid
min+max
2
7: while (abs(max - min) > 1 minute) do
8: amount percentageFull(mid)
9: if (abs(amount - 50) < 0.0001) then
10: return mid
11: end if
12: if amount < 50 then
13: min mid
14: else
15: max mid
16: end if
17: mid
min+max
2
18: end while
5
Task 3
Algorithm 4 FindValue(a, b)
1: input: Values a, b
2: output: c, where f(c) = 0
3: assumptions: Function f has been dened in a separate module.
4: min a
5: max b
6: gap 1.0 # This can be any real value make smaller to increase accuracy.
7: mid
min+max
2
8: while (abs(max - min) > gap) do
9: val f(mid)
10: if (val == 0) then
11: return mid
12: end if
13: if val > 0 then
14: min mid
15: else
16: max mid
17: end if
18: mid
min+max
2
19: end while
20: return mid
Task 4
Notice that the original triangle can be split into four equally sized triangles,
each half the size of the original triangle. If you place a trapezium tile at
the bottom of the original triangle in the middle, each of the four smaller
triangles have a triangle covered. You have now reduced the problem to
covering four triangles, each with a single triangle covered, and each half the
size as the original triangle. Continue in this way until you have triangles
with the side of of 2, with one triangle covered, then cover the remaining
part of these triangles with trapeziums.
6

S-ar putea să vă placă și