Exemplo n.º 1
0
def test_topological_sort_cyclic_graph():
    graph = DiGraph()
    for node in [0, 1, 2]:
        graph.add_node(node)

    graph.add_edge(0, 1)
    graph.add_edge(1, 2)
    graph.add_edge(2, 0)

    with pytest.raises(CyclicDiGraphError):
        graph.topological_sort()
Exemplo n.º 2
0
def test_topological_sort():
    # Example randomly generated with
    # https://www.cs.usfca.edu/~galles/visualization/TopoSortDFS.html
    graph = DiGraph()
    for node in range(8):
        graph.add_node(node)

    graph.add_edge(0, 2)
    graph.add_edge(0, 3)
    graph.add_edge(2, 4)
    graph.add_edge(2, 6)
    graph.add_edge(4, 7)
    graph.add_edge(6, 7)
    graph.add_edge(3, 5)
    graph.add_edge(1, 5)
    graph.add_edge(3, 7)

    assert [1, 0, 3, 5, 2, 6, 4, 7] == graph.topological_sort()
Exemplo n.º 3
0
def test_topological_sort_single_node():
    graph = DiGraph()
    graph.add_node(0)
    assert [0] == graph.topological_sort()
Exemplo n.º 4
0
def test_topological_sort_empty_graph():
    graph = DiGraph()
    assert [] == graph.topological_sort()