Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman

Slides:



Advertisements
Similar presentations
Chapter 7 Space and Time Tradeoffs Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Advertisements

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin Introduction to the Design & Analysis of Algorithms, 2 nd ed., Ch. 1 Chapter 2.
Space-for-Time Tradeoffs
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Bar Ilan University And Georgia Tech Artistic Consultant: Aviya Amir.
Chapter 7 Space and Time Tradeoffs. Space-for-time tradeoffs Two varieties of space-for-time algorithms: b input enhancement — preprocess the input (or.
1 CSC 421: Algorithm Design & Analysis Spring 2013 Space vs. time  space/time tradeoffs  examples: heap sort, data structure redundancy, hashing  string.
Dept of Computer Science, University of Bristol. COMS Chapter 5.2 Slide 1 Chapter 5.2 String Searching - Part 2 Boyer-Moore Algorithm Rabin-Karp.
Design and Analysis of Algorithms - Chapter 71 Space-time tradeoffs For many problems some extra space really pays off (extra space in tables - breathing.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Boyer-Moore string search algorithm Book by Dan Gusfield: Algorithms on Strings, Trees and Sequences (1997) Original: Robert S. Boyer, J Strother Moore.
String Matching COMP171 Fall String matching 2 Pattern Matching * Given a text string T[0..n-1] and a pattern P[0..m-1], find all occurrences of.
Design and Analysis of Algorithms - Chapter 31 Brute Force A straightforward approach usually based on problem statement and definitions Examples: 1. Computing.
Algorithms and Efficiency of Algorithms February 4th.
Designing Algorithms Csci 107 Lecture 4.
Chapter 7 Space and Time Tradeoffs Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Homework page 102 questions 1, 4, and 10 page 106 questions 4 and 5 page 111 question 1 page 119 question 9.
Pattern Matching COMP171 Spring Pattern Matching / Slide 2 Pattern Matching * Given a text string T[0..n-1] and a pattern P[0..m-1], find all occurrences.
Raita Algorithm T. RAITA Advisor: Prof. R. C. T. Lee
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Exact Matching Charles Yan Na ï ve Method Input: P: pattern; T: Text Output: Occurrences of P in T Algorithm Naive Align P with the left end.
A Fast Algorithm for Multi-Pattern Searching Sun Wu, Udi Manber May 1994.
1 Pattern Matching Using n-grams With Algebraic Signatures Witold Litwin[1], Riad Mokadem1, Philippe Rigaux1 & Thomas Schwarz[2] [1] Université Paris Dauphine.
String Matching. Problem is to find if a pattern P[1..m] occurs within text T[1..n] Simple solution: Naïve String Matching –Match each position in the.
Introduction n – length of text, m – length of search pattern string Generally suffix tree construction takes O(n) time, O(n) space and searching takes.
Advanced Algorithm Design and Analysis (Lecture 3) SW5 fall 2004 Simonas Šaltenis E1-215b
MA/CSSE 473 Day 24 Student questions Quadratic probing proof
Theory of Algorithms: Brute Force. Outline Examples Brute-Force String Matching Closest-Pair Convex-Hull Exhaustive Search brute-force strengths and weaknesses.
Theory of Algorithms: Space and Time Tradeoffs James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Application: String Matching By Rong Ge COSC3100
MA/CSSE 473 Day 27 Hash table review Intro to string searching.
MA/CSSE 473 Day 23 Student questions Space-time tradeoffs Hash tables review String search algorithms intro.
Design and Analysis of Algorithms - Chapter 71 Space-time tradeoffs For many problems some extra space really pays off: b extra space in tables (breathing.
String Searching CSCI 2720 Spring 2007 Eileen Kraemer.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
CPSC 252 Hashing Page 1 Hashing We have already seen that we can search for a key item in an array using either linear or binary search. It would be better.
String Matching By Joshua Yudaken. Terms Haystack A string in which to search Needle The string being searched for  find the needle in the haystack.
1 UNIT-I BRUTE FORCE ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 3:
Design and Analysis of Algorithms – Chapter 71 Space-Time Tradeoffs: String Matching Algorithms* Dr. Ying Lu RAIK 283: Data Structures.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
MA/CSSE 473 Day 25 Student questions Boyer-Moore.
CSC 421: Algorithm Design & Analysis
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
CSG523/ Desain dan Analisis Algoritma
MA/CSSE 473 Day 26 Student questions Boyer-Moore B Trees.
CSC 421: Algorithm Design & Analysis
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
13 Text Processing Hongfei Yan June 1, 2016.
String Processing.
CSCE350 Algorithms and Data Structure
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Space-for-time tradeoffs
Chapter 7 Space and Time Tradeoffs
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSC 421: Algorithm Design & Analysis
Space-for-time tradeoffs
3. Brute Force Selection sort Brute-Force string matching
Space-for-time tradeoffs
How to use hash tables to solve olympiad problems
3. Brute Force Selection sort Brute-Force string matching
CSC 380: Design and Analysis of Algorithms
String Processing.
Space-for-time tradeoffs
Space-for-time tradeoffs
Analysis and design of algorithm
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman

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

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

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

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 Array Values Frequencies132 Distribution146 D[0]D[1]D[2] S[0]S[1]S[2]S[3]S[4]S[5]

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.

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

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!)

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

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

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

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)

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

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

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

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

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

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

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