Example #1
0
def main():
    symbol = ['a', 'b', 'c', 'd', 'e', 'f', 'h', 'p', 'q', 'r', 'S', 'G']

    graph_matrix = [
        # a, b, c, d, e, f, h, p, q, r, S, G
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]

    S = 10
    G = 11

    graph = GraphSearch(graph_matrix)

    dfs = graph.dfs(S, G)
    print('Depth first search: ')
    print_search_tree(dfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], dfs.path)))

    bfs = graph.bfs(S, G)
    print('Breadth first search: ')
    print_search_tree(bfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], bfs.path)))
Example #2
0
def main():
    symbol = [
        'a', 'b', 'c', 'd', 'e', 'f', 'h', 'p', 'q', 'r', 'S', 'G'
    ]

    graph_matrix = [
        # a, b, c, d, e, f, h, p, q, r, S, G
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
        [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]

    S = 10
    G = 11

    graph = GraphSearch(graph_matrix)

    dfs = graph.dfs(S, G)
    print('Depth first search: ')
    print_search_tree(dfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], dfs.path)))

    bfs = graph.bfs(S, G)
    print('Breadth first search: ')
    print_search_tree(bfs.root, display=lambda d: symbol[d])
    print('The route:')
    print('→'.join(map(lambda d: symbol[d], bfs.path)))