Introduction to Algorithms
Introduction to Algorithms
3rd Edition
ISBN: 9780262033848
Author: Thomas H. Cormen, Ronald L. Rivest, Charles E. Leiserson, Clifford Stein
Publisher: MIT Press
bartleby

Concept explainers

Question
Book Icon
Chapter 32, Problem 1P

a.

Program Plan Intro

To give and efficient algorithm that takes pattern P [1 . . m ] as input and computes the values of ρ(Pi) for i=1,2,m

a.

Expert Solution
Check Mark

Explanation of Solution

String matching based on repetition factors:

String matching is the method of finding some particular pattern in the whole string. There are two things one is pattern and one is whole string. The string matching refers to just check whether the given pattern is present in the string or not also to find out the position of the first letter of the patter till the length of the pattern. It may be in form of sequence or tree structures.

Suppose the input is a pattern P[1..m] and the following algorithm calculates the value ρ(Pi)

The first approach is the Naïve Approach.

The algorithm is given below:

  1i= Text size 0

  // inputs text size

  2m= Pattern size 0

  // inputs the text pattern

3 for a=0 to nm \{4

  5 if ( pattern [1.m]=text[a+1a+m])

add-result (a)

6\}

The running of the algorithm would be Θ(m2) .This is because one power of m for loop that is “for” loop (worst case) and another for checking process of “if” statement because it is run up-to m in worst case scenario.

b.

Program Plan Intro

To prove that the expected value of ρ*(P) is O(1) if the pattern P is chosen randomly form the set of all binary strings of length m .

b.

Expert Solution
Check Mark

Explanation of Solution

Taking the pattern P[1..m] and is defined as ρ*(P) is defined as max1imρ(Pi)

Taking the pattern involving one string: xor y

When matching the above pattern, it iterates in one cycle maximum.

Taking the pattern involving two strings:

    Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  1Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  2orIntroduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  3Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  4orIntroduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  5Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  6

When trying to match the given string with the above pattern. It will match the string as single one or the text as a two together completing it in maximum one or two iteration.

Taking the pattern of three strings:

    Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  7Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  8Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  9

Again, while trying to match the above string either it matches a single string or a few strings together or all might be same.

Similarly, it can be done so for mstrings . Each time involving O(1) time maximum

c.

Program Plan Intro

To argue that the string matching algorithm correctly find all occurrences of the patternP in a text T[1....n] taking the time O(ρ*(P)n+m) .

c.

Expert Solution
Check Mark

Explanation of Solution

The given Galil-Seiferas algorithm consists of preprocessing phase and searching phase.The preprocessing phase of Galil-Seiferas algorithm involves finding decompositionab of x followingb that has at least one prefix period and |a|=O(per(b)) the searching phase works in scanning the text for each occurrence of bandb checks immediately if aoccurs just beforeb .

  Introduction to Algorithms, Chapter 32, Problem 1P , additional homework tip  10

While searching for x[s...m1] in y :

  1. If x[s....s+p1+q11] matches with a shift of length p1 , the comparisons are continued with x[s+q1] .
  2. Each time the mismatch occurs with x[s+q] and q=p1+q1 then a shift of length q/d+1 can be done and comparisons are carried with x[0] .

The preprocessing will incur O(m) times and the searching phase O(n) times and it is noticed in line 3 of algorithm that algorithm runs for ρ*(P) . The total process therefore will take O(ρ*(P)n+m) time.

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
3. 1. Prove that the naive string matching algorithm runs in time O(n) when all the characters in the pattern P are different. 4. We say that P occurs circularly in T if we can partition P to two strings A, B such that A is a suffix of T and B is a prefix of T. Example: T = abaacacaab, P = aabab. P occurs circularly in T because of the partition A = aab and B = ab. Let T be a string of length n and P be a string of length m < n. Design an O(n) time algorithm for deciding whether P occurs circularly in T.
A tandem repeat of a base string b in a string s is asubstring of s having at least two consecutive copies b (nonoverlapping). Develop andimplement a linear-time algorithm that, given two strings b and s, returns the index ofthe beginning of the longest tandem repeat of b in s. For example, your program shouldreturn 3 when b is abcab and s is abcabcababcababcababcab.
Create a simple matching algorithm for a round robin competition. in which the number of participants is n (1  n) and the round index is r (0  r  2  (n  1)/2.out: When i = 0,..., n/2  1, a sequence R of n player indices signaling the match pairings between players R2i and R2i+1; if n is odd, Rn1 denoting the resting player
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education