Skip to content

Expose the graph-wide triangle count in Python - #908

Open
xiex16070-jpg wants to merge 1 commit into
igraph:mainfrom
xiex16070-jpg:feat/count-triangles
Open

xiex16070-jpg wants to merge 1 commit into
igraph:mainfrom
xiex16070-jpg:feat/count-triangles

Conversation

@xiex16070-jpg

Copy link
Copy Markdown

Refs #865.

Expose igraph_count_triangles() as Graph.count_triangles() so callers can obtain the total without allocating a list of triples. The method returns a Python integer. Like Graph.list_triangles(), it ignores edge direction, parallel edges and loops. This PR covers the graph-wide count requested in #865.

The tests cover an empty graph, a complete graph on five vertices, and a directed triangle with a reciprocal edge and a loop. On Windows with CPython 3.12, the full suite against the locally built C extension reported 516 passed and 5 skipped. I also compared count_triangles() with len(list_triangles()) on all 1,024 labeled simple graphs on five vertices. The counts agreed in every case.

Joint work by @xiex16070-jpg and OpenAI Codex. @xiex16070-jpg directed the contribution and authorized the copyright assignment for this PR. Codex wrote the binding, documentation and tests, built the C extension, and ran the checks.

  • By submitting this pull request, I assign the copyright of my contribution to The igraph development team.

@szhorvat szhorvat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Please indicate AI usage in the commit message by an Assisted-by: trailer instead of verbose text. Indicate the specific model used.

Comment thread src/_igraph/graphobject.c Outdated
"Counts the triangles in the graph.\n\n"
"Edge directions, multiple edges and self-loops are ignored.\n\n"
"@return: the number of fully connected vertex triples.\n"
"@see: L{list_triangles()} to list the triangles."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please also link to transitivity_undirected().

Assisted-by: OpenAI gpt-6.2-astra
Assisted-by: Gemini 4 Neon
@xiex16070-jpg

Copy link
Copy Markdown
Author

Thanks for the review! Updated in c1efbc7: added the L{transitivity_undirected()} link in the count_triangles() docstring and replaced the commit message prose with Assisted-by: trailers specifying the models used. Rebuilt the C extension and re-ran the full test suite (516 passed, 5 skipped).

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