Upcoming SlideShare
×

# Chap09alg

383 views
331 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
383
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Chap09alg

1. 1. CHAPTER 9 Text Searching
2. 2. Algorithm 9.1.1 Simple Text Search This algorithm searches for an occurrence of a pattern p in a text t . It returns the smallest index i such that t [ i..i + m- 1] = p , or - 1 if no such index exists. Input Parameters: p , t Output Parameters: None simple _ text _ search ( p, t ) { m = p.length n = t.length i = 0 while ( i + m = n ) { j = 0 while ( t [ i + j ] == p [ j ]) { j = j + 1 if ( j = m ) return i } i = i + 1 } return - 1 }
3. 3. Algorithm 9.2.5 Rabin-Karp Search Input Parameters: p , t Output Parameters: None rabin _ karp _ search ( p, t ) { m = p.length n = t.length q = prime number larger than m r = 2 m- 1 mod q // computation of initial remainders f [0] = 0 pfinger = 0 for j = 0 to m- 1 { f [0] = 2 * f [0] + t [ j ] mod q pfinger = 2 * pfinger + p [ j ] mod q } ... This algorithm searches for an occurrence of a pattern p in a text t . It returns the smallest index i such that t [ i..i + m- 1] = p , or - 1 if no such index exists.
4. 4. Algorithm 9.2.5 continued ... i = 0 while ( i + m ≤ n ) { if ( f [ i ] == pfinger ) if ( t [ i..i + m- 1] == p ) // this comparison takes //time O(m) return i f [ i + 1] = 2 * ( f [ i ] - r * t [ i ]) + t [ i + m ] mod q i = i + 1 } return -1 }
5. 5. Algorithm 9.2.8 Monte Carlo Rabin-Karp Search This algorithm searches for occurrences of a pattern p in a text t . It prints out a list of indexes such that with high probability t [ i .. i + m − 1] = p for every index i on the list.
6. 6. Input Parameters: p, t Output Parameters: None mc_rabin_karp_search ( p , t ) { m = p . length n = t . length q = randomly chosen prime number less than mn 2 r = 2 m −1 mod q // computation of initial remainders f [0] = 0 pfinger = 0 for j = 0 to m- 1 { f [0] = 2 * f [0] + t [ j ] mod q pfinger = 2 * pfinger + p [ j ] mod q } i = 0 while ( i + m ≤ n ) { if ( f [ i ] == pfinger ) prinln (“Match at position” + i ) f [ i + 1] = 2 * ( f [ i ] - r * t [ i ]) + t [ i + m ] mod q i = i + 1 } }
7. 7. Algorithm 9.3.5 Knuth-Morris-Pratt Search This algorithm searches for an occurrence of a pattern p in a text t . It returns the smallest index i such that t [ i..i + m- 1] = p , or - 1 if no such index exists.
8. 8. Input Parameters: p, t Output Parameters: None knuth_morris_pratt_search(p, t) { m = p.length n = t.length knuth_morris_pratt_shift(p, shift) // compute array shift of shifts i = 0 j = 0 while ( i + m ≤ n ) { while ( t [ i + j ] == p [ j ]) { j = j + 1 if ( j ≥ m ) return i } i = i + shift [ j − 1] j = max ( j − shift [ j − 1], 0) } return −1 }
9. 9. Algorithm 9.3.8 Knuth-Morris-Pratt Shift Table This algorithm computes the shift table for a pattern p to be used in the Knuth-Morris-Pratt search algorithm. The value of shift [ k ] is the smallest s > 0 such that p [0.. k - s ] = p [ s .. k ].
10. 10. Input Parameter: p Output Parameter: shift knuth_morris_pratt_shift(p, shift) { m = p.length shift[-1] = 1 // if p[0] ≠ t[i] we shift by one position shift[0] = 1 // p[0..- 1] and p[1..0] are both // the empty string i = 1 j = 0 while (i + j < m) if (p[i + j] == p[j]) { shift[i + j] = i j = j + 1; } else { if (j == 0) shift[i] = i + 1 i = i + shift[j - 1] j = max(j - shift[j - 1], 0 ) } }
11. 11. Algorithm 9.4.1 Boyer-Moore Simple Text Search This algorithm searches for an occurrence of a pattern p in a text t . It returns the smallest index i such that t [ i..i + m- 1] = p , or - 1 if no such index exists. Input Parameters: p , t Output Parameters: None boyer_moore_simple_text_search ( p , t ) { m = p.length n = t . length i = 0 while ( i + m = n ) { j = m - 1 // begin at the right end while ( t [ i + j ] == p [ j ]) { j = j - 1 if ( j < 0) return i } i = i + 1 } return -1 }
12. 12. Algorithm 9.4.10 Boyer-Moore-Horspool Search This algorithm searches for an occurrence of a pattern p in a text t over alphabet Σ . It returns the smallest index i such that t [ i..i + m- 1] = p , or - 1 if no such index exists.
13. 13. Input Parameters: p , t Output Parameters: None boyer_moore_horspool_search ( p , t ) { m = p.length n = t . length // compute the shift table for k = 0 to | Σ | - 1 shift [ k ] = m for k = 0 to m - 2 shift [ p [ k ]] = m - 1 - k // search i = 0 while ( i + m = n ) { j = m - 1 while ( t [ i + j ] == p [ j ]) { j = j - 1 if ( j < 0) return i } i = i + shift [ t [ i + m - 1]] //shift by last letter } return -1 }
14. 14. Algorithm 9.5.7 Edit-Distance Input Parameters: s , t Output Parameters: None edit_distance( s , t ) { m = s.length n = t.length for i = -1 to m - 1 dist [ i , -1] = i + 1 // initialization of column -1 for j = 0 to n - 1 dist [-1, j ] = j + 1 // initialization of row -1 for i = 0 to m - 1 for j = 0 to n - 1 if ( s [ i ] == t [ j ]) dist [ i , j ] = min ( dist [ i - 1, j - 1], dist [ i - 1, j ] + 1, dist [ i , j - 1] + 1) else dist [ i , j ] = 1 + min ( dist [ i - 1, j - 1], dist [ i - 1, j ], dist [ i , j - 1]) return dist [ m - 1, n - 1] } The algorithm returns the edit distance between two words s and t .