Space-time tradeoffs - PowerPoint PPT Presentation

About This Presentation
Title:

Space-time tradeoffs

Description:

Sorting by Counting Algorithm ... 1980 Another algorithm proposed by Rabin and Karp virtually always runs in time proportional to n+m and has the ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 22
Provided by: JOHNL209
Category:

less

Transcript and Presenter's Notes

Title: Space-time tradeoffs


1
Space-time tradeoffs
  • For many problems some extra space really pays
    off (extra space in tables - breathing room)
  • input enhancement
  • non comparison-based sorting
  • auxiliary tables (shift tables for pattern
    matching)
  • prestructuring
  • hashing
  • indexing schemes (eg, B-trees)
  • tables of information that do all the work
  • dynamic programming

2
Sorting by Counting
  • Algorithm ComparisonCountingSort(A0..n-1)
  • for i ? 0 to n-1 do Counti?0
  • for i ? 0 to n-2 do
  • for j ? i1 to n-1 do
  • if Ai ltAj then Countj ? Countj1
  • else Counti ? Counti1
  • for i ? 0 to n-1 do SCounti ? Ai
  • Example 62 31 84 96 19 47
  • Efficiency

3
Sorting by Counting (2)
  • Algorithm DistributionCountingSort(A0..n-1)
  • for j ? 0 to u-l do Dj?0
  • for i ? 0 to n-1 do DAi-l ? DAi-l 1
  • for j ? 1 to u-l do Dj ? Dj-1Dj
  • for i ? n-1 down to 0 do
  • j ? Ai-l SDj-1 ? Ai Dj ? Dj-1
  • Example 13 11 12 13 12 12
  • Efficiency

4
String matching
  • pattern a string of m characters to search for
  • text a (long) string of n characters to search
    in
  • Brute force algorithm
  • Align pattern at beginning of text
  • moving from left to right, compare each character
    of pattern to the corresponding character in text
    until
  • all characters are found to match (successful
    search) or
  • a mismatch is detected
  • while pattern is not found and the text is not
    yet exhausted, realign pattern one position to
    the right and repeat step 2.

5
String searching - History
  • 1970 Cook shows (using finite-state machines)
    that problem can be solved in time proportional
    to nm
  • 1976 Knuth and Pratt find algorithm based on
    Cooks idea Morris independently discovers same
    algorithm in attempt to avoid backing up over
    text
  • At about the same time Boyer and Moore find an
    algorithm that examines only a fraction of the
    text in most cases (by comparing characters in
    pattern and text from right to left, instead of
    left to right)
  • 1980 Another algorithm proposed by Rabin and Karp
    virtually always runs in time proportional to nm
    and has the advantage of extending easily to
    two-dimensional pattern matching and being almost
    as simple as the brute-force method.

6
Horspools Algorithm
  • A simplified version of Boyer-Moore algorithm
    that retains key insights
  • compare pattern characters to text from right to
    left
  • given a pattern, create a shift table that
    determines how much to shift the pattern when a
    mismatch occurs (input enhancement)

7
How far to shift?
  • Look at first (rightmost) character in text that
    was compared. Three cases
  • The character is not in the pattern
  • .....c...................... (c not in
    pattern)
  • BAOBAB
  • The character is in the pattern (but not at
    rightmost position)
  • .....O...................... (O occurs once
    in pattern)
  • BAOBAB
  • .....A...................... (A occurs twice
    in pattern)
  • BAOBAB
  • The rightmost characters produced a match
  • .....B......................
  • BAOBAB
  • Shift Table Stores number of characters to shift
    by depending on first character compared

8
Shift table
  • Constructed by scanning pattern before search
    begins
  • All entries are initialized to length of pattern.
  • For c occurring in pattern, update table entry to
    distance of rightmost occurrence of c from end of
    pattern
  • Algorithm ShiftTable(P0..m-1)
  • for i?0 to size-1 do Tablei?m
  • for j?0 to m-2 do TablePj?m-1-j
  • return Table

9
Shift table
  • Example for pattern BAOBAB
  • Then

