Lably
Skip to content

Aho–Corasick algorithm

String-searching algorithm

  • 5 min read
  • 3 chapters

Related on Lably

Through Computer science, Algorithm and Finite-state machine

Informally, the algorithm creates a trie using the strings in the dictionary and then constructs a finite-state machine from the trie by adding additional links between the nodes. These extra links allow fast transitions between failed string matches (e.g. a search for cart in a trie that does not contain cart, but contains art, and thus would fail at the node prefixed by car), to other branches of the trie that share a common suffix (e.g., in the previous case, a branch for attribute might be the best lateral transition). This allows the automaton to transition between string matches without the need for backtracking.

When the string dictionary is known in advance (e.g. a computer virus database), the construction of the automaton can be performed once off-line and the compiled automaton stored for later use. In this case, its run time is linear in the length of the input plus the number of matched entries.

The Aho—Corasick string-matching algorithm formed the basis of the original Unix command fgrep.

History

Like many inventions at Bell Labs at the time, the Aho–Corasick algorithm was created serendipitously with a conversation between the two after a seminar by Aho. Corasick was an information scientist who got her PhD a year earlier at Lehigh University. There, she did her dissertation on securing proprietary data within open systems, through the lens of both the commercial, legal, and government structures and the technical tools that were emerging at the time. In a similar realm, at Bell Labs, she was building a tool for researchers to learn about current work being done by government contractors by searching government-provided tapes of publications.

She had written a primitive keyword-by-keyword search program to find chosen keywords within the tapes, but it scaled poorly with many keywords; One of the bibliographers using her algorithm hit the $600 usage limit on the Bell Labs machines before their search finished.

She ended up attending a seminar on algorithm design by Aho, and afterwards they got to speaking about her work and this problem. Aho suggested improving the efficiency of the program using the approach of the now Aho–Corasick algorithm, and Corasick designed a program based on those insights. This lowered the running cost of that bibliographer's search from over $600 to just $25.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication