Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email:...
-
Upload
domenic-loomis -
Category
Documents
-
view
215 -
download
0
Transcript of Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email:...
![Page 1: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/1.jpg)
Exhaustion, Branch and BoundGary Wong
If you have any question, please ask via
Email: [email protected]
MSN: [email protected]
![Page 2: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/2.jpg)
Welcome!
• Your 1st training on advanced topics this year• I assume you have sufficient basic knowledge
![Page 3: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/3.jpg)
Pre-requisites
• Recursion• Basic concepts of permutation and combination• Graph Searching (Optional)
![Page 4: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/4.jpg)
Exhaustion
• Check for (almost) all possible cases• Advantages– Simple– Correct in nearly all circumstances
• Provided that you implement correctly
• Disadvantages– Can be too slow if too many cases (e.g. exponential)– Implementation is sometimes difficult
![Page 5: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/5.jpg)
When do we use exhaustion?
• Not many possible cases• No other better methods are in your mind– Might not be your fault!– Many computational problems in the world have no
known polynomial-time solutions– P, NP, NP-hard, NP-completeness…
• Usually when the constraints are very small, you should think about whether exhaustion is possible
![Page 6: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/6.jpg)
Let me count the ways
• Now you have unlimited supply of coins of values 1, 5, 10, 25 and 50 cents
• Find the number of ways to make up n cents• 0 <= n <= 99
![Page 7: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/7.jpg)
Let me count the ways
• Have a rough (and conservative) estimation on number of possible cases
• If only 1 cent can be used, at most how many coins?• If only 5 cents can be used, at most how many coins?• …
• A very rough upper bound:– 100 x 20 x 10 x 4 x 2 = 1600000
• Surely acceptable within 1s
![Page 8: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/8.jpg)
Grid
• Given an N x N white grids (N <= 5)• You may toggle the grid colors like this (white -> black;
black -> white):
• Given the initial colors of the grids, find the minimum number of toggling required to make all grids to become black
![Page 9: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/9.jpg)
Grid
• Observations:– Toggle twice = Not toggle at all– Sequence of toggling does not affect the final appearance
of the board– How many possible sequences of toggling are there?
• Time Complexity: O(2NxN)
• A more efficient algorithm actually exists!• Hint: its time complexity is O(2N x N2)
![Page 10: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/10.jpg)
Magic triangle
• Given a magic triangle of N layers (1 <= N <= 5), comprising of N(N+1)/2 nodes
• Integers 1 to N(N+1)/2 are assigned into each node exactly once, such that:– The number in a node = absolute difference of the
numbers in its two children nodes
• M numbers (0 <= M <= N(N+1)/2) have filled into some nodes, fill in the remaining nodes
• Time limit: 1s
6
4
2
3
1
5
![Page 11: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/11.jpg)
Magic triangle
• Let’s fill in the N(N+1)/2 numbers into the circles!• O((N(N+1)/2 – M)!)• Worst case?– N = 5– M = 0, i.e. no numbers are filled– 15! = 1307674368000
![Page 12: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/12.jpg)
Magic triangle – 2nd attempt
• Observation:– Numbers in lower layers can be used to derive the
numbers above!– Why not just exhausting the lowest layer?
• Time complexity: O(TPN), where T = N(N+1)/2
• 15P5 = 15! / 10! = 360360
![Page 13: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/13.jpg)
What have you learnt?
• Smart exhaustion vs Stupid exhaustion• Now I would teach you what “smart” means…
![Page 14: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/14.jpg)
A Classical Problem – Bin Packing
• A number of objects are to be put into some identical bins
• Find the minimum number of bins required to pack all objects
10
20
30
50
20
Capacity: 50
![Page 15: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/15.jpg)
Solution 1
• S = Sum of volumes of all objects• C = Capacity of a container• Answer = ceil (S / V)
• Wrong!• Why?
![Page 16: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/16.jpg)
Solution 2
• Sort the objects in ascending order of their volumes• Put the smaller objects first
• Wrong!• Why?
![Page 17: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/17.jpg)
The correct solution
• Exhaustion
BIN PACKING()if All objects have been put in some bins
then update the best solution if a better one is foundpick a bin Bfor each object Odo if volume of O does not exceed remaining space in B
then Put O into BBIN PACKING()Remove O from B
![Page 18: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/18.jpg)
Search tree
• DFS• For N objects, O(N!)
Empty bins
B1(10) B1(50) B1(20)
B1(10,20)
B1(50); B2(10)
… … …
![Page 19: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/19.jpg)
The cruel reality
• N <= 40• Performance:– input1.txt: Accepted– input2.txt: Accepted– input3.txt: TLE
• How to speed up?
![Page 20: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/20.jpg)
Back to the search tree…
• Is it necessary to go further here? Why?
Empty bins
B1(10) B1(50) B1(20)
B1(10,20)
B1(50); B2(10)
… … …
Suppose you knowhere that at most2 bins are needed
![Page 21: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/21.jpg)
Optimization
• Tree Pruning (Branch and Bound!)• Suppose f is the solution at this moment• If f >= cur_best, do not go further
• Performance:– input1.txt: Accepted– input2.txt: Accepted– input3.txt: TLE
• Still too slow… • Can we do better?
![Page 22: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/22.jpg)
A* Search
• Let h be an estimated lower bound for the number of bins required to pack all unpacked objects
• If f + h >= best, do not go further
• h is also known as a heuristic function• But what makes a good function h in this case?
![Page 23: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/23.jpg)
The cruel reality, again
• Performance:– input1.txt: Accepted– input2.txt: Accepted– input3.txt: Accepted– input4.txt: TLE
• Further improved now• Can we do even better?
![Page 24: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/24.jpg)
Equivalent configurations
=
=
![Page 25: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/25.jpg)
Avoid equivalent configurations
• Sort the objects by their volumes before searching• When putting the objects, you should ensure that:– The volumes of objects in each bin are in non-decreasing
order, or– The volumes of smallest objects in all bins form a non-
increasing order
![Page 26: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/26.jpg)
A happy ending
• Performance:– input1.txt: Accepted– input2.txt: Accepted– input3.txt: Accepted– input4.txt: Accepted– input5.txt: Accepted– input6.txt: Accepted– input7.txt: Accepted– input8.txt: Accepted
• Result: Accepted
![Page 27: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/27.jpg)
Summary
• Tree Pruning• A*• Avoid equivalent configurations
![Page 28: Exhaustion, Branch and Bound Gary Wong If you have any question, please ask via Email: garywong612@gmail.com MSN: gary_wong612@hotmail.com.](https://reader036.fdocuments.net/reader036/viewer/2022062421/56649c7c5503460f94931566/html5/thumbnails/28.jpg)
Exercises
• 1049 Chocolate• 1050 Bin Packing• 2005 Magic Triangle• 2065 Toggle• 3031 Trishade