Plotting weighted graph

209 views
Skip to first unread message

akm

unread,
Dec 9, 2011, 3:53:46 PM12/9/11
to sage-support
Hi all, I'm confused why this code results in a graph plotted with the
(single) edges labeled with the weight between each pair of vertices:

M = Matrix([[0,1,-1,5],[1,0,-1/2,-1],[-1,-1/2,0,2],[5,-1,2,0]])
G = Graph(M,sparse=True)
G.plot(edge_labels=True)

Whereas this code does not and instead has multi-edges labeled "None":

M = Matrix([[0, 1, 0, 0], [1, 0, 1, 2], [0, 1, 0, 0], [0, 2, 0, 0]])
G = Graph(M,sparse=True)
G.plot(edge_labels=True)

What am I missing?

Thanks for any help.

Best
Andrew

D. S. McNeil

unread,
Dec 9, 2011, 8:47:14 PM12/9/11
to sage-s...@googlegroups.com

I think it's something like this (take with grain of salt, I'm bad at
multitasking):

Briefly, it's not interpreting the second matrix as a weighted
adjacency matrix. If you specifically tell it that it is, say by

M = Matrix([[0, 1, 0, 0], [1, 0, 1, 2], [0, 1, 0, 0], [0, 2, 0, 0]])

G = Graph(M,weighted=True)

it should behave.

Less briefly: the code in graph.py uses the following logic to decide
whether these two are weighted or not:

if format == 'adjacency_matrix':
entries = uniq(data.list())
for e in entries:
try:
e = int(e)
assert e >= 0
except:
if weighted is False:
raise ValueError("Non-weighted graph's"+
" adjacency matrix must have only nonnegative"+
" integer entries")
weighted = True
if multiedges is None: multiedges = False
break

IOW, to decide if it should set weighted to True, it loops over each
entry and (1) tries to convert it to an integer, and (2) asserts that
it's non-negative. If either of those fail, then it decides that
weighted should be True (in this case weighted starts as None).

So the first graph works because the nonnegative check fails (or the
integral check fails, I'm too lazy to confirm what direction the
matrix entries are iterated in..) The second graph's matrix is
integral and nonnegative, so it sneaks through, and things get silly
after that.

Would you like to open a bug report on trac? This should be easy to fix.


Doug

akm

unread,
Dec 10, 2011, 11:42:27 AM12/10/11
to sage-support

Thanks for the advice; works as expected now, though the edge labels
overlap where the edges intersect.

I'll file a report later today.

Best
Andrew

Reply all
Reply to author
Forward
0 new messages