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: implement bidirectional adjacency graph #78

Closed
wants to merge 13 commits into from

Conversation

jsjuni
Copy link

@jsjuni jsjuni commented Feb 15, 2023

If you're comfortable with a separate vertices_dict for in-edges, I think this approach is cleaner. A DirectedAdjacencyGraph has hardly any state other than the vertices_dict, so why not simply make a separate graph? This avoids having to copy any implementation from the base class; it is implemented using only methods exposed by DirectedAdjacencyGraph.

This was my original proposed approach.

The only downside that I can see is that it duplicates the vertex references used as keys in the two vertices_dicts and duplicates operations on them. As you noted a couple of days ago, however, it is impossible to eliminate that without pulling some implementation into the subclass.

I restored the original module and made the changes to the test you suggested.

Keeps private @reverse graph. Passes all unit tests for DirectedAdjacencyGraph. Once full functionality is verified we can think about a more efficient
implementation.
Original bidirectional.rb module restored.
Copy link
Owner

@monora monora left a comment

Choose a reason for hiding this comment

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

I am fine with this implementation. Thank you! When we are through with my comments, I would prepare a new release with your changes.

lib/rgl/bidirectional_adjacency.rb Show resolved Hide resolved
lib/rgl/bidirectional_adjacency.rb Outdated Show resolved Hide resolved
lib/rgl/bidirectional_adjacency.rb Outdated Show resolved Hide resolved
lib/rgl/bidirectional_adjacency.rb Show resolved Hide resolved
include RGL
include RGL::Edge

class TestBidirectionalAdjacencyGraph < TestDirectedGraph
Copy link
Owner

Choose a reason for hiding this comment

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

In #76 (comment) I meant to define two separate test classes:

  1. Your new class
  2. The test class which inherits from TestDirectedGraph

I think, that this would increase readability. Perhaps providing a comment, that we simply reuse the tests from the super test class.

Copy link
Author

Choose a reason for hiding this comment

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

I've been unable to make this work.

First of all, TestDirectedGraph has dependencies on DirectedAdjacencyGraph scattered throughout. It's not difficult to hoist them all up into #setup, but that would have to be done.

I tried it, but simply subclassing the TestDirectedGraph and overriding #setup did not work as expected. I'll push what I have but it's not right yet.

Copy link
Owner

@monora monora Feb 17, 2023

Choose a reason for hiding this comment

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

For me that worked. See changes in 8c98f8a.

It also contains a minor doc change:

# This implementation of {BidirectionalGraph} creates an internal
  # {DirectedAdjacencyGraph} to store the in-edges and overrides methods

Does not work as expected. TestBirectionalAdjacencyGraphBasic is never run.
I modifed setup() so all tests should fail but there are no failures.
monora added a commit that referenced this pull request Feb 17, 2023
@jsjuni
Copy link
Author

jsjuni commented Feb 17, 2023

It appears to work but when you run the tests there's no result for TestBidirectionalAdjacencyGraphBasic; only the superclass TestDirectedGraph appears. I tried overriding setup in a way that all tests would fail and nothing failed. I don't understand this.

@monora
Copy link
Owner

monora commented Feb 17, 2023

You are right. I do not understand it either. The POLS is violated here by the test library. I just ran the test in TestDirectedGraph using the new BidirectionalAdjacencyGraph. All passed!

I propose to forget this experiment. I do not want to dig deeper...

@jsjuni
Copy link
Author

jsjuni commented Feb 17, 2023

Agreed.

@jsjuni
Copy link
Author

jsjuni commented Feb 17, 2023

Before you finalize anything, I think there's a bug in the constructor for BidirectionalAdjacencyGraph. I'm not sure it works if you pass it a graph argument.

@monora
Copy link
Owner

monora commented Feb 18, 2023

Ok, which tests do we need?

Bug in BidirectionalAdjacencyGraph constructor fixed.
@jsjuni
Copy link
Author

jsjuni commented Feb 18, 2023

I fixed the constructor and added tests in both directed_graph_test.rb and bidirectional_graph_test.rb. I fixed test_reverse in both, which was modifying a graph used for multiple tests. I think it's good now.

