Proofs: de Bruijn graph
These are the human proofs of de Bruijn graph, at full length.
The tables of the de Bruijn graph are correct, and walks spell exactly the strings whose K-mers are edges full
This proof shows: correctness, termination.
Notation. is a finite alphabet, , and indices are 0-based. For a multiset of words of length , is the number of copies of in . The graph has a vertex for each word and with , and an edge labeled for each distinct , with multiplicity . An absent key of a table counts as 0.
Invariant. After the calls ADD-KMER ADD-KMER, with as a multiset:
- (T1) for every word of length and letter , ;
- (T2) for every word of length and letter , ;
- (T3) the keys of and of are both the vertex set of .
Proof. For all tables are empty, is empty and has no vertex. Suppose the invariant holds for and let be added, . Line L1 sets and . Line L2 adds and as keys of both tables, which are the two vertices that adds to ; no other key is added. Line L3 increases by one, and , so (T1) holds for the pair ; any other pair has , so and its entry is unchanged. Line L4 does the same for (T2), since .
ADD-READ. Line L5 calls ADD-KMER on for , which are the -mers of , each occurrence once. If , the range is empty. So after adding a set of reads, is the multiset of all their -mers.
SUCCESSORS and PREDECESSORS. An edge leaves exactly for the distinct with , that is, with , and it goes to . By (T1), these letters are the keys of with positive count, which are all its keys, since counts only grow from 1. Line L6 returns these vertices. The same argument with (T2) gives line L7: an edge enters from exactly when .
Spelling lemma. Let be words of length such that for , and let be the output of lines L8 to L10. Then
- and for ;
- for , the -mer is followed by the last letter of , which is the label of an edge if one exists.
Proof. Item 1 by induction on . For , starts with . Suppose . The letter appended for is at index and equals . The letters at are . So . Each step appends one letter, which gives the length. For item 2, is followed by . An edge has label with , so .
Converse. If is a string of length whose -mers all lie in , then for is a walk, since is an edge from to , and SPELL returns by item 1 of the lemma.
Consequence. If a walk uses each edge of exactly once, then by item 2 the -mers of its spelled string are the labels of the edges in the order of the walk, so they are the distinct -mers of , each exactly once.
Termination. Each operation is straight-line code or a loop over a finite range: line L5 runs times, lines L6 and L7 run once per key, at most keys, and line L9 runs times.