Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-gq7q9 Total loading time: 0 Render date: 2024-07-25T02:11:45.009Z Has data issue: false hasContentIssue false

1 - Why Data Structures? A Motivating Example

Published online by Cambridge University Press:  10 November 2016

Shmuel Tomi Klein
Affiliation:
Bar-Ilan University, Israel
Get access

Summary

To begin the study of data structures, I demonstrate the usefulness of even quite simple structures by working through a detailed motivating example. We shall afterward come back to the basics and build up our body of knowledge incrementally.

The algorithm presented in this introduction is due to R. S. Boyer and J S. Moore and solves the string matching problem in a surprisingly efficient way. The techniques, although sophisticated, do not require any advanced mathematical tools for their understanding. It is precisely because of this simplicity that the algorithm is a good example of the usefulness of data structures, even the simplest ones. In fact, all that is needed to make the algorithm work are two small arrays storing integers.

There are two sorts of algorithms that, when first encountered, inspire both perplexity and admiration. The first is an algorithm so complicated that one can hardly imagine how its inventors came up with the idea, triggering a reaction of the kind, “How could they think of that?” The other possibility is just the opposite – some flash of ingeniousity that gives an utterly simple solution, leaving us with the question, “How didn't I think of that?” The Boyer–Moore algorithm is of this second kind.

We encounter on a daily basis instances of the string matching problem, defined generically as follows: given a text T = T[1]T[2] · · · T[n] of length n characters and a string S = S[1]S[2] · · · S[m] of length m, find the (first, or all) location(s) of S in T, if one appears there at all. In the example of Figure 1.1, the string S = TRYME is indeed found in T, starting at position 22.

To solve the problem, we imagine that the string is aligned underneath the text, starting with both text and string left justified. One can then compare corresponding characters, until a mismatch is found, which enables us to move the string forward to a new potential matching position. We call this the naive approach. It should be emphasized that our discourse of moving the pattern along an imaginary sliding path is just for facilitating understanding.

Type
Chapter
Information
Publisher: Cambridge University Press
Print publication year: 2016

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×