summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--graph.py7
-rw-r--r--test/unittest_graph.py11
2 files changed, 12 insertions, 6 deletions
diff --git a/graph.py b/graph.py
index ae27d45..b66f10d 100644
--- a/graph.py
+++ b/graph.py
@@ -202,11 +202,14 @@ def ordered_nodes(graph):
break
else:
deps_ok.append(node)
- order.extend(sorted(deps_ok))
+ order.append(deps_ok)
order_set |= set(deps_ok)
for node in deps_ok:
del graph[node]
- return tuple(order)
+ result = []
+ for grp in reversed(order):
+ result.extend(sorted(grp))
+ return tuple(result)
def get_cycles(graph_dict, vertices=None):
diff --git a/test/unittest_graph.py b/test/unittest_graph.py
index e72d8d4..bd739bf 100644
--- a/test/unittest_graph.py
+++ b/test/unittest_graph.py
@@ -54,9 +54,12 @@ class ordered_nodesTC(TestCase):
self.assertEqual(ordered, ('a',))
def test_single_dependency(self):
- graph = {'a':[], 'b':['a']}
+ graph = {'a':['b'], 'b':[]}
ordered = ordered_nodes(graph)
self.assertEqual(ordered, ('a','b'))
+ graph = {'a':[], 'b':['a']}
+ ordered = ordered_nodes(graph)
+ self.assertEqual(ordered, ('b','a'))
def test_two_items_no_dependency(self):
graph = {'a':[], 'b':[]}
@@ -69,14 +72,14 @@ class ordered_nodesTC(TestCase):
self.assertEqual(ordered, ('a', 'b', 'c'))
def test_three_items_one_dependency(self):
- graph = {'a': ['b'], 'b': [], 'c':[]}
+ graph = {'a': ['c'], 'b': [], 'c':[]}
ordered = ordered_nodes(graph)
- self.assertEqual(ordered, ('b', 'c', 'a'))
+ self.assertEqual(ordered, ('a', 'b', 'c'))
def test_three_items_two_dependencies(self):
graph = {'a': ['b'], 'b': ['c'], 'c':[]}
ordered = ordered_nodes(graph)
- self.assertEqual(ordered, ('c', 'b', 'a'))
+ self.assertEqual(ordered, ('a', 'b', 'c'))
def test_bad_graph(self):
graph = {'a':['b']}