I'm about to create a program running a Hamiltonian cycle for TSP(symmetrical) problem which means that a path goes only to one point and then goes further. The distance between those points can be written as a square matrix. Because the problem is symmetrical, i only have a half of this matrix as a .txt file
I need to create a program which will allow me to insert values separated by spaces from a .txt file into an array, and create a symmetrical square matrix out of it.
URL to see present the idea and data layout in .txt file:
https://i.sstatic.net/GHAXh.jpg
I'm a newbie in Python and i don't really understand how to work with IO at this level.
I've tried using numpy function: loadtxt but did not work for me, i recieved an error message saying that i cannot convert string to float
i've also tried doing by creating loops that were supposed to split the text and create this matrix.
def createMatrix(rows):
matrix= [[0 for col in range(int(rows[0]))] for row in range(int(rows[0]))]
i = 0
for w in rows[1:]:
w = string.strip(w).split(' ')
j = 0
for dist in w:
matrix[i][j] = int(dist)
matrix[j][i] = int(dist)
j+=1
i+=1
return matrix
I expected the code result to at least guide me somehow on what should i do, but as i've mentioned i'm a newbie and i don't really know how to get started with this particular problem.