Crystal Graph Library (CGL)

Build Status

CGL is a Crystal library for the creation and manipulation of graph data structures.

All graph data structures are based on an adjacency list representation and heavily rely on Crystal Hash data structure.

Features

Documentation

Installation

  1. Add the dependency to your shard.yml:

`yaml dependencies:

 cgl:
   github: RomainFranceschini/cgl

`

  1. Run shards install

Usage

Import the CGL module with :

require "cgl"

Directed and undirected graphs types are provided. For each of those, multiple variants are offered so users can carefully select the ones that consume the least memory for their needs.

Undirected graphs

The following classes all implements undirected graphs. They allow self-loop edges that connects a vertex to itself. They ignore multiple edges between two vertices.

`crystal g = Graph(Char).new(edges: { {'a','b'}, {'a','f'}, {'f','b'} }) g.add_edge 'b', 'b' g.add_vertex 'e' g.order # => 5 g.size # => 4 `

`crystal g = WeightedGraph(Char, Int32).new(default_weight: 10) g.add_edge 'b', 'b', 1 g.add_edge 'a', 'b' g.weight_of('b', 'b') # => 1 g.weight_of('a', 'b') # => 10 `

`crystal g = LabeledGraph(String, Char).new(default_label: '👀') g.add_edge "hello", "world", label: '👍' g.add_edge "hello", "folks", label: '👎' g.add_edge "hello", "martians" g.label_of("hello", "martians") # => 👀 `

`crystal g = WeightedLabeledGraph(String, Char).new(default_weight: 10, default_label: '👀') g.add_edge "hello", "world", weight: 1, label: '👍' g.add_edge "hello", "folks", label: '👎' `

Directed graphs

The following classes all implements directed graphs. They allow self-loop edges, that connects a vertex to itself. They ignore multiple edges between two vertices.

`crystal g = DiGraph(Char).new(edges: { {'a','b'}, {'a','c'}, {'c','b'} }) `

`crystal g = WeightedDiGraph(Char, UInt8).new(default_weight: 1u8) `

`crystal g = LabeledDiGraph(String, Char).new(default_label: '👀') g.add_edge "hello", "world", label: '👍' `

`crystal g = WeightedLabeledDiGraph(String, Char).new(default_weight: 10, default_label: '👀') `

Multigraphs and Hypergraphs

TBD

Contributing

  1. Fork it (<https://github.com/RomainFranceschini/cgl/fork>)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors