Vous êtes sur la page 1sur 3

Tutorial 3

FIT 1029 Algorithmic Problem Solving


16 March 2015 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 the Greedy approach and its limitations.
To be able to find 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 filled 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 final ball in relation to the original number of black and white balls?

Task 4
Find three different denominations for coins such that:
Any amount of money can be expressed in with combinations of those coins
There is an amount of money for which the Greedy approach, using given denominations, does not find
the fewest number of coins that can make up that amount.

tutorial 3. fit 1029 algorithmic problem solving

Task 5
Identify the loop invariants1 in the following pseudo code.

A loop invariant is some condition


that holds for every iteration of the
loop.
1

Task 6

6
2

Using Prims algorithm find the minimum spanning tree for the graph above.

Task 7
Consider the following items:
Type
Value
Weight

1
$4
3kg

2
$13
4kg

3
$11
7kg

4
$5
8kg

5
$10
9kg

Suppose you have a knapsack of capacity 17kg. Determine what items you would take if you followed the
following strategies:
a At each step choose the item with the least weight that can fit.
b At each step choose the item with the greatest value that can fit.
c At each step find the item with the greatest value per unit weight that can fit.
This is known as the knapsack problem.

tutorial 3. fit 1029 algorithmic problem solving

Task 8
Consider a Knapsack problem in which all item have the same value. Describe an algorithm to solve the
Knapsack problem in this situation.

Task 9
Another algorithm for finding 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. Using Kruskals algorithm find the minimum spanning tree for the graph of Task 6.

Vous aimerez peut-être aussi