示例#1
0
    def fill(self, graph: Graph):
        for i in graph.get_edges(self.custom_vertex):
            self.matrix[i[0]][i[1]] += i[2]

        for i in range(self.n):
            for j in range(self.m):
                if i != j and self.matrix[i][j] == 0:
                    self.matrix[i][j] = inf
示例#2
0
 def fill(self, graph: Graph):
     for i, edge in enumerate(graph.get_edges()):
         if edge[0] == edge[1]:
             self.matrix[edge[0]][i] += 1
         self.matrix[edge[0]][i] += -1
         self.matrix[edge[1]][i] += 1
示例#3
0
    graph.add_edge('D', 'E')

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

    # 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()})')
    print(graph.get_edges())

    # 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)