Skip to main content

A Generalized Suffix Tree for any iterable, with Lowest Common Ancestor retrieval

Project description

A Generalized Suffix Tree for any Python sequence, with Lowest Common Ancestor retrieval.

pip install suffix-tree
>>> from suffix_tree import Tree

>>> tree = Tree({"A": "xabxac"})
>>> tree.find("abx")
True
>>> tree.find("abc")
False

This suffix tree:

  • works with any Python sequence, not just strings, if the items are hashable,

  • is a generalized suffix tree for sets of sequences,

  • is implemented in pure Python,

  • builds the tree in linear time with Ukkonen’s algorithm,

  • does constant-time Lowest Common Ancestor retrieval.

Three different builders have been implemented:

  • one that follows Ukkonen’s original paper ([Ukkonen1995]),

  • one that follows Gusfield’s variant ([Gusfield1997]),

  • and one simple naive algorithm.

Being implemented in Python this tree is not very fast nor memory efficient. The building of the tree takes time proportional to the length of the string of symbols. The query time is proportional to the length of the query string. You can get a rough idea of the performance under: Time Complexity. To get the best performance run with python -O.

PyPi: https://pypi-hypernode.com/project/suffix-tree/

Usage examples:

>>> tree = Tree()
>>> tree.add(1, "xabxac")
>>> tree.add(2, "awyawxawxz")
>>> tree.find("abx")
True
>>> tree.find("awx")
True
>>> tree.find("abc")
False
>>> tree = Tree({"A": "xabxac", "B": "awyawxawxz"})
>>> tree.find_id("A", "abx")
True
>>> tree.find_id("B", "abx")
False
>>> tree.find_id("B", "awx")
True
>>> tree = Tree(
...     {
...         "A": "sandollar",
...         "B": "sandlot",
...         "C": "handler",
...         "D": "grand",
...         "E": "pantry",
...     }
... )
>>> for k, length, path in tree.common_substrings():
...     print(k, length, path)
...
2 4 s a n d
3 3 a n d
4 3 a n d
5 2 a n
>>> tree = Tree({"A": "xabxac", "B": "awyawxawxz"})
>>> for C, path in sorted(tree.maximal_repeats()):
...     print(C, path)
...
1 a w
1 a w x
2 a
2 x
2 x a

Time Complexity

docs/graph_time_complexity.png

References

[Ukkonen1995]

Ukkonen, Esko. On-line construction of suffix trees. 1995. Algorithmica 14:249-60. http://www.cs.helsinki.fi/u/ukkonen/SuffixT1withFigs.pdf

[Gusfield1997]

Gusfield, Dan. Algorithms on strings, trees, and sequences. 1997. Cambridge University Press.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

suffix_tree-0.1.0.tar.gz (363.7 kB view details)

Uploaded Source

Built Distribution

suffix_tree-0.1.0-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

Details for the file suffix_tree-0.1.0.tar.gz.

File metadata

  • Download URL: suffix_tree-0.1.0.tar.gz
  • Upload date:
  • Size: 363.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for suffix_tree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 07721fdb8ddaf8f066f2d07ba08f74a3eb23f4cc669b1239a6e078489a41f87f
MD5 6edfc96d0d6f16fc422304b852637abc
BLAKE2b-256 96576f69dfd13ca1d321499d50d1f7eab938efb22f01bbca2d55ae4760e2a821

See more details on using hashes here.

File details

Details for the file suffix_tree-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: suffix_tree-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for suffix_tree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 411aa71c81907dbf16cd0a869310e0d74b85a6329dd4796e7dfc31b721752a53
MD5 6e818d46c2f0f74bd3875a61d3d396c7
BLAKE2b-256 4ab380b8528dab310d6930202b41fc8208f545fc56ecbc1c56416532b4f2e34e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page