Anda di halaman 1dari 8

FIT1029: Tutorial 4 Solutions

Semester 1, 2014
Task 1
Part 1. When k = 0, j = 0, .., 8, i.e., 9 values. When k = 1, j = 1, .., 8, i.e.,
8 values, and so on. So, the total number of substrings is 9 +8 +.. +1 = 45.
Part 2. Refer to Algorithm 1 below.
Algorithm 1 Printsubstrings(Str[0..n-1])
input: Str[0..n-1] (string)
output: Prints out all the substrings of the form Str[j..k]
k 0
while (k < n) do
j k
while (j < n) do
i k
while (i j) do
print S[i]
i i + 1
end while
newline
j j + 1
end while
k k + 1
end while
Task 2
Part 1. Algorithms 3 and 4 are two possible ways of generating subsets.
Alg. 3 uses bitstrings (calling the module dened in Alg. 2), while Alg. 4
doesnt (but is harder to understand).
1
Algorithm 2 Bitstring(i,n)
1: input: integer i, bitstring length n
2: output: Prints out a bitstring of length n representing integer i
3: assumption: Basic operations on lists are dened as in Tutorial 2
4: bStr []
5: if (i == 0) then
6: insert(0, 0, bStr)
7: end if
8: while (i > 0) do
9: insert(0, i%2, bStr) // i%2 is the remainder of i divided by 2
10: i (i i%2)/2
11: end while
12: while (length(bStr) < n) do
13: insert(0, 0, bStr) // make sure the bitstring has length n
14: end while
15: return bStr
Algorithm 3 GenerateSubsets(L[0..n-1])
1: input: list of n elements
2: output: Prints out all the subsets of list L
3: assumption: -
4: i 0
5: while (i < 2
n
) do
6: bitstr Bitstring(i,n) // bitstring of length n representing integer i
7: subset [ ]
8: c 0
9: while (c < n) do
10: if (bitstr[j] == 1) then
11: add(subset,L[c]) // add the item at index c in list L in the subset
12: end if
13: c c + 1
14: end while
15: print subset
16: i i + 1
17: end while
2
Algorithm 4 GenerateSubsets(L[0..n-1])
1: input: L[0. . .n-1], a list of n elements
2: output: Prints out all the subsets of list L
3: assumptions: -
4: allSubsets [ [ ] ] // a list containing the empty list
5: print [] // empty subset
6: counter 0
7: while counter < n do
8: m length(allSubsets) // will change at each iteration as allSubsets grows
9: i 0
10: while i < m do
11: subset allSubsets[i]
12: add(L[counter], subset)
13: print subset // L[counter] added to the subset
14: add(subset, allSubsets)
15: i i + 1
16: end while
17: counter counter + 1
18: end while
Part 2. In this problem, we have 4 objects (the farmer, the wolf, the
goat and the cabbage) and each of these objects has 2 possible states: ie.
either of the left side of the river or on the right side of the river.
We can use a bitstring to represent the current state of the problem.
Lets represent the left side by a 0 and the right side by a 1. We say
that bitstrings of size 4 will contain the state of the farmer at index 0, the
state of the wolf at index 1, the state of the goat at index 2 and nally, the
state of the cabbage at index 3.
By using Algorithm 3 to generate all possible bitstrings of length 4, we
can generate all possible 2
4
states to the problem. States that violate the
constraints of the problem (wolf and goat left alone; goat and cabbage left
alone) need to be removed. We then need to nd a valid sequence of tran-
sitions from the start state (0000) to the desired nal state (1111) (where
valid here means ipping the farmer bit and one other bit).
Task 3
One simple approach would be to use nested for loops, as follows:
3
Algorithm 5 FourQueens
1: input: -
2: output: Prints out all possible positions of queens on a 4x4 chess board,
so no two queens are in the same row or column.
3: assumption: iQ and jQ represent the row and column of queen Q
respectively, for Q 1 . . . 4.
4: for (i1, j1 1 . . . 4) do
5: for (i2, j2 1 . . . 4) do
6: for (i3, j3 1 . . . 4) do
7: for (i4, j4 1 . . . 4) do
8: if (i1 = i2 = i3 = i4) AND (j1 = j2 = j3 = j4) then
9: print (i1, j1), (i2, j2), (i3, j3), (i4, j4)
10: end if
11: end for
12: end for
13: end for
14: end for
Alternatively, you could use a list of four numbers, [0, 1, 2, 3], to represent
the board, so that each index (0 to 3) represents a column number, and each
value stored in the list (0 to 3) represents a row number. So the list [3,
1, 0, 2], for example, would represent a board with queens at coordinates
(0,3), (1,1), (2,0), (3,2). To generate all possible valid boards, then, we
can simply generate all possible permutations of the list (with the Johnson
Trotter algorithm see Task 4). Since the values stored in the list are
distinct (and each value is stored at a distinct index), no two queens will
ever be in the same row (or column).
The valid permutations are:
(0, 1, 2, 3)
(0, 1, 3, 2)
(0, 2, 1, 3)
(0, 2, 3, 1)
(0, 3, 1, 2)
(0, 3, 2, 1)
(1, 0, 2, 3)
(1, 0, 3, 2)
(1, 2, 0, 3)
(1, 2, 3, 0)
(1, 3, 0, 2)
(1, 3, 2, 0)
4
(2, 0, 1, 3)
(2, 0, 3, 1)
(2, 1, 0, 3)
(2, 1, 3, 0)
(2, 3, 0, 1)
(2, 3, 1, 0)
(3, 0, 1, 2)
(3, 0, 2, 1)
(3, 1, 0, 2)
(3, 1, 2, 0)
(3, 2, 0, 1)
(3, 2, 1, 0)
Once you have all permutations, note that representations with consec-
utive numbers next to each other correspond to queens atacking each other
(on the diagonal).
So the only possibilities are: (1, 3, 0, 2) and (2, 0, 3, 1).
Task 4
Part 1. The following is printed out as specied in the algorithm
the rst print statement, print: [

3 ,

1 ,

2 ]
2 is selected as the largest mobile integer and is swapped with 1 and
the directions on 2 and 3 are reversed, print: [

3 ,

2 ,

1 ]
3 is selected as the largest mobile integer and is swapped with 2 and
the directions on 3 is reversed, print: [

2 ,

3 ,

1 ]
3 is selected as the largest mobile integer and is swapped with 2 and
the directions on 3 is reversed, print: [

3 ,

2 ,

1 ]
3 is selected as the largest mobile integer and is swapped with 2 and
the directions on 3 is reversed, print: [

2 ,

3 ,

1 ]
3 is selected as the largest mobile integer and is swapped with 2 and
the directions on 3 is reversed, print: [

3 ,

2 ,

1 ]
and this goes on and on, . . .forever. . .
5
The error in the given algorithm is in the line Reverse the direction on
all integers larger than L[index]. To x it, the rst line of instruction inside
the while loop should be Temp L[index], which is then followed by the
instructions Swap Temp with its immediate neighbour it is pointing at
and Reverse the direction on all integers larger than Temp.
Part 2. To complete the step, Reverse the direction on all integers larger
than Temp, where Temp contains the integer just swapped with its neigh-
bour, you would need to carry out the following steps:
1: i 0
2: while i < length(L) do
3: if (L[i] > Temp) then
4: ReverseDirection(L[i])
5: end if
6: i i + 1
7: end while
The implementation of the module ReverseDirection would depend on
how the direction of each integer in L is represented. Two possibilities:
each item in the list L might be an ordered pair representing (integer,
direction)
the direction of each integer might be stored in a separate bitstring,
where 0 and 1 represent left and right respectively
Can you think of other possibilities? How would you write an algorithm
for ReverseDirection in each case?
Task 5
See Algorithm 6. In this problem, we use the generate subsets algorithm
from before (refer to Algorithm 4) to generate all the possible subsets of the
problem. We then check whether the subset is a solution to the problem, ie.
the total weight of that subset is less than or equal to 17kg and print the
subset and its value if it is valid.
6
Algorithm 6 Knapsack(weights[0..n-1], values[0..n-1], capacity)
1: input: List of weights, weights[0..n-1], of the items. List of values[0..n-1]
of the items.
2: output: Print out possible sets of items which are valid solutions
3: assumptions: Capacity of this knapsack problem is 17kg
4: assumptions: totalWeight(list) returns the sum of the weights of the
items in list
5: assumptions: totalV alue(list) returns the sum of the values of the
items in list
6: allSubsets GenerateSubsets(n) (items) // refer to Alg 4 for details
7: m length(allSubsets)
8: i 0
9: while i < m do
10: if (totalWeight(allSubsets[i], weights[0..n 1]) < 17) then
11: print allSubsets[i]
12: print value: totalV alue(allSubsets[i], values[0..n 1])
13: end if
14: i i + 1
15: end while
At this point, you can also try to write down Modules totalWeight(list)
and totalV alue(list) for your own benet.
Task 6
The rst step is to generate a list of bitstrings generating all subsets of S.
This can be done using the algorithm from Task 2.
The second step is to nd a valid ordering of the list. This can be done
by generating all permutations of the list, and selecting one that meets the
constraints.
Algorithm 8 performs these tasks, calling Algorithm 7 as a module to
check the validity of each permutation.
7
Algorithm 7 CheckValidSequence(L[0..n-1])
1: input: A list L of n bitstrings.
2: output: Returns true if each bitstring in L diers from its immediate
successor by one single bit. Returns false otherwise.
3: assumption: -
4: for (i = 1..n-1) do
5: di 0
6: for (j = 0..length(L[i])-1) do
7: if (L[i][j] = L[i-1][j]) then
8: di di+1 // L[i][j] is the jth bit of the bitstring at L[i]
9: end if
10: end for
11: if (di = 1) then
12: return false // di represents the number of diering bits in neighbouring
13: end if // bitstrings L[i], L[i-1], which should be 1 for valid sequences.
14: end for
15: return true // This point will only be reached if every consecutive pair of bitstrings is valid.
Algorithm 8 FindValidSubsetSequence(S[0..n-1])
1: input: A set S of n elements.
2: output: Prints out all subsets of S, represented as bitstrings, ordered
in such a way that each string diers from its immediate successor by
one single bit.
3: assumption: Modules exist for GenerateSubsets (see Task 2), Gener-
atePermutations (see Task 4), and CheckValidSequence.
4: subsets GenerateSubsets(S)
5: subsetOrderings GeneratePermutations(subsets)
6: for (i = 0..length(subsetOrderings)-1) do
7: if (CheckValidSequence(subsetOrderings[i])) then
8: return subsetOrderings[i]
9: end if
10: end for
8

Anda mungkin juga menyukai