liveBook · Manning

Chapter 5: What’s up with you, Directed Acyclic Word Graph?

What you will learn in chapter 5 of Fabulous Adventures in Data Structures and Algorithms

This chapter introduces the problem of storing and quickly searching large word lists, and previews a progression from sorted lists to tries and DAWGs.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces the problem of storing and quickly searching large word lists, and previews a progression from sorted lists to tries and DAWGs.

Key terms: word list, typing completion, word games, prefix, memory, sorted list, prefix tree, trie, Directed Acyclic Word Graph, DAWG

5.1 Two problems, two weak solutions

This section defines the word-list search and prefix-completion problems, their required interface operations, scope, and performance notation for comparing solutions.

Key terms: abstract data type, word list, enumerable sequence of strings, contained, prefix, autocompletion, IntelliSense completions, 30 milliseconds, n, m

5.1.1 Hash sets are a nonsolution

This subsection rejects hash sets for a word list because they make exact membership fast but cannot efficiently answer prefix queries.

Key terms: HashSet<string>, Contains, hash code, buckets, O(m), StartsWith, hashing algorithm

5.1.2 Sorted lists are… fine?

This section evaluates a sorted List<string> as a word-list representation, explaining its binary-search operations, time costs, and substantial memory overhead.

Key terms: List<string>, sorted list, BinarySearch, Contains, StartsWith, bitwise complement, O(m log n), O(log n), string comparison, string.StartsWith

5.2 The prefix tree

This section motivates prefix trees (tries) as a way to deduplicate shared word prefixes and introduces how a trie represents words visually.

Key terms: prefix tree, trie, deduplicate, prefix, ENABLE, edge, node, root, suffix, doubled circle

5.2.1 Nodes and edges

This section defines the mutable trie-node representation, its edge-oriented interface, and the constant-time operations used to navigate and extend it.

Key terms: node, edge, outgoing edges, edge labels, IsEnd, Dictionary<char, Node>, mutable, immutable, root, leaves

5.2.2 The trie-building algorithm

This section explains how to build a trie by sharing word prefixes and establishes the algorithm’s optimal O(mn) construction time.

Key terms: trie, common prefix, root node, end node, TryAddNewNode, outgoing edges, empty word list, Add, O(mn), O(1)

5.2.3 Using a trie as a word list

This section shows how a trie-backed WordGraph implements word containment and prefix search efficiently by locating a prefix node and enumerating its suffixes.

Key terms: WordGraph, Find, Contains, TryFollowEdge, end-of-word marker, StartsWith, Words, StringBuilder, prefix, suffixes

5.2.4 How much memory are we saving with a trie?

This subsection evaluates trie memory savings, exposes node overhead and duplicated suffix leaves, and motivates compressing a trie into a word graph.

Key terms: trie, prefix tree, common prefixes, edges, edge, node, dictionary, end-of-word node, leaf node, common suffixes

5.3 Tries are not-so-secretly finite state automata

This section recasts a trie as a finite state automaton and motivates building a minimal automaton directly rather than minimizing a fully built trie.

Key terms: finite state automaton, FSA, states, inputs, transitions, trie, start state, success states, Contains, language

5.4 Building a DAWG

This section introduces DAWGs and explains the sorted, incremental minimization strategy that builds a minimal word-recognizing graph while safely deferring optimization of the most recently added word.

Key terms: Directed Acyclic Word Graph, DAWG, Deterministic Acyclic Finite State Automaton, DAFSA, suffixes, prefixes, almost minimal DAWG, minimal DAWG, invariant, last word added

5.4.1 What problems must we solve to build a DAWG?

This subsection breaks DAWG construction into four tasks and introduces Node operations needed to revisit and optimize the previously added word.

Key terms: common prefix, optimization opportunities, already-optimized node, new suffix, DAWG, sorted order, highest letter, LastEdgeLabel, previously added word, Count

5.4.2 Equivalence of nodes

This section defines DAWG node equivalence, implements it as equality and hashing, and uses a canonical-node hash set to detect duplicates efficiently.

Key terms: equivalent nodes, IsEnd, outgoing edges, edge labels, reference-equal, Leaf nodes, IEqualityComparer<Node>, GetHashCode, HashCode, Add

5.4.3 The DAWG-building algorithm

This section presents the incremental algorithm for building a minimal DAWG by optimizing the previous word’s suffix before appending the new word’s suffix.

Key terms: Add, common prefix, suffix, TrieBuilder, TryAddNewNode, FixUpLastWord, equivalent, canonical, ReplaceEdge, HashSet<T>

5.4.4 How much expense does optimizing the graph as you go add?

This section shows that optimizing a DAWG during construction adds constant-factor work but does not worsen the builder’s asymptotic time or space complexity compared with building a trie.

Key terms: DAWG builder, trie builder, O(mn), FixUpLastWord, asymptotic complexity, canonical set, Max(), last edge label, alphabet, canonical nodes

5.5 DAWG vs. trie for ENABLE

This section compares a DAWG with a trie for the ENABLE word list, explaining its large structural savings, practical object-memory tradeoff, and compact byte-array representation.

Key terms: DAWG, trie, suffix deduplication, redundant leaf nodes, canonical nodes, ENABLE, inbound edges, tree, byte array, 4-byte header

5.6 Summary

This summary compares word-completion structures and explains how incrementally built, minimized DAWGs achieve compact prefix search.

Concepts explained in this chapter

← Chapter 4: Memoizing immutable quadtrees to make a better Life Chapter 6: Combinatorial algorithms →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.