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

Find the longest path to each node from a starting node #161

Merged
merged 16 commits into from Jun 6, 2018

Conversation

justcalamari
Copy link
Contributor

No description provided.

@@ -0,0 +1,40 @@
import networkx as nx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module should have a docstring

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, according to PEP8, Python standard library imports should come before 3rd part library imports. Please swap these two and add a blank line in between.

from copy import deepcopy


def cyclic_topological_sort(graph, source):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, all public functions should have docstrings.

@scopatz
Copy link
Contributor

scopatz commented May 30, 2018

Thanks for putting this in @justcalamari! This would be a prime candiate for adding some unit tests since it is so self-contained.

g2.remove_node(node)

dist = get_longest_paths(g2, source)
return {v: [k for k in dist if dist[k] == v] for v in dist.values()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is perhaps the most readable nested comprehension I have ever seen, there are a couple of more efficient methods. For example, with a defaultdict you can only have one loop.

from collections import defaultdict

rtn = defaultdict(list)
for k, v in dist.items():
    rtn[v].append(k)
return rtn

Or you can dive into using itertools.groupby() but I am not sure it will make the code more readable

@justcalamari
Copy link
Contributor Author

@scopatz Ready for review

@scopatz
Copy link
Contributor

scopatz commented May 31, 2018

Looks great, I think it just needs tests

@justcalamari
Copy link
Contributor Author

@scopatz Ready for review

return dist


def get_levels(graph_file, source):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this take in a graph rather than a file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's probably a good idea.

Copy link
Member

@CJ-Wright CJ-Wright left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Not needed for merge, but would it be possible to visualize the graphs in the tests?

@scopatz scopatz merged commit 7da16c3 into regro:master Jun 6, 2018
@justcalamari justcalamari deleted the rebuild branch June 6, 2018 03:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants