FIT1029: Tutorial 3 Solutions Semester 1, 2014: Task 1
FIT1029: Tutorial 3 Solutions Semester 1, 2014: Task 1
Semester 1, 2014
Task 1
Part 1. From lecture and tutorials we know that the sum of all numbers up
to N is given by the following formula:
Sum of all numbers from 1 to N = N(N + 1)/2
To extend this to the even numbers we note that:
Sum of even numbers from 1 to 999 = 2(Sum of all numbers from 1 to 499)
Therefore the answer = 2 (499(500)/2) = 249500
Part 2. The numbers from 000 to 999 can be written as:
000, 001, 002, ..., 010, 011, 012, ..., 100, 101, 102, ..., 999
There are 1000 numbers in total and the last digit can be either 0,1,2,3,4,5,6,7,8
or 9.
So, there are a 100 groups of numbers with the last digit consisting
of one of each digit. The sum of the last digits for each of these groups
is 0 + 1 + 2 + ... + 9 = 45. Therefore, the sum of all the last digits is
45 100 = 4500.
By the same logic the sum of all the rst digits is 4500 and the sum of
the second digits is 4500.
Therefore the sum of all digits between 1 and 999 (inclusive) = 4500 +
4500 + 4500 = 13500
1
Task 2
If we start with an even number of coins with heads downwards then we have
a feasible solution: i.e. by turning two coins at a time, we can turn all the
heads facing up. But if we start with an odd number of coins with heads
facing downwards, then there is no way of turning all the coins to be heads
up.
The invariant is the parity (oddness or evenness) of the number of heads
down (or heads up) this stays the same after each coin turn. Since our
desired nal state has an even number of heads down (zero), it follows that
our start state must also have an even number of heads down.
Task 3
There are two possibilities depending on the initial number of white balls:
(1) starting from an even number of whites results eventually in one black
ball and (2) starting from an odd number of whites results eventually in one
white ball.
This can be understood by noting the total number of balls (of any colour)
decreases by 1 after each draw, but that the number of white balls can only
decrease by 2 (or 0). So the parity of the number of white balls always stays
the same.
Task 4
One possible solution is to have coins with the values {1, 3, 4}. Since we
have a coin with value 1, we can express any amount in terms of the values
of these coins. Also, if we consider the amount 6, we see that a Greedy
approach would give us the following values {4, 1, 1}, while we could get the
same amount with fewer coins, namely {3, 3}.
Task 5
Assuming i is incremented (implicitly) at the end of each iteration of the
for loop, a simple loop invariant could be
i + j = 9
at the start of at each and every iteration of the loop.
2
Task 6
The minimum spanning tree consists of the edges with weights {2, 3, 4, 4}.
This is generated by both algorithms.
In the case of Kruskals algorithm, the edges will be added in order from
smallest to largest weight {2, 3, 4, 4}. The order in which the last two
(equally-weighted) edges are added is the only point at which the algorithm
may vary.
In the case of Prims algorithm, the order in which edges are added will
depend on which vertex the algorithm begins at. For example, starting at
the top left vertex will result in the order {4, 4, 2, 3} or {4, 2, 3, 4} (depending
on which of the equal-weighted edges are added rst). However, the same
set of edges will be found, no matter which vertex is used as the start point.
Task 7
The lists below represent the types of the items that would be picked in that
particular order according to the given strategies. Note that it is possible
that dierent strategies yield the same results, but is not always the case (to
be expected).
a) 1,2,3 (Value: $28)
b) 2,3,1 (Value: $28)
c) 2,3,1 (Value: $28)
3
Task 8
Algorithm 1 Knapsack(weights[0..n-1], capacity, value)
1: Finds the maximum value the knapsack can hold
2: input: A list weights[0..n-1] of weights of items and the capacity
3: output: Maximum value the knapsack can hold
4: Assumption: The weights are sorted in ascending order and all items
have the value, value.
5: totalWeight
6: i 0
7: while (i < n) AND (totalWeight < capacity) do
8: totalWeight totalWeight + weights[i]
9: i i + 1
10: end while
11: i i - 1
12: return i*value
4