-
Notifications
You must be signed in to change notification settings - Fork 132
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
Add hexagonal lattice graph generator #277
Conversation
Pull Request Test Coverage Report for Build 831926889
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm here that it's indeed intentional: it's a generator for the hexagonal graph and not heavy hex (that has some extra nodes in the edges), right?
If rows=0
or cols=0
should we return an empty graph as networkx does?
src/generators.rs
Outdated
// Add column edges | ||
for i in 0..collen { | ||
for j in 0..(rowlen - 1) { | ||
if i + 1 < rowlen { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe this check if i + 1 < rowlen
is needed, e.g hexagonal_graph(1, 3)
will give wrong result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thanks. I forgot that I added rowlen - 1
on the for loop.
Yep, I'm currently working on heavy hex.
Yep. |
Co-authored-by: georgios-ts <45130028+georgios-ts@users.noreply.github.com>
Co-authored-by: georgios-ts <45130028+georgios-ts@users.noreply.github.com>
Removed if statement ` i + 1 < rowlen` because it would yield wrong results as pointed out by @georgios-ts
Added coverage for creating an empty hexagonal graph
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few quick questions inline. Also do we want to add a directed graph version of this generator (networkx lets you do this, for example: nx.hexagonal_lattice_graph(10, 10, create_using=nx.DiGraph)
)?
src/generators.rs
Outdated
/// | ||
#[pyfunction(multigraph = true)] | ||
#[text_signature = "(/, rows=None, cols=None, multigraph=True)"] | ||
pub fn hexagonal_graph( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to not call this function hexagonal_latice_graph
like networkx?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really. It can be changed to hexagonal_latice_graph
.
graph.remove_node(nodes[rowlen - 1]); | ||
graph.remove_node( | ||
nodes[(collen - 1) * rowlen + (rowlen - 1) * ((collen - 1) % 2)], | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of removing the corner nodes here which will result in a graph with holes in the node indices couldn't we just detect the corner nodes in the edge loop and not add the edge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I will do an implementation without removing nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually deleting the nodes is required for the implementation. Thus, we have two options:
- Documenting that we are deleting nodes and which nodes will be missing;
Reindexing all nodes.
I will take a look at what will be the overhead of reindexing all nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran a benchmark comparing reindexing nodes:
Branches | Time |
---|---|
without reindexing | 0.115 |
with reindexing | 1.043 |
Reindexing this graph adds a significant overhead, therefore I will choose to document which nodes that we are deleting.
I guess so. I will do an implementation for directed hexagonal graph. |
I added the directed version of the hexagonal lattice graph: |
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function code LGTM, just a couple comments on the tests. It would be good if we had the tests validating the output edge list is actually a hexagonal lattice. I think once the tests are updated this is ready to go.
import retworkx | ||
|
||
|
||
class TestHexagonalLatticeGraph(unittest.TestCase): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some tests that use bidirectional=True
for directed_hexagonal_lattice_graph
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
tests/generators/test_hexagonal.py
Outdated
def test_hexagonal_graph_2_2(self): | ||
graph = retworkx.generators.hexagonal_lattice_graph(2, 2) | ||
self.assertEqual(len(graph), 16) | ||
self.assertEqual(len(graph.edges()), 19) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to verify that this is actually a hexagonal lattice so we know moving forward that if we make changes that we're not breaking this. Right now all we know is that there are 16 nodes and 19 edges in the graph, not how they're connected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll add that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks for updating the tests.
Related to #150.
Added new functions for the generator module:
undirected complete graph;Currently, the hexagonal generator does not support adding weights, because the algorithm to generate the graph needs to add auxiliary nodes and remove them at the end.
Update: Undirected complete graph was removed because it does the same thing that
mesh_graph
does and the itertools implementation is slower.