learngraphtheory.org

Guest

Sign in to keep your graphs and progress.

Algorithm Selection

Select an algorithm and generate steps to begin visualization

De Bruijn Graph Genome Assembly Visualizer

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)

Time: O(m)
Space: O(n + m)
Use Case: Genome assembly from short reads, de Bruijn sequences, combination lock codes
Showing the example graph for this algorithm.

8 different 3-mers

Try:

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.

Algorithm Execution

Select an algorithm and generate steps to begin visualization

About De Bruijn Graph Assembly

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.

How it works

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.

Applications

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.

Pseudocode

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 vertex

A 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.

Worked example, step by step

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.

  1. Balance. AT has one arc out and none in (+1), CA has one in and none out (−1), and every other vertex is balanced. So an Eulerian path exists, from AT to CA.
  2. First branch. From AT the walk takes ATG to TG. TG has two unused arcs, TGG and TGC, and the first one, TGG, is taken.
  3. Second branch. GGC leads to GC, which also has two arcs. GCG is taken, then CGT and GTG lead back to TG.
  4. End of the walk. TG has only TGC left, then GC only GCA, and the walk reaches CA. All 8 arcs are used without a detour.
  5. Unwinding the stack. CA has no arcs left and goes to the front of the path, then GC, TG, GT, CG, GC, GG, TG and AT follow, which gives AT, TG, GG, GC, CG, GT, TG, GC, CA.

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.

Complexity, and where it comes from

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.

When to use De Bruijn Graph Assembly, and when not to

Other ways to assemble, or to walk every arc.

AlternativePrefer it whenCost
Overlap, layout, consensusLong 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 graphThe overlap view without redundant edges. It keeps whole reads, so it resolves repeats shorter than a read.O(r²) to build naively
Fleury's algorithmTeaching: it avoids bridges and is easy to state, but every step needs a bridge test.O(m²)

Common pitfalls

  • Collapsing repeated k-mers. Here each distinct k-mer becomes one arc, so a k-mer that occurs twice in the string is used once and the string does not come back in full. Real assemblers keep counts or multiplicities.
  • Choosing k. A small k makes repeats likely and the graph tangled. A large k needs every k-mer to be read without error and breaks the graph where coverage is thin. Assemblers often try several values of k or combine them.
  • Expecting one answer. When a vertex has two arcs out, several Eulerian paths may exist and spell different strings. The k-mers alone cannot tell them apart, which is why assemblers stop at branches and report contigs.

Frequently asked questions

Why vertices for (k−1)-mers and arcs for k-mers?
Two consecutive k-mers of a string overlap in k − 1 letters. Making that overlap the shared vertex turns the string into a walk that uses each k-mer once, so assembly becomes an Eulerian path problem, solvable in linear time.
What is a de Bruijn sequence?
A cyclic sequence that contains every string of length n over an alphabet exactly once. It is an Eulerian circuit of the de Bruijn graph whose vertices are all strings of length n − 1. For binary strings and n = 3, the cyclic sequence 00010111 contains all eight 3-bit strings.
Why did my string not come back?
Either a k-mer repeats in it, so it appears only once in the graph, or a (k−1)-mer repeats, so the walk had a choice and took another path through the same k-mers. Try a larger k: once every (k−1)-mer is unique, the path is forced.

Related algorithms: Eulerian Path (Undirected), Fleury's Algorithm, Chinese Postman, Hamiltonian Path, Traveling Salesman Problem

An example graph: you can change it, or start blank.

Zoom Controls

37%
Nodes: 7
Edges: 8