CSC153, Class 22: Searching
Overview:
* How computer scientists approach the world:
Algorithms for generalized problems.
* A key problem: Searching.
* Searching examples.
* Lab.
Notes:
* Read "Searching".
* Why I was so critical yesterday.
* More discussion of my grading policy.
* Are there questions on homework 2?
* Closed-eye question:
How many of you have heard of binary search?
----------------------------------------
What is computer science?
* The study of algorithms and data structures
Find a problem, write an algorithm to solve it, implement
the algorithm, analyze the algorithm, test the algorithm,
....
At some point, you realize that you're writing a lot of
very similar algorithms.
(1) Write abstractions of the commonality.
(2) Generalize the problem before writing the algorithm.
Drawback?
* General problem may take more work.
Two common problems: Searching and Sorting
* Searching: Find something in a list/vector
* Sorting: Put a list or vector "in order"
Six P's of searching a list.
;;; Procedure:
;;; search
;;; Parameters:
;;; val, a Scheme value
;;; lst, a list of values
;;; extractor, a procedure
;;; Purpose:
;;; Find a value in lst.
;;; Produces:
;;; entry, a Scheme value.
;;; Preconditions:
;;; extractor must be applicable to each element of lst.
;;; extractor must return something naturally comparable
;;; to val.
;;; Postconditions:
;;; If no element of lst "matches" val, entry is
;;; #f.
;;; If at least one element of lst "matches" val,
;;; entry is one such element.
;;; An element, elt, of lst "matches" val if
;;; (equal? (extractor elt) val).
; What's the full entry for David?
(search "David" cs153 car)
; What's the full entry for my student with a nickname
; of "Andy"?
(search "Andy" cs153 caddr)
(search "Andy" cs153 (right-section list-ref 2))
; What's David's nickname?
(let ((result (search "David" cs153 car)))
(if result
(list-ref result 2)
crash-and-burn-because-you-have-no-david))
; Which translates nicely into
(or (search "David" cs153 car) "Hey You!")
; Did Oge take CS151?
(not (not (search "CS151" oges-amazing-classes (lambda (x) x))))
; Brian's idea: Return two values.
; Good idea, but ignored for now.
; Oge's idea: Add another parameter that extracts the thing
; you want to compare.
; Sam's Sample List
(define cs153
(list
(list "Cassandra" "Schmitz" "Cassie")
(list "David" "Ventresca" "Dave")
(list "Shobha" "Kazinka" "Shobha")
(list "Katherine" "Gray" "Kat")
(list "Ogechi Carl" "Nnadi" "Oge")
(list "Samuel" "Rebelsky" "SamR")
(list "Brian" "Marion" "The Guy Who Reads a Lot of Books")
(list "Evan" "Case" "Absent")))
; Sam's other sample list
(define cs195
(list
(list "Lindsey" "Kuper")
(list "Devin" "Lindsey")))
(define oges-amazing-classes
(list "CS153" "ENG118" "MAT215" "JPN102"))
; Running time of search?
; It depends on how you implement it.
; Traditional implementation: Look at each element in turn.
; O(n*cost-of-extractor)
Can we do better?
Suppose the list is ordered in such a way that values are
"in order" (small to large or vice versa)
; If there's nothing left in the list,
; Return #f
; Look in the middle.
; If the middle thing matches,
; You're done
; If the middle thing is smaller,
; Throw away everything in the smaller half
; Do it again
; If the middle thing is larger,
; Throw away everything in the larger half
; Do it again
What's the running time?
f(1) = d
f(n) = c + f(n/2)
f(n) is in O(log_2(n))
Finding the middle element in a list takes O(n) steps.
f(n) = n + f(n/2)
f(n) is in O(n)
;;; Procedure:
;;; binary-search
;;; Parameters:
;;; val, a value
;;; vec, a vector of values
;;; extract, a unary procedure
;;; less-than?, a binary procedure
;;; Purpose:
;;; See search
;;; Preconditions:
;;; See search
;;; vector must be ordered. That is
;;; (less-than? (vector-ref vec i) (vector-ref vec (+ i 1)))
;;; for all "reasonable" i.
;;; What requirements would you put on less-than?