Coder Social home page Coder Social logo

Check for a path between two vertices about rgl HOT 7 CLOSED

monora avatar monora commented on August 22, 2024 1
Check for a path between two vertices

from rgl.

Comments (7)

smithtim avatar smithtim commented on August 22, 2024 1

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.

monora avatar monora commented on August 22, 2024 1

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.

monora avatar monora commented on August 22, 2024 1

I think we can put it in an own module path.rb. Would you like to contribute it?

from rgl.

artkirienko avatar artkirienko commented on August 22, 2024 1

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.

monora avatar monora commented on August 22, 2024

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.

monora avatar monora commented on August 22, 2024

@smithtim does my answer help you? Can we close the issue?

from rgl.

smithtim avatar smithtim commented on August 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.