コード例 #1
0
ファイル: main.py プロジェクト: KorkemErtasova/seminar5
for line in f:
  mymatrix.append([int(x) for x in line.split()])
  graph.add_vertex(count)
  new_graph[count] = {}
  count += 1

for i in range(len(mymatrix)):
  weights.append(mymatrix[i][len(mymatrix[i]) - 1])



for j in range(len(mymatrix)):
  col = 0
  row = 0
  counter = 0
  for i in range(len(mymatrix[0]) - 1):
    if counter == 0 and mymatrix[j][i] == 1:
      col = i
      counter = 1
      continue
    if mymatrix[j][i] == 1 and counter == 1:
      row = i
      counter = 2
    if counter == 2:
      graph.add_edge(col, row)
      new_graph[col][row] = weights[j]

print('Number of edges: ', graph.num_edges())
print('Number of vertices: ', graph.num_vertices())
print('Minimum path from 1 to 4 is: ', dijkstra(new_graph, 1))
コード例 #2
0
ファイル: main.py プロジェクト: lberzhitskaya/seminar5
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()))


コード例 #3
0
ファイル: main.py プロジェクト: gulnaz8891/seminar5
for j in range(len(t)):
    x = None
    y = None
    cnt = 0
    for i in range(len(t[0]) - 1):
        if cnt == 0 and t[j][i] == 1:
            x = i
            cnt = 1
            continue
        if t[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('num_vertices:', g.num_vertices())
print('num_edges:', g.num_edges())
print('get_vertex: ', g.get_vertex(3))
print('get_edge: ', g.get_edge(2, 3))
print('adj_vertices: ', g.adj_vertices(1))
print('Minimum Spanning Tree:')
print(kruskalsalg(g.get_data(), g.num_vertices()))

#Yermagambet Gylnaz