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

Add hexagonal lattice graph generator #277

Merged
merged 31 commits into from
May 14, 2021
Merged

Conversation

nahumsa
Copy link
Contributor

@nahumsa nahumsa commented Mar 17, 2021

Related to #150.

Added new functions for the generator module:

  • undirected hexagonal lattice graph;
  • Directed hexagonal lattice graph;
  • 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.

@coveralls
Copy link

coveralls commented Mar 17, 2021

Pull Request Test Coverage Report for Build 831926889

  • 110 of 110 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 96.632%

Totals Coverage Status
Change from base Build 821058012: 0.05%
Covered Lines: 7059
Relevant Lines: 7305

💛 - Coveralls

Copy link
Collaborator

@georgios-ts georgios-ts left a 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 Show resolved Hide resolved
src/generators.rs Outdated Show resolved Hide resolved
// Add column edges
for i in 0..collen {
for j in 0..(rowlen - 1) {
if i + 1 < rowlen {
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@nahumsa
Copy link
Contributor Author

nahumsa commented Mar 23, 2021

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?

Yep, I'm currently working on heavy hex.

If rows=0 or cols=0 should we return an empty graph as networkx does?

Yep.

nahumsa and others added 4 commits March 23, 2021 19:52
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
Copy link
Member

@mtreinish mtreinish left a 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))?

///
#[pyfunction(multigraph = true)]
#[text_signature = "(/, rows=None, cols=None, multigraph=True)"]
pub fn hexagonal_graph(
Copy link
Member

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?

Copy link
Contributor Author

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.

Comment on lines +1071 to +1074
graph.remove_node(nodes[rowlen - 1]);
graph.remove_node(
nodes[(collen - 1) * rowlen + (rowlen - 1) * ((collen - 1) % 2)],
);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

@nahumsa nahumsa Apr 2, 2021

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.

Copy link
Contributor Author

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.

@nahumsa
Copy link
Contributor Author

nahumsa commented Mar 24, 2021

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))?

I guess so. I will do an implementation for directed hexagonal graph.

@mtreinish mtreinish changed the title Expanding generators Add hexagonal lattice graph generator Apr 2, 2021
@nahumsa
Copy link
Contributor Author

nahumsa commented Apr 13, 2021

I added the directed version of the hexagonal lattice graph: directed_hexagonal_lattice_graph, but I'm not sure if the name is too big.

Copy link
Member

@mtreinish mtreinish left a 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):
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

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)
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@mtreinish mtreinish left a 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.

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.

None yet

4 participants