Download presentation
Presentation is loading. Please wait.
Published byHelena Sandra Nørgaard Modified over 5 years ago
1
Problem Set 2 Martin Kay Stanford University
2
Problem 4 An on-line program for string searching is generally conducting several searches for a given pattern in a text at the same time, updating each of them when a new character of the text is considered. After it examines the character in position i of the next, it never examines character i-1 again.
3
advance has the following parameters:
A Prolog program for on-line naive string search might have the following overall structure: search(Pattern, Text, M) :- advance(Pattern, [Pattern], Text, [], 0, M). advance has the following parameters: Pattern—The pattern being sought. Always the same in each recursive call. Matches—A list of tails of the pattern that remain to be matched. Text—The tail of the text that remains to be matched. NewMatches—A new set of pattern tails to be matched beginning at the next position in the text N—The number of text characters matched so far. M—The position in which a match was found
4
Parameters of advance Advance can be a recursive predicate that calls no other predicates but itself, whose five clauses have the following functions: Reached the end of the text. Quit. The first member of the Matches list is exhausted, so a search succeeds. The next character of the first member of matches is the same as the next text character, so the reamining characters go on the NewMatches list. The next character of the first member of matches does not match. The current set of Matches have all be considered, so go on to the next.
5
An Example Here are the arguments of each call for a simple example.
| ?- search([a,b,a,b], [c,d,a,b,a,b,a,b,e,f], N). [a,b,a,b] [[a,b,a,b]] [c,d,a,b,a,b,a,b,e,f] [] 0 [a,b,a,b] [] [c,d,a,b,a,b,a,b,e,f] [] 0 [a,b,a,b] [[a,b,a,b]] [d,a,b,a,b,a,b,e,f] [] 1 [a,b,a,b] [] [d,a,b,a,b,a,b,e,f] [] 1 [a,b,a,b] [[a,b,a,b]] [a,b,a,b,a,b,e,f] [] 2 [a,b,a,b] [] [a,b,a,b,a,b,e,f] [[b,a,b]] 2 [a,b,a,b] [[a,b,a,b],[b,a,b]] [b,a,b,a,b,e,f] [] 3 [a,b,a,b] [[b,a,b]] [b,a,b,a,b,e,f] [] 3 [a,b,a,b] [] [b,a,b,a,b,e,f] [[a,b]] 3 [a,b,a,b] [[a,b,a,b],[a,b]] [a,b,a,b,e,f] [] 4 [a,b,a,b] [[a,b]] [a,b,a,b,e,f] [[b,a,b]] 4 [a,b,a,b] [] [a,b,a,b,e,f] [[b],[b,a,b]] 4 [a,b,a,b] [[a,b,a,b],[b],[b,a,b]] [b,a,b,e,f] [] 5 [a,b,a,b] [[b],[b,a,b]] [b,a,b,e,f] [] 5 [a,b,a,b] [[b,a,b]] [b,a,b,e,f] [[]] 5 [a,b,a,b] [] [b,a,b,e,f] [[a,b],[]] 5 [a,b,a,b] [[a,b,a,b],[a,b],[]] [a,b,e,f] [] 6 [a,b,a,b] [[a,b],[]] [a,b,e,f] [[b,a,b]] 6 [a,b,a,b] [[]] [a,b,e,f] [[b],[b,a,b]] 6 Here are the arguments of each call for a simple example. The last argument is never bound when the predicate is called, and it is therefore not shown.
6
Problem: WRITE THE PROGRAM!
N = 6 ? ; [a,b,a,b] [] [a,b,e,f] [[b],[b,a,b]] 6 [a,b,a,b] [[a,b,a,b],[b],[b,a,b]] [b,e,f] [] 7 [a,b,a,b] [[b],[b,a,b]] [b,e,f] [] 7 [a,b,a,b] [[b,a,b]] [b,e,f] [[]] 7 [a,b,a,b] [] [b,e,f] [[a,b],[]] 7 [a,b,a,b] [[a,b,a,b],[a,b],[]] [e,f] [] 8 [a,b,a,b] [[a,b],[]] [e,f] [] 8 [a,b,a,b] [[]] [e,f] [] 8 N = 8 ? ; [a,b,a,b] [] [e,f] [] 8 [a,b,a,b] [[a,b,a,b]] [f] [] 9 [a,b,a,b] [] [f] [] 9 [a,b,a,b] [[a,b,a,b]] [] [] 10 true ? yes Problem: WRITE THE PROGRAM!
7
Problem 5 The naive string-search algorithm has a worst-case time complexity of nm, where n is the length of the text and m is the length of the pattern. The program on the next two slides was discussed in class. What is its worst-case time complexity?
8
Building the table build_table(Pattern) :- retractall(table(_, _)),
reverse(Pattern, [C | RevPat]), append(Pref, _, [C | RevPat]), build(Pref, RevPat). build(Pref, RevPath) :- append(Offset, Suf, RevPath), append(Pref, _, Suf), length(Pref, LPref), !, length([_ | Offset], LOffset), asserta(table(LPref, LOffset)), fail. build(_, _).
9
Using the Table search(Pattern, Pattern, _, 0).
search(Pattern, Window0, Text0, N) :- common_prefix(Pattern, Window0, LCP), ( table(LCP, Shift) ; table(_, Shift) ), length(X, Shift), append(Window1, X, Window0), length(Y, Shift), append(Y, Text, Text0), reverse(Y, Z), append(Z, Window1, Window), !, search(Pattern, Window, Text, N0), N is N0+Shift.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.