Repository navigation
compiler: Refactor connected_components #3054
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -585,18 +585,25 @@ def topological_sort(self, choose_element=None): | |
|
|
||
| def connected_components(self, enumerated=False): | ||
| """ | ||
| Find all connected sub-graphs and return them as a list. | ||
| Find all weakly connected sub-graphs and return them as a list. | ||
| """ | ||
| groups = [] | ||
|
|
||
| for n0 in self.graph: | ||
| found = {n0} | set(self.all_downstreams(n0)) | ||
| for g in groups: | ||
| if g.intersection(found): | ||
| g.update(found) | ||
| break | ||
| else: | ||
| groups.append(found) | ||
| parent = {n: n for n in self.graph} | ||
|
|
||
| def find(n): | ||
| while parent[n] is not n: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We tend to avoid those while forms in the codebase.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason why? Asking because the refactor without the while loop seems uglier
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because they can loop forever if not bullet proof, which is the ugliest of the symptoms a user can ever encounter so... should we trust this? what would the refactor look like? |
||
| parent[n] = parent[parent[n]] # Path halving | ||
| n = parent[n] | ||
| return n | ||
|
|
||
| for n0, downstreams in self.graph.items(): | ||
| for n1 in downstreams: | ||
| parent[find(n1)] = find(n0) | ||
|
|
||
| # Ordered by first appearance in `self.graph` | ||
| groups = OrderedDict() | ||
| for n in self.graph: | ||
| groups.setdefault(find(n), set()).add(n) | ||
| groups = list(groups.values()) | ||
|
|
||
| if enumerated: | ||
| mapper = OrderedDict() | ||
|
|
||
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.
remind me what weakly connected means?