• No results found

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

N/A
N/A
Protected

Academic year: 2021

Share "Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques"

Copied!
29
0
0

Loading.... (view fulltext now)

Full text

(1)

Searching

(2)

Outline

– Introduction

– Linear Search

Ordered linear search Unordered linear search

– Transpose sequential search

– Transpose sequential search

– Interpolation search

– Binary search

– Fibonacci search

(3)

• In the discipline of computer science, the problem of search has assumed enormous significance

• It spans a variety of applications rather disciplines,

beginning from searching for a key in a list of data elements to searching for a solution to a problem in its search space

• Innumerable problems exist where one searches for patterns – images, voice, text, hyper text, photographs etc., in a

repository of data or patterns, for the solution of the problems concerned.

(4)

Linear search

• A linear search or sequential search is one where a key K is searched for in a linear list L of data

elements. The list L is commonly represented using a sequential data structure such as an array

• If L is ordered then the search is said to be an

ordered linear search and if L is unordered then it is said to be unordered linear search

(5)

Searching

• A question you should always ask when selecting a

search algorithm is “How fast does the search have to be?” The reason is that, in general, the faster the

algorithm is, the more complex it is.

• Bottom line: you don’t always need to use or should

use the fastest algorithm. use the fastest algorithm.

• Let’s explore the following search algorithms, keeping

speed in mind.

– Sequential (linear) search

(6)

Sequential Search on an Unordered File

Basic algorithm:

Get the search criterion (key) Get the first record from the file

While ( (record != key) and (still more records) ) Get the next record

Get the next record End_while

When do we know that there wasn’t a record in

the file that matched the key?

(7)

Sequential Search on an Ordered File

• Basic algorithm:

Get the search criterion (key) Get the first record from the file

While ( (record < key) and (still more records) ) Get the next record

End_while

If ( record = key ) Then success Then success

Else there is no match in the file End_else

• When do we know that there wasn’t a record in the file that matched the key?

(8)

Sequential Search of

Ordered vs. Unordered List

• Let’s do a comparison.

• If the order was ascending alphabetical on customer’s

last names, how would the search for John Adams on the ordered list compare with the search on the

unordered list?

– Unordered list

• if John Adams was in the list?

• if John Adams was not in the list?

– Ordered list

• if John Adams was in the list?

(9)

Ordered vs Unordered (con’t)

How about George Washington?

– Unordered

• if George Washington was in the list?

• If George Washington was not in the list?

• If George Washington was not in the list?

– Ordered

• if George Washington was in the list?

• If George Washington was not in the list?

(10)

Ordered vs. Unordered (con’t)

Observation: the search is faster on an ordered

list only when the item being searched for is not

in the list.

Also, keep in mind that the list has to first be

placed in order for the ordered search.

Conclusion: the

efficiency

of these algorithms is

roughly the same.

So, if we need a faster search, we need a

completely different algorithm.

(11)

Algorithm 15.1: Procedure for ordered linear search

procedure LINEAR_SEARCH_ORDERED(L, n, K)

/* L[0:n-1] is a linear ordered list of data elements. K is the key to be searched for in the list. In case of unsuccessful search, the procedure prints the message “KEY not found” otherwise prints “KEY found” and returns the index i*/

i = 0;

while (( i < n) and (K > L[i])) do

/* search for X down the list*/ i = i + 1;

endwhile

if ( K = L[i]) then { print (“ KEY found”); return (i);}

/* Key K found. Return index i */

else

print (“ KEY not found”); end LINEAR_SEARCH_ORDERED.

(12)

Transpose Sequential Search

• Searches a list of data items for a key, checking itself against the data items one at a time in a sequence

• If the key is found, then it is swapped with its predecessor and the search is

termed successful. The swapping of the search key with its predecessor, once it is found, favors faster search when one repeatedly looks for the key

• The more frequently one looks for a specific key in a list, the faster the retrievals take place in transpose sequential search, since the found key moves towards the beginning of the list with every retrieval operation

• Thus transpose sequential search is most successful when a few data items are repeatedly looked for in a list.

(13)

Interpolation Search

• Interpolation search is based on this principle of

attempting to look for a key in a list of elements, by comparing the key with specific elements at

“calculated” positions and ensuring if the key occurs “before” it or “after” it until either the key is found “before” it or “after” it until either the key is found or not found

• The list of elements must be ordered and we assume

that they are uniformly distributed with respect to requests.

(14)

Binary Search

If we have an ordered list and we know how

many things are in the list (i.e., number of

records in a file), we can use a different

strategy.

The

binary search

gets its name because the

The

binary search

gets its name because the

algorithm continually divides the list into two

parts.

(15)

How a Binary Search Works

