Question: Complete the implementation of the function add _ all _ suffix _ links that given a suffix trie without suffix links, walks the trie from

Complete the implementation of the function add_all_suffix_links that given a suffix trie without suffix links, walks the trie from root down to leaves and computes suffix nodes for a child node given that of a parent node. In particular, we would like you to complete the get_suffix_link_for_dest_node function below as speficied.
def add_all_suffix_links(root, debug=True):
root.add_suffix_link(root) # root must be suffix linked to itself.
worklist =[] # a list of edges wherein parent has a suffix link but child does not
for (_, edge) in root.outgoing_edges.items():
# iterate through all outgoing edges of the root
if not edge.is_leaf_edge():
worklist.append(edge) # add the edge from root to an internal non-leaf node to the worklist.
## now iterate through the worklist
while len(worklist)>=1:
edge = worklist.pop()
assert not edge.is_leaf_edge()
p = edge.src
n = edge.dest
# this is an edge from parent p to node n
s_n = get_suffix_link_for_dest_node(edge) # use the edge data to get the suffix node
n.add_suffix_link(s_n)
if debug:
print(f'Adding suffix link n{n.id}--> n{s_n.id}')
for (_, next_edge) in n.outgoing_edges.items():
if not next_edge.is_leaf_edge():
worklist.append(next_edge)
return
def get_suffix_link_for_dest_node(edge):
p = edge.src
n = edge.dest
assert p.suffix_link != None, f"parent {p.id} has no suffix link but asking for the suffix link of a child"
assert not edge.is_leaf_edge()
orig_str = edge.orig_str
lo = edge.lo
hi = edge.hi
assert 0<= lo <= hi and hi < len(orig_str)
## TODO: implement the algorithm to find the suffix link of n
## given that of p and the data for the edge from p -> n

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!