Your SlideShare is downloading. ×
0
The power of
symmetry
People love symmetry
They find symmetrical faces more attractive
Mathematicians are just like people :)
They too love symmetry
This is Johann Carl Friedrich Gauss (1777-1855)
He used symmetry to sum the numbers from 1 to 100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44...
You could do this…
Is this a smart solution?
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+
17+18+19+20+21+22+23+24+25+26+27+28+2...
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+
17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3
6+37+38+39+40+41+42+43+...
Remember Gauss?
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+
17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3
6+37+38...
Remember Gauss?
That’s me!
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+
17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+3...
Remember Gauss?
He could do it with only 2 operations, using symmetry
That’s me!
1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+
1...
1+2+3+4+5+ … +95+96+97+98+99+100
Let’s position the numbers a bit differently
Now we can ‘see’ the symmetry
1+2+3+4+5+ … +95+96+97+98+99+100
It’s like there’s a mirror in the middle!
This way we can make 50 groups op two
1+100 = 1...
1+2+3+4+5+ … +95+96+97+98+99+100
50 groups of 101 -> 50 * (100 + 1) = 5050
With only 2 operations!
1+100 = 101
2+99 = 101
...
Start loving symmetry already?
Start loving symmetry already?
I do!
Let’s have a look at the next problem
Concerning a shepherd, a sheep, a wolf and a cabbage
And a river, which they have to cross
And a boat. But, the boat can only carry the shepherd plus one item
So he takes either the sheep with him, or the wolf or the cabbage
Ow, and one more thing
When left alone (without the shepherd present)
The wolf will eat the sheep, and the sheep will eat the cabbage
Can we get the entire party safely to the other side?
Lets make a diagram representing the shepherds choices
Initially, all are at the right side of the river
|HSCW
So let’s place the whole party right of the |
The shepherd (his name is Hank), the Sheep, the Wolf and the Cabbage
|HSCW
Now what can Hank do?
He can take either one of the three items with him
|HSCW
HC|SW HS | CW HW | SC
However, remember
Wolf eats sheep, sheep eats cabbage
|HSCW
HC|SW HS | CW HW | SC
So these two options are no good!
|HSCW
HC|SW HS | CW HW | SC
Now Hank is at the left side of the river, he only has one choice
Going back alone (Taking the sheep gets him back where h...
So what’s next?
He can take either the wolf or the cabbage
|HSCW
HC|SW
S | HCW
HW | SCHS | CW
HSC | W HSW | C
Can you finish this diagram?
0|HSCW
HC|SW
S | HCW
HW | SCHS | CW
HSC | W HSW | C
Come on, try it yourself!
0|HSCW
HC|SW
S | HCW
HW | SCHS | CW
HSC | W HSW | C
Okay, here is it 
|HSCW
HC|SW
S|HCW
HW|SCHS|CW
HSC|W HSW|C
SC|HW C|HSW W|HSC SW|HC
HSC|W HWC|S
WC|SH
WCHS|
HSW|CHWC|S
WC|...
See how symmetrical it is?
|HSCW
HC|SW
S|HCW
HW|SCHS|CW
HSC|W HSW|C
SC|HW C|HSW W|HSC SW|HC
HSC|W HWC|S
WC|SH
WCHS|
HSW|CH...
It doesn’t matter what we choose at the yellow point
Which means: it doesn’t matter whether we take the wolf or the cabbag...
We see this symmetry now in the diagram
Could we have notice this before?
|HSCW
HC|SW
S|HCW
HW|SCHS|CW
HSC|W HSW|C
SC|HW C...
Lets go back to the original problem:
Wolf eats sheep, sheep eats cabbage
If we were to model this problem
We might come up with something this
But is this necessary?
All that matters is sheep can’t be with wolfs and cabbages
Who eats who…
Is irrelevant!
So let’s try modeling the problem again!
We have a shepherd, a sheep,
Plus two things that we shouldn’t combine with sheep
Lets call them anti-sheep (A)
It we construct the diagram now, it is way more simple
See how little choice there is?
We reduced the state space (# states) by half
|HSAA
HA|SA
S|HAA
HS|AA
HAS|A
SA|HA A|HSA
HA...
See how little choice there is?
We reduced the state space (# states) by half
|HSAA
HA|SA
S|HAA
HS|AA
HAS|A
SA|HA A|HSA
HA...
So next time when you are modeling something, be aware of antisheep
Don’t overmodel
The symmetric solution was invented by this guy
Edsger W. Dijkstra, a Dutch computer scientist
Edsger W. Dijkstra – Prunin...
So far we’ve see how symmetry can help in solving problems
Of course seeing this symmetry is not always easy
But if you practice a little, you start seeing it everywhere
Let’s try another problem!
Finding the greatest common divisor (gcd) of two numbers
Given two numbers A en B
Determine the maximum number x, that divides both A en B
6
12
For instance 6 and 12
2, 3 and 6 all divide both numbers, but 6 is the largest
6
12
But how do we calculate the gcd of larger numbers?
We could try all numbers up to 105
105
252
Can we do better?
Using symmetry?
105
252
So let’s search for a problem that is smaller
But can be solved in the same way
105
252
Rephrasing our problem definition
We search for an x that ‘fits’ into both A and B
105 = x * z
252 = x * y
But we don’t really care about how often x fit in A and B
Remember: be aware of antisheep!
105 = x * z
252 = x * y
So how can we reduce this problem?
While keeping it similar?
105 = x * z
252 = x * y
Well, we could subtract
Now we again have two numbers in which x fits nicely, but smaller!
105 = x * z -
252 = x * y
147 =...
Now we can continue with 105 and 147
And again have two numbers in which x fits nicely, but smaller!
105 = x * z -
147 = x...
You can check yourself that within 5 steps we reach the answer
That’s way better that 105 steps. Thanks to…
105 = 5 * 21
2...
Symmetry!
Wanna try it with a new problem?
Suppose you’re going on a backpacking trip
And you need to decide what items to bring with you
For example: Food, a towel, a first aid kit and entertainment
Each item has a certain weight and a value
But we cannot take all of them, we can only carry three kilos
Weight Value
1 2
...
How to decide on the maximum value we can bring?
Weight Value
1 2
2 1
1 3
3 4
Like before, we could make a decision tree
For each item, we can either take it (left arrow) or leave it (right arrow)
Sta...
Is that a smart move?
Well for four items it is okay, we have to create 16 path (2 ^ 4)
Start
W:1 V:2 W:0 V:0
W:3 V:3 W:1 ...
However: 2^n is not a sweet formula!!
For 10 items, we already have more than 1000 (1024) paths
Weight Value
1 2
2 1
1 3
3...
Like before, we want to find a smaller problem
That we can solve similar to the ‘big’ problem
Suppose this rectangle is an empty backpack
If we put one item is, what we in fact have left is…
If we put one item is, what we in fact have left is…
A smaller backpack!
With this idea we are going to solve the problem:
We put an item in the backpack, and continue with a smaller one
But we are cheating a little bit,
Since we don’t know beforehand which items to take
Therefore, we need a table listing all values for smaller backpack
From those values, we can calculate the end result
We calculate the maximum value to be obtained
For each weight (horizontal) choosing from a certain list of items (vertical...
This way, the final solution can be found in the lower right corner
Since there we can choose from all items, carrying wei...
So how to fill this nice table?
Some values are very easy to figure out. Do you know some?
W V
1 2
2 1
1 3
3 4
Max. value ...
Well, if you cannot carry any weight…
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3
I...
Well, if you cannot carry any weight…
You will not carry anything of value anyway
W V
1 2
2 1
1 3
3 4
Max. value Max weigh...
And if you cannot choose from any item…
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3...
And if you cannot choose from any item…
Nothing is to be gained either
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max wei...
What else is easy?
Lets try the second row…
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max weight:1 Max weight:2 Max weig...
Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3
Items up to 0
0 0 0 0
Items up to 1
0
Items up to 2
0
Items...
Now for the remainder of the second row
In the yellow box we have: items up to 2, max weight 1
W V
1 2
2 1
1 3
3 4
Max. va...
What can we do? We can’t take the towel, since it weights 2
So there’s no option, but just sticking with the burger (value...
Let’s do one more!
Now we can choose from the first three items
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max weight:1 M...
So now we could also take the mushroom.
It has weight 1, so at least it fits
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 M...
But is this better?
Well… What we have now is value 2, and the mushroom is worth 3
W V
1 2
2 1
1 3
3 4
Max. value Max weig...
But is this better?
Well… What we have now is value 2, and the mushroom is worth 3
W V
1 2
2 1
1 3
3 4
Max. value Max weig...
So let’s ditch the burger, and go for the mushrooms
Than our maximum value is 3, and that is obviously better than 2.
W V
...
This way, we can fill the entire table
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3
...
To calculate the values, we secretly used a recurrence relation
This means describing a value in the table with other valu...
Let’s call the table T. T has two dimensions, i (items) and w(weight)
Here we highlight T[2,1], indicating items up to 2, ...
How can we calculate this value T[2,1]?
In stead of reasoning about it as we did before?
W V
1 2
2 1
1 3
3 4
Max. value Ma...
Well what did we do before?
We said: Since the towel does not fit, we stick with the burger
W V
1 2
2 1
1 3
3 4
Max. value...
if wi < w (the new item is too heavy)
then T[i,w] = T[i-1,w] (we stick with what we already have)
W V
1 2
2 1
1 3
3 4
Max....
Now, let’s have a look at what we did at T[3,1]
We said: It fits, and now we have value 2, and the mushrooms is worth 3
W ...
if wi <= w (this new item fits)
then T[i,w] = Max (T[i-1,w], T[i-1-w-wi]+vi) (we check if we can do better)
W V
1 2
2 1
1 ...
To perform this calculation, we only need n2 operations
In stead of 2n Okay, in this case both are 16, but you get the ide...
Again, we reduced complexity by seeing the symmetry!
W V
1 2
2 1
1 3
3 4
Max. value Max weight:0 Max weight:1 Max weight:2...
So let’s do one little more problem
Calculating all prime numbers
Did you know they were named after this guy?
Okay they weren’t, but he does makes a nice background
Prime numbers are numbers only divisible by 1 and itself
2 3 4 5 6 ...
So how do we know what’s a prime number and what’s not?
We could check all divisors, per number.
2 3 4 5 6 7 8 9 10 11 12 ...
But that could take up to the root of each number
Lets use symmetry, and find a smaller problem like this one.
2 3 4 5 6 7...
Suppose we already have a number that’s prime.
Say 2. What do we know now?
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2...
A hint: It is something about every multiple of two…
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27...
Exactly, they are not primes!
Since they have 2 as a divisor
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 ...
We have eliminated half of the numbers
(Okay we have infinitely many left, but still…)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 1...
We can do the same trick with 3
Identify it as prime, and remove all numbers divisible by it
2 3 4 5 6 7 8 9 10 11 12 13 1...
This way we can quickly determine all prime numbers
Thanks to…
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 2...
The power of
symmetry
Thanks for your attention!
The power of
symmetry
And remember:
Try to find symmetry everywhere
2n is never fun
And beware of antisheep
The power of
symmetry
This presentation was created by Felienne Hermans,
PhD student and entrepreneur
And presented at the...
If you liked this presentation, you might also like these books
The author of the first two is nice to follow on Twitter: ...
Upcoming SlideShare
Loading in...5
×

The power of symmetry

12,137

Published on

This presentation by @Felienne presents programming problems, and how they can be solved efficiently and elegantly using symmetry.

Published in: Technology, Business
2 Comments
30 Likes
Statistics
Notes
No Downloads
Views
Total Views
12,137
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
498
Comments
2
Likes
30
Embeds 0
No embeds

No notes for slide
  • [NatureItem||Abstract Bool Eats(n:NatureItem)]^[Vegetable||Abstract Bool Eats(n:NatureItem)], [NatureItem]^[Animal||Abstract Bool Eats(n:NatureItem)], [Vegetable]^[Cabbage||Bool Eats(n:NatureItem)],[Animal]^[Sheep||Bool Eats(n:NatureItem)], [Animal]^[Wolf||Bool Eats(n:NatureItem)]
  • Transcript of "The power of symmetry"

    1. 1. The power of symmetry
    2. 2. People love symmetry They find symmetrical faces more attractive
    3. 3. Mathematicians are just like people :) They too love symmetry
    4. 4. This is Johann Carl Friedrich Gauss (1777-1855) He used symmetry to sum the numbers from 1 to 100
    5. 5. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 Suppose you have to sum these guys How would you do that?
    6. 6. You could do this… Is this a smart solution? 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+ 17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3 6+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55 +56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+ 75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+9 4+95+96+97+98+99+100
    7. 7. 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+ 17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3 6+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55 +56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+ 75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+9 4+95+96+97+98+99+100 Well… it are 99 plusses, so 99 points of effort (and possibly mistake) Could we do this with less operations(plusses)?
    8. 8. Remember Gauss? 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+ 17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3 6+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55 +56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+ 75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+9 4+95+96+97+98+99+100
    9. 9. Remember Gauss? That’s me! 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+ 17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3 6+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55 +56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+ 75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+9 4+95+96+97+98+99+100
    10. 10. Remember Gauss? He could do it with only 2 operations, using symmetry That’s me! 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+ 17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+3 6+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55 +56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+ 75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+9 4+95+96+97+98+99+100
    11. 11. 1+2+3+4+5+ … +95+96+97+98+99+100 Let’s position the numbers a bit differently Now we can ‘see’ the symmetry
    12. 12. 1+2+3+4+5+ … +95+96+97+98+99+100 It’s like there’s a mirror in the middle! This way we can make 50 groups op two 1+100 = 101 2+99 = 101 3+98 = 101
    13. 13. 1+2+3+4+5+ … +95+96+97+98+99+100 50 groups of 101 -> 50 * (100 + 1) = 5050 With only 2 operations! 1+100 = 101 2+99 = 101 3+98 = 101
    14. 14. Start loving symmetry already?
    15. 15. Start loving symmetry already? I do!
    16. 16. Let’s have a look at the next problem Concerning a shepherd, a sheep, a wolf and a cabbage
    17. 17. And a river, which they have to cross And a boat. But, the boat can only carry the shepherd plus one item
    18. 18. So he takes either the sheep with him, or the wolf or the cabbage Ow, and one more thing
    19. 19. When left alone (without the shepherd present) The wolf will eat the sheep, and the sheep will eat the cabbage
    20. 20. Can we get the entire party safely to the other side?
    21. 21. Lets make a diagram representing the shepherds choices Initially, all are at the right side of the river |HSCW
    22. 22. So let’s place the whole party right of the | The shepherd (his name is Hank), the Sheep, the Wolf and the Cabbage |HSCW
    23. 23. Now what can Hank do? He can take either one of the three items with him |HSCW HC|SW HS | CW HW | SC
    24. 24. However, remember Wolf eats sheep, sheep eats cabbage |HSCW HC|SW HS | CW HW | SC
    25. 25. So these two options are no good! |HSCW HC|SW HS | CW HW | SC
    26. 26. Now Hank is at the left side of the river, he only has one choice Going back alone (Taking the sheep gets him back where he started) |HSCW HC|SW S | HCW HW | SCHS | CW
    27. 27. So what’s next? He can take either the wolf or the cabbage |HSCW HC|SW S | HCW HW | SCHS | CW HSC | W HSW | C
    28. 28. Can you finish this diagram? 0|HSCW HC|SW S | HCW HW | SCHS | CW HSC | W HSW | C
    29. 29. Come on, try it yourself! 0|HSCW HC|SW S | HCW HW | SCHS | CW HSC | W HSW | C
    30. 30. Okay, here is it  |HSCW HC|SW S|HCW HW|SCHS|CW HSC|W HSW|C SC|HW C|HSW W|HSC SW|HC HSC|W HWC|S WC|SH WCHS| HSW|CHWC|S WC|SH WCHS|
    31. 31. See how symmetrical it is? |HSCW HC|SW S|HCW HW|SCHS|CW HSC|W HSW|C SC|HW C|HSW W|HSC SW|HC HSC|W HWC|S WC|SH WCHS| HSW|CHWC|S WC|SH WCHS|
    32. 32. It doesn’t matter what we choose at the yellow point Which means: it doesn’t matter whether we take the wolf or the cabbage |HSCW HC|SW S|HCW HW|SCHS|CW HSC|W HSW|C SC|HW C|HSW W|HSC SW|HC HSC|W HWC|S WC|SH WCHS| HSW|CHWC|S WC|SH WCHS|
    33. 33. We see this symmetry now in the diagram Could we have notice this before? |HSCW HC|SW S|HCW HW|SCHS|CW HSC|W HSW|C SC|HW C|HSW W|HSC SW|HC HSC|W HWC|S WC|SH WCHS| HSW|CHWC|S WC|SH WCHS|
    34. 34. Lets go back to the original problem: Wolf eats sheep, sheep eats cabbage
    35. 35. If we were to model this problem We might come up with something this
    36. 36. But is this necessary? All that matters is sheep can’t be with wolfs and cabbages
    37. 37. Who eats who… Is irrelevant!
    38. 38. So let’s try modeling the problem again!
    39. 39. We have a shepherd, a sheep, Plus two things that we shouldn’t combine with sheep
    40. 40. Lets call them anti-sheep (A) It we construct the diagram now, it is way more simple
    41. 41. See how little choice there is? We reduced the state space (# states) by half |HSAA HA|SA S|HAA HS|AA HAS|A SA|HA A|HSA HAS|A HAA|S AA|SH AAHS|
    42. 42. See how little choice there is? We reduced the state space (# states) by half |HSAA HA|SA S|HAA HS|AA HAS|A SA|HA A|HSA HAS|A HAA|S AA|SH AAHS| Symmetry for the win!
    43. 43. So next time when you are modeling something, be aware of antisheep Don’t overmodel
    44. 44. The symmetric solution was invented by this guy Edsger W. Dijkstra, a Dutch computer scientist Edsger W. Dijkstra – Pruning the search tree http://www.cs.utexas.edu/users/EWD/transcriptions/EWD12xx/EWD1255.html
    45. 45. So far we’ve see how symmetry can help in solving problems Of course seeing this symmetry is not always easy
    46. 46. But if you practice a little, you start seeing it everywhere Let’s try another problem!
    47. 47. Finding the greatest common divisor (gcd) of two numbers
    48. 48. Given two numbers A en B Determine the maximum number x, that divides both A en B 6 12
    49. 49. For instance 6 and 12 2, 3 and 6 all divide both numbers, but 6 is the largest 6 12
    50. 50. But how do we calculate the gcd of larger numbers? We could try all numbers up to 105 105 252
    51. 51. Can we do better? Using symmetry? 105 252
    52. 52. So let’s search for a problem that is smaller But can be solved in the same way 105 252
    53. 53. Rephrasing our problem definition We search for an x that ‘fits’ into both A and B 105 = x * z 252 = x * y
    54. 54. But we don’t really care about how often x fit in A and B Remember: be aware of antisheep! 105 = x * z 252 = x * y
    55. 55. So how can we reduce this problem? While keeping it similar? 105 = x * z 252 = x * y
    56. 56. Well, we could subtract Now we again have two numbers in which x fits nicely, but smaller! 105 = x * z - 252 = x * y 147 = x * (y-z)
    57. 57. Now we can continue with 105 and 147 And again have two numbers in which x fits nicely, but smaller! 105 = x * z - 147 = x * y 42 = x * (y-z)
    58. 58. You can check yourself that within 5 steps we reach the answer That’s way better that 105 steps. Thanks to… 105 = 5 * 21 252 = 21 * 21
    59. 59. Symmetry! Wanna try it with a new problem?
    60. 60. Suppose you’re going on a backpacking trip And you need to decide what items to bring with you
    61. 61. For example: Food, a towel, a first aid kit and entertainment
    62. 62. Each item has a certain weight and a value But we cannot take all of them, we can only carry three kilos Weight Value 1 2 2 1 1 3 3 4
    63. 63. How to decide on the maximum value we can bring? Weight Value 1 2 2 1 1 3 3 4
    64. 64. Like before, we could make a decision tree For each item, we can either take it (left arrow) or leave it (right arrow) Start W:1 V:2 W:0 V:0 W:3 V:3 W:1 V:2 W:2 V:1 W:0 V:0 Weight Value 1 2 2 1 1 3 3 4
    65. 65. Is that a smart move? Well for four items it is okay, we have to create 16 path (2 ^ 4) Start W:1 V:2 W:0 V:0 W:3 V:3 W:1 V:2 W:2 V:1 W:0 V:0 Weight Value 1 2 2 1 1 3 3 4
    66. 66. However: 2^n is not a sweet formula!! For 10 items, we already have more than 1000 (1024) paths Weight Value 1 2 2 1 1 3 3 4
    67. 67. Like before, we want to find a smaller problem That we can solve similar to the ‘big’ problem
    68. 68. Suppose this rectangle is an empty backpack
    69. 69. If we put one item is, what we in fact have left is…
    70. 70. If we put one item is, what we in fact have left is… A smaller backpack!
    71. 71. With this idea we are going to solve the problem: We put an item in the backpack, and continue with a smaller one
    72. 72. But we are cheating a little bit, Since we don’t know beforehand which items to take
    73. 73. Therefore, we need a table listing all values for smaller backpack From those values, we can calculate the end result
    74. 74. We calculate the maximum value to be obtained For each weight (horizontal) choosing from a certain list of items (vertical) W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 Items up to 1 Items up to 2 Items up to 3 Items up to 4
    75. 75. This way, the final solution can be found in the lower right corner Since there we can choose from all items, carrying weight 3 W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 Items up to 1 Items up to 2 Items up to 3 Items up to 4
    76. 76. So how to fill this nice table? Some values are very easy to figure out. Do you know some? W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 Items up to 1 Items up to 2 Items up to 3 Items up to 4
    77. 77. Well, if you cannot carry any weight… W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 Items up to 1 Items up to 2 Items up to 3 Items up to 4
    78. 78. Well, if you cannot carry any weight… You will not carry anything of value anyway W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 Items up to 1 0 Items up to 2 0 Items up to 3 0 Items up to 4 0
    79. 79. And if you cannot choose from any item… W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 Items up to 1 0 Items up to 2 0 Items up to 3 0 Items up to 4 0
    80. 80. And if you cannot choose from any item… Nothing is to be gained either W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 Items up to 2 0 Items up to 3 0 Items up to 4 0
    81. 81. What else is easy? Lets try the second row… W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 Items up to 2 0 Items up to 3 0 Items up to 4 0
    82. 82. Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 Items up to 2 0 Items up to 3 0 Items up to 4 0 With the burger only, we can never do better than value 2 Which is the value of the burger W V 1 2 2 1 1 3 3 4
    83. 83. Now for the remainder of the second row In the yellow box we have: items up to 2, max weight 1 W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 Items up to 3 0 Items up to 4 0
    84. 84. What can we do? We can’t take the towel, since it weights 2 So there’s no option, but just sticking with the burger (value 2) W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 Items up to 3 0 Items up to 4 0
    85. 85. Let’s do one more! Now we can choose from the first three items W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 Items up to 3 0 Items up to 4 0
    86. 86. So now we could also take the mushroom. It has weight 1, so at least it fits W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 Items up to 3 0 Items up to 4 0
    87. 87. But is this better? Well… What we have now is value 2, and the mushroom is worth 3 W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 Items up to 3 0 Items up to 4 0
    88. 88. But is this better? Well… What we have now is value 2, and the mushroom is worth 3 W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 Items up to 3 0 Items up to 4 0
    89. 89. So let’s ditch the burger, and go for the mushrooms Than our maximum value is 3, and that is obviously better than 2. W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 Items up to 3 0 3 Items up to 4 0
    90. 90. This way, we can fill the entire table W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    91. 91. To calculate the values, we secretly used a recurrence relation This means describing a value in the table with other values in the table W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    92. 92. Let’s call the table T. T has two dimensions, i (items) and w(weight) Here we highlight T[2,1], indicating items up to 2, maximum weight 1 W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    93. 93. How can we calculate this value T[2,1]? In stead of reasoning about it as we did before? W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    94. 94. Well what did we do before? We said: Since the towel does not fit, we stick with the burger W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    95. 95. if wi < w (the new item is too heavy) then T[i,w] = T[i-1,w] (we stick with what we already have) W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    96. 96. Now, let’s have a look at what we did at T[3,1] We said: It fits, and now we have value 2, and the mushrooms is worth 3 W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    97. 97. if wi <= w (this new item fits) then T[i,w] = Max (T[i-1,w], T[i-1-w-wi]+vi) (we check if we can do better) W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    98. 98. To perform this calculation, we only need n2 operations In stead of 2n Okay, in this case both are 16, but you get the idea :) W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    99. 99. Again, we reduced complexity by seeing the symmetry! W V 1 2 2 1 1 3 3 4 Max. value Max weight:0 Max weight:1 Max weight:2 Max weight:3 Items up to 0 0 0 0 0 Items up to 1 0 2 2 2 Items up to 2 0 2 2 3 Items up to 3 0 3 3 3 Items up to 4 0 3 3 5
    100. 100. So let’s do one little more problem
    101. 101. Calculating all prime numbers Did you know they were named after this guy?
    102. 102. Okay they weren’t, but he does makes a nice background Prime numbers are numbers only divisible by 1 and itself 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    103. 103. So how do we know what’s a prime number and what’s not? We could check all divisors, per number. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    104. 104. But that could take up to the root of each number Lets use symmetry, and find a smaller problem like this one. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    105. 105. Suppose we already have a number that’s prime. Say 2. What do we know now? 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    106. 106. A hint: It is something about every multiple of two… 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    107. 107. Exactly, they are not primes! Since they have 2 as a divisor 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    108. 108. We have eliminated half of the numbers (Okay we have infinitely many left, but still…) 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    109. 109. We can do the same trick with 3 Identify it as prime, and remove all numbers divisible by it 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    110. 110. This way we can quickly determine all prime numbers Thanks to… 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    111. 111. The power of symmetry Thanks for your attention!
    112. 112. The power of symmetry And remember: Try to find symmetry everywhere 2n is never fun And beware of antisheep
    113. 113. The power of symmetry This presentation was created by Felienne Hermans, PhD student and entrepreneur And presented at the yearly Devnology Community Day 6 november 2010 @Felienne
    114. 114. If you liked this presentation, you might also like these books The author of the first two is nice to follow on Twitter: @marcusdusautoy Books on symmetry (in increasing order of difficulty) General mathematical awesomeness More cool programming
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×