Creates a directed prefix tree from a list of paths.

Usually the paths are described as strings or lists of integers.

A “prefix tree” represents the prefix structure of the strings. Each node represents a prefix of some string. The root represents the empty prefix with children for the single letter prefixes which in turn have children for each double letter prefix starting with the single letter corresponding to the parent node, and so on.

More generally the prefixes do not need to be strings. A prefix refers to the start of a sequence. The root has children for each one element prefix and they have children for each two element prefix that starts with the one element sequence of the parent, and so on.

Note that this implementation uses integer nodes with an attribute. Each node has an attribute “source” whose value is the original element of the path to which this node corresponds. For example, suppose paths consists of one path: “can”. Then the nodes [1, 2, 3] which represent this path have “source” values “c”, “a” and “n”.

All the descendants of a node have a common prefix in the sequence/path associated with that node. From the returned tree, the prefix for each node can be constructed by traversing the tree up to the root and accumulating the “source” values along the way.

The root node is always 0 and has “source” attribute None. The root is the only node with in-degree zero. The nil node is always -1 and has “source” attribute "NIL". The nil node is the only node with out-degree zero.

paths: iterable of paths

An iterable of paths which are themselves sequences. Matching prefixes among these sequences are identified with nodes of the prefix tree. One leaf of the tree is associated with each path. (Identical paths are associated with the same leaf of the tree.)

tree: DiGraph

A directed graph representing an arborescence consisting of the prefix tree generated by paths. Nodes are directed “downward”, from parent to child. A special “synthetic” root node is added to be the parent of the first node in each path. A special “synthetic” leaf node, the “nil” node -1, is added to be the child of all nodes representing the last element in a path. (The addition of this nil node technically makes this not an arborescence but a directed acyclic graph; removing the nil node makes it an arborescence.)


The prefix tree is also known as a trie.


Create a prefix tree from a list of strings with common prefixes:

>>> paths = ["ab", "abs", "ad"]
>>> T = nx.prefix_tree(paths)
>>> list(T.edges)
[(0, 1), (1, 2), (1, 4), (2, -1), (2, 3), (3, -1), (4, -1)]

The leaf nodes can be obtained as predecessors of the nil node:

>>> root, NIL = 0, -1
>>> list(T.predecessors(NIL))
[2, 3, 4]

To recover the original paths that generated the prefix tree, traverse up the tree from the node -1 to the node 0:

>>> recovered = []
>>> for v in T.predecessors(NIL):
...     prefix = ""
...     while v != root:
...         prefix = str(T.nodes[v]["source"]) + prefix
...         v = next(T.predecessors(v))  # only one predecessor
...     recovered.append(prefix)
>>> sorted(recovered)
['ab', 'abs', 'ad']