Always look at the center

value. Each time you get

to discard half of the

to discard half of the

remaining list.

(16)

How Fast is a Binary Search?

Worst case: 11 items in the list took 4 tries

How about the worst case for a list with 32

items ?

– 1st try - list has 16 items – 2nd try - list has 8 items – 2nd try - list has 8 items – 3rd try - list has 4 items – 4th try - list has 2 items – 5th try - list has 1 item

(17)

How Fast is a Binary Search? (con’t)

List has 250 items 1st try - 125 items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items

List has 512 items 1st try - 256 items 2nd try - 128 items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item

(18)

What’s the Pattern?

List of 11 took 4 tries

List of 32 took 5 tries

List of 250 took 8 tries

List of 512 took 9 tries

32 = 2

5

and 512 = 2

9

8 < 11 < 16 2

3

< 11 < 2

4

(19)

A Very Fast Algorithm!

How long (worst case) will it take to find an

item in a list 30,000 items long?

210 = 1024 213 = 8192

211 = 2048 214 = 16384

2 = 2048 2 = 16384

212 = 4096 215 = 32768

(20)

Lg n Efficiency

We say that the binary search algorithm runs in

log

2

n time

. (Also written as

lg n

)

Lg n means the log to the base 2 of some value

of n.

8 = 2

3

lg 8 = 3

16 = 2

4

lg 16 = 4

8 = 2

3

lg 8 = 3

16 = 2

4

lg 16 = 4

There are no algorithms that run faster than lg

n time.

(21)

Binary Search Example

7 12 42 59 71 86 104 212

(22)

Binary Search Example

7 12 42 59 71 86 104 212

(23)

Binary Search Example

7 12 42 59 71 86 104 212

(24)

Binary Search Example

7 12 42 59 71 86 104 212

89 not found – 3 comparisons

(25)

Binary Search Big-O

An element can be found by comparing and cutting

the work in half.

We cut work in ½ each time

How many times can we cut in half?

How many times can we cut in half?

Log2N

(26)

Algorithm 15.5 Procedure for binary search

procedure binary_search(L, low, high, K)

/* L[low:high] is a linear ordered sublist of data elements. Initially low is set to 1 and high to n. K is the key to be searched in the list. */

if ( low > high) then {binary_search =0;

print(“Key not found”); exit();}

else

{ /* key K not found*/

    + = 2 high low mid ; case case

: K = L[mid]: { print (“Key found”); binary_search=mid;

return L[mid];}

: K < L[mid]: binary_search = binary_search(L, low, mid-1, K); : K > L[mid]: binary_search = binary_search(L, mid+1, high, K);

endcase

}

(27)

The decision tree for binary search supports the following characteristics:

(i) the indexes of the left and the right child nodes differ by the same amount from that of the parent node

This characteristic renders the search process to be uniform and This characteristic renders the search process to be uniform and therefore binary search is also termed as uniform binary search.

(ii) for n elements where n = 2t −1, the difference in the indexes of a parent node and its child nodes follows the sequence 20,21,22... from the leaf upwards.

(28)

Fibonacci Search

The Fibonacci number sequence is given by { 0, 1, 1, 2, 3, 5, 8, 13, 21,…..} and is generated by the following recurrence relation:

2 1 1 0 1 0 − − + = = = i i i F F F F F 2 1 − − + = i i i F F F

It is interesting to note that the Fibonacci sequence finds an application in a search technique termed Fibonacci search. While binary search selects

the median of the sublist as its next element for comparison, the Fibonacci search determines the next element of comparison as dictated by the Fibonacci number sequence.

(29)

Other Search Techniques

Tree Search

Graph Search

Indexed Sequential search

References

Related documents

A linear search (sequential search) is an algorithm that looks for a particular value in a sequence of values. The search starts from the beginning of the sequence and elements

• Academic Deans and Graduate Program Directors • Office of Student Financial Assistance.. • Vice Provost of

Search Range – User can enter the search offset, the search length and the total is also displayed.. Search Pattern – The pattern we are

generAl chAir oF The conFerence Martin Hägele Head of Department Robot and Assistive Systems, Fraunhofer Institute for Manufacturing Engineering and Automation IPA, Stuttgart,

Programming and Problem-Solving – Binary Search and Recursion Spring 2021 Dennis Komm 3 / 30.

In the previous chapter, a relay model with three zones of protection was defined and presented; two of the zones were forward looking and the remaining zone was

Binary search trees provide an excellent structure for searching Binary search trees provide an excellent structure for searching a list and at the same time for inserting

This research explores the potential of 16 Glandularia genotypes for semi-intensive green roof under low maintenance; they were evaluated by the dynamics of the coverage area,