def test_get_vertices():
    g = Graph()
    vertex_apple = g.add('apple')
    vertex_banana = g.add('banana')
    vertex_cake = g.add('cake')
    expected = g.get_vertices()
    assert expected == ['apple', 'banana', 'cake']
Exemplo n.º 2
0
    def test_create_directed_graph(self):
        """Create a graph."""
        graph = Graph(is_directed=True)
        vertex_a = graph.add_vertex('A')
        vertex_b = graph.add_vertex('B')
        vertex_c = graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')

        self.assertEqual(len(graph.get_vertices()), 3)

        self.assertEqual(len(vertex_a.get_neighbors()), 2)
        self.assertEqual(len(vertex_b.get_neighbors()), 1)
        self.assertEqual(len(vertex_c.get_neighbors()), 0)
    def test_create_undirected_graph(self):
        """Create a graph."""
        graph = Graph(is_directed=False)
        graph.add_vertex('A')
        graph.add_vertex('B')
        graph.add_vertex('C')
        graph.add_edge('A', 'B')
        graph.add_edge('A', 'C')
        graph.add_edge('B', 'C')

        self.assertEqual(len(graph.get_vertices()), 3)

        self.assertEqual(len(graph.get_neighbors('A')), 2)
        self.assertEqual(len(graph.get_neighbors('B')), 2)
        self.assertEqual(len(graph.get_neighbors('C')), 2)
Exemplo n.º 4
0
    graph.add_edge('A', 'B')
    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)
Exemplo n.º 5
0
    graph.add_vertex('F')
    graph.add_vertex('G')

    # Add connections
    graph.add_edge('A', 'B')
    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('The vertices are: {} \n'.format(graph.get_vertices()))

    # Print edges
    print('The edges are:')
    for vertex_obj in graph.get_vertices():
        for neighbor_obj in vertex_obj.get_neighbors():
            print('({} , {})'.format(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')