示例#1
0
def test_cyclic():
    g = Graph()
    g.add_edge(Edge(1, 3))
    g.add_edge(Edge(2, 1))
    assert not g.cyclic()
    g.add_edge(Edge(3, 2))
    assert g.cyclic()
示例#2
0
def test_traverse():
    g = Graph()
    g.add_edge(Edge(1, 2))
    g.add_edge(Edge(1, 3))
    g.add_edge(Edge(2, 4))
    g.add_edge(Edge(2, 5))
    g.add_edge(Edge(5, 3))
    g.add_vertex(6)
    """ Test bfs """
    trace = []
    g.bfs(1, func_in=lambda vertex: trace.append(vertex))
    assert trace == [1, 2, 3, 4, 5]
    """ Test dfs """
    trace = []
    g.dfs(1, func_in=lambda vertex: trace.append(vertex))
    assert trace == [1, 2, 4, 5, 3]
    """ Test traverse """
    trace = []
    g.traverse(order=g.ORDER_BFS, func_in=lambda vertex: trace.append(vertex))
    assert trace == [1, 2, 3, 4, 5, 6]
    """ Test cyclic """
    g.add_edge(Edge(3, 2))
    assert g.cyclic()
    """ Test acyclic """
    g.remove_edge(Edge(3, 2))
    assert not g.cyclic()
    """ Test topological sort """
    assert g.topological() == [6, 1, 2, 5, 3, 4]
    """ Test sptree """
    assert not g.sptree().cyclic()