For each of the riddles given below, devise an algorithm (a step-by-step method) for solving the task posed by the riddle.
Fox, rabbit, and carrot, take note,
Must be sent across the river by boat.
Rabbit would eat carrot, fox would eat rabbit, too.
This must be prevented by you.
The boat fits 2 things, you and one more.
Transporting everything safely is no easy chore.
Owing to an administrative foul-up, two trains have been sent toward each other along the same stretch of track. Luckily, the engineers see each other in time to stop. Between the trains there is a small, abandoned siding that will hold an engine or just one car at a time. The siding is equipped with a switch at both ends.
Can you develop a procedure for getting the trains past each other using the tiny siding? You may assume that each train has five cars. (Hint: The procedure may take a while.)
I'm thinking of a number between 1 and 1,000,000. Your task is to guess it... using at most 20 questions. You are only allowed to ask questions that require a "yes" or "no" response. Come up with an algorithm for asking questions that will find the number regardless of which one I pick.
A desert patrol vehicle has a 10-gallon tank and gets ten miles to the gallon no matter what load it carries. At the depot, there are two 50-gallon fuel drums. How far can the patrol vehicle travel before running out of gas if it can carry only one drum at a time?
The vehicle begins with a full tank of gas. It may drop off and pick up drums at will, traveling back and forth along its route, as necessary. So, you could transport a fuel drum to some location other than the depot and just leave it there.
You are given two identical marbles and asked to find the highest floor of a 100-story building from which it is safe to drop a marble without breaking it. In other words, you are to find the highest floor X such that if you drop a marble from any floor below the floor X, it will not break; but if you drop it from the floor X, or from any floor above X, the marble will break. In the process of finding floor X, you are permitted to break both marbles. Construct an algorithm to find the "breaking floor" X in the fewest attempts in the worst case (an attempt is a drop of the marble).
Note: By worst case, I mean the largest number of attempts your algorithm would ever require to find the breaking floor X. The most basic algorithm you can try would be a sequential search: try floor 1, then floor 2, then floor 3, etc. But then you'd never need the second marble! Also, the worst case number of attemtps for this algorithm would be 100 (which happens if floor X happens to be floor 100).
Consider the problem of trying to do a number of loads of laundry, given only one washer and one dryer. Washing a load takes 25 minutes, drying a load takes 25 minutes, and folding the clothes in a load takes 10 minutes, for a total of 1 hour per load (assuming that the time to transfer a load is built into the timings given). All the laundry can be done in 10 hours, 600 minutes, using the method of completing one load before starting the next one.
Write an algorithm that you (and then someone else) can use to determine the shortest time needed to do M loads of laundry. In other words, given an integer value, M, determine the smallest number of minutes needed to complete all loads of laundry.
Four people want to cross a bridge at night and they only have one flashlight among the four of them. The bridge is of a width such that a maximum of 2 people may cross at a time. One takes 10 minutes to cross, another takes 5 minutes to cross, another takes 2 minutes to cross, and the last can cross the bridge in 1 minute. If two people cross the bridge together, they must walk at the pace of the slower one. So if the first and second people above cross together, they take 10 minutes. Also, since they cannot toss the flashlight back across the bridge, one person must return to the far side. How fast can you get all 4 people over the bridge?
Your task is to devise a winning strategy for an Othello-like game. You have before you a board (like a checkerboard) with 10 rows and 10 columns. Your goal is to claim as many board squares as possible. You're initially given 10 game pieces to place on the board. You claim any square where you place a piece. Beyond your first 10 pieces, you can also place another piece in any square bordered by at least 2 squares where you currently have game pieces (directly bordered - not counting diagonal neighbors). How many board squares can you claim?
In solving the problems above we are (mostly) needing to come up with algorithms. One possible definition is that an algorithm is an ordered set of unambiguous, executable steps defining a terminating process. To create a good algorithm (and a good solution for any of the above riddles) we need it to be correct, complete, precise, and unambiguous. An algorithm is correct if it gives the answer we're looking for. Although, you should note that sometimes the hardest part of designing an algorithm is deciding what sort of answer we want it to return! An algorithm is complete if it always returns an answer. Thus, if an algorithm was correct and complete this it would always return a correct answer. An algorithm is unambiguous if its steps are clear-cut and easy to follow and execute. So, this algorithm wouldn't qualify:
Comic by: Sidney Harris
Beyond this there are many criteria we could have for deciding if one algorithm is preferred over another. The most common criteria is how fast or efficient an algorithm is: how much time it uses or how many steps it takes to complete. We might also look at how many resources an algorithm uses. If its in a computer, that might be how much memory space the program requires (think of it as notes the algorithm has to take to remember what it needs). For a robot acting in the real world an important resource might be how much power or energy it uses to execute a movement algorithm.
There's one other important quality for an algorithm. It seems to be lacking from the example below.
The problem is:
There are also several ways that we could state our algorithm: with pictures, natural language (plain english), pseudo-code, or a specific programming language. Pseudo-code is 'almost' a programming language, but it ignores the details of the exact structure of the language so that it can focus on just getting across the ideas of the algorithm.