Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.5 1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:

Slides:



Advertisements
Similar presentations
When do I need an invariant? CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Advertisements

Depth-First and Breadth-First Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.2 TexPoint fonts used in EMF. Read the TexPoint manual before.
Binary Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
Foldr and Foldl CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson 9.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Multi-way Trees CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.6 © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA 1 ©
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Ormap, andmap, and filter CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.5 TexPoint fonts used in EMF. Read the TexPoint manual.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Mutually-Recursive Data Definitions CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.4 © Mitchell Wand, This work is licensed under a Creative.
Foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.4 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.6 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
The Function Design Recipe CS 5010 Program Design Paradigms “Bootcamp” Lesson 1.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
The Design Recipe using Classes CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.5 © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.1 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Rewriting your function using map and foldr CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Case Study: Free Variables CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Two Draggable Cats CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Invariants and Performance CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
More examples of invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Over Functions CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Lists of Structures CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Testing Objects CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Searching in a Graph CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.1 © Mitchell Wand, This work is licensed under a.
Introducing General Recursion CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you.
Halting Measures and Termination Arguments CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual.
Non-Empty Lists CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
Using the List Template CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.2 TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Sometimes Structural Recursion Isn't Enough CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.1 TexPoint fonts used in EMF. Read the TexPoint manual.
More linear search with invariants CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Midterm Review CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.:
Solving Your Problem by Generalization CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under.
Classes, Objects, and Interfaces CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Generalizing Similar Functions
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
More About Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.3
More Recursive Data Types
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.7
Halting Functions for Tree-Like Structures
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.3
From Templates to Folds
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Case Study: Undefined Variables
CS 5010 Program Design Paradigms “Bootcamp” Lesson 7.5
Solving Your Problem by Generalization
Contracts, Purpose Statements, Examples and Tests
CS 5010 Program Design Paradigms “Bootcamp” Lesson 6.5
More examples of invariants
ormap, andmap, and filter
Generalizing Similar Functions
Rewriting your function using map and foldr
CS 5010 Program Design Paradigms “Bootcamp” Lesson 4.1
When do I need an invariant?
CS 5010 Program Design Paradigms “Bootcamp” Lesson 8.3
CS 5010 Program Design Paradigms “Bootcamp” Lesson 3.4
Presentation transcript:

Linear Search CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAA © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Creative Commons Attribution-NonCommercial 4.0 International License

Introduction Many problems involve searching General recursion is well-suited to search problems. In this lesson, we'll talk about a simple case: linear search 2

Learning Objectives At the end of this lesson you should be able to: – Recognize problems for which a linear search abstraction is appropriate. – Use general recursion and invariants to solve problems involving numbers 3

Example #1: function-sum function-sum : Nat Nat (Nat -> Number) -> Number GIVEN: natural numbers lo ≤ hi and a function f, RETURNS: SUM{f(j) | lo ≤ j ≤ hi} 4

Examples/Tests (begin-for-test (check-equal? (function-sum 1 3 (lambda (j) j)) ( )) (check-equal? (function-sum 1 3 (lambda (j) (+ j 10))) ( ))) 5

Let's generalize As we compute, we will have computed the sum of some of the values. Let's call that sum sofar. 6 lohi sofar contains the sum of the f(j) for j in this region

Representing this picture as data 7 lohi sofar contains the sum of the f(j) for j in this region i We can represent this picture with 4 numbers: lo i, which is the first value of j to right of the boundary hi, and sofar, which is the sum of the f(j) for j in the brown region So what we want to compute is sofar + SUM{f(j)|i ≤ j ≤ hi} This is a function of i, hi, sofar, and f.

Contract, Purpose Statement, and Examples ;; generalized-function-sum : ;; Nat Nat Number (Nat -> Number) -> Number ;; GIVEN: natural numbers i and hi, a number sofar, ;; and a function f, ;; WHERE: i ≤ hi ;; RETURNS: sofar + SUM{f(j) | i ≤ j ≤ hi} ;; EXAMPLES/TESTS: (begin-for-test (check-equal? (generalized-function-sum (lambda (j) j)) (+ 17 ( ))) (check-equal? (generalized-function-sum (lambda (j) (+ j 10))) (+ 42 ( )))) 8