lib/rgl/bidirectional_adjacency.rb Show resolved Hide resolved
test/bidirectional_graph_test.rb Outdated Show resolved Hide resolved
test/bidirectional_graph_test.rb Outdated Show resolved Hide resolved
test/bidirectional_graph_test.rb Outdated Show resolved Hide resolved
monora added a commit that referenced this pull request Feb 21, 2023
- New class BidirectionalAdjacencyGraph which implements the protocol defined in
  module BidirectionalGraph
- add has_in_edge?, in_neighbors in BidirectionalGraph
- add tests for DirectedAdjacencyGraph similar to those in TestBidirectionalAdjacencyGraph
@monora
Copy link
Owner

monora commented Feb 21, 2023

Close and release in #83

@monora monora closed this Feb 21, 2023
@monora monora reopened this Feb 21, 2023
@monora
Copy link
Owner

monora commented Feb 21, 2023

@jsjuni : I reopened to let you push the contents of e70fdc0. This way you would be the author of the changes in master.

@monora monora changed the title bidirectional adjacency graph feat: implement bidirectional adjacency graph Feb 21, 2023
@jsjuni
Copy link
Author

jsjuni commented Feb 21, 2023

I'm not very experienced at working across forks. Can you clarify what you want me to do?

@monora
Copy link
Owner

monora commented Feb 21, 2023

Something like this:

git remote add monora https://github.com/monora/rgl.git # perhaps you have already this remote
git fetch monora

# last commit must be e70fdc
git log monora/78-bidirectional-graph

# Now in your local branch (bidirectional-1), which should have no changes:
git reset --hard monora/master                  # Bring branch to top of master
git reset --soft monora/78-bidirectional-graph  # Apply changes from my branch

Now your branch should have the desired changes e70fdc0 (check this).
Now commit these with my comment.

git add .
git commit -m'my comment...'
git push --force

git reset is often simpler than git rebase...
I hope, this works as expected.

@monora
Copy link
Owner

monora commented Feb 21, 2023

I forgot: If you do not want to loose the commits in your branch
bidirectional-1, you should clone and push it before doing git reset:

git checkout -b bidirectional-1-original bidirectional-1
git push
git checkout bidirectional-1

@jsjuni
Copy link
Author

jsjuni commented Feb 22, 2023

After the git reset --soft monora/78-bidirectional-graph I see this, which doesn't look right:

$ git status
On branch bidirectional-1
Your branch and 'origin/bidirectional-1' have diverged,
and have 3 and 13 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   lib/rgl/base.rb
	modified:   lib/rgl/bidirectional.rb
	deleted:    lib/rgl/bidirectional_adjacency.rb
	modified:   lib/rgl/path_builder.rb
	deleted:    test/bidirectional_graph_test.rb
	modified:   test/directed_graph_test.rb

Unless it matters to you, I don't mind if the changes aren't attributed to me. It was a fun little project but sic transit gloria mundi. If it's easier to just make the release yourself, feel free.

monora pushed a commit that referenced this pull request Feb 22, 2023
- New class BidirectionalAdjacencyGraph which implements the protocol defined in
  module BidirectionalGraph
- add has_in_edge?, in_neighbors in BidirectionalGraph
- add tests for DirectedAdjacencyGraph similar to those in TestBidirectionalAdjacencyGraph
@monora
Copy link
Owner

monora commented Feb 22, 2023

sic transit gloria mundi
I found an easier way to share the glory:

git commit --amend --author "Steven Jenkins <j.s.jenkins@jpl.nasa.gov>"

See new commit 1d6c1b4

monora pushed a commit that referenced this pull request Feb 22, 2023
- New class BidirectionalAdjacencyGraph which implements the protocol defined in
  module BidirectionalGraph
- add has_in_edge?, in_neighbors in BidirectionalGraph
- add tests for DirectedAdjacencyGraph similar to those in TestBidirectionalAdjacencyGraph
@monora
Copy link
Owner

monora commented Feb 22, 2023

Closed after changing commit author of #78 to @StevenJenkinsJPL

@monora
Copy link
Owner

monora commented Feb 22, 2023

Closed after changing commit author of #78 to @StevenJenkinsJPL.

@monora monora closed this Feb 22, 2023
@jsjuni jsjuni deleted the bidirectional-1 branch February 25, 2023 19:10
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