Comments (7)
That helps, thanks!
If v1
is not in the graph, it throws an exception. This fixes that:
module RGL::Graph
def path?(v1, v2)
# avoid exception if v1 is not in the graph
return false if not self.has_vertex?(v1)
# create BFS Iterator starting from v1
it = self.bfs_iterator(v1)
# Use method any? of Enumerable
it.any?(v2)
end
end
I am surprised that path?
is not included in RGL::Graph
by default. Maybe it could be added?
from rgl.
I would not add the method to the basic Graph API, because:
- I like to have small APIs which do the most basic stuff
- Graph module should not depend on module traversal.rb
You could add the method in this module extending module Graph. But that would also surprise people violating the POLS.
I would rather add the code either in README or examples.rb. What do you think?
from rgl.
I think we can put it in an own module path.rb
. Would you like to contribute it?
from rgl.
Hi @monora,
Please check my PRs, I've tried both approaches: simply added info to the README, created a method in path.rb
from rgl.
You can use BFS or DFS graph iterator to get the answer:
require 'rgl/adjacency'
require 'rgl/traversal'
module RGL::Graph
def path?(v1, v2)
# create BFS Iterarator starting from v1
it = self.bfs_iterator(v1)
# Use method any? of Enumerable
it.any?(v2)
end
end
dg=RGL::DirectedAdjacencyGraph[1,2 ,2,3 ,2,4, 4,5, 6,4, 1,6]
dg.path?(1,5)
=> true
dg.path?(5,1)
=> false
Each GraphIterator is a Stream which implements Enumerable:
dg.bfs_iterator.class.ancestors
=> [RGL::BFSIterator, RGL::GraphVisitor, RGL::GraphIterator, RGL::GraphWrapper, Stream, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject]
from rgl.
@smithtim does my answer help you? Can we close the issue?
from rgl.
I would be okay with having it in traversal.rb
. Maybe traversal.rb
could be renamed path.rb
? Or maybe it could go in its own module? Just throwing out some ideas. I do think it should be part of the library, rather than something people have to copy-paste.
from rgl.
Related Issues (20)
- Automate release process HOT 4
- Fix links in README link section
- Fix link to LICENSE in CONTRIBUTING.md
- dependency on pairing_heap would downgrade from v3.0.1 to v0.3.0 HOT 1
- Gem is not loading HOT 2
- set_to_begin incorrect for graph iterator
- Unclear how to use bellman_ford_shortest_paths method HOT 2
- The License of the Project is Unclear HOT 6
- how i can use this in laravel ? HOT 2
- `depth_first_search` Doesn't work HOT 1
- Remove superfluous :GRAY assignment in depth first visit
- Support Ruby 3.2
- doc: Fix bad badge for codeclimate
- Fix wrong module and class comments in generated documentation
- How can I add a subgraph? HOT 2
- each_in_neighbor() does not call a passed block
- each_in_neighbor() raises exception on unconnected vertices
- Refactor edge/vertex settings to no longer require individual options initialization HOT 1
- undefined method `set_vertex_options' HOT 7
- calling require inside a mehtod is causing memory bloat if this method is called multiple times. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rgl.