10 Searching[1]

Embed Size (px)

Citation preview

  • 8/9/2019 10 Searching[1]

    1/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Data Structures and

    Algorithms for InformationProcessing

    (ecture "0: Searching II

  • 8/9/2019 10 Searching[1]

    2/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    )utline

    * )ne more )+A scheme , )rdered

    ashing ./ough Schoolo

    prolem1

    * Analsis of hashing algorithms

    * Some practical considerations

    * &adi searching

  • 8/9/2019 10 Searching[1]

    3/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    )pen 's% Chained ashing

    * o ig should the tale e4* )pen addressing can e incon'enient hen

    the numer of insertions and deletions isunpredictale - o'erflo%

    * Simple solution to o'erflo: &esi5e .doule1tale# rehashing e'erthing into the ne tale

    * 6se nuth8s approach and doule hashing to

    a'oid clustering%

  • 8/9/2019 10 Searching[1]

    4/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    ariant: )rdered ashing

    * In linear proing# e stop search hen

    e find an empt cell or a record ith a

    e e;ual to the search e

    * In ordered hashing e stop hen e

    find a e less than or e;ual to the

    search e .tough schoolo hashing1

  • 8/9/2019 10 Searching[1]

    5/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    /ough Schoolo hashing

    * "3 chairs in the classroom

    *

  • 8/9/2019 10 Searching[1]

    6/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Class in the morning

    * Inserts

    Don prefers 3 =umps 2

      >ill prefers ? =umps @

      Al prefers 3 =umps

      Boe prefers 3 =umps @

     

  • 8/9/2019 10 Searching[1]

    7/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 D)

    @ ? 7

    9 "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    8/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 D)

    @ ? >I(( 7

    9 "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    9/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 D)

    Al can8t sit

    hereEE

    @ ? >I(( 7

    9 "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    10/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 D)

    @ ? >I(( 7

    9 Al "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    11/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 D)

    Boe ics

    out Don

    @ ? >I(( 7

    9 Al "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    12/40

    90-723: Data Structuresand Algorithms for

    Information ProcessingCopright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 Boe

    @ ? Donicsout >ill

    7

    9 Al "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    13/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 3 Boe

    @ ? Don 7

    9 Al and>ill argueand Al gets

    iced out

    "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    14/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    0 " 2 A( 3 Boe

    @ ? Don 7

    9 >ill "0 ""

    "2

  • 8/9/2019 10 Searching[1]

    15/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Searching the classroom

    * Search for Don# >ill# Al# and Boe

    * Search for en ho prefers 3 and =umps"

  • 8/9/2019 10 Searching[1]

    16/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    ariant: )rdered ashing

    * /his reduces the time of unsuccessful

    search to aout the same as successful

    search

    * 6seful for applications here e epect

    to ha'e a large numer of unsuccessful

    searches

  • 8/9/2019 10 Searching[1]

    17/40

  • 8/9/2019 10 Searching[1]

    18/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    /ime Analsis

    * )pen address hashing methods store

    records in a tale of si5e $% $ G

    * /he performance of the operations

    depends on the load factor

    alpha H +$

    * or chained hashing# alpha ma e

    greater than "%

  • 8/9/2019 10 Searching[1]

    19/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    (inear Proing

    * )pen address hashing ith linear proingre;uires# on a'erage:

    "+2 . " J "+."-alpha1K21 operations for

    an unsuccessful search"+2 . " J "+."-alpha11 operations for a

    successful search

    *

  • 8/9/2019 10 Searching[1]

    20/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Doule ashing

    * )pen address hashing ith doulehashing re;uires# on a'erage:

    "+."-alpha1 operations for an

    unsuccessful search-log."-alpha1+alpha operations for a

    successful search

    *

  • 8/9/2019 10 Searching[1]

    21/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Chained ashing

    * Chained hashing re;uires# on a'erage:

    "Jalpha operations for an unsuccessful

    search

    "Jalpha+2 operations for a successful

    search

    *

  • 8/9/2019 10 Searching[1]

    22/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    /ime Analsis

    * /hese formulas re;uire significantmathematical analsis# hich e on8tgo into%

  • 8/9/2019 10 Searching[1]

    23/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    alpha  (inear

    proing)penaddressing

    Chainedhashing

    0%? "%?0 "%39 "%2?

    0%A "%7? "%?3 "%30

    0%7 2%"7 "%72 "%3?

    0%D 3%00 2%0" "%@0

    0%9 ?%?0 2%?A "%@?

    "%0 F F "%?0

    A'erage umer of ProesSuccessful Search

  • 8/9/2019 10 Searching[1]

    24/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    &adi Searching

    * or man applications# es can ethought of as numers

    * Searching methods that tae ad'antage

    of digital properties of these es arecalled radix searches

    * &adi searches treat es as numers inase $ .the radix 1 and or ithindi'idual digits

  • 8/9/2019 10 Searching[1]

    25/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    &adi Searching

    * Pro'ide reasonale orst-caseperformance ithout complication ofalanced trees%

    * Pro'ide a to handle 'ariale lengthes%

    * >iased data can lead to degenerate datastructures ith ad performance%

  • 8/9/2019 10 Searching[1]

    26/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    /he Simplest &adi Search

    * Digital Search Trees F lie >S/s ut

    ranch according to the e8s its%

    * e comparison replaced function

    that accesses the e8s net it%

  • 8/9/2019 10 Searching[1]

    27/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Digital Search

  • 8/9/2019 10 Searching[1]

    28/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Digital Search

    * &e;uires ).log 1 comparisons ona'erage

    * &e;uires comparisons in the orst

    case for a tree uilt ith random -ites

  • 8/9/2019 10 Searching[1]

    29/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Digital Search

    * Prolem: At each node e mae a fulle comparison F this ma eepensi'e# e%g% 'er long es

    * Solution: store es onl at the lea'es#use radi epansion to do intermediatee comparisons

  • 8/9/2019 10 Searching[1]

    30/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    &adi /ries

    * 6sed for &etrie'al LsicM

    * Internal nodes used for ranching#eternal nodes used for final e

    comparison# and to store data

  • 8/9/2019 10 Searching[1]

    31/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    &adi /rie

  • 8/9/2019 10 Searching[1]

    32/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    &adi /ries

    * (eft sutree has all es hich ha'e 0

    for the leading it# right sutree has all

    es hich ha'e " for the leading it* An insert or search re;uires ).log 1 it

    comparisons in the a'erage case# and

    it comparisons in the orst case

  • 8/9/2019 10 Searching[1]

    33/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    &adi /ries

    * Prolem: lots of etra nodes for es that

    differ onl in lo order its .See & and S

    nodes in eample ao'e1

    * /his is addressed Patricia trees# hich allo NlooaheadO to the net rele'ant it

    * Practical Algorithm /o &etrie'e Information

    Coded In Alphanumeric .Patricia)

    * In the slides that follo the entire alphaet

    ould e included in the indees

  • 8/9/2019 10 Searching[1]

    34/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

     ++ Insert ord .see Dro5de and Simon , needs or1

    iH0

    pHroot

    Qhile not inserted  if .LiM HH R081 set end-of-ord marer in p to true

      else if .p%ptrsLLiMM HH null1 create leaf containing and put its

      address in p%ptrsLLiMM

      else if .refernce p%ptrsLLiMM refers to a leaf1

      T( H e in leaf p%ptrsLLiMM

      do create a non-leaf and put its address in p%ptrsLLiMM

      p H the ne non-leaf

      iJJ

      hile .LiM HH T(LiM1  create a leaf containing and put its address in p%ptrsLL--iMM

      if .end-of-ord reached1 set end-of-ord marer in p to true

      else create leaf containing T( and put address in p%ptrsLT(LiMM

      else p H p%ptrsLLiJJMM

  • 8/9/2019 10 Searching[1]

    35/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    Insert NA&AO 

    U A < I P &

    A&A

  • 8/9/2019 10 Searching[1]

    36/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    U A < I P &

    A&A

    U A < I P &

    PU A < I P &

    P

    Insert NA&

  • 8/9/2019 10 Searching[1]

    37/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    U A < I P &

    U A < I P &

    U A < I P &

    A&AA&

  • 8/9/2019 10 Searching[1]

    38/40

    A (

  • 8/9/2019 10 Searching[1]

    39/40

    90-723: Data Structures

    and Algorithms forInformation Processing

    Copright ! "999# Carnegie $ellon% All &ights &eser'ed%

    (ecture "0: Searching

    A (

    ADA$

    )

    V

    V

    I

    &

    <

    D A

    ()VV

  • 8/9/2019 10 Searching[1]

    40/40

    90-723: Data Structures

    d Al ith f ( t "0 S hi

    A (0 @

    I<? ?

    D0

    &0

    A0

    0

    ADA$

    ()VV