The document discusses using symmetry to solve problems more efficiently. It provides examples of how mathematician Gauss used symmetry to sum the numbers from 1 to 100 with only two operations. It also shows how modeling a problem involving transporting a shepherd, sheep, wolf and cabbage across a river can be simplified by recognizing the symmetrical solutions. The document advocates practicing noticing symmetry in problems to find better solutions and reduce the number of states needing to be considered.
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
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
25. So these two options are no good!
|HSCW
HC|SW HS | CW HW | SC
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. 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. Can you finish this diagram?
0|HSCW
HC|SW
S | HCW
HW | SCHS | CW
HSC | W HSW | C
29. Come on, try it yourself!
0|HSCW
HC|SW
S | HCW
HW | SCHS | CW
HSC | W HSW | C
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. 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. 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. 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. Lets go back to the original problem:
Wolf eats sheep, sheep eats cabbage
35. If we were to model this problem
We might come up with something this
36. But is this necessary?
All that matters is sheep can’t be with wolfs and cabbages
39. We have a shepherd, a sheep,
Plus two things that we shouldn’t combine with sheep
40. Lets call them anti-sheep (A)
It we construct the diagram now, it is way more simple
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. 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. So next time when you are modeling something, be aware of antisheep
Don’t overmodel
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. So far we’ve see how symmetry can help in solving problems
Of course seeing this symmetry is not always easy
46. But if you practice a little, you start seeing it everywhere
Let’s try another problem!
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. How to decide on the maximum value we can bring?
Weight Value
1 2
2 1
1 3
3 4
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. 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. 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. Like before, we want to find a smaller problem
That we can solve similar to the ‘big’ problem
69. If we put one item is, what we in fact have left is…
70. If we put one item is, what we in fact have left is…
A smaller backpack!
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. But we are cheating a little bit,
Since we don’t know beforehand which items to take
73. Therefore, we need a table listing all values for smaller backpack
From those values, we can calculate the end result
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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
112. The power of
symmetry
And remember:
Try to find symmetry everywhere
2n is never fun
And beware of antisheep
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. 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