Ask Your Question
1

Make graph from matrix

asked 2016-06-23 22:11:29 +0100

littlemd gravatar image

Hi,

I'm a new user of SageMath and maybe my question is very simple or already answered somewhere but I wasn't able to find an answer to my problem. For my studies I have to implement a brute-force algorithm for solving the travelling salesman problem. I found out, that SM already has a solver for this problem that handles Graph - objects. I wan't to use it to check the results of my algorithm. I wrote some input data as a dictionary to feed into both alg, but if I wan't to do it for more than a few cities (I can handle only a few with bruteforce anyway because of the time it takes to calculate all the possibilities) and I've found a matrix with distances between cities. It is available here

https:// people.sc.fsu.edu/~jburkardt/datasets/cities/cities.html (ha30_dist.txt)

I threw the first two lines out and I can import this file with numpy.loadtxt(). But later, when I try to make Graph using this matrix, I get the error: "ValueError: There must be one or two nonzero entries per column in an incidence matrix. Got entries [39, 22, 59, 54...". What am I doing wrong?

Greetings

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-06-24 05:04:53 +0100

kcrisman gravatar image

Incidence and adjacency matrices will have plus or minus one depending on whether vertices lead to other vertices or are adjacent. What you want is to make a complete graph, and then label the edges, I think.

The TSP solver assumes you have weighted edges already. See that documentation for ways to either add edges one by one, or you can separately use add_edge_label. In any case, you'll have to probably take your matrix and programmatically use those values to set the edge labels. Probably by going through the columns, and then through the rows in each column - but watch out, since such a matrix had better be symmetric, so you only need the first i-1 entries of the ith column, I suppose!

edit flag offensive delete link more

Comments

Thanks for your answer! I know what to do now

littlemd gravatar imagelittlemd ( 2016-06-24 12:23:38 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-06-23 21:56:02 +0100

Seen: 1,549 times

Last updated: Jun 24 '16