Fundamentals of Computer Science I (CS151.02 2007S)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
This reading is also available in PDF.
Summary: We consider a typical problem of computing and a variety of algorithms used to solve that problem.
Contents:
To search a data structure is to examine its elements onebyone
until either (a) an element that has a desired property is found or
(b) it can be concluded that the structure contains no element with
that property.
For instance, one might search a vector of integers for an even element,
or a vector of pairs for a pair having the string "elephant"
as its cdr.
In a linear data structure  such as a flat list, a vector, or a file  there is an obvious algorithm for conducting a search: Start at the beginning of the data structure and traverse it, testing each element. Eventually one will either find an element that has the desired property or reach the end of the structure without finding such an element, thus conclusively proving that there is no such element. We used such a strategy for searching association lists. Here are a few alternate versions of the algorithm.
;;; Procedure: ;;; sequentialsearchlist ;;; Parameters: ;;; pred?, predicate ;;; lst, a list ;;; Purpose: ;;; Searches the list for a value that matches the predicate. ;;; Produces: ;;; match, a value ;;; Preconditions: ;;; pred? can be applied to all values in lst. ;;; Postconditions: ;;; If lst contains an element for which pred? holds, match ;;; is one such value. ;;; If lst contains no elements for which pred? holds, match ;;; is false (#f). (define sequentialsearchlist (lambda (pred? lst) (cond ; If the list is empty, no values match the predicate. ((null? lst) #f) ; If the predicate holds on the first value, use that one. ((pred? (car lst)) (car lst)) ; Otherwise, look at the rest of the list (else (sequentialsearchlist pred? (cdr lst)))))) ;;; Procedure: ;;; sequentialsearchvector ;;; Parameters: ;;; pred?, predicate ;;; vec, a vector ;;; Purpose: ;;; Searches the vector for a value that matches the predicate. ;;; Produces: ;;; match, a value ;;; Preconditions: ;;; pred? can be applied to all elements of vec. ;;; Postconditions: ;;; If vec contains an element for which pred? holds, match ;;; is the index of one such value. That is, ;;; (pred? (vectorref vec match)) holds. ;;; If vec contains no elements for which pred? holds, match ;;; is false (#f). (define sequentialsearchvector (lambda (pred? vec) ; Grab the length of the vector so that we don't have to ; keep recomputing it. (let ((len (vectorlength vec))) ; Helper: Keeps track of the position we're looking at. (let kernel ((position 0)) ; Start at position 0 (cond ; If we've run out of elements, give up. ((= position len) #f) ; If the current element matches, use it. ((pred? (vectorref vec position)) position) ; Otherwise, look in the rest of the vector. (else (kernel (+ position 1)))))))) > (define sample (vector 1 3 5 7 8 11 13)) > (sequentialsearchvector even? sample) 4 > (sequentialsearchvector (rightsection = 12) sample) #f
These search procedures return #f
if the search is
unsuccessful. The first returns the matched value if the search is
successful. The second returns returns the position in the specified
vector at which the desired element can be found. There are many variants
of this idea: One might, for instance, prefer to signal an error or display
a diagnostic message if a search is unsuccessful. In the case of a
successful search, one might simply return #t
, if all that is
needed is an indication of whether an element having the desired property
is present in or absent from the list.
One of the most common realworld
searching problems is that of
searching a collection compound values for one which matches a particular
portion of the value, known as the key. For example, we might
search a phone book for a phone number using a person's name as the key
or we might search a phone book for a person using the number as key.
As you've probably noted, association lists implement this kind of
searching if we use the first value of a list as the key for that list.
Of course, it is also possible to make a getkey
procedure
a parameter to the search procedure.
;;; Procedure: ;;; searchlistforkeyedvalue ;;; Parameters: ;;; key, a key to search for. ;;; values, a list of compound values. ;;; getkey, a procedure that extracts a key from a compound value. ;;; Purpose: ;;; Finds a member of the list that has a matching key. ;;; Produces: ;;; match, a Scheme value ;;; #f, otherwise. ;;; Preconditions: ;;; The getkey procedure can be applied to each element of values. ;;; Postconditions: ;;; If there is no index for which ;;; (equal? key (getkey (listref values index))) ;;; holds then ;;; match is #f. ;;; Otherwise, ;;; match is a member of values: (member match values) ;;; (equal? key (getkey match)) (define searchlistforkeyedvalue (lambda (key values getkey) (sequentialsearchlist (lambda (val) (equal? key (getkey val))) values)))
The linear search algorithms just described can be quite slow if the data
structure to be searched is large. If one has a number of searches to
carry out in the same data structure, it is often more efficient to
preprocess
the values, sorting them and transferring them to a vector,
before starting those searches. The reason is that one can then use the
much faster binary search algorithm.
Binary search is a more specialized algorithm than linear search. It requires a randomaccess structure, such as a vector, as opposed to one that offers only sequential access, such as a list. Binary search is limited to the kind of test in which one is looking for a particular value that has a unique relative position in some ordering. For instance, one could use a binary search to look for an element equal to 12 in a vector of integers ordered from smallest to largest, since 12 is uniquely located between integers less than 12 and integers greater than 12; but one wouldn't use binary search to look for an even integer, since the even integers don't have a unique position in any natural ordering of the integers.
In binary search, we keep track of the vector, the value searched for, and the lower and upper bounds of the region still of interest. The key idea is to divide the region of interest of the sorted vector into two approximately equal parts, examining the element at the point of division to determine which of the parts must contain the value sought.
There are usually three possibilities for the relationship between the value sought and the element at the point of division.
(0) The value sought is the element at the point of division. The search has succeeded.
(1) The value sought cannot follow the element at the point of division in the ordering that was used to sort the vector. In this case, the value sought must be in a position with a lower index that the element at the point of division (if it is present at all)  in other words, it must be in the left half of the region of interest. The search procedure invokes itself recursively to search just the left half of that region.
(2) The value sought cannot precede the element at the point of division. In this case, the value sought must be in a higherindexed position  in the right half of the region  if it is present at all. The search procedure invokes itself recursively to search just the right half of the region.
There is one other way in which the recursion can terminate: If, in some recursive call, the region to be searched (which will be half of a half of a half of ... of the original vector) contains no elements at all, then the search obviously cannot succeed and the procedure should take the appropriate failure action.
Here, then, is the basic binarysearch algorithm. The identifiers
lowerbound
and upperbound
denote the starting
and ending positions of the region of the vector within which the value
sought must lie, if it is present at all. (We use the convention that
the starting and ending positions are inclusive in that they
are positions within the vector that we must include in the search.)
;;; Procedure: ;;; binarysearch ;;; Parameters: ;;; key, a key we're looking for ;;; vec, a vector to search ;;; getkey, a procedure of one parameter that, given a data item, ;;; returns the key of a data item. ;;; lessthan?, a binary predicate that tells us whether or not ;;; one key is lessthan another. ;;; Produces: ;;; match, a number. ;;; Preconditions: ;;; The vector is "sorted". That is, ;;; (lessthan? (getkey (vectorref vec i)) ;;; (getkey (vectorref vec (+ i 1)))) ;;; holds for all reasonable i. ;;; The lessthan? procedure can be applied to all pairs of keys ;;; in the vector (and to the supplied key) ;;; The lessthan? procedure is transitive. That is, if ;;; (lessthan? a b) and (lessthan? b c) then it must ;;; be that (lessthan? a c). ;;; The lessthan? procedure is inclusive. If a is not lessthan b ;;; and b is not lessthan a, then a equals b. ;;; Postconditions: ;;; If vector contains no element whose key matches key, match is 1. ;;; If vec contains an element whose key equals key, match is the ;;; index of one such value. That is, key is ;;; (getkey (vectorref vec match)) (define binarysearch (lambda (key vec getkey lessthan?) ; Search a portion of the vector from lowerbound to upperbound (let searchportion ((lowerbound 0) (upperbound ( (vectorlength vec) 1))) ; If the portion is empty (if (> lowerbound upperbound) ; Indicate the value cannot be found 1 ; Otherwise, identify middle point, element, and key (let* ((midpoint (quotient (+ lowerbound upperbound) 2)) (middleelement (vectorref vec midpoint)) (middlekey (getkey middleelement))) (cond ; If the middle key is too large, look in the left half ; of the region. ((lessthan? key middlekey) (searchportion lowerbound ( midpoint 1))) ; If the middle key is too small, look in the right half ; of the region. ((lessthan? middlekey key) (searchportion (+ midpoint 1) upperbound)) ; If the middle key is neither too large nor too small, ; it's just right. (else midpoint)))))))
So, how do we use binary search to search a sorted vector? It depends on what the vector contains. Let's suppose it contains a list of name/grade pairs, sorted by name. Here's one such vector
(define grades (vector (cons "Amy" 85) (cons "Bob" 60) (cons "Charlotte" 91) (cons "Danielle" 80) (cons "Devon" 85) (cons "Erin" 100) (cons "Fred" 70) (cons "Greg" 0) (cons "Heather" 50) (cons "Ira" 80) (cons "Jesse" 90) (cons "Karla" 85) (cons "Leo" 75) (cons "Maria" 88) (cons "Ned" 90) (cons "Otto" 55) (cons "Paula" 56) (cons "Quentin" 88) (cons "Rebecca" 95) (cons "Sam" 110) (cons "Ted" 5) (cons "U" 99) (cons "Violet" 82) (cons "Xerxes" 67) (cons "Yvonne" 95) (cons "Zed" 100)))
Now, binarysearch
has four parameters: a key to search for,
a vector to search, the procedure that extracts a key from each element
in the vector, and the procedure used to compare keys. For this example,
the vector to search will be grades
and the name to search for
will be whatever name we want. To get the name from a pair, we use
car
. To compare two names, we use stringci<?
.
So, to find out the index of my entry, I would write something like the following:
> (binarysearch "Sam" grades car stringci<?) 19 > (vectorref grades 19) ("Sam" . 110)
We might even use this strategy to write a procedure that looks up grades.
;;; Procedure: ;;; getgrade ;;; Parameters: ;;; name, a string ;;; grades, a vector of name/grade pairs, sorted by name ;;; Purpose: ;;; Find the grade given to name. ;;; Produces: ;;; grade, a grade (or #f) ;;; Preconditions: ;;; For all reasonable i, ;;; (stringci<? (car (vectorref grades i)) (car (vectorref grades (+ i 1)))) ;;; Postconditions: ;;; If there exists i s.t. (equal? name (car (vectorref grades i))), ;;; grade is (cdr (vectorref grades i)). ;;; Otherwise, grade is #f. (define getgrade (lambda (name grades) (let ((tmp (binarysearch name grades car stringci<?))) (if (= tmp 1) #f (cdr (vectorref grades tmp))))))
Let's see it work
> (getgrade "Sam" grades) 110 > (getgrade "Janet" grades) #f > (getgrade "Amy" grades) 85 > (getgrade "Zed" grades) 100
There are a number of ways to determine whether or not a value is prime. For small primes, the easiest technique is to search through a vector of known primes.
(define smallprimes (vector 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997))
We could, of course, use a sequential search technique to look for a value
in this vector. However, binary search is much more efficient. What procedure
should we use for getkey
? Well, each value is its own key, so
we use (lambda (x) x)
. The values are ordered numerically, so
we use <
for lessthan.
For example,
> (binarysearch 231 smallprimes (lambda (x) x) <) 1 > (binarysearch 241 smallprimes (lambda (x) x) <) 52 > (vectorlength smallprimes) 168
In procedure form, we might write
(define issmallprime (lambda (candidate) (binarysearch candidate smallprimes (lambda (x) x) <)))
Now, how many recursive calls do we do in determining whether or not a candidate value is a small prime? If we were doing a linear search, we'd need to look at all 168 primes less than 1000, so approximately 168 recursive calls would be necessary. In binary search, we split the 168 into two groups of approximately 84 (one step), split one of those groups of 84 into two groups of 42 (another step), split one of those groups into two groups of 21 (another step), split one of those groups of 21 into two groups of 20 (we'll assume that we don't find the value), split 10 into 5, 5 into 2, 2 into 1, and then either find it or don't. That's only about six recursive calls. Much better than the 168.
Now, suppose we listed another 168 or so primes. In linear search, we would now have to do 336 recursive calls. With binary search, we'd only have to do one more recursive call (splitting the 336 or so primes into two groups of 168).
This slow growth in the number of recursive calls (that is, when you double the number of elements to search, you double the number of recursive calls in sequential search, but only add one to the number of recursive calls in binary search) is one of the reasons that computer scientists love binary search.
For binarysearch
to work correctly, we need to have a sorted
vector. Checking that a vector is sorted will require looking at every
neighboring pair of values, so it is not something we want to do every time
we call binary search. However, it is helpful to have such a procedure available.
;;; Procedure: ;;; sorted? ;;; Parameters: ;;; vec, a vector ;;; getkey, a procedure that extracts keys from the elements of vec ;;; lessthan?, a procedure that compares keys ;;; Purpose: ;;; Determine if vec is sorted by key ;;; Produces: ;;; issorted?, a Boolean ;;; Preconditions: ;;; getkey should be applicable to any value in vec. ;;; lessthan? should be applicable to any two values returned by getkey. ;;; Postconditions: ;;; If, for all reasonable i, ;;; (lessthan? (getkey (vectorref vec i)) (getkey (vectorref vec (+ i 1)))) ;;; then issorted is #t. ;;; Otherwise, ;;; issorted is #f. (define sorted? (lambda (vec getkey lessthan?) (let ((veclen (vectorlength vec))) (letrec ((kernel (lambda (i) (or (= i ( veclen 1)) (and (lessthan? (getkey (vectorref vec i)) (getkey (vectorref vec (+ i 1)))) (kernel (+ i 1))))))) (kernel 0)))))
Here are some tests for the vectors we defined earlier.
> (sorted? smallprimes id <) #t > (sorted? grades car stringci<?) #t
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/History/Readings/searching.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Assignment]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151 2006F (Rebelsky)]
[CSC151.01 2007S (Davis)]
[CSCS151 2005S (Stone)]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Thu Sep 13 20:55:16 2007.
The source to the document was last modified on Tue Apr 17 09:40:31 2007.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2007S/Readings/searching.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.