Mathyobani
Mathyobani

Reputation: 121

is it possible to build a graph having the adjacency matrix?

I am readying about graph theory in sagemath or python,

g2=Graph({0:[1,2,3,4,5], 1:[2,3,4,5], 2:[5], 3:[4,5], 4:[5]})

if g2 is the graph, then with

g2.adjacency_matrix()

SAGEMATH easily constructs the adjacency matrix, I was wondering if it is possible or is there some code where if I have the adjacency matrix sage or python build the graph



thanks davidlowryduda,, Now I have this matrix

T= array([[ 0, 0, 0,  1,  1],
          [0,  0,  1, 0,  1],
          [0,  1,  0,  1, 0],
          [ 1, 0,  1,  0, 0],
          [ 1,  1, 0, 0,  0]])            

but, for

 G = Graph(T, format='adjacency_matrix'), 

I need this type!

matrix([[0, 0, 0,  1,  1],[0,  0,  1, 0,  1],[0,  1,  0,  1, 0],[ 1, 0,  1,  0, 0],[ 1,  1, 0, 0,  0]]). 

Is it possible to transform from array to the form I need to be able to load that code?

Upvotes: 1

Views: 898

Answers (1)

davidlowryduda
davidlowryduda

Reputation: 2559

Sage will very happily plot the graph for you if you give it a matrix.

For your g2, if you simply type g2 at an interactive sage session, then it will present you with a plot of a graph. g2.show() and g2.plot() are other ways to cause the graph to display.

You can save the image using something like

p = g2.plot()
p.sage_image("file.png")

If you start from a adjacency matrix, sage will happily make a plot for you. For example,

M = matrix([[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: G = DiGraph(M, format='adjacency_matrix')
sage: G.show()

Given a symmetric matrix, it makes sense to use Graph instead of Digraph.

Upvotes: 2

Related Questions