Timothy J. Ham Western Michigan University April 23, 2010.

17
String Abstract Data Type Timothy J. Ham Western Michigan University April 23, 2010

Transcript of Timothy J. Ham Western Michigan University April 23, 2010.

Page 1: Timothy J. Ham Western Michigan University April 23, 2010.

String Abstract Data TypeTimothy J. Ham

Western Michigan UniversityApril 23, 2010

Page 2: Timothy J. Ham Western Michigan University April 23, 2010.

What is it?Quite literally, the string abstract data type is

"an array of [characters]. The array consists of the string characters followed by the null character which signals the end of the string." (Textbook, p104, 2.6 para 3)

Page 3: Timothy J. Ham Western Michigan University April 23, 2010.

Where is it found?Strings are used in a number of places:

Page / window titlesWeb page / document titlesWeb page / document body text

Page 4: Timothy J. Ham Western Michigan University April 23, 2010.

What can I do with it?One of the most common operations performed on strings

is the search. There are a number of places in which this operation is employed:

Operating systems -> File names

Word processors -> Text documents

Web pages -> Body text**Anyone ever use the "Find" feature in Firefox that

searches as you type?

Databases -> Miscellaneous data

Page 5: Timothy J. Ham Western Michigan University April 23, 2010.

How does this occur?The searches are performed using one of

several algorithms; this presentation will compare a simple and a complex string search algorithm:Exhaustive - easy to code, but very inefficientKnuth-Morris-Pratt - much more efficient than

simple algorithm

Page 6: Timothy J. Ham Western Michigan University April 23, 2010.

How will we test this? The environment for testing these algorithms

will feature a string to search, as well as a pattern of characters for which we will search

In addition to providing the theoretical run time, we will measure the actual time (in milliseconds) required to run the algorithm

Page 7: Timothy J. Ham Western Michigan University April 23, 2010.

Simple / Exhaustive

Page 8: Timothy J. Ham Western Michigan University April 23, 2010.

Simple / Exhaustive

Page 9: Timothy J. Ham Western Michigan University April 23, 2010.

Simple / ExhaustiveEasy to code; largely inefficientTheoretical run time is O( len(P) * len(S) ):

Len(P) is the length of the pattern for which we are searching

Len(S) is the length of the string we are searching

Page 10: Timothy J. Ham Western Michigan University April 23, 2010.

Simple / ExhaustiveStarting with the first character, attempts to

match pattern; when it encounters a mismatch, it repeats the entire “matching” attempt with the next character

Actual run time??? LET’S FIND OUT!

Page 11: Timothy J. Ham Western Michigan University April 23, 2010.

Knuth-Morris-Pratt

Page 12: Timothy J. Ham Western Michigan University April 23, 2010.

Knuth-Morris-Pratt

Page 13: Timothy J. Ham Western Michigan University April 23, 2010.

Knuth-Morris-PrattMore complex to code; shorter run timeInstead of starting over every time it

encounters a mismatch:the algorithm “remembers” how far along the

string it matched the patternfrom there, it attempts to match again

Page 14: Timothy J. Ham Western Michigan University April 23, 2010.

Knuth-Morris-PrattTheoretical run time is O(len(S) )*

*Assuming failure function has been previously run

Actual run time??? LET’S FIND OUT!!!

Page 15: Timothy J. Ham Western Michigan University April 23, 2010.

Conclusion

Page 16: Timothy J. Ham Western Michigan University April 23, 2010.

ConclusionThe string abstract data type is found in

many placesWithout it, many search functions we take for

granted would not be possibleWhile a more complex searching algorithm

may be more difficult to code, it brings great rewards in time and energy efficiency

Page 17: Timothy J. Ham Western Michigan University April 23, 2010.

Thanks for being such a great audience!