https://github.com/knowald/simple_graph
A simple graph library for Ruby.
https://github.com/knowald/simple_graph
bfs datastructures graphs pathfinding ruby
Last synced: 11 months ago
JSON representation
A simple graph library for Ruby.
- Host: GitHub
- URL: https://github.com/knowald/simple_graph
- Owner: knowald
- License: mit
- Created: 2017-11-23T16:33:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-06T15:31:55.000Z (over 8 years ago)
- Last Synced: 2025-07-12T12:11:44.140Z (12 months ago)
- Topics: bfs, datastructures, graphs, pathfinding, ruby
- Language: Ruby
- Size: 93.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SimpleGraph
A very basic graph gem for Ruby.
Currently only unweighted, undirected graphs are supported.
This means that multiple edges between nodes are ignored, although self loops are allowed.
#### Warning
Note that this is a very early version, and everything about this library is subject to change at any given time without notice. Expect breaking changes.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'simple_graph'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install simple_graph
## Documentation
Docs are built using YARD and are available at https://vesther.github.io/simple_graph/
## Usage
### Quickstart
```ruby
require "simple_graph"
# Creating a new, empty graph
graph = SimpleGraph::Graph.new
# Adding nodes to the graph
# Creates a empty node containing only a autogenerated identifier
# Returns a graph-unique identifier for the newly created node
foo = graph.add_node()
# IDs can also be set manually
graph.add_node(id: "Kevin")
# Graphs can also hold arbitrary information in the 'data' hash
stuff = {
age: 21,
depression: true
}
bar = graph.add_node(id: "Igor", data: stuff)
# Edges can be created by passing the two node IDs to the connect_nodes method
graph.connect_nodes(foo, "Kevin")
# Paths between two nodes can be found by breadth-first search
paths = graph.find_paths(foo, bar)
# Retrieving info about the graph
graph.nodes # Lists all of the nodes in the graph
graph.node_count # Returns the amount of nodes in the graph
graph.node_ids # Array of node identifiers in the graph
graph.are_connected?(foo, bar) # Checks whether two nodes are connected by an Edge
graph.include?("Kevin") # Checks whether the graph includes a node with the given ID
# Graphs can be written to files in the DOT format to be used with Graphviz
# Note that the node ID will be used for labels
File.write("test.dot", graph.to_dot_string)
# Export a graph to a JSON file
File.write("output.json", graph.to_json)
# Import a graph from a JSON file
graph.load_from_json(File.read("input.json"))
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/vesther/simple_graph.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).