Demo Search Final

download Demo Search Final

of 36

Transcript of Demo Search Final

  • 8/7/2019 Demo Search Final

    1/36

    Searching Algorithms

    Sequential Search

    Binary Search

    Binary Search Tree (BST)

  • 8/7/2019 Demo Search Final

    2/36

    2

    The Problem

    Fig. 1 Searching is an every day

    occurrence.

  • 8/7/2019 Demo Search Final

    3/36

    Searching Arrays

    Searching is the process of looking for a specific element in an array;

    for example, discovering whether a certain score is included in a list of

    scores. Searching, like sorting, is a common task in computer

    programming. There are many algorithms and data structures devoted

    to searching.

    3

  • 8/7/2019 Demo Search Final

    4/36

    Linear Search

    The linear search approach compares the key element, key, with each

    element in the array list[]. The method continues to do so until the key

    matches an element in the list or the list is exhausted without a match

    being found. If a match is made, the linear search returns the index of

    the element in the array that matches the key. If no match is found,the search returns -1.

    4

  • 8/7/2019 Demo Search Final

    5/36

    Suppose that you want to determine whether 27 is in the list

    This search is successful!

    Searching Algorithms (Contd)

    5

  • 8/7/2019 Demo Search Final

    6/36

    Lets now search for 10

    This is an unsuccessful search

    Searching Algorithms (Contd)

    6

  • 8/7/2019 Demo Search Final

    7/36

    Sequential Search(Linear Search)

    void main() {

    int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30};int x, n, index;

    cout > x;index = -1;for(n=0; n

  • 8/7/2019 Demo Search Final

    8/36

    Searching

    Linear Search is not very efficient.In the worst case, the target we are searching couldbe placed at the very end of the array, requiringthat we search every single element in the array.On average, the target might be somewhere in the

    middle of the array, requiring that we search half ofall the elements in the array.For example, if we had a 1000 element array: worst case: we must search 1000 elements

    on average: we search about 500 elements

    8

  • 8/7/2019 Demo Search Final

    9/36

    Binary Search

    Binary Search is a more efficient option forsearching arrays.There are two tricks to using Binary Search: First, the array must be sorted. (Before you use

    Binary Search, you might first sort the array.)

    Second, Binary Search works by dividing thesearch area in half after each comparison (moreon this soon.)

    Binary Search is used very commonly in computerscience, and there is a related concept called Binary

    Search Trees. If you take an Algorithms course, you will

    definitely spend time researching Binary SearchTrees.

    9

  • 8/7/2019 Demo Search Final

    10/36

    Binary Search Pseudo Code

    1. Create a sorted array of sample data.2. Prompt user for a search target.3. Locate the middle of the array.4. Compare the middle element with the search

    target:a) If the search key is equal to the middle

    element, we have a match! Exit Loopb) If the search key is less than the middle

    element, search the first half of the array(back to Step 4)

    c) If the search key is larger than the middle

    element, search the second half of the array(back to Step 4)

    10

  • 8/7/2019 Demo Search Final

    11/36

    Binary Search Efficiency

    Binary Search is very efficient.

    For example, if you have 1024 elements in you array,in the worst case, binary search only requires 10comparisons. Linear Search Worst Case: 1024 Binary Search Worst Case: 10

    If you have 1 billion elements, binary search onlyrequires 30 comparisons! Linear Search Worst Case: 1 billion Binary Search Worst Case: 30 !

    11

  • 8/7/2019 Demo Search Final

    12/36

    821 3 4 65 7 109 11 12 1413 15

    641413 25 33 5143 53 8472 93 95 97966

    Binary Search

    low

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    hi

    12

  • 8/7/2019 Demo Search Final

    13/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    821 3 4 65 7 109 11 12 1413 15

    641413 25 33 5143 53 8472 93 95 97966

    low himid

    13

  • 8/7/2019 Demo Search Final

    14/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low hi

    821 3 4 65 7 109 11 12 1413 15

    14

  • 8/7/2019 Demo Search Final

    15/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low mid hi

    821 3 4 65 7 109 11 12 1413 15

    15

  • 8/7/2019 Demo Search Final

    16/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low hi

    821 3 4 65 7 109 11 12 1413 15

    16

  • 8/7/2019 Demo Search Final

    17/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low himid

    821 3 4 65 7 109 11 12 1413 15

    17

  • 8/7/2019 Demo Search Final

    18/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low

    hi

    821 3 4 65 7 109 11 12 1413 15

    18

  • 8/7/2019 Demo Search Final

    19/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low

    hi

    mid

    821 3 4 65 7 109 11 12 1413 15

    19

  • 8/7/2019 Demo Search Final

    20/36

    Binary Search

    Binary search. Given value and sorted array a[], find index i

    such that a[i] = value, or report that no such index exists.

    Invariant. Algorithm maintains a[lo] e value e a[hi].

    Ex. Binary search for 33.

    641413 25 33 5143 53 8472 93 95 97966

    low

    hi

    mid

    821 3 4 65 7 109 11 12 1413 15

    20

  • 8/7/2019 Demo Search Final

    21/36

    Binary Search

    void main() {int A[8] = {1, 5, 6, 7, 9, 10, 17, 30};int x, lower, middle, upper;

    cout > x;low = 0;

    hi = 7; // array size - 1mid = (low + hi)/2;while(A[mid]!=x && low

  • 8/7/2019 Demo Search Final

    22/36

    Binary Trees, Binary Search Trees

  • 8/7/2019 Demo Search Final

    23/36

    Binary Trees

    A tree in which no node can have more than twochildren

    The depth of an average binary tree is considerably smallerthan N, even though in the worst case, the depth can be as largeas N 1.

    Generic

    binary tree

    Worst-case

    binary tree

    23

  • 8/7/2019 Demo Search Final

    24/36

    Node Struct of Binary Tree

    Possible operations on the Binary Tree ADT

    Parent, left_child, right_child, sibling, root, etcImplementation

    Because a binary tree has at most two children, we can keep direct

    pointers to them

    24

  • 8/7/2019 Demo Search Final

    25/36

    Binary Search Trees (BST)

    A data structure for efficient searching, inser-tion and deletion

    Binary search tree property

    For every node X

    All the keys in its left

    subtree are smaller than

    the key value in X

    All the keys in its right

    subtree are larger than the

    key value in X

    25

  • 8/7/2019 Demo Search Final

    26/36

    Binary Search Trees

    A binary search tree Not a binary search tree

    26

  • 8/7/2019 Demo Search Final

    27/36

    Binary Search Trees

    Average depth of a node is O(log N)Maximum depth of a node is O(N)

    The same set of keys may have different BSTs

    27

  • 8/7/2019 Demo Search Final

    28/36

    Searching BST

    If we are searching for 15, then we are done.

    If we are searching for a key < 15, then we should search in the left

    subtree.

    If we are searching for a key > 15, then we should search in the right

    subtree.

    28

  • 8/7/2019 Demo Search Final

    29/36

    29

  • 8/7/2019 Demo Search Final

    30/36

    Searching (Find)

    Find X: return a pointer to the node that has key X, or NULL if there is

    no such node

    Time complexity: O(height of the tree)

    BinaryNode * BinarySearchTree::Find(const float &x,

    BinaryNode *t) const

    {

    if (t == NULL)return NULL;

    else if (x < t->element)

    return Find(x, t->left);

    else if (t->element < x)

    return Find(x, t->right);

    else

    return t; // match

    }

    30

  • 8/7/2019 Demo Search Final

    31/36

    findMin/ findMax

    Goal: return the node containing the smallest (largest) key in the tree

    Algorithm: Start at the root and go left (right) as long as there is a left(right) child. The stopping point is the smallest (largest) element

    Time complexity = O(height of the tree)

    BinaryNode * BinarySearchTree::FindMin(BinaryNode *t) const

    {

    if (t == NULL)

    return NULL;

    if (t->left == NULL)return t;

    return FindMin(t->left);

    }

    31

  • 8/7/2019 Demo Search Final

    32/36

    Insertion

    Proceed down the tree as you would with a findIf X is found, do nothing (or update something)

    Otherwise, insert X at the last spot on the path traversed

    Time complexity = O(height of the tree)

    32

  • 8/7/2019 Demo Search Final

    33/36

    Deletion

    When we delete a node, we need to consider how we take care of the

    children of the deleted node. This has to be done such that the property of the search tree is

    maintained.

    33

  • 8/7/2019 Demo Search Final

    34/36

    Deletion under Different Cases

    Case 1: the node is a leaf

    Delete it immediately

    Case 2: the node has one child

    Adjust a pointer from the parent to bypass that node

    34

  • 8/7/2019 Demo Search Final

    35/36

    Deletion Case 3

    Case 3: the node has 2 children

    Replace the key of that node with the minimum element at the rightsubtree Delete that minimum element

    Has either no child or only right child because if it has a leftchild, that left child would be smaller and would have beenchosen. So invoke case 1 or 2.

    Time complexity = O(height of the tree)

    35

  • 8/7/2019 Demo Search Final

    36/36

    Q&A

    36