Ask Your Question
0

How to get a graph from a given symmetric matrix

asked 2018-07-06 17:22:18 +0100

anonymous user

Anonymous

using the following sage code, I have obtained the matrix u. I know that u may represent a graph whose adjacency matrix is u itself. Now can we draw the graph in sage so that we can visualise.

G=graphs.EmptyGraph()

G.add_edges([(1,2),(2,3),(3,4),(3,5),(2,5),(5,6)])

G.show()

b=G.adjacency_matrix()

show(b)

u=~b

show(u)

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-07-11 13:08:31 +0100

slelievre gravatar image

Define the graph G and plot it:

sage: G = Graph([(1, 2), (2, 3), (3, 4), (3, 5), (2, 5), (5, 6)]); G
Graph on 6 vertices
sage: G.plot()
Launched png viewer for Graphics object consisting of 13 graphics primitives

Find adjacency matrix b of G, and inverse u of b:

sage: b = G.adjacency_matrix()
sage: u = ~b
sage: b, u
(
[0 1 0 0 0 0]  [ 0  1  0 -1  0 -1]
[1 0 1 0 1 0]  [ 1  0  0  0  0  0]
[0 1 0 1 1 0]  [ 0  0  0  1  0  0]
[0 0 1 0 0 0]  [-1  0  1  0  0 -1]
[0 1 1 0 0 1]  [ 0  0  0  0  0  1]
[0 0 0 0 1 0], [-1  0  0 -1  1  0]
)

Some entries in u are -1, so it's slightly weird to use it as adjacency matrix, but it works, with edges for nonzero entries.

sage: H = Graph(u)
sage: H.plot()
Launched png viewer for Graphics object consisting of 13 graphics primitives
edit flag offensive delete link more
1

answered 2018-07-06 19:29:54 +0100

dan_fulea gravatar image

Try ?Graph and/or ?DiGraph to get the documentation and examples of these "constructors".

The following dialog with the sage interpreter was leading to a directed graph D and an undirected one, G.

sage: M = matrix(6,6)
sage: for j,k in [(1,2),(2,3),(3,4),(3,5),(2,5),(5,6)]:
....:     M[j-1,k-1] = 1
....:         
sage: M
[0 1 0 0 0 0]
[0 0 1 0 1 0]
[0 0 0 1 1 0]
[0 0 0 0 0 0]
[0 0 0 0 0 1]
[0 0 0 0 0 0]
sage: D = DiGraph(M, format='adjacency_matrix')
sage: D.show()
Launched png viewer for Graphics object consisting of 13 graphics primitives
sage: G = Graph(M, format='adjacency_matrix')
sage: G.show()
Launched png viewer for Graphics object consisting of 13 graphics primitives
sage:

Possible link, one among many:

http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph.html

edit flag offensive delete link more

Comments

Ok.Thanks for your suggestion.

rewi gravatar imagerewi ( 2018-07-06 21:30:06 +0100 )edit

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: 2018-07-06 17:22:18 +0100

Seen: 2,491 times

Last updated: Jul 11 '18