Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman

Similar presentations


Presentation on theme: "Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman"— Presentation transcript:

1 Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman (jgain,sonia@cs.uct.ac.za)

2 Objectives To introduce the mind set of trading space for time To show a variety of tradeoff solutions: Sorting by Counting Input Enhancement for String Matching To discuss the strengths and weaknesses of space and time tradeoffs

3 Space-Time Tradeoffs For many problems some extra space really pays off cf. log tables, stats tables Similarly, electronic computers can preprocess inputs and store additional info to accelerate the problem Input Enhancement Sorting by Counting Horspool’s and Boyer-Moore’s Algorithms for String Matching Prestructuring Hashing B-trees Dynamic Programming

4 Sorting by Counting elements in [ L..U] Constraint: we cannot overwrite the original list Distribution Counting: compute the frequency of each element Algorithm: for j  0 to U-L do D[ j ]  0 // init frequencies for i  0 to n-1 do D[ A[i]-L ]  D[ A[i] – L ] + 1 // compute frequencies for j  1 to U-L do D[ j ]  D[ j-1 ] + D[ j ] // reuse for distribution for i  n-1 downto 0 do j  A[ I ] - l S[ D[j] – 1 ]  A[i] D[j]  D[j] - 1 return S

5 Sorting by Counting Example: A = A[5] = 12 A[4] = 12 A[3] = 13 A[2] = 12 A[1] = 11 A[0] = 13 Efficiency:  (n) Best so far but only for specific types of input 1311121312 Array Values111213 Frequencies132 Distribution146 D[0]D[1]D[2] 1 4 6 1 3 6 12 6 1 2 5 1 15 01 5 S[0]S[1]S[2]S[3]S[4]S[5] 12 13 12 11 13

6 Reminder: String Matching Pattern: m characters to search for Text: n characters to search in Brute force algorithm: 1.Align pattern at beginning of text 2. Moving 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 3.While pattern is not found and the text is not yet exhausted, realign pattern one position to the right and repeat step 2.

7 Horspool’s Algorithm As before, start the pattern at the left and keep moving it rightwards until found/mismatch But, at each position, compare pattern characters to text characters from right to left Use shift table to determine how far to shift up when mismatch occurs Shift table: Has a shift value for each possible character input enhancement

8 How far to shift a pattern of length m? look to last character C in mismatched text segment C is not in the pattern  shift by m.....D...................... ( D not in pattern) BAOBAB  BAOBAB C is in the first m-1 positions of the pattern  align rightmost occurrence in pattern with text.....O...................... ( O occurs once in pattern) BAOBAB  BAOBAB Another:.....A...................... (A occurs 2ce in pattern) BAOBAB  BAOBAB (right).....A...................... (A occurs 2ce in pattern) BAOBAB  BAOBAB (wrong!)

9 How far to shift a pattern of length m? look to last character C in mismatched text segment C is not in the pattern  shift by m.....D...................... ( D not in pattern) BAOBAB  BAOBAB C is in the first m-1 positions of the pattern  align rightmost occurrence in pattern with text.....O...................... ( O occurs once in pattern) BAOBAB  BAOBAB Another:.....A...................... BAOBAB  BAOBAB.BAOBAB..................... BAOBAB  BAOBAB

10 How far to shift a pattern of length m? look to last character C in mismatched text segment C is not in the pattern  shift by m.....D...................... ( D not in pattern) BAOBAB  BAOBAB C is in the first m-1 positions of the pattern  align rightmost occurrence in pattern with text.....O...................... ( O occurs once in pattern) BAOBAB  BAOBAB Why don’t we include in the last position?......B...................... CONFLAB  CONFLAB.....B...................... BAOBAB  BAOBAB

11 Shift Table Stores number of characters to shift by depending on first character compared (i.e. rightmost) Construct shift table before searching starts Indexed by the alphabet of text All entries are initialized to pattern length. Eg, BAOBAB: For c occurring in pattern, update table entry to distance of rightmost occurrence of c from end of pattern should we do this L  R or R  L ? A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 6 6 6 6 6 6 6 6 6 6 6 6 6

12 Horspool’s Algorithm 1.Construct the Shift Table 2.Align the pattern against the beginning of the text 3.Repeat until a match is found or the pattern reaches beyond the text: Starting from the pattern end, compare corresponding characters until all m are matched (success!) or a mismatch is found On a mismatch, retrieve the shift table entry T(c), where c is the text character aligned with the end of the pattern. Shift the pattern right by T(c)

13 Example: Horspool’s Algorithm Pattern: ZIGZAG Text: A ZIG, A ZAG, AGAIN A ZIGZAG Shift Table: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 6 6 6 6 6 3 6 4 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 2

14 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 Except: Uses an additional good-suffix shift table with same idea applied to the number of matched characters Efficiency: Horspool’s approach is simpler and at least as efficient in the average case

15 Boyer-Moore Algorithm Good-suffix shift: If k symbols matched before failing, shift the pattern up G(k) positions Bad-symbol shift: If k symbols matched before failing, shift the pattern up T(c) – k positions (where c is the character that didn’t match) actually shift = max ( T(c) – k, 1) Algorithm: 1.Build tables T and G 2.When searching, use either bad-symbol shift or good-suffix shift, whichever is larger

16 Example good-suffix tables E.g. 1 Pattern: BIGWIG E.g. 2 Pattern: BAOBAB k= 1 k= 2 k= 3 k= 4 k= 5 3 3 6 6 6 k= 1 k= 2 k= 3 k= 4 k= 5 25555

17 Find BAOBAB in: BESS KNEW ABOUT BAOBABS Bad-Symbol Shift Table: Good-Suffix Shift Table: AB…O … 1 2 63 6 k= 1 k= 2 k= 3 k= 4 k= 5 25555

18 Find ZIGZAG in: A ZIG, A ZAG, AGAIN A ZIGZAG Bad-Symbol Shift Table: Good-Suffix Shift Table: A…G…I… Z 1 6 3 6 4 6 2 k= 1 k= 2 k= 3 k= 4 k= 5 36666

19 Strengths and Weaknesses of Space-Time Tradeoffs Strengths: Suited to amortised situations Particularly effective in accelerating access to data  Weaknesses: Can be space expensive and this might have empirical effects on speed


Download ppt "Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman"

Similar presentations


Ads by Google