本文整理汇总了Python中Graph.Graph.topological_sort方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.topological_sort方法的具体用法?Python Graph.topological_sort怎么用?Python Graph.topological_sort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph.Graph
的用法示例。
在下文中一共展示了Graph.topological_sort方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_DAG_sort
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_DAG_sort(self):
graph = Graph()
graph.add_directed_link(1, 2)
graph.add_directed_link(1, 3)
graph.add_directed_link(2, 4)
graph.add_directed_link(3, 4)
graph.add_directed_link(4, 5)
graph.add_directed_link(2, 3)
self.assertEqual(graph.topological_sort(), [1, 2, 3, 4, 5])
示例2: test_two_connected_components
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_two_connected_components(self):
graph = Graph()
graph.add_directed_link(1, 2)
graph.add_directed_link(2, 3)
graph.add_directed_link(3, 1)
graph.add_directed_link(4, 5)
graph.add_directed_link(5, 6)
graph.add_directed_link(6, 4)
self.assertEqual(graph.topological_sort(), None)
示例3: test_cycle_with_isolated_points
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_cycle_with_isolated_points(self):
graph = Graph()
graph.add_directed_link(1, 2)
graph.add_vertex(6)
graph.add_vertex(7)
graph.add_directed_link(2, 3)
graph.add_vertex(1)
graph.add_directed_link(6, 4)
graph.add_directed_link(3, 1)
graph.add_directed_link(3, 4)
self.assertEqual(graph.topological_sort(), None)
示例4: test_many_connected_components_with_cycle
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_many_connected_components_with_cycle(self):
graph = Graph()
graph.add_directed_link(1, 2)
graph.add_directed_link(1, 3)
graph.add_directed_link(1, 4)
graph.add_directed_link(2, 4)
graph.add_directed_link(5, 6)
graph.add_directed_link(7, 5)
graph.add_directed_link(7, 9)
graph.add_directed_link(11, 12)
graph.add_directed_link(13, 12)
graph.add_directed_link(11, 15)
graph.add_directed_link(15, 16)
graph.add_directed_link(15, 17)
graph.add_directed_link(17, 10)
graph.add_directed_link(10, 11)
graph.add_directed_link(10, 17)
self.assertEqual(graph.topological_sort(), None)
示例5: test_not_connected_DAG_sort
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_not_connected_DAG_sort(self):
graph = Graph()
graph.add_directed_link(1, 2)
graph.add_directed_link(1, 3)
graph.add_directed_link(2, 4)
graph.add_directed_link(3, 4)
graph.add_directed_link(4, 5)
graph.add_directed_link(2, 3)
graph.add_directed_link(6, 7)
graph.add_directed_link(6, 8)
graph.add_directed_link(7, 9)
graph.add_directed_link(8, 9)
graph.add_directed_link(9, 10)
graph.add_directed_link(7, 8)
ans1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ans2 = [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
arr = graph.topological_sort()
match1 = True
match2 = True
for i in range(10):
match1 = match1 or (arr[i] == ans1[i])
for i in range(10):
match2 = match2 or (arr[i] == ans2[i])
self.assertTrue(match1 or match2)
示例6: test_empty
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_empty(self):
graph = Graph()
self.assertEqual(graph.topological_sort(), [])
示例7: test_cycle
# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import topological_sort [as 别名]
def test_cycle(self):
graph = Graph()
graph.add_directed_link(1, 2)
graph.add_directed_link(2, 3)
graph.add_directed_link(3, 1)
self.assertEqual(graph.topological_sort(), None)