示例#1
0
from mygraph import Graph

f=open('graph_18.txt','r')
line = f.readline()
mymatrix = []
weights = []
graph = Graph()
new_graph = {}
count=0
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
示例#2
0
from myalgorithm import prims_algorithm

with open('graph_10.txt') as f:
    l = f.readline()
    m = []
    for line in f:
        m.append([int(x) for x in line.split()])
cnt = 0
g = Graph()

weight = []
for i in range(len(m)):
    weight.append(m[i][len(m[i]) - 1])

for i in range(len(m[0]) - 1):
    g.add_vertex(i)

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])
示例#3
0
with open('graph_36.txt') as file:
    line1 = file.readline()
    incidence_matrix = []
    for line in file:
        incidence_matrix.append([int(x) for x in line.split()])
counter = 0
graph = Graph()
new_graph = {}

weight = []
for i in range(len(incidence_matrix)):
    weight.append(incidence_matrix[i][len(incidence_matrix[i]) - 1])

for i in range(len(incidence_matrix[0]) - 1):
    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: