Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Similar presentations


Presentation on theme: "Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons."— Presentation transcript:

1 Searching UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. http://jagger.me.berkeley.edu/~pack/e77http://creativecommons.org/licenses/by-sa/2.0/

2 Searching Finding a specific entry in a 1-dimensional (eg, column vector, row vector) object. Brute force approach, scan (potentially) the entire list. function Idx = bfsearch(A,Key) N = length(A); Idx=1; while Idx<=N & A(Idx)~=Key Idx = Idx + 1; end if Idx==N+1 Idx = []; % no match found end Loop exits if Idx>N or A(Idx)==Key Match found Entire list scanned, no match found

3 Matlab logical conditionals The Matlab logical conditionals ( ==, ~=, >, >=, <, <= ) all do this form of searching, but –actually check every entry, and –return an vector of 0s and 1s reflecting the outcome They are implemented at “low level” and are essentially as fast as the brute force can be. PseudoCode looks like: function Idx = ltsearch(A,Key) szA = size(A); Idx = zeros(szA); for i=1:prod(szA) if A(i)<Key Idx(i) = 1; end If the number of elements in A doubles, we expect program will take about twice as long to run.

4 Matlab find The Matlab command find looks for all nonzero entries. Again, brute force, but implemented at “low level” and essentially as fast as the brute force can be. PseudoCode looks like: function Idx = find(A) szA = size(A); Idx = zeros(prod(szA),1); Cnt = 0; for i=1:prod(szA) if A(i)~=0 cnt = cnt + 1; Idx(cnt) = i; end End Idx = Idx(1:cnt); If the number of elements in A doubles, we expect program will take about twice as long to run.

5 Searching in a sorted list If the list is sorted, it should be easier to search, since checking for a match at any location in the list –finds the match, or (more likely) –Splits the list (at that location) into two lists, one to the “left” of the location, and one to the “right” Since the list is sorted, we immediately know which of the two sublists the match must belong to. Take a sorted list A, of length N. Match must be over here

6 Searching in a sorted list Take a sorted list A, of length N. Choose k in the “middle”, halving the size of the relevant list each time, until a match is found. Looks a lot like Bisection for root finding Match must be over here

7 Simple Binary Search Example Lets do an example with A as below, and Key = 0.1; L = 1; R = length(A); while R>L M = floor((L+R)/2); if A(M)<Key L = M+1; else R = M; end

8 Matlab code for Binary Search function Idx = bsearch(A,Key) Left = 1; Right = length(A); while Right>Left Mid = floor((Left+Right)/2); if A(Mid)<Key Left = Mid+1; else Right = Mid; end if A(Left)==Key Idx = Left; else Idx = []; % no match found end If Right-Left==1, then Mid equals Left, and subsequently Left==Right, leading to the correct final case If Right-Left>1, then Mid is between them, and subsequent value of Right-Left is reduced (essentially halved).

9 Operation Count for Binary Search Let R(n) denote the number of operations it takes to search for a key in a sorted list of length n. Clearly, if n=1, then R(n)=0, as there is nothing to do. Also, it only takes one comparison to split the list length in half (since it is sorted!), so R(n) = R(n/2) + 1. We can then prove that R(n) ≤ log 2 (n)

10 Rough Operation Count for BSearch The recursive relation for R R(1)=0, R(n) = R(n/2) + 1 Claim: For n=2 m, it is true that R(n) ≤ log 2 (n) Case (m=0): true, since log 2 (1)=0 Case (derive m=k+1 case from m=k) Recursive relation Induction hypothesis

11 Matlab code for Binary Search (Recursive) function Idx = bsearch(A,Key,Left,Right) if Left==Right if A(Left)==Key Idx = Left; else Idx = []; % no match found end else Mid = floor((Left+Right)/2); if A(Mid) < Key % match must be beyond Mid Idx = bsearch(A,Key,Mid+1,Right); else Idx = bsearch(A,Key,Left,Mid); end If Right-Left==1, then Mid equals Left, and both recursive calls have Left==Right, leading to the correct base case If Right-Left>1, then Mid is between them, both recursive calls involve “ smaller ” intervals


Download ppt "Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons."

Similar presentations


Ads by Google