Searching Algorithms
Searching Algorithms
Goals: This laboratory exercise provides experience with the
simulation of a simple game, with developing loops, and with various
searching strategies.
Consider the following simple number game between two parties, A and B.
Initially, A chooses an integer between 1 and 100. B then tries to guess
this number. In this guessing process, B makes a guess and then A responds
by saying either that the guess is correct, or that the guess is too high,
or that the guess is too low. This process continues until B makes a
correct guess. (In playing the game, B might have some incentive to try to
determine the correct number quickly, but the game itself does not require
this.)
-
Working in partners, play this game a couple times, so you are
sure how it works.
-
Write a procedure that simulates A's role in this game, with the person
running the program filling B's role. The procedure first should pick an
integer randomly between 1 and 100. Then B (the user) will enter a guess
(at the keyboard), and the procedure should respond to the guess. This
second part will continue until the correct number is guessed. (All of
this can be done with a simple do expression.)
For this part, you may want to review the use of Scheme's random
procedure from the previous labs on Random Number
Generators and Input and Output.
-
Now, define a separate variable for A's number, and write the following
logical operations which A might undertake:
-
(reset-number) should make A pick a new number.
-
(make-guess number) should return A's response: "too high",
"too low", and "correct".
Now, consider B's strategy in the game. Several approaches are possible,
including the following:
-
B might guess numbers randomly until choosing the correct one.
-
B might guess numbers in sequence 1, 2, 3, ..., 100
(or 100, 99, 98, ..., 2, 1), stopping once the correct integer is
found.
-
B might guess a number in the middle (e.g., 50 or 51). On the basis of the
answer (``correct'', ``too low'', ``too high''), B could then infer that
some numbers need not be tried at all, and B could reduce the range of
numbers possible for [1..100] to [1..50] or [51..100] (depending upon A's
response). As a second guess, B could try the middle of this new interval,
and again the interval of possible values could be cut in half on the basis
of A's response. This process of guessing a middle value and cutting the
interval of possible candidates in half could continue until the number is
found.
Each of these approaches naturally involves a loop. For approach a, each
guess should be made without regard for the past. For approaches b or c,
subsequent steps should depend upon feedback from A in earlier rounds.
Thus, for approaches b and c, we would need to keep some information as we
work through a loop.
-
Write a simple procedure random-B to simulate approach a above.
Use the reset-number and make-guess procedures when
working with A.
-
For approach b, write out (in English) what information should be kept
from one iteration of the loop to the next. Explain what inferences might
be made from this kept information, and describe (again, in English) how
this information should be updated when the next feedback from A is
obtained.
-
Write procedure sequential-B which implement's approach b above.
In writing your code, consider the information you described in the
previous step. You may want to use local variables or parameters to store
that relevant information.
-
Repeat step 5 for approach c.
-
Write procedure binary-B which implements approach c above.
B's strategies for identifying a number between 1 and 100 may be applied to
many other situations as well. One common problems in computing, for example,
involves determining whether or not specific information has been stored
previously. A second problem involves locating a specific piece of
information and retrieving it.
-
Suppose information was stored in a vector. How might you adapt
sequential-B to determine whether a given value was stored in the
vector? In your approach, would it matter if the data in the vector were
sorted? Why or why not?
-
How would you adapt binary-B to
determine whether a sorted vector contains a given value?
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/151/lab-merge-sort.html
created April 16, 1997
last revised December 1, 2000