3
Tutorial 3 FIT 1029 – Algorithmic Problem Solving 16 March 2015 – Prepared by Julian García 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

  • Upload
    stacy

  • View
    213

  • Download
    0

Embed Size (px)

DESCRIPTION

fit

Citation preview

  • Tutorial 3FIT 1029 Algorithmic Problem Solving16 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 whichinitial 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 onlyone ball. If both balls have the same colour, throw them away and put another black ball into the urn. Ifthey have different colours, throw the black ball away and put the white ball back into the urn. What canbe 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 findthe fewest number of coins that can make up that amount.

  • tutorial 3. fit 1029 algorithmic problem solving 2

    Task 5

    Identify the loop invariants1 in the following pseudo code. 1 A loop invariant is some conditionthat holds for every iteration of theloop.

    Task 6

    5

    2 3

    4 5

    4

    7 6

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

    Task 7

    Consider the following items:

    Type 1 2 3 4 5Value $4 $13 $11 $5 $10Weight 3kg 4kg 7kg 8kg 9kg

    Suppose you have a knapsack of capacity 17kg. Determine what items you would take if you followed thefollowing 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 3

    Task 8

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

    Task 9

    Another algorithm for finding a minimum spanning tree is known as the Kruskals Algorithm. It assumesthat the weights on each edge are positive. The idea behind this algorithm is at each stage the algorithmconstructs a minimum spanning tree by choosing an edge with the least weight that doesnt create a cyclewith the edges in the tree, and adding it to the tree. Using Kruskals algorithm find the minimum span-ning tree for the graph of Task 6.

    Task 1Task 2Task 3Task 4Task 5Task 6Task 7Task 8*Task 9*