Final Project, Preliminary Presentations
Input.
TEXT
PATTERN
Output.
TEXT
contains PATTERN
as a substringPATTERN
in TEXT
-1
if PATTERN
does not appearTEXT = "C C T G T C T C T G T A A T C A T G A A"
PATTERN1 = "T A A T"
PATTERN2 = "T A C T"
Question. How to solve the problem in general?
let idx = 0;
let matches = 0;
while (idx < TEXT.length - PATTERN.length) {
if (matches == PATTERN.length) return idx;
if (TEXT[idx + matches] == PATTERN[matches]) {
matches++;
} else {
idx++;
matches = 0;
}
return (matches == PATTERN.length) ? idx : -1;
}
Question. If TEXT
has length $n$ and PATTERN
has length $m$, what is the worst-case running time (big 0) of the procedure as a function of $n$ and $m$?
Question. What example TEXT
s and PATTERN
s are especially inefficient for our procedure?
idx = 0;
matches = 0;
while (idx < TEXT.length - PATTERN.length) {
if (matches == PATTERN.length) return idx;
if (TEXT[idx + matches] == PATTERN[matches]) {
matches++;
} else {
idx++;
matches = 0;
}
return (matches == PATTERN.length) ? idx : -1;
}
TEXT = "C C T G T C T C T G T A A T C A T G A A"
PATTERN = "T C A T"
TEXT = "C C T G T C T C T G T A A T C A T G A A"
PATTERN = "T C A T"
Today
Wednesday
TEXT
PATTERN
Display TEXT
and PATTERN
as a horizontal rows of boxes
TEXT
and PATTERN
aligned to show current value of idx
TEXT
and PATTERN
colored to indicate (mis) matched character with current alignment
Each step does
matches
incremented)PATTERN
text (idx
incremented)index.html
:
'text-container'
, 'pattern-container'
letter-box
s<input>
elements for user input<button>
elements to update the text and stepstyle.css
:
.container
, .letter-box
pattern-matching.js
Structurediv
s with class letter-box
)lec20-pattern-matching.zip