What do we know about this function? if i = hi, then (generalized-function-sum i hi sofar f) = sofar + SUM{f(j)|i ≤ j ≤ hi} = sofar + SUM{f(j)|hi ≤ j ≤ hi} = (+ sofar (f hi)) = (+ sofar (f i)) 9

What else do we know about this function? if i < hi, then (generalized-function-sum i hi sofar f) = sofar + SUM{f(j)|i ≤ j ≤ hi} = (sofar + f(i)) + SUM{f(j)|i+1 ≤ j ≤ hi} = (generalized-function-sum (+ i 1) hi (+ sofar (f i)) f) 10 take (f i) out of the SUM

So now we can write the function definition ;; STRATEGY: If not done, recur on i+1. (define (generalized-function-sum i hi sofar f) (cond [(= i hi) (+ sofar (f i))] [else (generalized-function-sum (+ i 1) hi (+ sofar (f i)) f)])) 11

What happens at the recursive call? 12 sofar contains the sum of the f(j) for j in this region lohii i lohi lohii The shaded region expands by one

What's the halting measure? Proposed halting measure: (hi – i). Termination argument: – (hi – i) is non-negative, because of the invariant i ≤ hi – i increases at every call, so (hi – i) decreases at every call. So (hi – i) is a halting measure for generalized- function-sum 13

We still need our original function ;; function-sum : ;; Nat Nat (Nat -> Number) -> Number ;; GIVEN: natural numbers lo and hi, and a ;; function f ;; WHERE: lo ≤ hi ;; RETURNS: SUM{f(j) | lo ≤ j ≤ hi} ;; STRATEGY: call a more general function (define (function-sum lo hi f) (generalized-function-sum lo hi 0 f)) 14 Just call generalized-function-sum with sofar = 0.

Example #2: Linear Search ;; linear-search : Nat Nat (Nat -> Bool) -> MaybeNat ;; GIVEN: 2 natural numbers lo and hi, ;; and a predicate pred ;; WHERE: lo ≤ hi ;; RETURNS: the smallest number in [lo,hi) that satisfies ;; pred, or false if there is none. ;; EXAMPLES/TESTS (begin-for-test (check-equal? (linear-search 7 11 even?) 8) (check-false (linear-search 2 4 (lambda (n) (> n 6))))) 15 Remember, this means the half- open interval: { j | lo ≤ j < hi}

What are the trivial cases? if (= lo hi), then [lo,hi) is empty, so the answer is false. if (pred lo) is true, then lo is the smallest number in [lo,hi) that satisfies pred. 16

What have we got so far? (define (linear-search lo hi pred) (cond [(= lo hi) false] [(pred lo) lo] [else ???])) 17

What's the non-trivial case? If (< lo hi) and (pred lo) is false, then the smallest number in [lo,hi) that satisfies pred (if it exists) must be in [lo+1, hi). So, if (< lo hi) and (pred lo) is false, then (linear-search lo hi pred) = (linear-search (+ lo 1) hi pred) 18

Function Definition ;; STRATEGY: If more to search and not found, then recur ;; on (+ lo 1) (define (linear-search lo hi pred) (cond [(= lo hi) false] [(pred lo) lo] [else (linear-search (+ lo 1) hi pred)])) 19

What's the halting measure? The invariant tells us that lo ≤ hi, so (- hi lo) is a non-negative integer. lo increases at every recursive call, so (- hi lo) decreases. So (- hi lo) is our halting measure. 20

Summary We've seen how generative recursion can deal with problems involving numerical values We've seen how context arguments and invariants can help avoid recalculating expensive values We've seen how invariants can be an invaluable aid in understanding programs 21

Learning Objectives At the end of this lesson you should be able to: – Recognize problems for which a linear search abstraction is appropriate. – Use general recursion and invariants to solve problems involving numbers 22

Next Steps Study the files 08-6-function-sum.rkt and linear-search.rkt If you have questions about this lesson, ask them on the Discussion Board Go on to the next lesson 23