ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 11 Jul 2018 06:08:31 -0500How to get a graph from a given symmetric matrixhttp://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/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)Fri, 06 Jul 2018 10:22:18 -0500http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/Answer by dan_fulea for <p>using the following sage code, I have obtained the matrix <strong>u</strong>. I know that <strong>u</strong> may represent a graph whose adjacency matrix is <strong>u</strong> itself. Now can we draw the graph in sage so that we can visualise.</p>
<p>G=graphs.EmptyGraph()</p>
<p>G.add_edges([(1,2),(2,3),(3,4),(3,5),(2,5),(5,6)])</p>
<p>G.show()</p>
<p>b=G.adjacency_matrix()</p>
<p>show(b)</p>
<p>u=~b</p>
<p>show(u)</p>
http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/?answer=42851#post-id-42851Try `?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](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph.html)
Fri, 06 Jul 2018 12:29:54 -0500http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/?answer=42851#post-id-42851Comment by Kuldeep for <p>Try <code>?Graph</code> and/or <code>?DiGraph</code> to get the documentation and examples of these "constructors".</p>
<p>The following dialog with the sage interpreter was leading to a directed graph <code>D</code> and an undirected one, <code>G</code>.</p>
<pre><code>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:
</code></pre>
<p>Possible link, one among many:</p>
<p><a href="http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph.html">http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/graph.html</a></p>
http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/?comment=42853#post-id-42853Ok.Thanks for your suggestion.Fri, 06 Jul 2018 14:30:06 -0500http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/?comment=42853#post-id-42853Answer by slelievre for <p>using the following sage code, I have obtained the matrix <strong>u</strong>. I know that <strong>u</strong> may represent a graph whose adjacency matrix is <strong>u</strong> itself. Now can we draw the graph in sage so that we can visualise.</p>
<p>G=graphs.EmptyGraph()</p>
<p>G.add_edges([(1,2),(2,3),(3,4),(3,5),(2,5),(5,6)])</p>
<p>G.show()</p>
<p>b=G.adjacency_matrix()</p>
<p>show(b)</p>
<p>u=~b</p>
<p>show(u)</p>
http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/?answer=42917#post-id-42917Define 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
Wed, 11 Jul 2018 06:08:31 -0500http://ask.sagemath.org/question/42850/how-to-get-a-graph-from-a-given-symmetric-matrix/?answer=42917#post-id-42917