Skip to content

Commit

Permalink
Refactored eades append/prepend
Browse files Browse the repository at this point in the history
  • Loading branch information
thvitt committed Mar 6, 2019
1 parent 5201a2a commit aaa2e75
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
19 changes: 14 additions & 5 deletions src/macrogen/fes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

class Eades:

def to_start(self, node):
"""
Removes the node from the graph and appends it to the start sequence.
"""
self.start.append(node)
self.graph.remove_node(node)

def to_end(self, node):
self.end.insert(0, node)
self.graph.remove_node(node)

def _exhaust_sinks(self, sink: bool = True):
"""
Produces all sinks until there are no more.
Expand All @@ -25,7 +36,6 @@ def _exhaust_sinks(self, sink: bool = True):
sinks = [u for (u, d) in sink_method() if d == 0]
if sinks:
yield from sinks
self.graph.remove_nodes_from(sinks)
else:
return

Expand Down Expand Up @@ -77,14 +87,13 @@ def solve(self) -> List[Tuple[V, V]]:
self.graph.remove_edges_from(list(self.graph.selfloop_edges()))
while self.graph:
for v in self._exhaust_sinks():
self.end.insert(0, v)
self.to_end(v)
for v in self._exhaust_sources():
self.start.append(v)
self.to_start(v)
if self.graph:
u = max(self.graph.nodes, key=lambda v: self.graph.out_degree(v, weight='weight')
- self.graph.in_degree(v, weight='weight'))
self.start.append(u)
self.graph.remove_node(u)
self.to_start(u)
ordering = self.start + self.end
pos = dict(zip(ordering, itertools.count()))
feedback_edges = list(self.original_graph.selfloop_edges())
Expand Down
10 changes: 4 additions & 6 deletions tests/test_fes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ def graph1():

def test_all_sinks(graph1):
eades = Eades(graph1)
sinks = list(eades._exhaust_sinks())
sinks = []
for sink in eades._exhaust_sinks():
eades.graph.remove_node(sink)
sinks.append(sink)
assert sinks == [5, 4]


def test_all_sources(graph1):
assert list(Eades(graph1)._exhaust_sources()) == [1]


def test_eades(graph1):
assert list(eades(graph1)) == [(3, 2)]

Expand Down

0 comments on commit aaa2e75

Please sign in to comment.