Example #1
0
def test_bfs():
    gm = MatrixGraph()
    d = ['s', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
    for data in d:
        gm.insert_vertex(data)

    gm.insert_edge(0, 1, 1)
    gm.insert_edge(0, 3, 1)
    gm.insert_edge(0, 4, 1)

    gm.insert_edge(1, 0, 1)
    gm.insert_edge(1, 3, 1)
    gm.insert_edge(1, 5, 1)

    gm.insert_edge(2, 3, 1)
    gm.insert_edge(2, 4, 1)
    gm.insert_edge(2, 7, 1)

    gm.insert_edge(3, 0, 1)
    gm.insert_edge(3, 1, 1)
    gm.insert_edge(3, 2, 1)

    gm.insert_edge(4, 0, 1)
    gm.insert_edge(4, 2, 1)

    gm.insert_edge(5, 1, 1)
    gm.insert_edge(5, 6, 1)
    gm.insert_edge(5, 7, 1)

    gm.insert_edge(6, 5, 1)
    gm.insert_edge(6, 7, 1)

    gm.insert_edge(7, 2, 1)
    gm.insert_edge(7, 5, 1)
    gm.insert_edge(7, 6, 1)

    for i in gm.bfs(0):
        print i.data, i.distance, i.dtime, i.parent
Example #2
0
def test_dfs():
    gm = MatrixGraph()
    d = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    for data in d:
        gm.insert_vertex(data)

    gm.insert_edge(0, 1, 1)
    gm.insert_edge(0, 3, 1)
    gm.insert_edge(0, 4, 1)

    gm.insert_edge(1, 0, 1)
    gm.insert_edge(1, 2, 1)
    gm.insert_edge(1, 4, 1)

    gm.insert_edge(2, 1, 1)
    gm.insert_edge(2, 5, 1)

    gm.insert_edge(3, 0, 1)
    gm.insert_edge(3, 6, 1)
    gm.insert_edge(3, 8, 1)

    gm.insert_edge(4, 0, 1)
    gm.insert_edge(4, 1, 1)

    gm.insert_edge(5, 2, 1)
    gm.insert_edge(5, 6, 1)
    gm.insert_edge(5, 7, 1)

    gm.insert_edge(6, 3, 1)
    gm.insert_edge(6, 5, 1)
    gm.insert_edge(6, 7, 1)
    gm.insert_edge(6, 8, 1)
    gm.insert_edge(6, 9, 1)

    gm.insert_edge(7, 5, 1)
    gm.insert_edge(7, 6, 1)

    gm.insert_edge(8, 3, 1)
    gm.insert_edge(8, 6, 1)

    gm.insert_edge(9, 6, 1)

    gm.dfs(0, first=True)

    for i in gm.V:
        print i.data, i.dtime, i.ftime