* assert'u yapistir
This commit is contained in:
@@ -88,5 +88,7 @@ class digraph(object):
|
|||||||
def topological_sort(self):
|
def topological_sort(self):
|
||||||
l = []
|
l = []
|
||||||
self.dfs(lambda u: l.append(u))
|
self.dfs(lambda u: l.append(u))
|
||||||
|
l.reverse()
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+8
-3
@@ -11,10 +11,15 @@ class GraphTestCase(unittest.TestCase):
|
|||||||
g0.from_list([ (1,2), (1,3), (2,3), (3,4), (4, 5), (4,1)])
|
g0.from_list([ (1,2), (1,3), (2,3), (3,4), (4, 5), (4,1)])
|
||||||
|
|
||||||
g1 = graph.digraph()
|
g1 = graph.digraph()
|
||||||
g1.from_list([ (0,2), (0,3), (3,4), (2,4), (5,4) ])
|
g1.from_list([ (0,2), (0,3), (3,4), (2,4), (0,5), (5,4) ])
|
||||||
self.g1 = g1
|
self.g1 = g1
|
||||||
|
|
||||||
|
def testCycle(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def testTopologicalSort(self):
|
def testTopologicalSort(self):
|
||||||
print self.g1.topological_sort()
|
order = self.g1.topological_sort()
|
||||||
|
self.assertEqual(order[0], 0)
|
||||||
|
self.assertEqual(order[len(order)-1], 4)
|
||||||
|
|
||||||
suite = unittest.makeSuite(GraphTestCase)
|
suite = unittest.makeSuite(GraphTestCase)
|
||||||
|
|||||||
Reference in New Issue
Block a user