Interpolation Search - Electrical and Computer Engineering

Interpolation Search
Douglas Wilhelm Harder, M.Math. LEL
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
[email protected]
© 2012 by Douglas Wilhelm Harder. Some rights reserved.
Interpolation Search
Outline
In this laboratory, we will
– Determine how to make very fast searches on appropriately
ordered data
2
Interpolation Search
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
The Problem
Given a solution to an IVP in the form of two vectors tout
and yout, given a point t where
t0 < t < tfinal
how do we find the index k such that
tout,k < t < tout,k + 1
4
Interpolation Search
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
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 t5—should we not start close to 5 and
not 9?
6
Interpolation Search
Finding the Point
Question, proportionally, how far is t into the interval
[t0, tfinal]?
t  t0
tfinal  t0
7
Interpolation Search
Finding the Point
Question, proportionally, how far is t into the interval [t0,
tfinal]?
t  t0
tfinal  t0
Why not calculate

t  t0 
k  1  17  1

t

t
final
0

8
Interpolation Search
Finding the Point
Consider binary search:
– A binary search starts with [0, 17]
– Based on where t falls relative to t9, 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

t  t0 
k  1  17  1

t

t
final
0

and get the value 5, we continue with [0, 5] or [5, 17]
9
Interpolation Search
Interpolation Search
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
end
end
10
Interpolation Search
Interpolation Search
Why use an interpolation search?
If the points within t_vec are distributed uniformly, the
run time will be Q( ln(ln(n)) )
– The distribution of widths of n intervals in a uniform distribution
on [a, b] is given by the exponential distribution
f :t
Distribution function:
n  b n a t
e
ba
t
F :t
n  b n a
0 b  a e d
Cumulative distribution:
1 e

n
t
ba
11
Interpolation Search
Interpolation Search
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
Interpolation Search
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 Q(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
Interpolation Search
Therefore, given t_out and a point t, we can find which
points
surround it
[t8b, y8b] = dp45( @f8b, [1, 2],
t8b =
1.0000
1.1000
1.3000
y8b =
1.5000
1.6508
1.9047
1.7000
1.3609
1.2663
[1.5, 1.7]', 0.1, 1e-1 )
1.5000
1.7000
1.9000
2.0000
2.1816
1.5376
2.5316
1.9875
2.9883
2.6124
3.2688
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
Summary
In this quick topic, we’ve looked at interpolation searches
– Finding points in o( ln(n) ) time
15
Interpolation Search
References
[1]
Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall,
2007, p.778.
[2]
Glyn James, Advanced Modern Engineering Mathematics, 4th 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