This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Towers of Hanoi (from Wikipedia)Towers of Hanoi (from Wikipedia)
If n>1, then somewhere along the sequence of moves, the If n>1, then somewhere along the sequence of moves, the largest disk must be moved from peg f to another peg, largest disk must be moved from peg f to another peg, preferably to peg t. preferably to peg t.
The only situation that allows this move is when all smaller The only situation that allows this move is when all smaller n-1 disks are on peg r. n-1 disks are on peg r. • First all h-1 smaller disks must go from f to r. First all h-1 smaller disks must go from f to r.
• Move the largest disk Move the largest disk
• Finally move the h-1 smaller disks from peg r to peg t. Finally move the h-1 smaller disks from peg r to peg t.
Now the problem is reduced to moving h-1 disks from one Now the problem is reduced to moving h-1 disks from one peg to another one, first from f to r and subsequently from peg to another one, first from f to r and subsequently from r to t r to t • The same method can be used both times by renaming the pegs. The same method can be used both times by renaming the pegs.
• The same strategy can be used to reduce the h-1 problem to h-2, h-The same strategy can be used to reduce the h-1 problem to h-2, h-3, and so on until only one disk is left. This is called 3, and so on until only one disk is left. This is called recursion..
The following is a procedure for moving a tower of h disks The following is a procedure for moving a tower of h disks from a peg f onto a peg t, with r being the remaining third from a peg f onto a peg t, with r being the remaining third peg:peg:• Step 1: If h>1 then first move the h-1 smaller disks from peg f to Step 1: If h>1 then first move the h-1 smaller disks from peg f to
peg r.peg r.
• Step 2: Now the largest disk, i.e. disk h-1 can be moved from peg f Step 2: Now the largest disk, i.e. disk h-1 can be moved from peg f to peg t.to peg t.
• Step 3: If h>1 then again use this procedure to move the h-1 smaller Step 3: If h>1 then again use this procedure to move the h-1 smaller disks from peg r to peg t.disks from peg r to peg t.
A straightforward approach, usually based directly on the A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involvedproblem’s statement and definitions of the concepts involved
Just do it, and don’t be fancyJust do it, and don’t be fancy
Examples:Examples: Computing Computing aan n ((a a > 0, > 0, nn a nonnegative integer) a nonnegative integer)
Computing Computing nn!!
Multiplying two matricesMultiplying two matrices
Searching for a key of a given value in a listSearching for a key of a given value in a list
So, When and Why Brute ForceSo, When and Why Brute Force
Why not brute force?Why not brute force?• You’ll never get an elegant algorithmYou’ll never get an elegant algorithm
• You’ll never give yourself a chance to see the hidden patterns You’ll never give yourself a chance to see the hidden patterns necessary for getting an optimal solutionnecessary for getting an optimal solution
• For problems where solutions that are better than all pairs exist, For problems where solutions that are better than all pairs exist, brute force will not be optimalbrute force will not be optimal
Why brute force?Why brute force?• Exceedingly easy to conceptualizeExceedingly easy to conceptualize
• The method can be applied to a large range of problems (not true The method can be applied to a large range of problems (not true for a lot of the other strategies we’ll see)for a lot of the other strategies we’ll see)
• Works well for small values (input sizes) and its easy to implementWorks well for small values (input sizes) and its easy to implement
• Expense of a more advanced algorithm may not be justifiedExpense of a more advanced algorithm may not be justified
– Only a few difficult instances to solveOnly a few difficult instances to solve• Makes a good yard stickMakes a good yard stick
– i.e. We can do at least this well very easilyi.e. We can do at least this well very easily
Selection SortSelection Sort Scan the array to find its smallest element and Scan the array to find its smallest element and swap it with the first element. Then, starting with the second swap it with the first element. Then, starting with the second element, scan the elements to the right of it to find the element, scan the elements to the right of it to find the smallest among them and swap it with the second elements. smallest among them and swap it with the second elements. Generally, on pass Generally, on pass i i (0 (0 i i n-n-2), find the smallest element in 2), find the smallest element in AA[[i..n-i..n-1] and swap it with 1] and swap it with AA[[ii]:]:
Bubble SortBubble Sort Scan the array looking at each consecutive Scan the array looking at each consecutive pair of elements. Swap the two elements if they are out of pair of elements. Swap the two elements if they are out of order. Repeatedly scan the array in an all-pairs manner.order. Repeatedly scan the array in an all-pairs manner.
See pg 100 for the pseudocodeSee pg 100 for the pseudocode
Example:Example:• 7 3 2 57 3 2 5
• 89 45 68 90 29 34 1789 45 68 90 29 34 17
Note: we can do better by stopping if no swaps are neededNote: we can do better by stopping if no swaps are needed• i.e. – the list is sortedi.e. – the list is sorted
With brute force, its often possible to do small things that With brute force, its often possible to do small things that decrease the run-timedecrease the run-time• These don’t generally change the asymptotic boundsThese don’t generally change the asymptotic bounds
Example: Sequential SearchExample: Sequential Search• Make sure that you have the value you are looking for appended to Make sure that you have the value you are looking for appended to
the end of the listthe end of the list
– That way, the item will have to be found and we That way, the item will have to be found and we won’t have to constantly check for the end of the listwon’t have to constantly check for the end of the list
• Obviously, we could first sort our listObviously, we could first sort our list
patternpattern: a string of : a string of mm characters to search for characters to search for texttext: a (longer) string of : a (longer) string of nn characters to search in characters to search in problem: find a substring in the text that matches the patternproblem: find a substring in the text that matches the pattern
Brute-force algorithmBrute-force algorithm
Step 1 Align pattern at beginning of textStep 1 Align pattern at beginning of text
Step 2 Moving from left to right, compare each character ofStep 2 Moving from left to right, compare each character of pattern to the corresponding character in text until pattern to the corresponding character in text until
– all characters are found to match (successful search); orall characters are found to match (successful search); or
– a mismatch is detecteda mismatch is detected
Step 3 While pattern is not found and the text is not yetStep 3 While pattern is not found and the text is not yet exhausted, realign pattern one position to the right and exhausted, realign pattern one position to the right and repeat Step 2 repeat Step 2