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

Fix/157 #161

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

Fix/157 #161

wants to merge 7 commits into from

Conversation

fgrunewald
Copy link
Member

First Draft of generalized restraint algorithm

Comment on lines +188 to +195
all_paths = list(nx.all_simple_edge_paths(graph, source, target=target))
max_path_length = 0
idx = 0
for jdx, path in enumerate(all_paths):
if len(path) > max_path_length:
max_path_length = len(path)
idx = jdx
return all_paths[idx]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
all_paths = list(nx.all_simple_edge_paths(graph, source, target=target))
max_path_length = 0
idx = 0
for jdx, path in enumerate(all_paths):
if len(path) > max_path_length:
max_path_length = len(path)
idx = jdx
return all_paths[idx]
all_paths = list(nx.all_simple_edge_paths(graph, source, target=target))
return max(all_paths, key=len)

Unless you want to deal with multiple longest paths

Copy link
Member Author

Choose a reason for hiding this comment

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

I want to deal with it in the sense that there can be more than one with equivalent path length. But the smaller code should account for that I guess?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Then you should get the one with the lowest index, but don't pin me on that.

Comment on lines +197 to +221
def polyply_custom_tree(graph, source, target):
# find all simple paths from source to target
longest_path = _find_longest_path(graph, source, target)
path_nodes = { u for edge in longest_path for u in edge }
# substract path and extract the remaining graphs of the branches
graph_copy = graph.copy()
graph_copy.remove_edges_from(longest_path)
components = list(nx.connected_components(graph_copy))
# loop over the edges in the longest path and
# see if they are part of a connected component
# then add the bfs tree edges of that component to the
# graph before the edge
tree_graph = nx.DiGraph()
for from_node, to_node in longest_path:
for c in components:
if from_node in c:
break

subgraph = graph_copy.subgraph(c)
for u, v in nx.bfs_tree(subgraph, source=from_node).edges:
if u not in path_nodes or v not in path_nodes:
tree_graph.add_edge(u, v)
tree_graph.add_edge(from_node, to_node)

return tree_graph
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

yes but that is intended. The polyply search tree should be a directed graph without cycles. The tree should have been a separate PR but since it made it in. Basically the idea is to get rid of the dfs vs bfs thing for cycles. To do that I need to make a tree which essentially is a DFS tree along the backbone (i.e. longest path) but the branches are placed bfs like with cycles being replaced by distance restraints.

target_node=None):

# graph distances can be computed from the path positions
graph_distances_ref = {node: path.index(node) for node in path}
Copy link
Member

Choose a reason for hiding this comment

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

Expensive! path.index costs N for the N=len(path)

Copy link
Member Author

Choose a reason for hiding this comment

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

but its cheaper than computing the disjstra path length no?

Copy link
Member

Choose a reason for hiding this comment

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

Sure. Consider however, the following:
{node: idx for (idx, node) in enumerate(path)}

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.

2 participants