ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 05 Jul 2020 13:36:40 -0500Combination under constrained situation with a conditionhttps://ask.sagemath.org/question/52345/combination-under-constrained-situation-with-a-condition/Step 1: I want to take a number `n` as input from user
Step 2: We form the set `S` consisting of elements from `0` to `n*(2^{n-1})`
Step 3: Now I pick each possible two-element subsets of `S` and store it in `P`.
Step 4: Now I need to pick `n*(2^{n-1})` two-element subsets from `P`
such that each number that occurs in that set occurs exactly `n` times
neither less nor more and put them all in a list.
Example
n = 2
n*(2^{n-1}) = 2*(2^{2-1}) = 4
S = {0,1,2,3,4}
p = {(0,1),(0,2),(0,3),(0,4),(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)}
Step 4 One element which satisfies condition of step 4; HERE `n = 2`.
`{(0,1),(1,2),(2,3),(0,3)}` which has `2*(2^{n-1}) = 2*(2^{2-1}) = 4` elements.
Now see in the above set
- `0` occurred `n=2` times only in `(0,1)` and `(0,3)` only
- `1` occurred `n=2` times only in `(0,1)` and `(1,2)` only
- `2` occurred `n=2` times only in `(1,2)` and `(2,3)` only
- `3` occurred `n=2` times only in `(2,3)` and `(0,3)` only
Similarly we may get for `{(0,1),(1,4),(4,2),(2,0)}` we can easily verify like above.
Now based on `n` the number of elements size etc will vary.
Kindly help if possible any one.sriramSun, 05 Jul 2020 13:36:40 -0500https://ask.sagemath.org/question/52345/How to obtain the graph having highest algebraic connectivity?https://ask.sagemath.org/question/52205/how-to-obtain-the-graph-having-highest-algebraic-connectivity/ for G in graphs.nauty_geng("10-c"):
if G.size()==11:
L = G.laplacian_matrix().eigenvalues()
L.sort()
show(L)
G.show()
Using this code I have obtained the connected graphs on 10 vertices with 11 edges. Also I have obtained the Laplacian eigenvalues also for the corresponding graphs. The second smallest eigenvalue of Laplacian matrix is called the algebraic connectivity. Now among all these graphs, I need those graphs which have highest algebraic connectivity among all the graphs on 10 vertices with 11 edges. How we can do that?KuldeepWed, 24 Jun 2020 15:24:16 -0500https://ask.sagemath.org/question/52205/Displaying Chessboard Graphshttps://ask.sagemath.org/question/49976/displaying-chessboard-graphs/I'm looking at various graphs and their plotting methods. For the code
G = graphs.KnightGraph([3,3])
G.show()
Sage displays:
![image description](/upfiles/15820828503619033.jpg)
I'd like the vertices to be arranged in a 3 by 3 grid so that the chessboard nature of the graph is more apparent.
From the documentation [here](http://doc.sagemath.org/html/en/reference/plotting/sage/graphs/graph_plot.html) I tried specifying the position by using a dictionary:
G = graphs.KnightGraph([3,3])
pos_dict = {}
for i in range(0,2):
for j in range(0,2):
pos_dict[G.vertices()[3*i+j]] = [i*.5,j*.5]
pl = G.graphplot(pos=pos_dict)
pl.show()
This doesn't give the intended result. Even stranger, running the code multiple times shows the position of only some of the
vertices is fixed while others change position.
How can I display an n by n chessboard graph so that it's vertices form an n by n square?
dazedANDconfusedTue, 18 Feb 2020 21:38:26 -0600https://ask.sagemath.org/question/49976/Stochastic block modelhttps://ask.sagemath.org/question/38929/stochastic-block-model/Hi all,
I would like to draw a random graph by Sage. The general stochastic model is the following: The graph contains $n$ vertices and the n vertices are divided into $r$ communities $C_1\cdots,C_r$. For two vertices within the same community, there is a probability $P_r$ that they are connected directly by an edge. How do I plot such a graph in Sage? References appreciated.Dianbin BaoThu, 21 Sep 2017 13:19:26 -0500https://ask.sagemath.org/question/38929/How do I write function to test if a graph is apex?https://ask.sagemath.org/question/35112/how-do-i-write-function-to-test-if-a-graph-is-apex/ I am working on topological graph theory problems and using SageMath.
I want to create a function that give a boolean True or False answer, so I can use this answer for further use.
My current function that I use:
def is_apex(a):
for v in a.vertex_iterator():
l=a.neighbors(v)
if a.is_planar(a.delete_vertex(v)):
print("Deleting vertex ",v," makes a planar graph")
a.add_vertex(v)
a.add_edges([(v, y) for y in l])
else:
a.add_vertex(v)
a.add_edges([(v, y) for y in l])
print("Deleting vertex ",v," does not make a planar graph")
I am a noob when it comes to programming, and any help would be awesome.
I want a True returned if the graph is apex and a False value to return for not apex.
Thoughts?fieldofnodesMon, 10 Oct 2016 15:18:52 -0500https://ask.sagemath.org/question/35112/Dotted and dashed lines in directed graphshttps://ask.sagemath.org/question/31304/dotted-and-dashed-lines-in-directed-graphs/Hi,
I would like to know if it is possible to do (one of) the following with Sage.
Reading
http://doc.sagemath.org/html/en/reference/plotting/sage/graphs/graph_plot.html
I saw that it is possible to draw dashed and dotted lines. Now my questions are
1) Is it possible to produce an output graphic where some edges are dotted and some edges are dashed?
2) Is it possible to force the text in the labels of the vertices, e.g. M2, M4, M1 instead of 0,1,2?
Thanks for the help!
BernThu, 03 Dec 2015 20:39:18 -0600https://ask.sagemath.org/question/31304/load a graph from a dot filehttps://ask.sagemath.org/question/29477/load-a-graph-from-a-dot-file/ Sage allows me to get the representation of a Graph, G, in the [dot language](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) by using the method
> [G.graphviz_string()](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_string)
I can even have this string written directly to a file by using:
> [G.graphviz_to_file_named('my_graph_file.dot')](http://doc.sagemath.org/html/en/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.graphviz_to_file_named)
Is there a way of reloading G from such a file? I tried doing
> G = Graph('my_graph_file.dot')
> G = Graph(open('my_graph_file.dot'))
> G = Graph(open('my_graph_file.dot').read())
Where the contents of 'my_graph_file.dot' are:
> graph MyGraph{
> foo -- {bar}
> bar -- {foo; bas}
> bas -- {bar}
> }
But none of these worked.
Mark BellWed, 16 Sep 2015 14:47:01 -0500https://ask.sagemath.org/question/29477/Graph: colored edge labelshttps://ask.sagemath.org/question/25413/graph-colored-edge-labels/I'm trying to plot a graph with labels on the edges as follows:
G = Graph(multiedges=True)
G.add_edges( [(0,1,'a'),(1,2,'b'),(2,3,'c'),(3,4,'b'),(4,0,'a')] )
H = G.plot(edge_labels=True, graph_border=True)
H.show()
Is it possible to color the edge labels, e.g. making the "a" red, "b" blue etc?
herbTue, 06 Jan 2015 13:10:47 -0600https://ask.sagemath.org/question/25413/defining function for graph (help needed)https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/I need help defining a function that does the following (I'm very new to Python/Sage).
I need a function called ihara(G) where G is any graph input by the user.
I need the function ihara(G) to return the Z resulting from all the calculations below:
D = G.to_directed()
L = D.line_graph()
L.delete_edges([((x,y,None), (y,x,None)) for x,y in G.edges( labels=None)])
L.delete_edges([((x,y,None), (y,x,None)) for y,x in G.edges( labels=None)])
IM = identity_matrix(QQ,D.size())
T = L.adjacency.matrix()
var('u')
X = IM-u*T
Z = X.det()
Could anybody help me with this? or at least get me started in the right direction...jtaaWed, 27 Feb 2013 07:14:51 -0600https://ask.sagemath.org/question/9859/How to list all the connected graphs with 9 vertices?https://ask.sagemath.org/question/24299/how-to-list-all-the-connected-graphs-with-9-vertices/ What I want to do is exactly to list all the connected graphs with 9 vertices which are also `is_long_hole_free()==False`, i.e., contains an induced cycle of length at least 5. But I can only list all the graphs with a given number of vertices, for example
G = GraphQuery(display_cols=['graph6'], num_vertices=2)
L = G.get_graphs_list()
graphs_list.show_graphs(L)
SolomanFri, 26 Sep 2014 06:31:29 -0500https://ask.sagemath.org/question/24299/Create curved edges in a graphhttps://ask.sagemath.org/question/10081/create-curved-edges-in-a-graph/Hello,
It seems that I am only able to create rectilinear graphs in sage. e.g. $K_{13}$ in two concentric circles:
g = graphs.CompleteGraph(13)
_circle_embedding(g,[0,1,2,3,4,5,6],center=(0,0),radius=6,shift=0)
_circle_embedding(g,[7,8,9,10,11,12],center=(0,0),radius=12,shift=0)
g.show()
I'd like to add an image of what this code produces, but I don't have enough karma.
I want my edges to have curves; something like this [Paley Graph](http://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Paley13.svg/500px-Paley13.svg.png).
Any ideas on how to do this?torrhoThu, 02 May 2013 06:20:32 -0500https://ask.sagemath.org/question/10081/How to get an arbitrary orientation of a graph.https://ask.sagemath.org/question/9835/how-to-get-an-arbitrary-orientation-of-a-graph/**I'VE COMPLETELY REVISED MY QUESTION**
I wish to take a simple undirected graph (i.e. the complete graph K_4)
Arbitrarily direct said graph, and then create a line graph from the directed version of the graph.
However, in Sage it appears to create a line graph that shows a connection between two edges (that are just inverses of each other), so what I really want is a line graph that doesn't give an edge connected to its own inverse.
**That's why I asked if we could remove cycles of length 2, but that doesn't seem to solve the problem.**
Here's what I am trying to work out:
G = graphs.RandomGNP(4,1)
GD = G.to_directed() #orients G
m = GD.size() #number of edges of digraph GD
LG = GD.line_graph() #the line graph of the digraph
IM = identity_matrix(QQ,GD.size())
T = LG.adjacency_matrix()#returns the adjacency matrix of the line graph
var('u') #defines u as a variable
X=IM-u*T #defines a new matrix X
Z=X.det() #defines polynomial in u aka inverse of the Ihara zeta function
Z #computes determinant of X
Z.coefficients(u) #extracts coefficients
considering my graph is a complete graph on 4 vertices - the coefficients should be as such:
[coeff,degree of u]
[1,0], [0,1], [0,2],[-8,3],[-2,4]
**NOTE:**
im only interested in coefficients up to the order of n=#of nodes in the graph, so here for K_4 obviously n=4.
where the coefficient of u^3 corresponds to the negative of twice the number of triangles in K_4
where the coefficient u^4 corresponds to the negative of twice the number of squares in K_4
**Here is an image of a K_4 graph minus an edge and the line graph construction of K_4 that i want**
![Here is an image of a K_4 graph minus an edge and the line graph construction of K_4 that i am after](http://dl.dropbox.com/u/2399196/graph.png)jtaaFri, 22 Feb 2013 13:59:05 -0600https://ask.sagemath.org/question/9835/