• Keine Ergebnisse gefunden

Sample Solution

N/A
N/A
Protected

Academic year: 2022

Aktie "Sample Solution"

Copied!
1
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

University of Freiburg Dept. of Computer Science Prof. Dr. F. Kuhn

Algorithms and Datastructures Summer Term 2021

Sample Solution Exercise Sheet 12

Exercise 1: Knuth-Morris-Pratt Algorithm

Consider the pattern P =BBABAB and the textT =ABBABBABABBABABBA.

(a) Compute the arrayS of the Knuth-Morris-Pratt algorithm.

(b) Use the Knuth-Morris-Pratt algorithm to find all appearances of P in T. Document the steps analogously to the lecture.

Sample Solution

(a) S = [−1,0,1,0,1,0,1]

(b) A B B A B B A B A B B A B A B B A

B B A B A B

B B A B A B

B B A B A B X

B B A B A B X

B B A

Exercise 2: Rabin-Karp Algorithm

LetT be a given text of lengthnand letP1, . . . , Pkbekpatterns, each of length exactlym. The goal is to know if there is at least one pattern in the text, that is, we want to answer True if there exists at least one index i∈ {1, . . . , k} such that Pi ∈T, and answer False if for anyi∈ {1, . . . , k},Pi ∈/T. It is easy to solve this problem in O(k(n+m)) by running the Rabin-Karp algorithm once for each pattern. Give an algorithm (based on Rabin-Karp) that requires onlyO(n+km).

Sample Solution

On average, Rabin-Karp takes O(n+m) for searching if a pattern P appears in a text T, hence we can trivially run Rabin-Karp k times, once for each pattern Pi, and in this way we would solve the exercise ink·O(n+m) =O(kn+km). In any case, we need to spendO(km) in order to compute the k hash values of the patterns, where each pattern is of length m. So, how to avoid spending O(kn), and instead spend onlyO(n)?

Let’s see where we spend thisO(kn) time. Using Rabin-Karp, we go through all O(n) positions of the text, and for each window, we check if the hash value of that portion of the text matches the hash value of the pattern, and since here we have k patterns, for each window, we perform O(k) checks.

What can we do such that, for each text window, on average, we spend O(1) to check if the hash value of the text window matches the hash value of one of the patters? We can put the hash values of the patterns into a hash table, and now we spend O(1) for performing a search, spending in total O(n+km).

Referenzen

ÄHNLICHE DOKUMENTE

Ancient Jewish tradition attaches significant consideration to the book of Leviticus, as it is by far the most quoted in rabbinic literature such as the Mishna or the Talmud. In

We take the whole of this body of work as read, in what we present here, for the simple reason that we do not have the time to repeat it. We do not repeat it also because we have

Incidents of burglary and vehicle crime have fallen by more than the other offence types (Figure 4), and as these offences also have high mean values (Table 3), their effect on

The performance of organizations that handle a problem change is primarily positively affected (that is, cycle time is reduced) by high task awareness and high update rate of

The standard vNM independence axiom is taken over lotteries that lead only to final outcomes, without specifying whether the agent observes the resolution of these lotteries.. In

In particular, we found that the number “8” is associated with plates with significantly higher winning bids, and the number “4” is associated with plates with significantly

(7), when the prey and the predator are asymmetric with respect to their valuations, the type of equilibrium depends both on the effectiveness of their armies and on the ratio of

Moreover, Pearce (1984) holds the view that for strategies to be rational, the NE is not necessary or sufficient. In a game where there is equilibrium, an NE, if one player