Skip to content

Conversation

szhorvat
Copy link
Member

@szhorvat szhorvat commented Aug 14, 2025

@Zepeacedust, this is an example of exposing a new graph generator in python-igraph, that you can later use as a model to expose the Delaunay generator.

This is not my comfort zone so let's wait for comments from @ntamas.

@ntamas, do you prefer Nearest_Neighbor_Graph or Nearest_Neighbor here? Since this term is used in other contexts as well, I vote for keeping the Graph suffix.

@szhorvat szhorvat requested review from ntamas and Copilot August 14, 2025 21:30
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new graph generator function Graph.Nearest_Neighbor_Graph() to python-igraph, which creates k-nearest neighbor graphs from spatial point data. The implementation serves as an example for exposing additional graph generators, specifically to guide the future implementation of a Delaunay generator.

Key changes:

  • Adds C implementation of the nearest neighbor graph generator with support for different metrics (euclidean, manhattan) and configurable parameters
  • Includes Python binding and method registration for the new graph generator
  • Adds basic test coverage for the new functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/_igraph/graphobject.c Implements the C function and registers the Python method for nearest neighbor graph generation
src/_igraph/convert.h Declares the metric conversion function for parameter parsing
src/_igraph/convert.c Implements conversion from Python objects to igraph metric types
tests/test_generators.py Adds basic test case for the new graph generator
Comments suppressed due to low confidence (2)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@szhorvat szhorvat force-pushed the feat/nearest-neighbor branch from 9ea8ead to 4b09056 Compare August 14, 2025 21:33
@szhorvat szhorvat force-pushed the feat/nearest-neighbor branch from 4b09056 to fb6f407 Compare August 14, 2025 21:38
Copy link
Member

@ntamas ntamas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer Graph.Nearest_Neighbor() so I'll do the rename.

Nope, I changed my mind ;-)

* Spatial graphs *
**********************************************************************/

PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type,
PyObject *igraphmodule_Graph_Nearest_Neighbor(PyTypeObject *type,

def testNearestNeighborGraph(self):
points = [[0,0], [1,2], [-3, -3]]

g = Graph.Nearest_Neighbor_Graph(points)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
g = Graph.Nearest_Neighbor_Graph(points)
g = Graph.Nearest_Neighbor(points)

self.assertEqual(g.vcount(), 3)
self.assertEqual(g.ecount(), 2)

g = Graph.Nearest_Neighbor_Graph(points, directed=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
g = Graph.Nearest_Neighbor_Graph(points, directed=True)
g = Graph.Nearest_Neighbor(points, directed=True)

self.assertEqual(g.ecount(), 3)

# expecting a complete graph
g = Graph.Nearest_Neighbor_Graph(points, k=2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
g = Graph.Nearest_Neighbor_Graph(points, k=2)
g = Graph.Nearest_Neighbor(points, k=2)

@ntamas ntamas merged commit fbb332f into develop Aug 24, 2025
29 of 33 checks passed
@ntamas ntamas deleted the feat/nearest-neighbor branch August 24, 2025 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants