# How to get a graph from a given symmetric matrix

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.show()

show(b)

u=~b

show(u)

edit retag close merge delete

Sort by ยป oldest newest most voted

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

more

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.show()
Launched png viewer for Graphics object consisting of 13 graphics primitives
sage: G.show()
Launched png viewer for Graphics object consisting of 13 graphics primitives
sage:


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

more

( 2018-07-06 21:30:06 +0200 )edit