Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interpolation Search Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.

Similar presentations


Presentation on theme: "Interpolation Search Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada."— Presentation transcript:

1 Interpolation Search Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharder@alumni.uwaterloo.ca © 2012 by Douglas Wilhelm Harder. Some rights reserved.

2 Outline In this laboratory, we will –Determine how to make very fast searches on appropriately ordered data 2 Interpolation Search

3 Outcomes Based Learning Objectives By the end of this laboratory, you will: –Understand how to quickly find a point in a sorted list with certain properties 3 Interpolation Search

4 The Problem Given a solution to an IVP in the form of two vectors t out and y out, given a point t where t 0 < t < t final how do we find the index k such that t out,k < t < t out,k + 1 4 Interpolation Search

5 Finding the Point Possible solutions: –Linear search—just go through the vector t_out Problem: very slow: O(n) –Binary search? Faster— O( ln(n) ) —but still slower than necessary... 5 Interpolation Search

6 Finding the Point Assume equal spacing—the same h is used –A binary search would start us at k = 9 –Suppose t is very close to t 5 —should we not start close to 5 and not 9 ? 6 Interpolation Search

7 Finding the Point Question, proportionally, how far is t into the interval [t 0, t final ] ? 7 Interpolation Search

8 Finding the Point Question, proportionally, how far is t into the interval [t 0, t final ] ? Why not calculate 8 Interpolation Search

9 Finding the Point Consider binary search: –A binary search starts with [0, 17] –Based on where t falls relative to t 9, we continue with [0, 9] or [9, 17] With the interpolation search, use this interpolated point –Again, we start with [0, 17] –If we calculate and get the value 5, we continue with [0, 5] or [5, 17] 9 Interpolation Search

10 Being careful about how we update allows us to implement: function [k] = interpolation_search( t, t_vec ) lower = 1; upper = length( t_vec ); while true k = lower + floor( (upper - lower)*(t - t_vec(k1))/(t_vec(k2) - t_vec(k1)) ); if t == t_vec(k) return elseif t > t_vec(k) && t < t_vec(k + 1) return; elseif t < t_vec(k) upper = k; else % t > t_vec(k + 1) lower = k + 1; end 10 Interpolation Search

11 Why use an interpolation search? If the points within t_vec are distributed uniformly, the run time will be  ( ln(ln(n)) ) –The distribution of widths of n intervals in a uniform distribution on [a, b] is given by the exponential distribution Distribution function: Cumulative distribution: 11 Interpolation Search

12 You can try this: N = 1000; t = sort( rand( N + 1, 1 ) ); % 1001 uniformly random entries dt = sort( diff( t ) ); % a sorted list of the interval widths plot( dt, linspace( 0, 1, N ), '.' ); % the cumulative distribution lambda = N/1; % the width is 1 ts = linspace( 0, 10*1/lambda, 10000 ); hold on plot( ts, 1 - exp(-lambda*ts), 'r' ); % the actual cumulative distribution % of the exponential function 12 Interpolation Search

13 Our data is even more well behaved than a uniform distribution If the entries of t_vec are equally spaced, an interpolation search will run in  (1) time For many of the returned t_out vectors, the width sizes have been reasonably consistent except when we have discontinuities 13 Interpolation Search

14 Therefore, given t_out and a point t, we can find which points surround it [t8b, y8b] = dp45( @f8b, [1, 2], [1.5, 1.7]', 0.1, 1e-1 ) t8b = 1.0000 1.1000 1.3000 1.5000 1.7000 1.9000 2.0000 y8b = 1.5000 1.6508 1.9047 2.1816 2.5316 2.9883 3.2688 1.7000 1.3609 1.2663 1.5376 1.9875 2.6124 3.0092 interpolation_search( 1.253, t6c ) ans = 4 interpolation_search( 1.793, t6c ) ans = 8 interpolation_search( 1.813, t6c ) ans = 9 Searching for 10000 numbers on [1, 2] : 1834 intervals were found in one step 5932 intervals were found in two steps 2234 intervals were found in three steps 14 Interpolation Search

15 Summary In this quick topic, we’ve looked at interpolation searches –Finding points in o( ln(n) ) time 15 Interpolation Search

16 References [1]Glyn James, Modern Engineering Mathematics, 4 th Ed., Prentice Hall, 2007, p.778. [2]Glyn James, Advanced Modern Engineering Mathematics, 4 th Ed., Prentice Hall, 2011, p.164. [3]J.R. Dormand and P. J. Prince, "A family of embedded Runge-Kutta formulae," J. Comp. Appl. Math., Vol. 6, 1980, pp. 19-26. 16 Interpolation Search


Download ppt "Interpolation Search Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada."

Similar presentations


Ads by Google