Guest
Sign in to keep your graphs and progress.
Select an algorithm and generate steps to begin visualization
Spell a genome back from its pieces
Cuts a DNA string into k-mers, turns each into an arc between overlapping (k−1)-mers and spells the genome back along an Eulerian path (Hierholzer)
8 different 3-mers
Each distinct k-mer becomes an arc from its first k−1 letters to its last k−1 letters. The run looks for a path through every arc once.
Select an algorithm and generate steps to begin visualization
DNA sequencers do not read a genome from end to end. They return millions of short overlapping pieces, and assembly has to put them back together. A de Bruijn graph does this by cutting the reads into k-mers, substrings of length k. Each (k−1)-mer becomes a vertex and each k-mer an arc from its first k−1 letters to its last k−1 letters, so consecutive k-mers of the genome share a vertex. A string that contains every k-mer once is then a walk that uses every arc once: an Eulerian path, which is easy to find, where the older overlap approach needed a Hamiltonian path, which is not.
First the run checks the balance of every vertex, arcs out minus arcs in. An Eulerian path exists when the arcs form one connected piece and at most two vertices are unbalanced, one with +1, where the path starts, and one with −1, where it ends. If every vertex is balanced, the k-mers close into a cycle. Hierholzer's algorithm then walks from the start along unused arcs, pushing vertices on a stack. When a vertex has no unused arc left, it moves from the stack to the front of the path, and the walk resumes from the vertex below, splicing in a detour if that vertex still has arcs. Every arc is used once, so the time is O(m) for m k-mers. Reading the path, the first (k−1)-mer plus the last letter of each next one, spells the string. The panel builds the graph from a DNA string and a k, the visualizer shows the balance badges, the stack and each arc as it is used, and the run says when a vertex offered a choice, since a repeat can make the assembly ambiguous.
De Bruijn graphs are the core of short-read assemblers such as Velvet and SPAdes, and of tools that compare genomes or count k-mers. Real data adds sequencing errors, uneven coverage and repeats longer than k, so assemblers prune error branches and output contigs, the longest stretches without ambiguity, instead of one full path. The same graph, built on all strings of a given length, gives de Bruijn sequences, which contain every code once and test every combination of a keypad lock with the fewest presses.
Build the graph, check the balance of the vertices, then run Hierholzer with an explicit stack.
Assemble(kmers):
for each distinct k-mer x: // one arc per k-mer
add arc prefix(x) → suffix(x)
check: the arcs are connected, and out - in is 0 everywhere
except +1 at a start s and -1 at an end
if every vertex is balanced: s = any vertex with an arc
stack = [s], path = []
while stack is not empty: // Hierholzer
v = top of stack
if v has an unused arc v → w:
mark it used, push w
else:
pop v, put it at the front of path
return path[0] + the last letter of each later vertexA vertex leaves the stack only when it has no arcs left, so vertices come out in reverse order of the final path, which is why each goes to the front. A detour found deeper in the stack ends up spliced into the path at the right place without extra work.
The example on this page: the string ATGGCGTGCA cut into 3-mers.
Example graph: The eight 3-mers ATG, TGG, GGC, GCG, CGT, GTG, TGC and GCA give the vertices AT, TG, GG, GC, CG, GT and CA, with one arc per 3-mer. TG and GC each occur twice in the string, so each has two arcs out.
Reading AT and then the last letter of each next vertex spells ATGGCGTGCA, the original string. Taking TGC first at TG can spell ATGCGTGGCA instead, which has exactly the same eight 3-mers. The repeated TG and GC make 3-mers ambiguous here, and with 4-mers the path would be forced.
Time: O(m) · Space: O(n + m)
Building the graph reads each of the L − k + 1 k-mers of a string of length L once, with hashing. Hierholzer marks every arc used once and pushes and pops a vertex once per arc that enters it, so the walk is O(n + m), which is O(m) when the arcs are connected. Checking the balance is one more pass over the arcs.
Other ways to assemble, or to walk every arc.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Overlap, layout, consensus | Long reads with many errors, as from nanopore or PacBio sequencers. Reads become vertices and overlaps edges, and a path through the reads is laid out. | O(r²) overlaps for r reads |
| String graph | The overlap view without redundant edges. It keeps whole reads, so it resolves repeats shorter than a read. | O(r²) to build naively |
| Fleury's algorithm | Teaching: it avoids bridges and is easy to state, but every step needs a bridge test. | O(m²) |
Related algorithms: Eulerian Path (Undirected), Fleury's Algorithm, Chinese Postman, Hamiltonian Path, Traveling Salesman Problem