Theory of Algorithms: Space and Time Tradeoffs James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.

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
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
Bar Ilan University And Georgia Tech Artistic Consultant: Aviya Amir.
Theory of Algorithms: Brute Force James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August.
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.
1 A simple fast hybrid pattern- matching algorithm Department of Computer Science and Information Engineering National Cheng Kung University, Taiwan R.O.C.
Design and Analysis of Algorithms - Chapter 71 Space-time tradeoffs For many problems some extra space really pays off (extra space in tables - breathing.
Analysis & Design of Algorithms (CSCE 321)
Efficiency of Algorithms
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.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Boyer-Moore Algorithm 3 main ideas –right to left scan –bad character rule –good suffix rule.
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.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Chapter 3: The Efficiency of Algorithms
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
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.
Theory of Algorithms: Transform and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape.
Sorting HKOI Training Team (Advanced)
Chapter 7 Space and Time Tradeoffs James Gain & Sonia Berman
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
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.
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.
Strings and Pattern Matching Algorithms Pattern P[0..m-1] Text T[0..n-1] Brute Force Pattern Matching Algorithm BruteForceMatch(T,P): Input: Strings T.
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:
 Author: Ricardo A. Baeza-Yates, Gaston H. Gonnet  Publisher: 1992 Communications of the ACM  Presenter: Yuen-Shuo Li  Date: 2013/08/14 1.
Theory of Algorithms: Greedy Techniques James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
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
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
CSG523/ Desain dan Analisis Algoritma
MA/CSSE 473 Day 26 Student questions Boyer-Moore B Trees.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSCE350 Algorithms and Data Structure
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Space-for-time tradeoffs
Chapter 3: The Efficiency of Algorithms
Chapter 7 Space and Time Tradeoffs
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Space-for-time tradeoffs
3. Brute Force Selection sort Brute-Force string matching
Space-for-time tradeoffs
3. Brute Force Selection sort Brute-Force string matching
Space-for-time tradeoffs
Space-for-time tradeoffs
Analysis and design of algorithm
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

Theory of Algorithms: Space and Time Tradeoffs James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town August - October 2004

Objectives lTo introduce the mind set of trading space for time lTo show a variety of transform-and-conquer solutions:  Sorting by Counting  Input Enhancement for String Matching lTo discuss the strengths and weaknesses of space and time tradeoffs

Space-Time Tradeoffs lFor many problems some extra space really pays off lOriginally human ‘computers’ would precompute a function’s values and record the results in volumes of mathematical tables lSimilarly, electronic computers can preprocess inputs and store additional info to accelerate the problem afterwards lInput Enhancement: store additional input information  Sorting by Counting  Horspool’s and Boyer-Moore’s Algorithms for String Matching lPrestructuring: use extra space for faster data access  Hashing  B-trees lDynamic Programming: record solutions to overlapping subproblems

Sorting by Counting lAssume elements to be sorted belong to a known set of small values between l and u, with potential duplication lConstraint: we cannot overwrite the original list lDistribution Counting: compute the frequency of each element and later the accumulate sum of frequencies (distribution) lAlgorithm: 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

Notes on Sorting by Counting lExample: A = A[5] = 12 A[4] = 12 A[3] = 13 A[2] = 12 A[1] = 11 A[0] = 13 lEfficiency:  (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 lPattern: a string of m characters to search for lText: a (long) string of n characters to search in lBrute force algorithm: 1.Align pattern at beginning of text 2.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 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 lAccording to Cook [1970] problem can be solved in time proportional to n+m lHorspool’s Algorithm: a simplified version of Boyer-Moore algorithm that retains key insights lCompare pattern characters to text from right to left lGiven a pattern, create a shift table:  Has a shift value for each possible character  Determines how much to shift the pattern when a mismatch occurs (input enhancement) lNote: the pattern is still shifted left to right. Only comparison is right to left

How Far to Shift? l Three cases (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 pattern (but not at last position)  align rightmost occurrence in pattern with text.....O ( O occurs once in pattern) BAOBAB  BAOBAB  C produced a match  shift by m OR align to rightmost occurrence before last.....B BAOBAB  BAOBAB

Shift Table lStores number of characters to shift by depending on first character compared lConstruct by scanning pattern before searching starts lIndexed by the alphabet of text and pattern 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 lWe can do this by processing pattern from L  R 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 for a given pattern and alphabet 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 lPattern: ZIGZAG lText: A ZIG, A ZAG, AGAIN A ZIGZAG lShift Table: lExercise: Simulate the execution of Horspool’s Algorithm 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 lBased 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 lExcept:  Uses an additional good-suffix shift table with same idea applied to the number of matched characters lEfficiency:  Horspool’s approach is simpler and at least as efficient in the average case

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