67
FIT 1029 Algorithmic Problem Solving Lecture 5 Invariants Prepared by Julian García COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

Invariants

  • Upload
    stacy

  • View
    227

  • Download
    0

Embed Size (px)

DESCRIPTION

algo

Citation preview

  • FIT 1029 Algorithmic Problem Solving

    Lecture 5 Invariants

    Prepared by Julian Garca

    COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969

    WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further

    reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.

  • Smaller examples

  • ! Consider two glasses. One of milk andanother with the same amount of water.

    ! Take a spoonful of milk and mix it into the glass of water.

    ! Now, take a spoonful of the mixture of milk and water and put it back into the glass of milk.

    milk and water problem

  • milk and water problem

  • ! Consider two glasses. One of milk andanother with the same amount of water.

    ! Take a spoonful of milk and mix it into the glass of water.

    ! Now, take a spoonful of the mixture of milk and water and put it back into the glass of milk.

    milk and water problem

  • Some things never change...

  • source: http://www.panelsonpages.com/

  • InvariantAn invariant is a property that does not

    change.

  • Invariants.

    Total amount of water = Total amount of milk

    Amount taken out (1 spoonful)

    = Amount put back in (1 spoonful)

    =Amount in glass A Amount in glass B

    (in the water and milk problem)

  • milk and water must have been equally exchanged.

    Each glass ends at the same level it started.

    Liquid is conserved

  • Selection SortInput: A list L[0, n-1] of real numbersOutput: A list sorted in ascending order

    Sorts a list using selection sort

    k n-1

    k =0?

    j index of max of L[0, k]

    swap L[j] and L[k]

    k k-1

    Output L

  • 2 1 5 23 5

    2 1 23 55

    2 12 3 55

    1 2 3 552

    1 2 3 552

    2 3 5521

    2 5 1 5 2 3

    k=5

    k=4

    k=3

    k=2

    k=1

    k=0

  • 2 1 5 23 5

    2 1 23 55

    2 12 3 55

    1 2 3 552

    1 2 3 552

    2 3 5521

    2 5 1 5 2 3k=5

    k=4

    k=3

    k=2

    k=1

    k=0

    Any invariants?

    L[k+1, n-1]

    0 1 2 3 4 5

    0 1 2 3 4 5

    0 1 2 3 4 5

    0 1 2 3 4 5

    0 1 2 3 4 5

    0 1 2 3 4 5

    0 1 2 3 4 5

  • Prims algorithmInput: A graph G, simple and connectedOutput: A spanning tree of G

    Finds a spanning tree of a graph G

    Put the vertex in the tree

    is there a vertex not in the tree?

    Output tree

    Choose a vertex in the Graph

    Choose and edge between the tree

    and a vertex not in the tree.

    add the edge to the tree

    yes

    no

  • Put the vertex in the tree

    is there a vertex not in the tree?

    Output tree

    Choose a vertex in the Graph

    Choose and edge between the tree

    and a vertex not in the tree.

    add the edge to the tree

    yes

    no1

    2 3 45

  • 32

    5

    1

    4

    Put the vertex in the tree

    is there a vertex not in the tree?

    Output tree

    Choose a vertex in the Graph

    Choose and edge between the tree

    and a vertex not in the tree.

    add the edge to the tree

    yes

    no

    Any invariants? always have a tree

  • Train yourself to look for invariants

  • Cutting the Chocolate BlockA chocolate block is

    divided into squares by horizontal and vertical grooves. The object is to cut the chocolate block into individual squares.

    Assume each cut is made on a single piece along a groove. How many cuts are needed?

    2006-2009 Chocablog.com (http://www.chocablog.com/reviews/ms-organic-fairtrade-milk-chocolate-with-rose/)

  • How many pieces?

  • How many pieces?

    1 cut 2 pieces

  • How many pieces?

    2 cuts 3 pieces

  • How many pieces?

    3 cuts 4 pieces

  • How many pieces?

    4 cuts 5 pieces

  • How many pieces?

    5 cuts 6 pieces

  • How many pieces?

  • How many pieces?

    1 cut 2 pieces

  • How many pieces?

    2 cuts 3 pieces

  • How many pieces?

    3 cuts 4 pieces

  • How many pieces?

    4 cuts 5 pieces

  • How many pieces?

    5 cuts 6 pieces

    Any invariants?

  • number of pieces = n +1number of cuts = n

  • Summing Sequences

    Sum the numbers 1 to 100.

    S = 1 + 2 + 3 + 4 + ... + 97 + 98 + 99 + 100

    try it out

  • S = 1 + 2 + 3 + 4 + ... + 97 + 98 + 99 + 100

    One way to do it...

    Any invariants?

  • S = 1 + 2 + 3 + 4 + ... + 97 + 98 + 99 + 100

    Another way to do it...

    Any invariants?

    101101101

    101(50) = 5050101

    50101

  • S = 1 + 2 + 3 + 4 + ... + 97 + 98 + 99 + 100S =100 + 99 + 98 + 97+ ...+ 4 + 3 + 2 + 1+

    2S = 101*100S = 101*50S = 5050

  • nXi=1

    i =n(n+ 1)

    2

  • Mutilated Grid

  • Lets go back a little bit...

  • 32 W32 B

  • 31 W31 B

  • 30 W30 B

  • 29 W29 B

  • 28 W28 B

    Any invariants?

  • Mutilated Grid

  • Mutilated Chessboard

    30 W32 B

  • Cover Non-star Squares

  • Smaller problem?

  • One Dimensional Problem

  • One Dimensional Problem

  • Circle Problem

  • Circle Problem

  • Using the Circle

  • Using the Circle

  • Using the Circle

    Many different circles - so many different tilings..

  • Sam Loyd, Cyclopedia of Puzzles ,Lamb Publishing Company, 1914

  • Slocum and Sonneveld (2006)

  • 14-15 Puzzle

    Consider the puzzle of sliding blocks below. Any block adjacent (left, right, above or below) the blank spot can be moved into the position of the blank spot. No diagonal moves are allowed. Can you find a series of moves so that all the blocks are in the order given, except that the 14 and 15 squares have exchanged places?

    Image at: www.showmedistributors.com/items.php?id=games

  • Example Moves

    1 2 3 45 6 7 89 10 11 1213 14 15

    1 2 3 45 6 7 89 10 11 1213 14 15

    1 2 3 45 6 7 89 10 12

    13 14 1511

    1 2 3 45 6 7 89 1213 14 1511

    10

    1 2 3 45 6 7 89 12

    1314

    151110

    1 2 3 45 6 7 89 1213

    141511

    10

  • Swaps and

    Moves 1 2 3 45 6 7 89 10 11 1213 14 15 16

    1 2 3 45 6 7 89 10 11 1213 14 1516

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    16

    15

    1 move on the board=

    1 permutation swap

  • 1 2 3 45 6 7 89 10 11 1213 14 1516

    1 2 3 45 6 7 89 10 1213 14 1511

    16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    16

    12

    13

    14

    11

    15

    1 2 3 45 6 7 89 10 11 1213 14 15 16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    16

    15

  • 1 2 3 45 6 7 89 10 11 1213 14 1516

    1 2 3 45 6 7 89 10 1213 14 1511

    16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    16

    15

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    16

    12

    13

    14

    11

    15

    1 2 3 45 6 7 89 10 11 1213 14 15 16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    2 moves on the board=

    2 permutation swaps

  • Moves and Swaps

    ! For an even number of moves there is an even number of swaps.

    ! For an odd number of moves there is an odd number of swaps.

  • Moves and Swaps

    ! For an even number of moves there is an even number of swaps.

    ! For an odd number of moves there is an odd number of swaps.

    1 2 3 45 6 7 89 10 11 1213 15 14

    ! For the blank square to return to the starting position requires an even number of moves.

  • 1 2 3 45 6 7 89 10 11 12

    13 15 14

    1 2 3 45 6 7 89 10 11 1213 14 15

    ! odd number of swaps. ! even number of moves

  • Some things never change...What have we learnt?

    which is very useful.

  • Before Next Lecture

    Attempt the following problem

  • A Torch Problem

    Four people wish to cross a bridge at night. They need a torch to cross and they only have one torch between them. Also, since the bridge is narrow, they can only

    cross at most two at a time, and can only travel at the speed of the slowest. The four people can cross in 1

    minute, 2 minutes, 5 minutes and 10 minutes, respectively. Find a way for them to cross in the

    minimum about of time.

    Ando Hiroshige (http:www.hiroshige.org.uk)