Ask Your Question
0

Weighted adjacency matrix of a graph.

asked 2020-02-05 14:13:56 +0100

anonymous user

Anonymous

updated 2020-02-06 17:53:33 +0100

vdelecroix gravatar image

The code:

G=Graph(sparse=True, weighted=True)
G.add_edges([(0, 1, i), (0,8,1),(1,2,1),(2,3,1),(3,4,1),(4,5,1),(4,6,1),(6,7,1),(6,8,1),(8,9,1)])
M = G.weighted_adjacency_matrix()
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 flag offensive close merge delete

Comments

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)?

rburing gravatar imagerburing ( 2020-02-05 15:40:28 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-02-06 19:54:52 +0100

Emmanuel Charpentier gravatar image

Following Ricardo's suggestion works:

sage: G=DiGraph(sparse=True, weighted=True)
sage: G.add_edges([(0, 1, i), (0,8,1),(1,2,1),(2,3,1),(3,4,1),(4,5,1),(4,6,1),(6,
....: 7,1),(6,8,1),(8,9,1)])
sage: G.add_edges([(u[1], u[0], -u[2]) for u in list(G.edges())])
sage: G.weighted_adjacency_matrix()
[ 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,

edit flag offensive delete link more

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: 2020-02-05 14:13:56 +0100

Seen: 593 times

Last updated: Feb 06 '20