ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 21 Aug 2017 18:08:35 +0200Changing the weights in a graphhttps://ask.sagemath.org/question/38603/changing-the-weights-in-a-graph/ Suppose I have a weighted graph G, and I want to change the weights of some of the edges. One way to do this is to output the weighted adjacency matrix, modify it, and then recreate the graph. Is there a more efficient way?Mon, 21 Aug 2017 05:11:49 +0200https://ask.sagemath.org/question/38603/changing-the-weights-in-a-graph/Answer by David Coudert for <p>Suppose I have a weighted graph G, and I want to change the weights of some of the edges. One way to do this is to output the weighted adjacency matrix, modify it, and then recreate the graph. Is there a more efficient way?</p>
https://ask.sagemath.org/question/38603/changing-the-weights-in-a-graph/?answer=38611#post-id-38611You can use the set_edge_label
sage: G = graphs.PetersenGraph()
sage: print G.edges()
[(0, 1, None), (0, 4, None), (0, 5, None), (1, 2, None), (1, 6, None), (2, 3, None), (2, 7, None), (3, 4, None), (3, 8, None), (4, 9, None), (5, 7, None), (5, 8, None), (6, 8, None), (6, 9, None), (7, 9, None)]
sage: i = 0
sage: for u,v in G.edges(labels=0):
....: G.set_edge_label(u, v, i)
....: i += 1
sage: print G.edges()
[(0, 1, 0), (0, 4, 1), (0, 5, 2), (1, 2, 3), (1, 6, 4), (2, 3, 5), (2, 7, 6), (3, 4, 7), (3, 8, 8), (4, 9, 9), (5, 7, 10), (5, 8, 11), (6, 8, 12), (6, 9, 13), (7, 9, 14)]
Mon, 21 Aug 2017 18:08:35 +0200https://ask.sagemath.org/question/38603/changing-the-weights-in-a-graph/?answer=38611#post-id-38611