# Weighted adjacency matrix of a graph.

The code:

G=Graph(sparse=True, weighted=True)
show(M)


Here the (1,0) entry in the output matrix will be i, but I need -i. For that purpose, even I had defined (1,0,-i), it is not coming. Please give some hint.

edit retag close merge delete

Probably you should create a DiGraph from G. Do you need the weight of (directed) edge (w,v) to be minus the weight of (v,w)?

( 2020-02-05 08:40:28 -0500 )edit

Sort by ยป oldest newest most voted

Following Ricardo's suggestion works:

sage: G=DiGraph(sparse=True, weighted=True)
....: 7,1),(6,8,1),(8,9,1)])
sage: G.add_edges([(u[1], u[0], -u[2]) for u in list(G.edges())])
[ 0  I  0  0  0  0  0  0  1  0]
[-I  0  1  0  0  0  0  0  0  0]
[ 0 -1  0  1  0  0  0  0  0  0]
[ 0  0 -1  0  1  0  0  0  0  0]
[ 0  0  0 -1  0  1  1  0  0  0]
[ 0  0  0  0 -1  0  0  0  0  0]
[ 0  0  0  0 -1  0  0  1  1  0]
[ 0  0  0  0  0  0 -1  0  0  0]
[-1  0  0  0  0  0 -1  0  0  1]
[ 0  0  0  0  0  0  0  0 -1  0]


But, of course, G.adjacency_matrix() is still boolean:

sage: G.adjacency_matrix()
[0 1 0 0 0 0 0 0 1 0]
[1 0 1 0 0 0 0 0 0 0]
[0 1 0 1 0 0 0 0 0 0]
[0 0 1 0 1 0 0 0 0 0]
[0 0 0 1 0 1 1 0 0 0]
[0 0 0 0 1 0 0 0 0 0]
[0 0 0 0 1 0 0 1 1 0]
[0 0 0 0 0 0 1 0 0 0]
[1 0 0 0 0 0 1 0 0 1]
[0 0 0 0 0 0 0 0 1 0]


HTH,

more