def test_hamburger():
    peel_onions = 'peel onions'
    chop_onions = 'chop onions'
    fry_onions = 'fry onions'
    grill_patty = 'grill patty'
    put_patty_on_bun = 'put patty on bun'
    put_onion_on_patty = 'put onion on patty'
    put_top_on_bun = 'put_top_on_bun'
    serve = 'serve'

    gr = g.Graph(is_directed=True)
    gr.add_edge(put_top_on_bun, serve)
    gr.add_edge(put_onion_on_patty, put_top_on_bun)
    gr.add_edge(put_patty_on_bun, put_onion_on_patty)
    gr.add_edge(grill_patty, put_patty_on_bun)
    gr.add_edge(fry_onions, put_onion_on_patty)
    gr.add_edge(chop_onions, fry_onions)
    gr.add_edge(peel_onions, chop_onions)

    assert t.topological_sort(gr) == [
        peel_onions,
        chop_onions,
        fry_onions,
        grill_patty,
        put_patty_on_bun,
        put_onion_on_patty,
        put_top_on_bun,
        serve
    ]
def test_cycle():
    gr = g.Graph(is_directed=True)
    gr.add_edge('a', 'b')
    gr.add_edge('b', 'c')
    gr.add_edge('c', 'a')
    with pytest.raises(Exception):
        t.topological_sort(gr)
Example #3
0
def test():
    graph = g.Graph(is_directed=True)
    graph.add_edge('a', 'b')
    graph.add_edge('b', 'c')
    graph.add_edge('c', 'd')
    graph.add_edge('d', 'e')
    graph.add_edge('e', 'f')
    graph.add_edge('a', 'f')

    assert sp.find(graph, 'a', 'e') == ['a', 'b', 'c', 'd', 'e']
    assert sp.find(graph, 'a', 'f') == ['a', 'f']
    assert sp.find(graph, 'e', 'a') is None
def test():
    graph = g.Graph(is_directed=True)
    graph.add_edge('a', 'b')
    graph.add_edge('a', 'c')
    graph.add_edge('a', 'e')
    graph.add_edge('b', 'd')
    graph.add_edge('b', 'f')
    graph.add_edge('c', 'g')
    graph.add_edge('f', 'e')
    l = []
    b.bfs(graph, 'a', visit=l.append)
    assert l == ['a', 'b', 'c', 'e', 'd', 'f', 'g']
Example #5
0
def test():
    ug = g.Graph(is_directed=False)
    assert ug.graph == {}
    ug.add_edge('a', 'b', 5)
    assert ug.graph == {
        'a': {
            'b': 5
        },
        'b': {
            'a': 5
        },
    }
    ug.delete_edge('a', 'b')
    assert ug.graph == {
        'a': {},
        'b': {},
    }
    ug.delete_vertex('a')
    ug.delete_vertex('b')
    assert ug.graph == {}

    ug.add_edge('c', 'd', 10)
    assert ug.graph == {
        'c': {
            'd': 10
        },
        'd': {
            'c': 10
        },
    }
    ug.delete_vertex('c')
    assert ug.graph == {
        'd': {},
    }
    ug.delete_vertex('d')
    assert ug.graph == {}

    for i in range(10):
        ug.add_edge('v{}'.format(i), '#v{}'.format(i))

    for i in range(10):
        assert list(ug.edges_from('v{}'.format(i))) == [
            g.Edge('v{}'.format(i), '#v{}'.format(i), 1)
        ]
        assert list(ug.edges_from('#v{}'.format(i))) == [
            g.Edge('#v{}'.format(i), 'v{}'.format(i), 1)
        ]

    expected_edges = []
    for i in range(10):
        expected_edges.append(g.Edge('v{}'.format(i), '#v{}'.format(i), 1))
        expected_edges.append(g.Edge('#v{}'.format(i), 'v{}'.format(i), 1))
    assert sorted(list(ug.edges())) == sorted(expected_edges)

    expected_vertices = []
    for i in range(10):
        expected_vertices.append('v{}'.format(i))
        expected_vertices.append('#v{}'.format(i))
    assert sorted(list(ug.vertices())) == sorted(expected_vertices)

    for i in range(10):
        ug.delete_vertex('v{}'.format(i))
        ug.delete_vertex('#v{}'.format(i))
    assert ug.graph == {}