Пример #1
0
for j in range(len(m)):
    x = None
    y = None
    cnt = 0
    for i in range(len(m[0]) - 1):
        if cnt == 0 and m[j][i] == 1:
            x = i
            cnt = 1
            continue
        if m[j][i] == 1 and cnt == 1:
            y = i
            cnt = 2
        if cnt == 2:
            g.add_edge(x, y, weight[j])
            break

g.print_graph()

print('Vertices:', g.vertices())
print('Edges: ', g.edges())
print('Number of edges:', g.num_edges())
print('Number of vertices:', g.num_vertices())
print('Get vertex: ', g.get_vertex(2))
print('Get edge: ', g.get_edge(3, 2))
print('Get adjacents: ', g.adj_vertices(1))

print(prims_algorithm(g.get_data()))


Пример #2
0
    graph.add_vertex(i)
    new_graph[i] = {}

for j in range(len(incidence_matrix)):
    first_one = None
    second_one = None
    counter = 0
    for i in range(len(incidence_matrix[0]) - 1):
        if counter == 0 and incidence_matrix[j][i] == 1:
            first_one = i
            counter = 1
            continue
        if incidence_matrix[j][i] == 1 and counter == 1:
            second_one = i
            counter = 2
        if counter == 2:
            graph.add_edge(first_one, second_one)
            new_graph[first_one][second_one] = weight[j]

graph.print_graph()

print('Number of edges: ', graph.num_edges())
print('Number of vertices: ', graph.num_vertices())
print('Vertices: ', graph.vertices())
print('Edges: ', graph.edges())
print('Get vertex: ', graph.get_vertex(2))
print('Get edge: ', graph.get_edge(3, 2))
print('Get adjacents: ', graph.adj_vertices(1))
print(dijkstra(new_graph, 2, 9))