Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lcsm() to retrieve multiple lcs when max size is equal #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ print(st.find_all("ab")) # [0, 8]
a = ["xxxabcxxx", "adsaabc", "ytysabcrew", "qqqabcqw", "aaabc"]
st = STree.STree(a)
print(st.lcs()) # "abc"

# Generalized Suffix-Tree example with multiple lcs equal sized
a = ["klexxxabc", "kleyyyabc"]
st = STree.STree(a)
print(st.lcsm()) # ["kle", "abc"]
```
46 changes: 42 additions & 4 deletions suffix_trees/STree.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,44 @@ def lcs(self, stringIdxs=-1):
end = deepestNode.idx + deepestNode.depth
return self.word[start:end]

def lcsm(self, stringIdxs=-1):
"""Returns all Largest Common Substrings of Strings provided in stringIdxs
if there are lcs of equal size, a list with all lcs is returned.
If stringIdxs is not provided, the LCS of all strings is returned.

::param stringIdxs: Optional: List of indexes of strings.
"""
if stringIdxs == -1 or not isinstance(stringIdxs, list):
stringIdxs = set(range(len(self.word_starts)))
else:
stringIdxs = set(stringIdxs)

deepestNodes = self._find_lcsm(self.root, stringIdxs)
if len(deepestNodes) == 0:
return []

return [ self.word[n.idx:(n.idx + n.depth)] for n in deepestNodes ]

def _find_lcsm(self, node, stringIdxs):
"""Helper method that finds LCS by traversing the labeled GSD."""
deepestNodes = []
nodes = [self._find_lcs(n, stringIdxs)
for (n,_) in node.transition_links
if n.generalized_idxs.issuperset(stringIdxs)]

if nodes == []:
return deepestNodes

nodes.sort(key=lambda x: x.depth, reverse=True)
deepestNodes = []
maxDepth = nodes[0].depth

for n in nodes:
if n.depth == maxDepth:
deepestNodes = deepestNodes + [n]

return deepestNodes

def _find_lcs(self, node, stringIdxs):
"""Helper method that finds LCS by traversing the labeled GSD."""
nodes = [self._find_lcs(n, stringIdxs)
Expand Down Expand Up @@ -191,18 +229,18 @@ def find(self, y):
edge = self._edgeLabel(node, node.parent)
if edge.startswith(y):
return node.idx

i = 0
while(i < len(edge) and edge[i] == y[0]):
y = y[1:]
i += 1

if i != 0:
if i == len(edge) and y != '':
pass
else:
return -1

node = node._get_transition_link(y[0])
if not node:
return -1
Expand All @@ -219,7 +257,7 @@ def find_all(self, y):
while(i < len(edge) and edge[i] == y[0]):
y = y[1:]
i += 1

if i != 0:
if i == len(edge) and y != '':
pass
Expand Down