FIT1029: Tutorial 8 Solutions Semester 1, 2014: Task 1
FIT1029: Tutorial 8 Solutions Semester 1, 2014: Task 1
Semester 1, 2014
Task 1
Fig. 1 represents the dierent recursive calls made on MergeSort Algorithm
when it is called to sort a list [A,L,G,O,R,I,T,H,M]. The numbers on the
arrows represent the order in which these parts are executed. The black
parts represent MergeSort, while the blue parts represent MergeLists.
Figures 2 and 3 represent the dierent recursive calls made on QuickSort
Algorithm when it is called to sort a list [A,L,G,O,R,I,T,H,M]. Fig. 2 shows
the lazy partitioning method, where three separate loops are used, while
Fig. 3 uses the more ecient partitioning method.
The red characters represent the positions of the pivot after the Parti-
tion module is called. The blue text represent the smaller lists on which
QuickSort module is called.
Task 2
The bitstrings will be generated from highest value to lowest value, i.e.:
111
110
101
100
011
010
001
000
1
Figure 1: MergeSort on ALGORITHM
2
Figure 2: QuickSort on ALGORITHM with lazy partitioning method
3
Figure 3: QuickSort on ALGORITHM with ecient partitioning method
4
Task 3
Algorithm 1 backtrackWithStack(partialSolution, otherParameters)
1: input: current partial solution, other parameters as required
2: output: (depends on problem)
3: assumptions: possibleItems is a stack.
4: if (isPossibleSolution(partialSolution)) then
5: processSolution(partialSolution)
6: else
7: possibleItems getPossibleItems(partialSolution)
# This assumes getPossibleItems has been modied to return a stack. If getPossibleItems
# returns a list, items in the list will need to be pushed one by one onto possibleItems.
8: while (stackEmpty(possibleItems) == FALSE) do
9: nextItem pop(possibleItems)
10: add(nextItem, partialSolution)
11: backtrackWithStack(partialSolution, otherParameters)
12: delete(length(partialSolution)-1, partialSolution)
13: end while
14: end if
Task 4
Recall the Torch Problem: 4 people wish to cross a bridge at night. They
have only 1 torch and the bridge is so narrow that at most 2 people may
pass at a time. Person A takes 1 minute, Person B takes 2 minutes, Person
C takes 5 mins and Person D takes 10 mins. The two people crossing at any
time also have to cross at the speed of the slowest person. The aim is to nd
the least amount of time needed for all 4 people to cross the bridge.
Now the question asks us to nd a partial solution for this problem (hope-
fully, we will use that partial solution in the Backtracking Algorithm). We
can clearly see that this problem will have many dierent states, where cer-
tain people will have already crossed the bridge and some will have not.
One possible partial solution is the list of people who are on the other
side of the bridge: eg. When everyone is still on the initial side, the list
of people on the other side is empty (L = []). When say Person B and
Person C have crossed over, the list of people on the other side is L =
[B,C]. When everyone has crossed over, the list of people on the other side is
L = [A,B,C,D].
Another possible representation is to use bitlists: eg. representing all
5
people on the initial side would be a list of four zeros: [0,0,0,0]. If, for
example, the rst two people cross over, the rst two bits would be ipped
to ones: [1,1,0,0]. The desired nal state is [1,1,1,1].
Note that to be able to undo the last move, as the backtracking algorithm
requires, it is necessary to keep track of all states up until the current state,
not just the current state. So a partial solution representation needs to be a
list of lists, for example: [[0,0,0,0], [1,1,0,0], [0,1,0,0], [1,1,1,0]].
(What would the getPossibleItems and processSolution modules look like
for these representations? How would you determine which side of the river
the torch is on? How would you calculate the total crossing time of a partic-
ular partial solution?)
Task 5
Algorithm 2 getPossibleItems(partialSolution, capacity, weights)
1: input: partialSolution: items currently in knapsack
2: input: capacity: integer representing capacity of the knapsack
3: input: weights: list of item weights.
4: output: a list of items that can still t in the knapsack
5: assumptions: module sumWeights dened (see Algorithm 3 below)
6: possibleItems = []
7: for (i = partialSolution[length(partialSolution)-1]..length(weights)-1)
do
8: if (sumWeights(partialSolution, weights) + weights[i] capacity)
then
9: add(i, possibleItems)
10: end if
11: end for
12: return possibleItems
6
Algorithm 3 sumWeights(itemList, weights)
1: input: itemList: a list of items currently in the knapsack
2: input: weights: a list of item weights
3: output: the total weight of all items in itemList
4: assumptions: each item stored in itemList is a valid index in the list
weights
5: sum 0
6: for (i = 0..length(itemList)-1) do
7: sum sum + weights[itemList[i]]
8: end for
9: return sum
Algorithm 4 processSolution(partialSolution, bestSolution, values)
1: input: partialSolution: items currently in knapsack
2: input: bestSolution: best combination of items seen so far
3: input: values: list of item values
4: output: the higher-valued solution out of partialSolution and bestSo-
lution
5: assumptions: module sumValues dened (see Algorithm 5 below).
6: assumptions: The main backtracking module is assumed to reset the
value of bestSolution to the returned output of this module.
7: if (sumValues(partialSolution > sumValues(bestSolution)) then
8: return bestSolution
9: else
10: return partialSolution
11: end if
Algorithm 5 sumValues(itemList, values)
1: input: itemList: a list of items currently in the knapsack
2: input: values: a list of item values
3: output: the total value of all items in itemList
4: assumptions: each item stored in itemList is a valid index in the list
values
5: sum 0
6: for (i = 0..length(itemList)-1) do
7: sum sum + values[itemList[i]]
8: end for
9: return sum
7
Task 6
A
\
L
/ \
G O
\ / \
I M R
/ \
H T
Task 7
Heapsort: build a heap and then delete items from the heap placing the items
in order in a list. See Figure 4. Read the gure left to right, top to bottom.
The heap is rst built from the string of letters which is then modied to
a valid heap. Now we select the smallest element at the root and add it to
the array ({A}). This process continues until no elements are left on the
heap. The sequence of letters removed amounts to a sorted list. Points to
remember:
Building the heap: insert the element at the bottom level and while
a parent is greater than the current element swap the current element
with the parent
Deleting an item: remove the root and place the right-most element
from the bottom level to the root. Move the root down while it is
greater than any of its children (swap with child). If both children are
smaller, choose the smallest to swap with.
8
Figure 4: Heapsort on ALGORITHM
9