monora / rgl

RGL is a framework for graph data structures and algorithms in Ruby.
https://monora.github.io/rgl/
Other
415 stars 59 forks source link

Example of Edge Properties Map #34

Closed emanuelhfarias closed 7 years ago

emanuelhfarias commented 8 years ago

Could you give me an example of how to set properties to edges? Or an example of adding new edges with weight and other attributes?

monora commented 8 years ago

RGL does not support this out of the box. In RGL any object can be a vertex of a graph. Edges are simply a pair of objects (ordered in case of an DirectedGraph or unordered when the graph is undirected, see comment in DirectedEdge. If you want to store properties to edges you could either use property maps (similar to BGLs PropertyMaps) or subclass RGLs Edge classes. You could simply use standard ruby hash maps in your own graph implementation to attach properties to edges.

A simple mixin to provide this functionality could look like this:

require 'rgl/base'
module RGL

  # A PropertyGraph can attach properties as key value pairs to vertices and edges.
  #
  module PropertyGraph
    def put_vertex_property(v, key, value)
    end
    def get_vertex_property(v, key)
    end
    def put_edge_property(v, w, key, value)
    end
    def get_edge_property(v, w, key)
    end
  end
end