ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 22 Sep 2017 12:29:43 +0200Stochastic 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.Thu, 21 Sep 2017 20:19:26 +0200https://ask.sagemath.org/question/38929/stochastic-block-model/Comment by FrédéricC for <p>Hi all, </p>
<p>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.</p>
https://ask.sagemath.org/question/38929/stochastic-block-model/?comment=38942#post-id-38942Maybe use
sage: SetPartitions(5).random_element()
{{1, 5}, {2, 4}, {3}}Fri, 22 Sep 2017 10:50:00 +0200https://ask.sagemath.org/question/38929/stochastic-block-model/?comment=38942#post-id-38942Comment by fidbc for <p>Hi all, </p>
<p>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.</p>
https://ask.sagemath.org/question/38929/stochastic-block-model/?comment=38943#post-id-38943Are the sizes of the communities given as part of the input? Maybe this is what the comment above is trying to address.
Is the probability of edges between vertices in different communities equal to 0?Fri, 22 Sep 2017 12:06:05 +0200https://ask.sagemath.org/question/38929/stochastic-block-model/?comment=38943#post-id-38943Answer by fidbc for <p>Hi all, </p>
<p>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.</p>
https://ask.sagemath.org/question/38929/stochastic-block-model/?answer=38944#post-id-38944Assuming the input consists of `n`, `r`, and the probabilities for edges within a community; and assuming that the probability of edges between vertices in different communities is 0, then here is a possible way to generate such a graph.
community_sizes = Partitions(n,length=r).random_element()
H = Graph()
for comm_size,Pr in zip(community_sizes,probs):
H=H.disjoint_union(graphs.RandomGNP(comm_size,Pr),labels='integers')
This is assuming that `n`, `r` and `probs` (assumed to be a list of length `r` containing $P_1,P_2,\ldots,P_r$) are given as part of the input. To see the resulting graph you can use `H.show()`.Fri, 22 Sep 2017 12:29:43 +0200https://ask.sagemath.org/question/38929/stochastic-block-model/?answer=38944#post-id-38944