-
-
Notifications
You must be signed in to change notification settings - Fork 528
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
Provide an iterative version on some functions on trees #39519
base: develop
Are you sure you want to change the base?
Conversation
if not subtree.is_empty(): | ||
stack.append(subtree) | ||
else: | ||
stack.pop() | ||
node = stack.pop() | ||
action(node) | ||
|
||
def contour_traversal(self,first_action=None, middle_action=None, final_action=None, leaf_action=[]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def contour_traversal(self,first_action=None, middle_action=None, final_action=None, leaf_action=[]): | |
def contour_traversal(self, first_action=None, middle_action=None, final_action=None, leaf_action=None): |
I don't see why leaf_action
should not be None
by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want that if you change only first_action
and final_action
, leaf_action
will be the action of both of them but it's perhaps a bad idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand, can you give an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you look at the depth code : i only change the final_action
and it change accordingly the leaf_action
.
I would like it to work like that.
The algorithm is:: | ||
|
||
manipulate the root with function `first_action`; | ||
iteratively manipulate the root with function `middle_action` | ||
and explore each subtree (by the algorithm) from the | ||
leftmost one to the rightmost one; | ||
then manipulate the root with function `final_action`; | ||
if the root is a leaf it only manipulate with function `leaf_action`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The algorithm is:: | |
manipulate the root with function `first_action`; | |
iteratively manipulate the root with function `middle_action` | |
and explore each subtree (by the algorithm) from the | |
leftmost one to the rightmost one; | |
then manipulate the root with function `final_action`; | |
if the root is a leaf it only manipulate with function `leaf_action`. | |
ALGORITHM: | |
- apply `first_action` to the root | |
- iteratively apply `middle_action` to the root and traverse each subtree from the | |
leftmost one to the rightmost one | |
- apply `final_action` to the root if it is not a leaf, and apply `leaf_action` to the root otherwise |
(I'm not sure whether my modification introduce mistakes, please double check!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The algorithm is:: | |
manipulate the root with function `first_action`; | |
iteratively manipulate the root with function `middle_action` | |
and explore each subtree (by the algorithm) from the | |
leftmost one to the rightmost one; | |
then manipulate the root with function `final_action`; | |
if the root is a leaf it only manipulate with function `leaf_action`. | |
ALGORITHM: | |
- if the root is a leaf, apply `leaf_action` | |
- else | |
- apply `first_action` to the root | |
- iteratively apply `middle_action` to the root and traverse each subtree | |
from the leftmost one to the rightmost one | |
- apply `final_action` to the root |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does but i fixed it here.
sage: t.contour_traversal(first_action = lambda node: l.append(0),leaf_action = None) | ||
sage: len(l) | ||
7 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
""" | ||
if first_action is None: | ||
def first_action(x): | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return None | |
return |
return None | ||
if middle_action is None: | ||
def middle_action(x): | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return None | |
return |
return None | ||
if final_action is None: | ||
def final_action(x): | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return None | |
return |
return None | ||
if leaf_action is None: | ||
def leaf_action(x): | ||
None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None | |
return |
final_action(x) | ||
stack = [] | ||
stack.append(self) | ||
corners = [0,0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
corners = [0,0] | |
corners = [0, 0] |
corners = [0,0] | ||
while stack: | ||
node = stack.pop() | ||
if not(bool(node)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not(bool(node)): | |
if not node: |
leaf_action(node) | ||
corners.pop() | ||
corners[-1] += 1 | ||
elif corners[-1] == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif corners[-1] == 0: | |
elif not corners[-1]: |
def fr_action(node, depths, m, depth): | ||
if depths[-1] == depth: | ||
m[0] += 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def fr_action(node, depths, m, depth): | |
if depths[-1] == depth: | |
m[0] += 1 | |
m = 0 | |
def fr_action(node): | |
nonlocal m, depths, depth | |
if depths[-1] == depth: | |
m += 1 |
def m_action(node,depths): | ||
depths.append(depths[-1]+1) | ||
def fn_action(node,depths): | ||
depths.pop() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def m_action(node,depths): | |
depths.append(depths[-1]+1) | |
def fn_action(node,depths): | |
depths.pop() | |
def m_action(node): | |
nonlocal depths | |
depths.append(depths[-1] + 1) | |
def fn_action(node): | |
nonlocal depths | |
depths.pop() |
m = [0] | ||
self.contour_traversal(lambda node: fr_action(node, depths,m, depth),lambda node: m_action(node, depths),lambda node: fn_action(node, depths)) | ||
return m[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m = [0] | |
self.contour_traversal(lambda node: fr_action(node, depths,m, depth),lambda node: m_action(node, depths),lambda node: fn_action(node, depths)) | |
return m[0] | |
self.contour_traversal(fr_action, m_action, fn_action) | |
return m |
return Integer(0 if self.is_empty() else 1) | ||
if self.is_empty(): | ||
return 0 | ||
def action(node,m): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def action(node,m): | |
m = [] | |
def action(node): | |
nonlocal m |
m.append(0) | ||
else: | ||
mx = max(m.pop() for _ in node) | ||
m.append(mx+1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m.append(mx+1) | |
m.append(mx + 1) |
m = [] | ||
self.contour_traversal(final_action = lambda node: action(node,m)) | ||
return m[0]+1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m = [] | |
self.contour_traversal(final_action = lambda node: action(node,m)) | |
return m[0]+1 | |
self.contour_traversal(final_action=action) | |
return m[0] + 1 |
This pull request provide iterative methods on AbstractTrees which work both on OrderedTree and BinaryTree.
The problem was that some functions like node_number reach the recursion limit on huge instances.
📝 Checklist