Skip to content

Commit

Permalink
sort edges in Grid's graph_edges() method
Browse files Browse the repository at this point in the history
related to #97
  • Loading branch information
nesnoj committed Dec 15, 2016
1 parent f8af5e3 commit 07ed7a0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions dingo/core/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,27 @@ def graph_edges(self):
Note
----
There are generator functions for nodes (`Graph.nodes()`) and edges
(`Graph.edges()`) in NetworkX but unlike graph nodes, which can be
represented by objects, branch objects can only be accessed by using an
edge attribute ('branch' is used here)
To make access to attributes of the branch objects simplier and more
To make access to attributes of the branch objects simpler and more
intuitive for the user, this generator yields a dictionary for each edge
that contains information about adjacent nodes and the branch object.
Note, the construction of the dictionary highly depends on the structure
of the in-going tuple (which is defined by the needs of networkX). If
this changes, the code will break.
"""
for edge in nx.get_edge_attributes(self._graph, 'branch').items():

# get edges with attributes
edges = nx.get_edge_attributes(self._graph, 'branch').items()

# sort them according to connected nodes
edges_sorted = sorted(list(edges), key=lambda _: repr(_[0]))

for edge in edges_sorted:
yield {'adj_nodes': edge[0], 'branch': edge[1]}

def find_path(self, node_source, node_target):
Expand Down

0 comments on commit 07ed7a0

Please sign in to comment.