Skip to content

Commit

Permalink
fixup! Allow to collapse parallel edges in graphs.
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed Jul 20, 2020
1 parent 985abda commit 4fee5ad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/graphviewer/graphviewer.py
Expand Up @@ -8,7 +8,7 @@
from networkx import DiGraph

from macrogen import MacrogenesisInfo, write_dot
from macrogen.graphutils import remove_edges, simplify_timeline, expand_edges, collapse_edges
from macrogen.graphutils import remove_edges, simplify_timeline, expand_edges, collapse_edges, collapse_parallel_edges

app = Flask(__name__)

Expand Down Expand Up @@ -73,7 +73,7 @@ def prepare_agraph():
flash('Cannot produce DAG – subgraph is not acyclic!?', 'error')
g = simplify_timeline(g)
if collapse:
g = collapse_edges(g)
g = collapse_parallel_edges(g)
g.add_nodes_from(nodes)
if order:
g = info.order_graph(g)
Expand Down
6 changes: 3 additions & 3 deletions src/macrogen/graphutils.py
Expand Up @@ -105,15 +105,15 @@ def collapse_parallel_edges(graph: nx.MultiDiGraph) -> nx.MultiDiGraph:
"""
result = nx.MultiDiGraph()
result.add_nodes_from(graph.nodes)
for u, v in set(graph.edges):
for u, v in set(graph.edges(keys=False)):
parallel_edges = list(graph[u][v].values())
attrs = dict(parallel_edges[0])
if len(parallel_edges) > 1:
attrs['source'] = [e['source'] for e in parallel_edges]
attrs['comment'] = '\n'.join(e['comment'] for e in parallel_edges)
attrs['comment'] = '\n'.join(e.get('comment', '') for e in parallel_edges)
attrs['weight'] = sum(e['weight'] for e in parallel_edges)
attrs['iweight'] = 1/attrs['weight']
result.add_edge(u, v, *attrs)
result.add_edge(u, v, **attrs)
return result


Expand Down

0 comments on commit 4fee5ad

Please sign in to comment.