Download presentation
Presentation is loading. Please wait.
Published byWinfred Thomas Modified over 9 years ago
1
Searching Algorithms Finding what you are looking for.
2
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!
3
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?
4
Types of Searching Algorithms 1. Linear Search 2. Tree Search 3. Binary Search
5
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 Λ.
6
Search Item: Anie Index (box): 1 BukkyJessiAnieLauren 1234 Does box 1 = Anie? Linear Search
7
Search Item: Anie Index: 2 BukkyJessiAnieLauren 1234 Does box 2 = Anie? Linear Search
8
Search Item: Anie Index: 3 BukkyJessiAnieLauren 1234 Does box 3 = Anie? YES! Linear Search
9
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”
10
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
11
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?
12
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?
13
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
14
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?
15
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?
16
Tree Search A B C G E F DH Result: A B E G F C Depth First Search
17
Tree Search A B C G E F DH Result: A B E G F C H Depth First Search
18
Tree Search Result: A B E G F C H D A B C G E F DH Depth First Search
19
So the result for this one would be... Result: 1 2 3 4 5 6 7 8 9 10 11 12
20
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
21
Tree Search Breadth First Search A B C G E F DH Result: A B Tree Search A B C G E F DH
22
Breadth First Search A B C G E F DH Result: A B D Tree Search A B C G E F DH
23
Breadth First Search A B C G E F DH Tree Search A B C G E F DH Result: A B D G
24
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
25
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
26
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
27
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
28
So the result for this one would be... Result: 1 2 3 4 5 6 7 8 9 10 11 12
29
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
30
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”
31
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
32
Binary Search Another Example: Can you find the number 38? 32
33
3250 Binary Search Another Example: Can you find the number 38?
34
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
35
Now we can find what we are looking for
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.