rgl and vertices on path

M

meruby

How can get list of vertices from start node to end node in rgl?

For example:

require 'rgl/adjacency'
dg=RGL::DirectedAdjacencyGraph[1,2 ,2,3 ,2,4, 4,5, 6,4, 1,6]

How to do following?
get_path(1,5) should give [1,2,4,5] or [1,6,4,5] or both

Thanks in advance
 
M

Matthew Smillie

How can get list of vertices from start node to end node in rgl?

For example:

require 'rgl/adjacency'
dg=RGL::DirectedAdjacencyGraph[1,2 ,2,3 ,2,4, 4,5, 6,4, 1,6]

How to do following?
get_path(1,5) should give [1,2,4,5] or [1,6,4,5] or both

Thanks in advance

If you just to find one or two paths per graph, you can use breadth-
first search, keeping track of visited nodes at each step.

There's a breadth-first iterator in rgl which might help with this,
combined with a graph visitor to track the visited nodes. Remember
that not all directed graphs have paths between all nodes.

If you know something about the structure of the graphs you're
expecting, you might be able to come up with something slightly
better, but the breadth-first search will work.

matthew smillie.
 
H

Horst Duchene

Matthew said:
How can get list of vertices from start node to end node in rgl?

For example:

require 'rgl/adjacency'
dg=RGL::DirectedAdjacencyGraph[1,2 ,2,3 ,2,4, 4,5, 6,4, 1,6]

How to do following?
get_path(1,5) should give [1,2,4,5] or [1,6,4,5] or both

Thanks in advance

If you just to find one or two paths per graph, you can use breadth-
first search, keeping track of visited nodes at each step.

There's a breadth-first iterator in rgl which might help with this,
combined with a graph visitor to track the visited nodes. Remember
that not all directed graphs have paths between all nodes.
Using BFS is the right way. As an example usage have a look at the
method RGL::Graph#bfs_search_tree_from
(http://rgl.rubyforge.org/rgl/classes/RGL/Graph.html#M000067):

Returns a DirectedAdjacencyGraph, which represents a BFS search tree
starting at v. This method uses the tree_edge_event of BFSIterator to
record all tree edges of the search tree in the result.

# File lib/rgl/traversal.rb, line 238
238: def bfs_search_tree_from (v)
239: require 'rgl/adjacency'
240: bfs = bfs_iterator(v)
241: tree = DirectedAdjacencyGraph.new
242: bfs.set_tree_edge_event_handler { |from, to|
243: tree.add_edge(from, to)
244: }
245: bfs.set_to_end # does the search
246: tree
247: end

However you will not get all paths between two given nodes, but only a
single one which is determined by the actual sequence of the neigbors
of the visited vertices. If the adjacency list is a set, this sequence
is undefined. Getting all paths between two vertices is an exhaustive
search, which is very inefficient for larger graphs.

Horst Duchêne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top