Reconnecting the Dots
189
spanning tree. Another function,
represent_tree
, turns the key and value pairs
of the dictionary into a tuple and then sorts each of the resulting tuples for better
readability of the tree path:
def represent_tree(treepath):
progression = list()
for node in treepath:
if node != treepath[node]:
progression.append((treepath[node], node))
return sorted(progression, key=lambda x:x[0])
print (represent_tree(treepath))
[('A','B'), ('B','C'), ('B','D'), ('D','E'), ('E','F')]
The
represent_tree
function reorders the output of Prim’s algorithm for better
readability. However, the algorithm works on an undirected graph, which means
that you can traverse the edges in both directions. The algorithm incorporates this
assumption because there is no edge directionality check to add to the priority
queue for later processing.
Do'stlaringiz bilan baham: |