Browse files

Initial commit

  • Loading branch information...
0 parents commit c2839a505ea0f2406bb27b92936a5fbaeb1ea9aa @curiousily curiousily committed May 6, 2012
Showing with 230 additions and 0 deletions.
  1. +23 −0 .gitignore
  2. +1 −0 .rspec
  3. +3 −0 Gemfile
  4. +22 −0 LICENSE
  5. +33 −0 README.md
  6. +2 −0 Rakefile
  7. +20 −0 graphlz.gemspec
  8. +6 −0 lib/graphlz.rb
  9. +47 −0 lib/graphlz/graph.rb
  10. +29 −0 lib/graphlz/node.rb
  11. +3 −0 lib/graphlz/version.rb
  12. +2 −0 spec/spec_helper.rb
  13. +39 −0 spec/specs/graph_spec.rb
23 .gitignore
@@ -0,0 +1,23 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
+
+# Ignore idea stuff
+
+.idea/
+*.iml
+*.swp
1 .rspec
@@ -0,0 +1 @@
+--color
3 Gemfile
@@ -0,0 +1,3 @@
+source 'https://rubygems.org'
+
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Venelin Valkov, venelin@bapplz.com, bapplz
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 README.md
@@ -0,0 +1,33 @@
+# Graphlz
+
+A dead simple implementation of an Undirected (for now) graph
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'graphlz'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install graphlz
+
+## Usage
+
+ graph = Graph.new
+ graph.add_node 3
+ graph.add_node 5
+ graph.connect(3, 5)
+ graph.connected?(3, 5) == true
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2 Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
20 graphlz.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/graphlz/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Venelin Valkov"]
+ gem.email = %w(venelin@bapplz.com)
+ gem.description = %q{Simple Graph implementation}
+ gem.summary = %q{Dead simple graph implementation}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "graphlz"
+ gem.require_paths = %w(lib)
+ gem.version = Graphlz::VERSION
+
+ gem.add_development_dependency "rspec", "~>2.10.0"
+ gem.add_dependency "yard", "~>0.8.1"
+end
6 lib/graphlz.rb
@@ -0,0 +1,6 @@
+require "graphlz/node"
+require "graphlz/graph"
+require "graphlz/version"
+
+module Graphlz
+end
47 lib/graphlz/graph.rb
@@ -0,0 +1,47 @@
+module Graphlz
+
+ # Represents a Undirected general graph stored via Adjacency list
+ #
+ # Example
+ # graph = Graph.new
+ # graph.add_node 3
+ # graph.add_node 5
+ # graph.connect(3, 5)
+ # graph.connected?(3, 5) == true
+ class Graph
+ def initialize
+ @nodes = Hash.new
+ end
+
+ def add_node node_id
+ @nodes[node_id] = Node.new(node_id)
+ end
+
+ def connect(source_id, target_id)
+ if contain_nodes(source_id, target_id)
+ @nodes[source_id].connect_to @nodes[target_id]
+ @nodes[target_id].connect_to @nodes[source_id]
+ end
+ end
+
+ def connected?(source_id, target_id)
+ if contain_nodes(source_id, target_id)
+ return @nodes[source_id].connected?(@nodes[target_id])
+ end
+ false
+ end
+
+ def neighbours_of node_id
+ if @nodes.has_key? node_id
+ return @nodes[node_id].neighbours
+ end
+ Set.new
+ end
+
+ private
+
+ def contain_nodes(node1, node2)
+ @nodes.has_key?(node1) && @nodes.has_key?(node2)
+ end
+ end
+end
29 lib/graphlz/node.rb
@@ -0,0 +1,29 @@
+module Graphlz
+ # Vertex or node or point in a graph
+ #
+ # The id of the node can be anything that responds to `#eql?` and `#hash`
+ class Node
+ attr_reader :id, :neighbours
+
+ def initialize(id)
+ @id = id
+ @neighbours = Set.new
+ end
+
+ def connect_to node
+ @neighbours << node
+ end
+
+ def connected? node
+ @neighbours.include? node
+ end
+
+ def eql?(other)
+ @id.eql?(other.id)
+ end
+
+ def hash
+ @id.hash
+ end
+ end
+end
3 lib/graphlz/version.rb
@@ -0,0 +1,3 @@
+module Graphlz
+ VERSION = "0.0.1"
+end
2 spec/spec_helper.rb
@@ -0,0 +1,2 @@
+require 'graphlz'
+include Graphlz
39 spec/specs/graph_spec.rb
@@ -0,0 +1,39 @@
+require "rspec"
+require "spec_helper"
+
+describe Graph do
+
+ before(:each) do
+ @graph = Graph.new
+ @graph.add_node 1
+ @graph.add_node 2
+ @graph.add_node 5
+ end
+
+ it "should have properly connected nodes" do
+ @graph.connect(1, 5)
+ @graph.connected?(1, 5).should == true
+ end
+
+ it "should have not connected nodes" do
+ @graph.connected?(2, 9).should == false
+ end
+
+ it "should have the right size of neighbours for a node" do
+ @graph.connect(1, 5)
+ @graph.connect(1, 2)
+ @graph.neighbours_of(1).size == 2
+ end
+
+ it "should add the same neighbour only once" do
+ @graph.connect(1, 5)
+ @graph.connect(1, 5)
+ @graph.neighbours_of(1).size == 1
+ end
+
+ it "should have undirected connection between nodes" do
+ @graph.connect(1, 5)
+ @graph.connected?(1, 5).should == true
+ @graph.connected?(5, 1).should == true
+ end
+end

0 comments on commit c2839a5

Please sign in to comment.