Sei sulla pagina 1di 2

Tutorial 3

FIT 1029 Algorithmic Problem Solving


12 March 2014 Prepared by Julian Garca
The objectives of this tutorial are:
To solve problems by identifying invariants.
To be able to identify invariants in algorithms.
To understand Greedy approach for solving problems and its limitations.
To be able to nd Minimum Spanning Trees.
To understand the Knapsack problem.
Task 1
Find the sum of the even numbers between 1 and 999.
Find the sum of all the digits in numbers between 1 and 999, inclusively.
Task 2
Several coins are placed in a line on a table. Some of the coins are heads up, and some are heads down.
You are required to turn all the coins heads up. However, you must turn two coins at a time. From which
initial states is it possible to turn all the coins heads up?
Task 3
Consider an urn that is lled with black and white balls. At each step take two balls out until there is only
one ball. If both balls have the same colour, throw them away and put another black ball into the urn. If
they have different colours, throw the black ball away and put the white ball back into the urn. What can
be said about the colour of the nal ball in relation to the original number of black and white balls?
Task 4
Find three different values for coins such that:
Any amount can be expressed in coins of those values
There is an amount for which the Greedy approach does not nd the fewest number of coins that can
make up that amount.
Task 5
Identify the loop invariants
1
in the following pseudo code.
1
A loop invariant is some condition
that holds for every iteration of the
loop.
j 9
for i 0 to 9 do
j j 1
tutorial 3. fit 1029 algorithmic problem solving 2
Task 6
Another algorithm for nding a minimum spanning tree is known as the Kruskals Algorithm. It assumes
that the weights on each edge are positive. The idea behind this algorithm is at each stage the algorithm
constructs a minimum spanning tree by choosing an edge with the least weight that doesnt create a cycle
with the edges in the tree, and adding it to the tree.
Consider the following graph:
5
2 3
4 5
4
7 6
Figure 1: A graph.
Using Kruskals algorithm nd the minimum spanning tree for the graph in Figure 1.
Using Prims algorithm nd a minimum spanning tree for the graph in Figure 1.
Task 7
Consider the following type of items:
Type 1 2 3 4 5
Value $4 $13 $11 $5 $10
Weight 3kg 4kg 7kg 8kg 9kg
Suppose you have a knapsack of capacity 17kg. Determine what items you would take if you followed the
following strategies
2
:
2
This is known as the knapsack prob-
lem.
a At each step choose the item with the least weight that can t.
b At each step choose the item with the greatest value that can t.
c At each step nd the item with the greatest value per unit weight that can t.
Task 8
Consider the Knapsack problem where every item has the same
value. Describe an algorithm to solve the Knapsack problem in this
situation.

Potrebbero piacerti anche