# Searching Algorithms Finding what you are looking for.

## Presentation on theme: "Searching Algorithms Finding what you are looking for."— Presentation transcript:

Searching Algorithms Finding what you are looking for.

What’s an Algorithm? A step by step procedure to solve calculations and process data A set of rules that precisely defines a sequence of operations Like a Flowchart!

Searching algorithms are algorithms for finding an item in a collection of items used for calculations, procedures, etc. Some searches involve looking for something in a database (real space), like looking up something in the Artemis records. Some search algorithms look through a virtual space, like looking for the best chess move among all the possible moves that can be made. Searching Algorithms?

Types of Searching Algorithms 1. Linear Search 2. Tree Search 3. Binary Search

Linear Search Usually for a small list of items First, look at the first item to see if that is what you were looking for If yes, then you’re done. If not, Then move to the second item Repeat until you have either found the item being searched or reached the end of the list For each item in the list: if that item has the desired value, stop the search and return the item's location. Return Λ.

Search Item: Anie Index (box): 1 BukkyJessiAnieLauren 1234 Does box 1 = Anie? Linear Search

Search Item: Anie Index: 2 BukkyJessiAnieLauren 1234 Does box 2 = Anie? Linear Search

Search Item: Anie Index: 3 BukkyJessiAnieLauren 1234 Does box 3 = Anie? YES! Linear Search

Process of visiting each node (branch) of a tree structure, exactly once, in a systematic way When visiting the nodes (branches), the computer either examines and/or update the items in the nodes Two Types of Tree searching: BF (breadth first search) and DF (depth first search) Tree Search or “Tree Traversal”

Tree Search Depth First Search A B C G E F DH With Linear search you started with box 1 then box 2 and so on, with DF search you start with the chosen circle (A) and then go through the branches Start with A Move to the next branch in alphabetical order (B, D, G), so B Then choose between the branches on B (A, E, F), so E because A has already been chosen Repeat until all branches have been chosen Result: A

Tree Search A B C G E F DH Result: A B Depth First Search Check the B branches Which would be the next item?

Tree Search A B C G E F DH Result: A B E Depth First Search Check the E branches Which would be the next item?

Tree Search A B C G E F DH Result: A B E G Depth First Search Check the G branches Which would be the next item? Think of a maze: Now that we have reached a dead end, we have to retrace our steps

Tree Search A B C G E F DH Result: A B E G Depth First Search Since both E and A have been already been chosen go back to the E branches, B branches...? Which would be the next item?

Tree Search A B C G E F DH Result: A B E G F Depth First Search Check the F branches Which would be the next item?

Tree Search A B C G E F DH Result: A B E G F C Depth First Search

Tree Search A B C G E F DH Result: A B E G F C H Depth First Search

Tree Search Result: A B E G F C H D A B C G E F DH Depth First Search

So the result for this one would be... Result: 1 2 3 4 5 6 7 8 9 10 11 12

Tree Search Start with A Move to the next branch in alphabetical order (B, D, G), so B Then the next letter between the branches, so D then, G After all the A branches have been completes, move to the B branches, then the D branches.... Tree Search BREADTH First Search A B C G E F DH Result: A

Tree Search Breadth First Search A B C G E F DH Result: A B Tree Search A B C G E F DH

Breadth First Search A B C G E F DH Result: A B D Tree Search A B C G E F DH

Breadth First Search A B C G E F DH Tree Search A B C G E F DH Result: A B D G

Tree Search Breadth First Search A B C G E F DH Tree Search A B C G E F DH Result: A B D G E

Tree Search Breadth First Search A B C G E F DH Tree Search A B C G E F DH Result: A B D G E F

Tree Search Breadth First Search A B C G E F DH Tree Search A B C G E F DH Result: A B D G E F C

Tree Search Breadth First Search A B C G E F DH Tree Search A B C G E F DH Result: A B D G E F C H

So the result for this one would be... Result: 1 2 3 4 5 6 7 8 9 10 11 12

Tree Search A B E G F C H D A B C G E F DH Depth First Search Breadth First Search A B C G E F DH A B C G E F DH A B D G E F C H Different paths

Popular search and good for large databases Binary search helps find a specific item in an array (group of items) Binary Search or “Half-interval search”

Binary Search The Phonebook Lets say we are looking for a number in a phonebook with 1000 pages We can use the Linear search and look through the book from page 1 to page 1000 (but that will take forever) OR we can use binary search. Open the middle of the book. If the number is on the left side, forget about the other 500 pages Then split the first section of the phone book, and keep splitting in half until you find the number! Santa’s Sock

Binary Search Another Example: Can you find the number 38? 32

3250 Binary Search Another Example: Can you find the number 38?

32384450 Binary Search Another Example: Can you find the number 38? For the phonebook search, we could have had to do at worst 1000 tries for the 1000 pages, but with Binary search, we could have found the number in no more than 10 tries using a formula. (log_2[n]= ans) where 2^ans = n

Now we can find what we are looking for