Commit 3927ddf7 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Merge branch 'master' of /cygdrive/c/Users/BRAM/Documents/Documenten/Projecten/todo.py.bundle

parents e393029a 8c47b843
......@@ -149,3 +149,21 @@ class DirectedGraph(object):
if self.is_isolated(p_to):
self.remove_node(p_to)
def transitively_reduce(self):
"""
Performs a transitive reduction on the graph.
"""
removals = set()
for from_node, neighbors in self._edges.iteritems():
childpairs = \
[(c1, c2) for c1 in neighbors for c2 in neighbors if c1 != c2]
for pair in childpairs:
if self.has_path(pair[0], pair[1]) \
and not self.has_path(pair[0], from_node):
removals.add((from_node, pair[1]))
for edge in removals:
self.remove_edge(edge[0], edge[1])
......@@ -113,3 +113,10 @@ class GraphTest(unittest.TestCase):
self.assertFalse(self.graph.has_edge(1, 3))
self.assertFalse(self.graph.has_edge(3, 5))
self.assertFalse(self.graph.has_path(1, 5))
def test_transitive_reduce1(self):
self.graph.transitively_reduce()
self.assertTrue(self.graph.has_edge(4, 3))
self.assertFalse(self.graph.has_edge(1, 3))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment