Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Vertex degree [free function version] #29

Merged
merged 4 commits into from
May 31, 2023

Conversation

joweich
Copy link
Collaborator

@joweich joweich commented May 30, 2023

See thread in #24 for details

@codecov
Copy link

codecov bot commented May 31, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.02 🎉

Comparison is base (0a5188b) 99.84% compared to head (2b83196) 99.86%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #29      +/-   ##
==========================================
+ Coverage   99.84%   99.86%   +0.02%     
==========================================
  Files          16       18       +2     
  Lines         655      754      +99     
==========================================
+ Hits          654      753      +99     
  Misses          1        1              
Impacted Files Coverage Δ
include/graaflib/properties/vertex_properties.tpp 100.00% <100.00%> (ø)
...est/graaflib/properties/vertex_properties_test.cpp 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Owner

@bobluppes bobluppes left a comment

Choose a reason for hiding this comment

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

Nice, looks excellent!

Some smaller comments in case you want to take a look, but can also do that in a follow-up.
Just give me the 👍🏻 and I am happy to merge

include/graaflib/properties/vertex_properties.h Outdated Show resolved Hide resolved
include/graaflib/properties/vertex_properties.tpp Outdated Show resolved Hide resolved
Comment on lines 31 to 37
int indegree{0};
for (const auto& current_vertex : graph.get_vertices()) {
if (graph.get_neighbors(current_vertex.first).contains(vertex_id)) {
indegree++;
}
}
return indegree;
Copy link
Owner

Choose a reason for hiding this comment

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

Depending on whether we expect graphs to be spart/dense it might also be worth to loop over the edges. I would propose to leave this as-is for now until we have some actual usage though.

If you are up for it, a slightly more modern C++ experience would be to use std::algorithm. However I can also do this in a follow-up.

Iterating over all vertices

// Requires including <algorithm>
return std:ranges::count_if(graph.get_vertices(), [&graph, vertex_id](const vertex_id_to_vertex_t::value_type& kv_pair) {
    const auto& [current_vertex_id, _]{kv_pair};
    return graph.get_neighbors(current_vertex_id).contains(vertex_id);
});

Iterating over all edges

// Requires including <algorithm>
return std::ranges::count_if(graph.get_edges(), [vertex_id](const vertex_ids_to_edge_t::value_type& kv_pair) {
    const auto& [edge_id, _]{kv_pair};
    return edge_id.first == vertex_id || edge_id.second == vertex_id;
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Depending on whether we expect graphs to be spart/dense it might also be worth to loop over the edges. I would propose to leave this as-is for now until we have some actual usage though.

I think it's hard to come up with a generic approach here. Depending on the graph architecture, the one or the other might be faster.

If you are up for it, a slightly more modern C++ experience would be to use std::algorithm.

Damn, I feel this wasn't there in C++14. Covered in 50d78ac.

Copy link
Owner

Choose a reason for hiding this comment

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

Very nice, thanks!

Yes, luckily we have C++20 now 😉

@bobluppes bobluppes added the enhancement New feature label May 31, 2023
@bobluppes bobluppes merged commit 62d5cc6 into bobluppes:main May 31, 2023
3 checks passed
@joweich joweich deleted the feat/vertex_degree_free branch May 31, 2023 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants