Commit 491d21a7 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Implement circular_graph()

parent 203d04bb
...@@ -26,7 +26,14 @@ using namespace std; ...@@ -26,7 +26,14 @@ using namespace std;
using namespace boost; using namespace boost;
using namespace graph_tool; using namespace graph_tool;
void complete(GraphInterface& gi, size_t N, bool directed, bool self_loops) void complete(GraphInterface& gi, size_t N, bool directed,
bool self_loops)
{ {
get_complete()(gi.GetGraph(), N, directed, self_loops); get_complete()(gi.GetGraph(), N, directed, self_loops);
} }
void circular(GraphInterface& gi, size_t N, size_t k, bool directed,
bool self_loops)
{
get_circular()(gi.GetGraph(), N, k, directed, self_loops);
}
...@@ -36,11 +36,37 @@ struct get_complete ...@@ -36,11 +36,37 @@ struct get_complete
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
{ {
for (size_t j = (directed) ? 0 : i; j < N; ++j) for (int j = directed ? 0 : i; j < N; ++j)
{ {
if (!self_loops && j == i) if (!self_loops && j == i)
continue; continue;
add_edge(vertex(i, g), vertex(j, g), g); add_edge(vertex(i, g),
vertex(j, g), g);
}
}
}
};
struct get_circular
{
template <class Graph>
void operator()(Graph& g, size_t N, size_t k, bool directed,
bool self_loops) const
{
for (int i = 0; i < N; ++i)
add_vertex(g);
for (int i = 0; i < N; ++i)
{
for (int j = i; j < i + k + 1; ++j)
{
if (!self_loops && j == i)
continue;
add_edge(vertex(i, g),
vertex(j % N, g), g);
if (directed && j != i)
add_edge(vertex(j % N, g),
vertex(i, g), g);
} }
} }
} }
......
...@@ -100,6 +100,7 @@ void geometric(GraphInterface& gi, python::object opoints, double r, ...@@ -100,6 +100,7 @@ void geometric(GraphInterface& gi, python::object opoints, double r,
void price(GraphInterface& gi, size_t N, double gamma, double c, size_t m, void price(GraphInterface& gi, size_t N, double gamma, double c, size_t m,
rng_t& rng); rng_t& rng);
void complete(GraphInterface& gi, size_t N, bool directed, bool self_loops); void complete(GraphInterface& gi, size_t N, bool directed, bool self_loops);
void circular(GraphInterface& gi, size_t N, size_t k, bool directed, bool self_loops);
using namespace boost::python; using namespace boost::python;
...@@ -117,4 +118,5 @@ BOOST_PYTHON_MODULE(libgraph_tool_generation) ...@@ -117,4 +118,5 @@ BOOST_PYTHON_MODULE(libgraph_tool_generation)
def("geometric", &geometric); def("geometric", &geometric);
def("price", &price); def("price", &price);
def("complete", &complete); def("complete", &complete);
def("circular", &circular);
} }
...@@ -38,6 +38,7 @@ Summary ...@@ -38,6 +38,7 @@ Summary
geometric_graph geometric_graph
price_network price_network
complete_graph complete_graph
circular_graph
Contents Contents
++++++++ ++++++++
...@@ -56,7 +57,7 @@ import sys, numpy, numpy.random ...@@ -56,7 +57,7 @@ import sys, numpy, numpy.random
__all__ = ["random_graph", "random_rewire", "predecessor_tree", "line_graph", __all__ = ["random_graph", "random_rewire", "predecessor_tree", "line_graph",
"graph_union", "triangulation", "lattice", "geometric_graph", "graph_union", "triangulation", "lattice", "geometric_graph",
"price_network", "complete_graph"] "price_network", "complete_graph", "circular_graph"]
def random_graph(N, deg_sampler, deg_corr=None, cache_probs=True, directed=True, def random_graph(N, deg_sampler, deg_corr=None, cache_probs=True, directed=True,
...@@ -1205,6 +1206,49 @@ def complete_graph(N, self_loops=False, directed=False): ...@@ -1205,6 +1206,49 @@ def complete_graph(N, self_loops=False, directed=False):
libgraph_tool_generation.complete(g._Graph__graph, N, directed, self_loops) libgraph_tool_generation.complete(g._Graph__graph, N, directed, self_loops)
return g return g
def circular_graph(N, k=1, self_loops=False, directed=False):
r"""
Generate a circular graph.
Parameters
----------
N : ``int``
Number of vertices.
k : ``int`` (optional, default: ``True``)
Number of nearest neighbours to be connected.
self_loops : bool (optional, default: ``False``)
If ``True``, self-loops are included.
directed : bool (optional, default: ``False``)
If ``True``, a directed graph is generated.
Returns
-------
circular_graph : :class:`~graph_tool.Graph`
A circular graph.
Examples
--------
>>> g = gt.circular_graph(30, 2)
>>> pos = gt.sfdp_layout(g, cooling_step=0.95)
>>> gt.graph_draw(g, pos=pos, output_size=(300,300), output="circular.pdf")
<...>
.. testcode::
:hide:
gt.graph_draw(g, pos=pos, output_size=(300,300), output="circular.png")
.. figure:: circular.*
A circular graph with :math:`N=30` vertices, and :math:`k=2`.
"""
g = Graph(directed=directed)
libgraph_tool_generation.circular(g._Graph__graph, N, k, directed, self_loops)
return g
def geometric_graph(points, radius, ranges=None): def geometric_graph(points, radius, ranges=None):
r""" r"""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment