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
Let B be the set of strings over alphabet {0,1}. Consider the following problem: Input: A string x from B Yes/No Question: Is x, when interpreted as a binary number, a multiple of 2?   Note that we ignore leading zeros when interpret the strings.  For example, we consider a string like ' 001 ' as the binary number 1. For /\, the empty string, we consider it as 0.   a. Give the three shortest yes input instances for this problem. b. Give the three shortest no input instances for this problem. c. Suppose that we list the strings in B in numerical order and break the ties based on the length of the strings. Which of the following strings have a fixed position on the list?   /\, 00, 1, 001, 10, 0000   d. From c), we have observed that when we list the strings in numerical order, some strings do not appear in a fixed position on the list. Does this prove that B is uncountably infinite? Why or why not?
Input: A set of n movies. A positive integer k. A positive integer q. A function f that takes two movies X, Y, and gives a positive similarity score f(X,Y) in constant time. Task: Design an efficient algorithm that partitions the movies into exactly k disjoint sets such that movies from different sets have a similarity score at most q. If such groups cannot be made, then print 'Unsatisfiable'.  Give an efficient algorithm (to the best of your knowledge) and a formal proof of correctness for your algorithm. An answer without any formal proof will be considered incomplete. Analyze the running time of the algorithm. You must write down your algorithm as pseudocode or describe it as a set of steps (as we do in the class). Do not provide code that is written in a code editor using C/C++/Python/Java, etc.
The reverse of a string x denoted by rev(x) and is defined by the following recursiverule: •rev(ε) = ε•rev(xa) = a.rev(x) Prove that if A is a regular set then so is the following set:rev(A) = {rev(x) : x ∈A}
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