1.12k likes | 1.14k Views
Chapter 05 Brute Force and Exhaustive Search and String Matching. Brute Force and Exhaustive Search “Just do it!” would be another way to describe the prescription of the brute-force approach
E N D
Chapter 05 Brute Force and Exhaustive Search and String Matching
Brute Force and Exhaustive Search “Just do it!” would be another way to describe the prescription of the brute-force approach Brute force is a straightforward approach to solving a problem, usually directly based on the problem statement and definitions of the concepts involved. The “force” implied by the strategy’s definition is that of a computer and not that of one’s intellect.
For example of Brute Force: • Consider the exponentiation problem: • compute an for a given nonzero number a and a nonnegative integer n. • 2. Compute an mod m for some large integers is a principal component of a leading encryption algorithm. • 3. Consecutive integer checking algorithm for computing gcd(m, n). • Consecutive integer checking algorithm for computing gcd(m, n). • 4. Algorithm MatrixMultiplication(A[0..n-1, 0..n-1], B [0..n-1, 0..n-1]).
Selection Sort • Start selection sort by scanning the entire given list to find its smallest element and exchange it with the first element, putting the smallest element in its final position in the sorted list. • 2. Then scan the list, starting with the second element to find the smallest among the last n-1 elements and exchange it with the second element, putting the second smallest element in its final position. • 3. Generally, on the ith pass through the list, which we number from 0 to n-2, the algorithm searches for the smallest item among the last n-i elements and swaps it with Ai : • A0 ≤ A1 ≤ … ≤ Ai-1 | Ai , . . . , Amin , . . . , An-1 • in their final position the last n-i elements. [n-1-(i-1) = n-i] • 4. After n-1 passes, the list is sorted.
Algorithm SelectionSort(A[0..n-1]) //Sorts a given array by selection sort //input: An array A[0..n-1] of orderable elements //output: Array A[0..n-1] sorted in ascending order for i ← 0 to n-2 do // i goes from 0 to n-2, that is, n-1 elements { min ← i //for each i for j ← i+1 to n-1 do //find the smallest element A[min] after i which is less than A[i] { if A[j] < A[min] then min ←j; } //end inner for //sit the smallest numbers in the left. i swap (A[i], A[min]) } //end of outer for. j Note: Number of “<” = = = Θ(n2 ) Number of“swap” = = (n-2) – 0 + 1 = n -1 = Θ(n )
Why does it need to run for only the first n-1 elements, rather than for all • n elements? Give the best-case and worst-case running times of selection • sort in Ɵ-notation for ascending and descendingorders. • Analysis of the Algorithm SelectionSort(A[0..n-1]): • Input’s size is the given number of elements n. • The basic operation is the key comparison A[j] < A[min]. • The number of times C(n) the basic operation is executed depends on the array’s size and is given by the following sum: • C(n) = ≈ ε Θ(n2 ) • 4. Thus, selection sort is a Θ(n2 ) algorithm on all inputs. • 5. Note that the number of key swaps is only n -1, one for each repetition of the i loop, and therefore Θ(n). This property distinguishes selection sort positively from many other sorting algorithms.
C(n) = = = = n ∑n-2i=0 1 – ∑n-2i=0 1 - ∑n-2i=0i = n (n - 2 + 1) – ( n - 2 + 1) – ½*(n – 2)(n -1) = n2 - 2n + n – n + 2 -1 – ½* (n2 – 3n + 2) = (2 n2 – 4n + 2 - n2 + 3n - 2 )/2 = (n2 – n )/2 ≈ n2 / 2 ε Θ(n2 )
Bubble Sort • Another brute-force application to the sorting problem” Repeatedly, bubbling up the largest element to the last position on the list. The next pass bubbles up the second largest element and so on until after n-1 passes, the list is sort. Pass i (0 ≤ i ≤ n-2) of bubble sort can be represented by the following diagrams: • A0, … , Aj ←?→ Aj+1, … An-i-1 | An-i ≤ … ≤ An-1 • in their final positions
A pseudocode of this algoritm is as follows: Algorithm BubbleSort(A[0..n-1]) //Sort a given array by bubble sort //Input: An array A[0..n-1] of orderable elements //Output: Array A[0..n-1] sorted in ascending order for i ← 0 to n -2 do{ //consider only n-1 elements for j ← 0 to n - 2 - i do { //allow j moves from 0 to next largest // elements which placed orderly in the right. //move the largest element to the right ... if A[j+1] < A[j] then swap(A[j], A[j+1]) ; // compare every two //elements from left to the right within j range. } //end of inner for loop } //end of outer for loop i j
Note: Algorithm BubbleSort(A[0..n-1]) Number of “<” = number of “swap” = = ε Ɵ(n2) What Bubble Sort would gain from: Best case: given 1, 3, 7, 17, 21 … Sort this in ascending order. (save swap only) Worst case: given … 21, 17, 7, 3, 1 sort this in ascending order (costly swap all the way)
Analysis of Algorithm • Inputs size: the number of elements of an array, n. • Basic operation is A[j+1] < A[j] which is same for all array of size n. • The number of times C(n) the basic operation is executed depends on the array’s size and is given by the following sum: • C(n) = • ε Ɵ(n2) • The number of key swaps depends on the input. For the worst case of • decreasing arrays, it is the same as the number of key comparisons: • Sworst (n) = C(n) = • ε Ɵ(n2)
Analysis of Algorithm C(n) = = = = - = (n – 1) – [1 + 2 + … + (n – 2)] = (n – 1) ( (n – 2) – 0 + 1) - (n – 2) ( 1 + (n – 2))/2 = (n – 1) (n – 1) - (n – 2)(n – 1)/2 = (n – 1) (2(n – 1) – (n - 2))/2 = (n – 1) (2n – 2 – n + 2)/2 = (n – 1)n/2 ε Ɵ(n2)
A sorting algorithm is called STABLE if it preserves the relative order of any two equal elements in its input. That is, if an input list contains two equal elements in positions iand j where i < j, then in the sorted list they have to be in positions i’ and j’, respectively, such that i’ < j’. Before sorted, A[i] =x = A[j] = y After sorted, A[i’] =x = A[j’] = y
Sequential Search To repeat, the algorithm compares successive elements of a given list with a given search key until either a match is encountered (successful search) or the list is exhausted without finding a match (unsuccessful search). A simple trick: if we append the search key to the end of the list, the search for the key will have to be successful, and therefore we can eliminate a check for the list’s end on each iteration of the algorithm.
Algorithm SequentialSearch(A[0..n], K) // Implements sequential search with a search key as a sentinel // Input: An array A of n elements and a search key K // Output: The index of the first element in A[0..n-1] whose value is equal to // K or -1 if no such element is found. A[n] ← K; i ← 0; while A[i] ≠ K do { | while i < n and A[i] ≠ K do i ← i + 1; } if i < n then return i else return -1;
Another improvement for the sequential search is: if a given list is known to be sorted: search in such a list can be stopped as soon as an element greater than or equal to the search key is encountered. while K < A[i] do { i ← i + 1; } if K = A[i] then { if i < n then return i else return -1;} else return -1;
Analysis of Algorithm: • Input size is the number of elements, n. • The basic operation is key comparison A[i] ≠ K or addition in i ← i + 1. • The running time of this algorithm can be quite different for the same list size n. • 4. In the worst case, when there are no matching elements or the first matching element happens to be the last one on the list, the algorithm makes the largest number of key comparisons among all possible inputs of size n: • Sworst (n) = n.
5. The best-case efficiency of the algorithm is its efficiency for the best-case input of size n: that is, the best-case inputs are lists of size n with their first elements equal to a search key; accordingly, • Sbest (n) = 1. • 6. The average-case efficiency of the algorithm: • We must make some assumptions about possible inputs of size n: • (a) the probability of a successful search is equal to p (0 ≤ p ≤ 1) and • (b) the probability of the first match occurring in the ith position of the list is the same for every i. • Under these assumptions, we can find the average number of key comparisons Savg (n) as follows:
In the case of a successful search, • the probability of the first match occurring in the ith position of the list is p/n for every i,and • the number of comparisons made by the algorithm in such a situation is obviously i. • In the case of an unsuccessful search, • the number of comparisons is n with the probability of such a search being (1 – p). • Therefore,
Savg (n) = [1*p/n + 2*p/n + … + i*p/n + … + n*p/n] + n*(1 – p) = p/n[ 1 + 2 + …. + i + … + n] + n*(1-p) = p/n*[ n(n + 1)/2] + n*(1 – p) = p( n + 1)/2 + n(1 – p). This general formula yields some quite reasonable answer: If p = 1(i.e., the search must be successful), the average number of key comparisons made by sequential search is (n + 1)/2 (that is, the algorithm will inspect, on average, about half of the list’s elements. If p = 0 (i.e., the search must be unsuccessful), the average number of key comparisons will be n because the algorithm will inspect all n elements on all such inputs. (n + 1)/2, if the search is successful (p = 1). Savg (n) = n, if search is unsuccessful (p = 0).
Brute-force String Matching Given a string of n characters called the text, and a string of m characters (m ≤ n) called pattern,find a substring of the text that matches the pattern. Problem: we want to find i – the index of the leftmost character of the first matching substring in the text – such that ti = p0, ti+1 = p1, …, ti+j= pj, …, ti+m-1 = pm-1. t0, … ti-1, ti, ti+1, …, ti+j , …, ti+m-1 , …., tn-1text T ↕ ↕ ↕ ↕ → … p0, p1, …, pj, …, pm-1pattern P s = i If matches, other than the first one, need to be found, a string-matching algorithm can simply continue working until the entire text is exhausted.
A brute-force algorithm for the string-matching problem is: • Giventhe text: t0, … ti, ti+1, …, ti+j , …, ti+m-1 , …., tn-1and • a pattern: p0, p1, …, pj, …, pm-1 , find a substring of the text that matches the pattern。 • Starting at the first position in the text, i = 0, align the pattern against the firstm characters of the text [i.e., the leftmost character of the first matching substring in the text] and • start matching the corresponding pairs of characters from left to right • until either all m pairs of the characters match and the algorithm stops • or a mismatching pair is encountered. • If a mismatching pair is encountered, then • shift the entire pattern one position s = i-1 → s = i to the right of the text and • resume character comparisons, starting again with the first character of the pattern and its counterpart in the text.
The last position in the text which can still be a beginning of a matching substring is n - m (if the text’s positions are indexed from 0 to n – 1;). Beyond that position, there are not enough characters to match the entire pattern; hence, the algorithm need not make any comparisons there. • t0, … ti, …, tn-m , ..., tn-m+j , …., tn-1text T • ↕ ↕ ↕ • s = i =n-m → … p0, …, pj, …, pm-1pattern P m characters. That also is, n -1 – (n-m) + 1 = m.
skip For the remaining section, assume that T[0 .. n-1] contains the textT t0, … ti, …, ti+j , …, ti+m-1 , …., tn-1 and P[0 .. m-1] contains the pattern P p0, …, pj, …, pm-1 Pictorially, T[0], ..., T[i], …, T[i+j], …, T[i+m-1], …., T[n-1] t0, … ti, …, ti+j , …, ti+m-1 , …., tn-1 and s = i P[0], …, P[j], …, P[m-1] p0, …, pj, …, pm-1 The last comparison happens at T(n-m) t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12 p0 p1 p2 p3 p4 p5 p6 n -1 = 12 n = 13 items m – 1 = 6 m = 7 items i = n – m = 13 – 7 = 6 moves
Formulate the String-Matching Problem We say that pattern Poccurs with shifts in text T (or equivalently, that pattern Poccurs beginning at position sin text T) if 0 ≤ s ≤ n – mand T[s .. s + m -1] = P[0 .. m -1] (that is, if T[s + j] = P[j], for 0 ≤ j ≤ m - 1). Pictorially, T[0], ..., T[s], …, T[s+j], T[s+j+1], …, T[s+m-1] …., T[n-1] t0, … ts, …, ts+j , ts+j+1 , …, ts+m-1 , …., tn-1 and s = i =s P[0], …, P[j], P[j+1], …, P[m-1] p0, …, pj, pj+1, …, pm-1 skip
If P occurs with shift s in T, then we call sa valid shift; otherwise, we call san invalid shift. [“P occurs” means T[s .. s + m-1] = P[0 .. m-1]?] The string-matching problem is the problem of finding all valid shifts with which a given pattern P occurs in a given text T. Example skip The last comparison happens at T(n-m)
n – m = 12 – 4 = 8. i.e., t8 Content n – m + 1 = 12 – 4 + 1 = 9. i.e., T[8] Location Let s=8 and m=4. Then examine T[s.. s+m-1]=T[8 ..11]=P[0..3]=P[0 ..m-1] An instance T[s+j] = T[8+3]= T[11]= P[j=3], for 1 ≤ j=3 ≤ m-1=3 For the worst case for finding out whether P occurs in the given T. We need (n-m+1)m = (12-4+1)4 = 9x4 = 36. (n-m+1) sets of m comparison for the worst case
An example of brute-force string matching. The pattern’s characters that are compared with their text counterparts are in bold type. Given Text T N O B O D Y N O T I C E D … H I M N O T N O T N O T N O T N O T N O T → → → → → → N O T pattern T s = 6 shifts Note that for this example, the algorithm shifts the pattern almost always after a single character comparison.
Each time, it needs m comparisons, for pattern has m characters. The last comparison has to be at T(n-m). Therefore you have (n – m +1) times of m comparisons.
The reason for max matching times (n – m + 1)m is as follows: 1 2 3 4 5 6 …………. | | ………. n |< m >| n-m n-m+1 |< m >| … |< m >| (n-m+1times) The string-matching algorithms and their preprocessing time and matching time. Example: T: 1 2 3 4 5 6 7 8 9 10 11 12 P: 1 2 3 | | 12-3 12-3+1 1 2 3 Total number of comparison (12-3+1) *3 =30, after s=9.
The total number of comparison for the worst case is 12 – 4 +1 = 9, which is 9 * 4 = 36 character comparisons. The last one is begin at T(11-3). Example: Consider
Algorithm BruteForceStringMatching(T[0..n-1], P[0..m-1]) //Implements brute-force string matching //Input: An array T[0 .. n-1] of n characters representing a text and // an array P[0 ..m-1] of m characters representing a pattern //Output: The index of the first character in the text that starts matching // substring or -1 if the search is unsuccessful. for i ← 0 to n - m do //i is pointing at T from 0 up to n-m. {j ← 0; //j is pointing at P from 0 thru m-1 then m. while j < m and P[j] = =T[i+j] //Check next one do { //if char of P matches. j ← j + 1;} //end do-while //from P[0] up to P[m-1] if j = m return i;} //end for //if all the m char of P matched. return -1; //if not matched. //The BruceForceStringMatching algorithms uses i to denote s shifts.
An example of brute-force string matching. The pattern’s characters that are compared with their text counterparts are in bold type. Given Text T N O B O D Y …N O T I C E D … H I M N O T N O T N O T N O T N O T N O T → → → → → → N O T pattern P s = 6 Note that for this example, the algorithm shifts the pattern almost always after a single character comparison.
Consider again Algorithm BruteForceStringMatching(T[0..n-1], P[0..m-1]) //Implements brute-force string matching //Input: An array T[0..n-1] of n characters representing a text and // an array P[0..m-1] of m characters representing a pattern //Output: The index of the first character in the text that starts matching // substring or -1 if the search is unsuccessful. for i ← 0 to n - m do //i is up to n-m, let the do-while to go up to n. j ← 0; while j < m and P[j] == T[i+j] do //Check next one if char of P matches, j ← j + 1; //end do-while //from P[0] up to P[m-1] if j = m return i; //end for //if all the m char of P matched. return -1;
Analysis of Algorithm: • Input size: a string of n characters and a pattern of m characters. • The basic operation: addition of j ← j + 1 or • comparison of j < m and P[j] = = T[i+j] • 1. The worst case is that the algorithm may have to make all m comparisons before shifting the pattern, and this can happen for each of the n – m + 1 tries. Thus, in the worst case running time for the algorithm [also called, the naïve string-matching] is Θ((n – m +1)m), which is Θ(n m). And this is Θ(n2) if m = └ (n/2) ┘.
Analysis of Algorithm: • …. • For a typical word search in a natural language text, we should expect that most shifts would happen after very few comparisons. • The average-case efficiency should be considerably better than the worst-case efficiency. • T[n-m] • T[0]|---n-m match of 1st char of P---|---match m chars of P---| T[n-1] • For searching in random texts, it has been shown to be linear, i.e., Θ(((n-1)–(m-1))+ m) = Θ(n). [Prove it! Best case: m comparisons and worse case: (n – m + 1)m. The average case: ½(m + (n-m+1)m.] • (The BruceForceStringMatching algorithms uses i to denote s shifts.)
String Matching with Finite Automata Let Σ be a finite set of alphabet Let Σ* denote the set of all finite-length strings formed using characters from the alphabet Σ. For example: Σ = {0, 1}. Then Σ* = {ε, 0, 1, 00, 01, 10, 11, 000, 001, ..} where ε is a zero-length empty string. The concatenation of two strings x and y denote xy, has length |x| + |y| and consists of the characters from x followed by the characters from y.
A string w is a prefix of a string x, denoted w x, if x =wy for some string y in Σ*. If w x, then |w| ≤ |x|. Similarly, a string w is a suffix of a string x denoted w x, if x = yw for some y in Σ*. It follows from w x that | w| ≤ |x|. The empty string ε is both a suffix and a prefix of every string. For example: ab abcca and ccaabcca.
Note that: • For any strings x any y and any character a, we have x y [ that is, y = wx ] if and only if xaya [as a result, ya = wxa]. • and are transitive relations.
Lemma 5.1 (Overlapping-suffix lemma) Suppose that x, y, and z are strings such that x z and y z. If |x| ≤ |y|, then x y. If |x| ≥ |y|, then y x. If |x| = |y|, then x = y. Proof: For a graphical proof, x z y x y (a) (b) (c) A graphical proof of Lemma 5.132.1 . We suppose that x ≫ z and y ≫ z. The three parts of the figure illustrate the three cases of the lemma. Vertical lines connect matching regions (shown shaped) of the strings.(a) If |x| ≤ |y|, then x ≫ y. (b) If |x| ≥ |y|, then y ≫ x. (c) If |x| = |y|, then x = y.
Note that the test “x = y” is assumed to take time Θ(t + 1), where t is the length of the longest string z such that z x and z y. We write Θ(t + 1) rather Θ(t) to handle the case in which t = 0; the first characters compared do not match, but it takes a positive amount of time to perform this comparison.
Figure 32.5. The Rabin-Karp algorithm. Each character is a decimal digit, and we compute value modulo 13. • Mod 13 p • A text string. A window of length 5 is shown shaded. The numerical • value of the shaded number, computed modulo 13, yields the value 7.
(b) The same text string with values computed modulo 13 for each possible position of a length-5 window. Assuming the pattern P = 31415, we look for windows whose value modulo 13 is 7, since 31415 ≡ 7 (mod 13). The algorithm finds two such windows, shown shaded in the figure. The first, beginning at text position 7, is indeed an occurrence of the pattern, while the second, beginning at text position 13, is a spurious hit. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … mod 13 valid spurious match hit
If 31415 mod 13 = 7, compute 14152 mod 13 is as follows: • old new • high-order low-order • digit digit old new • high-order low-order • digit shift digit • 14152 ≡ (31415 – 3*10000)*10 + 2 (mod 13) • ≡ (7 – 3*3)*10 + 2 (mod 13) • ≡ -18 (mod 13) • ≡ -5 (mod 13) • ≡ 8 (mod 13) • 10000 (mod 13) = 3 (mod 13) (c)
Figure 32.5. The Rabin-Karp algorithm. • (c) How to compute the value for a window in constant time, given the value for the previous window. The first window has value 31415. Dropping the high-order digit 3, shifting left (multiplying by 10), and then adding in the low-order digit 2 gives us the new value 14152. Because all computations are performed modulo 13, the value for the first window is 7, and the value for the new window is 8. [ Note that …, -18, -5, 8, 21, … are the same class of 8 (mod 13).
Given a pattern P[1 .. m], let p denote its corresponding decimal value. For example, Let P[1]=3, P[2]=1, P[3]=4, P[4]=1 and P[5]=5; and p = 7 if 31415 mod 13 = 7 Given a text T[1 .. n], lettsdenote the decimal value of the length-m substring T[s + 1 .. s +m] for s = 0, 1, …, n – m. For example, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 … … … mod 13 t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 when s=6 and m=5, T[s + 1 .. s +m]= T[7..11] which has 31415. And t6 = 31415 mod 3 = 7.
ts = p if and only if T[s + 1 .. s +m] = P[1 .. m]; thus, s is a valid shift if and only ifts = p. If we could compute p in time Θ(m) and allthe ts values in a total of Θ(n – m + 1) time, then we could determine all valid shift s in time Θ(m) + Θ(n – m + 1) = Θ(n) by comparing p with each of the ts values. For example, n=19, m=5 and (n-m+1)=(19-5+1)=15 to get all t0 , t1, …, t14. We can compute p in time Θ(m) using Horner’s rule: A(x0 ) = a0 + x (a1 + x (a2 + … + x (an-2 + x (an-1 )) .. )) . Then p = P[m] + 10(P[m – 1] + 10(P[m – 2] + … + 10(P[2] + 10P[1]) … )). Likewise, we compute t0 from T[1 .. m] in time Θ(m).
skip To compute the remaining values t1 , t2 , …, tn-m in time Θ(n – m), we can compute ts+1 from ts in constant time, since ts+1 = 10(ts - 10m-1 T[s +1] ) + T[s + m + 1].…… (I) Subtracting 10m-1 T[s +1] removes the high-order digit from ts , multiplying the result by 10 shifts the number left by one digit position and adding T[s + m + 1] brings in the appropriate low-order digit. • 14152 ≡ (31415 – 3*10000)*10 + 2 (mod 13) • ≡ (7 – 3*3)*10 + 2 (mod 13)
For example: Let m = 5 and ts= 31415. We remove the high-order digit T[s + 1] = 3 and bring in the new order digit ( suppose it is T[s + 5 + 1] = 2 to obtain ts+1 = 10(31415 – 10000 * 3) + 2 = 14152. If we compute the constant 10m-1 (which we can do in time O(lg m)using the technique we will see later, although for this application a straightforward O(m)-time method suffices), then each execution of equation (I) takes a constant number of arithmetic operations. Thus we can compute p in time Θ(m), and we can compute t0, t1 , t2 , …, tn-m in time Θ(n – m + 1).
Therefore, we can find all occurrences of the pattern P[1 .. m] in the text T[1 .. n] with Θ(m) preprocessing time and Θ(n – m + 1) matching time. Assume that p and ts are too large. If P contains m characters, then we cannot reasonably assume that each arithmetic operation on p (which is m digits long) takes “constant time.” Fortunately, we can solve this problem easily, as the above figure shows: compute p and the ts values modulo a suitable modulus q. We can compute p modulo q in Θ(m) time and all the ts values modulo q in Θ(n – m + 1) time.