Skip to content

Commit

Permalink
Merge pull request #383 from manpen/Feature/ErdosRenyiSelfLoops
Browse files Browse the repository at this point in the history
Add selfLoops flag to Python's ErdosRenyiGenerator
  • Loading branch information
angriman committed Aug 2, 2019
2 parents 4065095 + a3d40af commit ffe4e4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions networkit/_NetworKit.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2220,14 +2220,14 @@ cdef class PubWebGenerator(StaticGraphGenerator):
cdef extern from "<networkit/generators/ErdosRenyiGenerator.hpp>":

cdef cppclass _ErdosRenyiGenerator "NetworKit::ErdosRenyiGenerator"(_StaticGraphGenerator):
_ErdosRenyiGenerator(count nNodes, double prob, bool_t directed) except +
_ErdosRenyiGenerator(count nNodes, double prob, bool_t directed, bool_t selfLoops) except +

cdef class ErdosRenyiGenerator(StaticGraphGenerator):
""" Creates random graphs in the G(n,p) model.
The generation follows Vladimir Batagelj and Ulrik Brandes: "Efficient
generation of large random networks", Phys Rev E 71, 036113 (2005).
ErdosRenyiGenerator(count, double)
ErdosRenyiGenerator(count nNodes, double prob, directed = False, selfLoops = False)
Creates G(nNodes, prob) graphs.
Expand All @@ -2239,10 +2239,12 @@ cdef class ErdosRenyiGenerator(StaticGraphGenerator):
Probability of existence for each edge p.
directed : bool
Generates a directed
selfLoops : bool
Allows self-loops to be generated (only for directed graphs)
"""

def __cinit__(self, nNodes, prob, directed=False):
self._this = new _ErdosRenyiGenerator(nNodes, prob, directed)
def __cinit__(self, nNodes, prob, directed = False, selfLoops = False):
self._this = new _ErdosRenyiGenerator(nNodes, prob, directed, selfLoops)

@classmethod
def fit(cls, Graph G, scale=1):
Expand Down
5 changes: 4 additions & 1 deletion networkit/cpp/generators/ErdosRenyiGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ namespace NetworKit {

ErdosRenyiGenerator::ErdosRenyiGenerator(count nNodes, double prob, bool directed, bool self_loops) :
nNodes{nNodes}, prob{prob}, directed{directed}, self_loops{self_loops}
{}
{
if (self_loops && !directed)
throw std::runtime_error("Self-loops are only supported for directed graphs");
}

Graph ErdosRenyiGenerator::generate() {
GraphBuilder builder(nNodes, false, directed);
Expand Down

0 comments on commit ffe4e4c

Please sign in to comment.