@@ -14034,6 +14034,46 @@ PyObject *igraphmodule_Graph_random_walk(igraphmodule_GraphObject * self,
1403414034 }
1403514035}
1403614036
14037+ /**********************************************************************
14038+ * Spatial graphs *
14039+ **********************************************************************/
14040+
14041+ PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type,
14042+ PyObject *args, PyObject *kwds) {
14043+ static char *kwlist[] = {"points", "k", "r", "metric", "directed", NULL};
14044+ PyObject *points_o = Py_None, *metric_o = Py_None, *directed_o = Py_False;
14045+ double r = -1;
14046+ Py_ssize_t k = 1;
14047+ igraph_matrix_t points;
14048+ igraphmodule_GraphObject *self;
14049+ igraph_t graph;
14050+ igraph_metric_t metric = IGRAPH_METRIC_EUCLIDEAN;
14051+
14052+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ndOO", kwlist,
14053+ &points_o, &k, &r, &metric_o, &directed_o)) {
14054+ return NULL;
14055+ }
14056+
14057+ if (igraphmodule_PyObject_to_metric_t(metric_o, &metric)) {
14058+ return NULL;
14059+ }
14060+
14061+ if (igraphmodule_PyObject_to_matrix_t(points_o, &points, "points")) {
14062+ return NULL;
14063+ }
14064+
14065+ if (igraph_nearest_neighbor_graph(&graph, &points, metric, k, r, PyObject_IsTrue(directed_o))) {
14066+ igraph_matrix_destroy(&points);
14067+ return igraphmodule_handle_igraph_error();
14068+ }
14069+
14070+ igraph_matrix_destroy(&points);
14071+
14072+ CREATE_GRAPH_FROM_TYPE(self, graph, type);
14073+
14074+ return (PyObject *) self;
14075+ }
14076+
1403714077/**********************************************************************
1403814078 * Special internal methods that you won't need to mess around with *
1403914079 **********************************************************************/
@@ -18894,6 +18934,22 @@ struct PyMethodDef igraphmodule_Graph_methods[] = {
1889418934 " the given length (shorter if the random walk got stuck).\n"
1889518935 },
1889618936
18937+ /**********************/
18938+ /* SPATIAL GRAPHS */
18939+ /**********************/
18940+ {"Nearest_Neighbor_Graph", (PyCFunction)igraphmodule_Graph_Nearest_Neighbor_Graph,
18941+ METH_VARARGS | METH_CLASS | METH_KEYWORDS,
18942+ "Nearest_Neighbor_Graph(points, k=1, r=-1, metric=\"euclidean\", directed=False)\n--\n\n"
18943+ "Constructs a k nearest neighbor graph of a give point set. Each point is\n"
18944+ "connected to at most k spatial neighbors within a radius of 1.\n\n"
18945+ "@param points: coordinates of the points to use, in an arbitrary number of dimensions\n"
18946+ "@param k: at most how many neighbors to connect to. Pass a negative value to ignore\n"
18947+ "@param r: only neighbors within this radius are considered. Pass a negative value to ignore\n"
18948+ "@param metric: the metric to use. C{\"euclidean\"} and C{\"manhattan\"} are supported.\n"
18949+ "@param directed: whethe to create directed edges.\n"
18950+ "@return: the nearest neighbor graph.\n"
18951+ },
18952+
1889718953 /**********************/
1889818954 /* INTERNAL FUNCTIONS */
1889918955 /**********************/
0 commit comments