Docsity

Design and Analysis of Algorithms - Chapter 7: Space-Time Tradeoffs, Study notes of Algorithms and Programming

Toggle sidebar
bg1
1
Design and Analysis of Algorithms Chapter 7
Design and Analysis of Algorithms -Chapter 7 1
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
Space-Time Tradeoffs
Dr. Steve Goddard

Design and Analysis of Algorithms -Chapter 7 2
Giving credit where credit is due:
•Most of the lecture notes are based on the slides from
the Textbook’s companion website
–http://www.aw.com/cssuport/
•Some examples and slides are based on lecture notes
created by Dr. Ben Choi, Louisiana Technical University
and Dr. Chuck Cusack, UNL
•I have modified many of their slides and added new
slides.

Design and Analysis of Algorithms -Chapter 7 3

For many problems some extra space really pays off:
extra space in tables (breathing room?)
•hashing
•non comparison-based sorting
input enhancement
•indexing schemes (eg, B-trees)
•auxiliary tables (shift tables for pattern matching)
tables of information that do all the work
•dynamic programming
Design and Analysis of Algorithms -Chapter 7 4

pattern: a string of mcharacters to search for
text: a (long) string of ncharacters to search in
Brute 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.
Design and Analysis of Algorithms -Chapter 7 5
 
1970: Cook shows (using finite-state machines) that problem can be
solved in time proportional to n+m
1976 Knuth and Pratt find algorithm based on Cook’s 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 n+mand has the advantage of extending
easily to two-dimensional pattern matching and being almost as simple
as the brute-force method.
Design and Analysis of Algorithms -Chapter 7 6
! 
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)
pf3
pf4

Partial preview of the text

Download Design and Analysis of Algorithms - Chapter 7: Space-Time Tradeoffs and more Study notes Algorithms and Programming in PDF only on Docsity!

Design and Analysis of Algorithms - Chapter 7 1

http://www.cse.unl.edu/~goddard/Courses/CSCE310J

Space-Time Tradeoffs

Dr. Steve Goddard

[email protected]

Design and Analysis of Algorithms - Chapter 7 2

 Giving credit where credit is due:

  • Most of the lecture notes are based on the slides from

the Textbook’s companion website

  • http://www.aw.com/cssuport/
  • Some examples and slides are based on lecture notes

created by Dr. Ben Choi, Louisiana Technical University

and Dr. Chuck Cusack, UNL

  • I have modified many of their slides and added new

slides.

Design and Analysis of Algorithms - Chapter 7 3

For many problems some extra space really pays off:

 extra space in tables (breathing room?)

  • hashing
  • non comparison-based sorting

 input enhancement

  • indexing schemes (eg, B-trees)
  • auxiliary tables (shift tables for pattern matching)

 tables of information that do all the work

  • dynamic programming

Design and Analysis of Algorithms - Chapter 7 4

 pattern : a string of m characters to search for

 text : a (long) string of n characters to search in

 Brute 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.

Design and Analysis of Algorithms - Chapter 7 5

 1970: Cook shows (using finite-state machines) that problem can be solved in time proportional to n + m

 1976 Knuth and Pratt find algorithm based on Cook’s 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 n + m and has the advantage of extending easily to two-dimensional pattern matching and being almost as simple as the brute-force method.

Design and Analysis of Algorithms - Chapter 7 6

 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 )

Design and Analysis of Algorithms - Chapter 7 7

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

Design and Analysis of Algorithms - Chapter 7 8

 Constructed by scanning pattern before search begins

 Indexed by text and pattern alphabet

 All entries are initialized to length of pattern. Eg, BAOBAB:

 For c occurring in pattern, update table entry to distance

of rightmost occurrence of c from end of pattern

 We 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

Design and Analysis of Algorithms - Chapter 7 9

BARD LOVED BANANAS

BAOBAB

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

Design and Analysis of Algorithms - Chapter 7 10

 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

Design and Analysis of Algorithms - Chapter 7 11

 Dynamic set (e.g., dictionary) operations are required

by many applications:

  1. Insert
  2. Search
  3. Delete

 A Hash Table is an effective data structure that can

provide

  • Average time complexity of ΟΟΟΟ(1) for the basic operations
  • Worst case time complexity can be as bad as a linked list, ΟΟΟΟ( n ).

Design and Analysis of Algorithms - Chapter 7 12

 Imagine that we could assign a unique array index to every possible key that could occur in an application.

  • Locating, inserting, deleting elements could be done very easily and quickly.
  • However, the key space may be much too large to use an array in a real system.  A Hash Table is a generalization of an ordinary array that does not require one position for every possible key.
  • Advantageous when # of keys actually stored << # keys possible
  • Uses an array whose size is proportional to the # of keys stored  The key is not used as the index!  Instead, the array index is computed, by a hashing function, using the key.

Design and Analysis of Algorithms - Chapter 7 19

   " 

  

 Basic Operation: comparisons

  • Assume computing a hash code equals a unit of comparison
  • there are total of n elements stored in the table,
  • each elements is equally likely to be search  Average number of comparison for an unsuccessful search (including hashing) is Au( n ) = 1+ n / h = ΘΘΘΘ(1+αααα)  Average cost of a successful search
  • When key i = 1, …, n , was inserted at the end of a linked list, each linked list had average length given by (i – 1)/h
  • The expected number of key comparisons = 1 + comparisons made for inserting an element at the end of a linked list

1 1

= + ^ +−

  = = h h

n n n nh

i h nh

i n

An

n

i

n

i

s

Design and Analysis of Algorithms - Chapter 7 20

 How do we implement Insert?

 How do we implement Delete?

   " 

  

Design and Analysis of Algorithms - Chapter 7 21

  •   , .

 All elements are stored in the array of the hash table, rather than using linked lists to accommodate collisions

  • If the hash cell corresponding to the hash code is occupied by a different element,
  • then a sequence of alternative locations for the current element is defined (by rehashing )  Rehashing by linear probing
  • rehash( j ) = ( j +1) mod h
  • where j is the location most recently probed,
  • initially j = i , the hash code for k  Rehashing by double hashing
  • rehash( j , d ) = ( j + d ) mod h
  • e.g., d = hashIncr( k ) = (2 k + 1) mod h
  • computing an odd increment ensures that whole hash table is accessed in the search (provided h is a power of 2) Design and Analysis of Algorithms - Chapter 7 22
  •   " 

/  $

 hashCode: key

 Now insert 1812, hashCode(1812) = 4, i.e., i = 4

» h = 8, initially j = i = 4 » rehash( j ) = ( j +1) mod h » rehash(4) = (4+1) mod 8 = 5 // collision again » rehash(5) = (5+1) mod 8 = 6 // collision again » … put in 7

Design and Analysis of Algorithms - Chapter 7 23

0  1     -

  

 Retrieval procedure imitates the insertion procedure, stop

search as soon as an empty cell is encountered.

 Deletion of a key

  • Cannot simply delete the the key and label the cell empty. Why?
  • Need to label the cell “obsolete”

 How do we implement Insert?

Design and Analysis of Algorithms - Chapter 7 24

   " 

  •   

 Basic Operation: comparisons

  • Assume computing a hash code equals a unit of comparison
  • there are total of n elements stored in the table,
  • each elements is equally likely to be search
  • Again, αααα= n / h is the average number of keys per array index.
    • Note: (^) αα ≤αα≤≤≤ 1 for open addressing

 The average number of comparison for an insertion or an

unsuccessful search (including hashing) is at most 1/(1-αααα)

 The average number of comparison for a successful search

is at most

α α α

ln