Problem Set 2 Martin Kay Stanford University.

Slides:



Advertisements
Similar presentations
1 Average Case Analysis of an Exact String Matching Algorithm Advisor: Professor R. C. T. Lee Speaker: S. C. Chen.
Advertisements

Combinatorial Pattern Matching CS 466 Saurabh Sinha.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Algorithm : Design & Analysis [19]
15-853Page : Algorithms in the Real World Suffix Trees.
296.3: Algorithms in the Real World
Advisor: Prof. R. C. T. Lee Speaker: Y. L. Chen
Pattern Matching II COMP171 Fall Pattern matching 2 A Finite Automaton Approach * A directed graph that allows self-loop. * Each vertex denotes.
String Matching COMP171 Fall String matching 2 Pattern Matching * Given a text string T[0..n-1] and a pattern P[0..m-1], find all occurrences of.
Pattern Matching COMP171 Spring Pattern Matching / Slide 2 Pattern Matching * Given a text string T[0..n-1] and a pattern P[0..m-1], find all occurrences.
Raita Algorithm T. RAITA Advisor: Prof. R. C. T. Lee
String Matching with Mismatches Some slides are stolen from Moshe Lewenstein (Bar Ilan University)
Aho-Corasick Algorithm Generalizes KMP to handle sets of strings New ideas –keyword trees –failure functions/links –output links.
String Matching Input: Strings P (pattern) and T (text); |P| = m, |T| = n. Output: Indices of all occurrences of P in T. ExampleT = discombobulate later.
String Matching. Problem is to find if a pattern P[1..m] occurs within text T[1..n] Simple solution: Naïve String Matching –Match each position in the.
KMP String Matching Prepared By: Carlens Faustin.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Boyer Moore Algorithm Idan Szpektor. Boyer and Moore.
MCS 101: Algorithms Instructor Neelima Gupta
UNIVERSITY OF SOUTH CAROLINA College of Engineering & Information Technology Bioinformatics Algorithms and Data Structures Chapter 1: Exact String Matching.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
Timothy J. Ham Western Michigan University April 23, 2010.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Here is how to use the table. ( an excerpt of the table is shown ) One tail Two tail We either have.
MCS 101: Algorithms Instructor Neelima Gupta
Design and Analysis of Algorithms - Chapter 71 Space-time tradeoffs For many problems some extra space really pays off: b extra space in tables (breathing.
String Searching CSCI 2720 Spring 2007 Eileen Kraemer.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
String Matching String Matching Problem We introduce a general framework which is suitable to capture an essence of compressed pattern matching according.
Exact String Matching Algorithms Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU.
© Love Ekenberg Hashing Love Ekenberg. © Love Ekenberg In General These slides provide an overview of different hashing techniques that are used to store.
String Matching By Joshua Yudaken. Terms Haystack A string in which to search Needle The string being searched for  find the needle in the haystack.
Martin KayString Matching 11 Martin Kay Stanford University.
ICS220 – Data Structures and Algorithms Analysis Lecture 14 Dr. Ken Cosh.
Design and Analysis of Algorithms – Chapter 71 Space-Time Tradeoffs: String Matching Algorithms* Dr. Ying Lu RAIK 283: Data Structures.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Rabin & Karp Algorithm. Rabin-Karp – the idea Compare a string's hash values, rather than the strings themselves. For efficiency, the hash value of the.
Advanced Data Structures Lecture 8 Mingmin Xie. Agenda Overview Trie Suffix Tree Suffix Array, LCP Construction Applications.
Test1 Here some text. Text 2 More text.
Generic Trees—Trie, Compressed Trie, Suffix Trie (with Analysi
CSG523/ Desain dan Analisis Algoritma
Data Structures.
COMP261 Lecture 20 String Searching 2 of 2.
String Matching.
Rabin & Karp Algorithm.
Knuth-Morris-Pratt algorithm
KMP algorithm.
Tuesday, 12/3/02 String Matching Algorithms Chapter 32
Prolog Lists.
Chapter 7 Space and Time Tradeoffs
[type text here] [type text here] [type text here] [type text here]
Your text here Your text here Your text here Your text here Your text here Pooky.Pandas.
Suffix trees.
Type & Typeclass Syntax in function
Your text here Your text here Your text here Your text here
Prolog III.
Formal Languages, Automata and Models of Computation
Prolog Lists.
Programming Techniques
[type text here] [type text here] [type text here] [type text here]
A New String Matching Algorithm Based on Logical Indexing
Knuth-Morris-Pratt Algorithm.
Improved Two-Way Bit-parallel Search
Logic programming and Prolog
2019/5/14 New Shift table Algorithm For Multiple Variable Length String Pattern Matching Author: Punit Kanuga Presenter: Yi-Hsien Wu Conference: 2015.
Building Java Programs
Prolog III Lists 8-Jul-19.
PROLOG.
Presentation transcript:

Problem Set 2 Martin Kay Stanford University

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.

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

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.

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.

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!

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?

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(_, _).

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.