Exemplo n.º 1
0
    graph.add_edge('B', 'C')
    graph.add_edge('B', 'D')
    graph.add_edge('D', 'E')
    graph.add_edge('F', 'G')

    # Or, read a graph in from a file
    # graph = read_graph_from_file('test_files/graph_small_directed.txt')

    # Output the vertices & edges
    # Print vertices
    print(f'The vertices are: {graph.get_vertices()} \n')

    # Print edges
    print('The edges are:')
    for vertex_obj in graph.get_vertices():
        for neighbor_obj in vertex_obj.get_neighbors():
            print(f'({vertex_obj.get_id()} , {neighbor_obj.get_id()})')

    # Search the graph
    print('Performing BFS traversal...')
    graph.bfs_traversal('A')

    # Find shortest path
    print('Finding shortest path from vertex A to vertex E...')
    shortest_path = graph.find_shortest_path('A', 'E')
    print(shortest_path)

    # Find all vertices N distance away
    print('Finding all vertices distance 2 away...')
    vertices_2_away = graph.find_vertices_n_away('A', 2)
    print(vertices_2_away)