> 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?
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 -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org