10
The Algorithm
  • Horspool Matching(P0..m-1,T0..n-1)
  • ShiftTable(P0..m-1)
  • i ? m-1
  • while iltn-1 do
  • k?0
  • while kltm-1 and Pm-1-kTi-k do
  • k ? k1
  • if km return i-m1
  • else i ? iTableTi
  • return -1

11
Boyer-Moore algorithm
  • Based on same two ideas
  • compare pattern characters to text from right to
    left
  • given a pattern, create a shift table that
    determines how much to shift the pattern when a
    mismatch occurs (input enhancement)
  • Uses additional shift table with same idea
    applied to the number of matched characters

12
The bad-symbol shift
  • Based on the Horspool idea of using the extra
    table
  • However, this table is computed differently
  • If c, the text character corresponding to the
    last pattern character, is not in the pattern,
    then shift in the same way by m characters
    (actually c is a bad symbol)
  • If the mismatching character (the bad symbol)
    does not appear in the pattern, then shift to
    overpass it
  • If the mismathcing character (the bad symbol)
    appears in the pattern, then shift to align the
    bad symbol to the same text character (lying to
    the left of the mismatching position).

13
The bad-symbol shift - example
  • The bad symbol IS NOT in the pattern
  • ...SER......................
  • BARBER
  • BARBER shift 4 positions
  • The bad symbol IS in the pattern
  • ...AER......................
  • BARBER
  • BARBER shift 2 positions,
  • This shift is given by dmaxt1(c)-k,1, where
    t1 is the Horspool table, k the distance between
    the bad symbol from the end of the pattern

14
The good-suffix shift - example
  • What happens if a matched suffix appears again in
    the pattern (eg. ABRACADABRA)
  • Important to find another suffix with a different
    previous character. Calculate the shift as the
    distance between two occurrences of the suffix.
  • Also, important to find the longest prefix of
    size lltk that matches the suffix of size l.
    Calculate the shift as the distance between the
    suffix and the prefix.

15
The good-suffix shift example (2)
K pattern d2
1 ABCBAB 2
2 ABCBAB 4
3 ABCBAB 4
4 ABCBAB 4
5 ABCBAB 4
K pattern d2
1 BAOBAB 2
2 BAOBAB 5
3 BAOBAB 5
4 BAOBAB 5
5 BAOBAB 5
16
Final rule for Boyer-Moore
  • Calculate shift as
  • d1 if k0
  • d
  • max(d1,d2) if kgt0
  • where d1max(t1(c)-k)
  • Example
  • BESS_KNEW_ABOUT_BAOBABS
  • BAOBAB


17
Hashing
  • A very efficient method for implementing a
    dictionary, i.e., a set with the operations
  • insert
  • find
  • delete
  • Applications
  • databases
  • symbol tables

18
Hash tables and hash functions
  • Hash table an array with indices that correspond
    to buckets
  • Hash function determines the bucket for each
    record
  • Example student records, keySSN. Hash
    function
  • h(k) k mod m
  • (k is a key and m is the number of buckets)
  • if m1000, where is record with SSN 315-17-4251
    stored?
  • Hash function must
  • be easy to compute
  • distribute keys evenly throughout the table

19
Collisions
  • If h(k1) h(k2) then there is a collision.
  • Good hash functions result in fewer collisions.
  • Collisions can never be completely eliminated.
  • Two types handle collisions differently
  • Open hashing - bucket points to linked list of
    all keys hashing to it.
  • Closed hashing one key per bucket, in case of
    collision, find another bucket for one of the
    keys
  • linear probing use next bucket
  • double hashing use second hash function to
    compute increment

20
Open hashing
  • If hash function distributes keys uniformly,
    average length of linked list will be n/m
  • Average number of probes S 1a/2, U a
  • Worst-case is still linear!
  • Open hashing still works if ngtm.

21
Closed hashing
  • Does not work if ngtm.
  • Avoids pointers.
  • Deletions are not straightforward.
  • Number of probes to insert/find/delete a key
    depends on load factor a n/m (hash table
    density)
  • successful search (½) (1 1/(1- a))
  • unsuccessful search (½) (1 1/(1- a)²)
  • As the table gets filled (a approaches 1),
    number of probes increases dramatically
Write a Comment
User Comments (0)
About PowerShow.com