Trivial Graph Format (TGF)
Trivial Graph Format (TGF) is a simple, plain-text file format for serializing a graph — the structure of nodes and edges that make up a graph — with no accompanying schema. It was adopted informally by the graph drawing community as the smallest notation that is still useful, and it is widely supported by graph editors and analysis tools precisely because it is so easy to read, write, and parse. The format is not governed by a formal specification, so implementations vary in detail.
A TGF file is two lists separated by a line containing the single character
#. The first list defines nodes, one per line, as a node identifier followed
optionally by a space and a label. The second list defines edges, one per
line, as the two endpoint identifiers separated by a space, optionally followed
by a space and an edge label. There is no header, no attribute syntax, and no
way to attach weights, coordinates, colors, or any other data to a node or edge
beyond a single label string.
A small directed graph with three nodes and a cycle between them looks like this.
1 node one 2 node two 3 node three # 1 2 edge a 2 3 edge b 3 1 edge c
The file says nothing about whether the graph is directed. A reader interprets the edges as directed or undirected according to its own convention. To represent a bidirectional edge in a directed reading, either emit two edges (one in each direction) or distinguish the edge through its label. Node identifiers are most often integers, but some implementations accept alphanumeric identifiers, which is one of the many small ways the lack of a standard shows up.
TGF’s strength is its minimalism. A graph can be authored by hand in any text editor, generated by a few lines of script, and parsed with nothing more than a line splitter and a single delimiter check. That makes it well suited to teaching, quick prototyping, and ad hoc exchange between tools that already agree on what the labels mean. Its weakness is the flip side of the same minimalism. There is no way to record graph-level metadata such as whether the graph is directed, no place for typed attribute data, and no schema to validate against. Two tools exchanging a TGF file must agree out of band on every convention that the format leaves open.
For richer interchange, the neighbouring formats trade readability for
expressiveness. Graph Modelling Language (GML) keeps the
plain-text, human-readable character but adds hierarchical key-value pairs so
that nodes and edges can carry arbitrary attributes, weights, and a
directed flag. GraphML goes further still, with an
XML schema that supports nested subgraphs, ports, hyperedges, and typed
application data. TGF sits at the sparse end of the same family — the format to
reach for when the graph is small and the labels are enough.
TGF is supported by graph editors and toolkits including yEd, which uses it as one of its import and export formats, the yFiles graph drawing library, and Wolfram Mathematica. It is rarely the native format of any tool, but it is a common lingua franca for moving a small graph from one place to another with no ceremony.