algorithmable 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0dfae481af55f0814906035338566941f6fcfaa5
4
- data.tar.gz: f26f40b975a6dee84e3139b28da2297b3021254f
3
+ metadata.gz: faa2cfc1ad0200b15adfbfa778a72b89d605e15f
4
+ data.tar.gz: 4b4a5326da1e136fb0513e35a29bae2b1171dfe0
5
5
  SHA512:
6
- metadata.gz: 7a9e436a366de2922dfa888d82e46a9d2ebb18ff163f7fe11aa0181e1b640cad9df860a4358e01f98467bb1601730e77cc3f76fdc1ca8600e17cfe67fa515f20
7
- data.tar.gz: 3438f7922c2cd0ec507a89289185d1121f0139933d89bd03abd3ba14137f338a254cc8cd16406a73d02c8440447368adcd47c3ceef8eea2bd89c8f05da957b7d
6
+ metadata.gz: 2a79e522c0901adb9fbbc434eb0dc760556ba27d932f8036d124ad6bce9e4145f77c4a1cae96c2dd5f806add3b5f5a94f7580d8597a79f96c65c05a7b8f37d7c
7
+ data.tar.gz: c22a35a53cf69acd26b10f5c127da3f6bc5e92ab053c795fa74877410c91b2bbcafc3b484fe9fa226ce10bf61093fab3851389ecb92e757259170bea0bd64603
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
+ [![Build Status](https://travis-ci.org/rlishtaba/algorithmable.svg?branch=master)](https://travis-ci.org/rlishtaba/algorithmable)
2
+
1
3
  # Algorithmable
2
4
 
3
- Useful algorithms for everyday usage implemented using Ruby.
5
+ Useful algorithms such as Sorting, Searches, Graph Traversal strategies & data structures such as Symbol Tables, BST, Hash Tables, Bag, Linked List, Queue, Stack, Graphs for everyday usage. Implemented using Ruby, for fun.
4
6
 
5
7
  ## Installation
6
8
 
data/lib/algorithmable.rb CHANGED
@@ -5,6 +5,9 @@ require 'pathname'
5
5
  require 'English'
6
6
 
7
7
  module Algorithmable
8
+ autoload :Sort, 'algorithmable/sort'
9
+ autoload :Graphs, 'algorithmable/graphs'
10
+
8
11
  class << self
9
12
  def logger
10
13
  @logger ||= Logger.new $stdout
@@ -0,0 +1,16 @@
1
+ module Algorithmable
2
+ module Graphs
3
+ autoload :Undirected, 'algorithmable/graphs/undirected'
4
+ autoload :Traversals, 'algorithmable/graphs/traversals'
5
+
6
+ class << self
7
+ def new_undirected_graph(vertices = [])
8
+ graph = Undirected.new(vertices.size)
9
+ vertices.each do |left, right|
10
+ graph.add_edge left, right
11
+ end
12
+ graph
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module Algorithmable
2
+ module Graphs
3
+ module Traversals
4
+ autoload :Errors, 'algorithmable/graphs/traversals/errors'
5
+ autoload :DepthFirst, 'algorithmable/graphs/traversals/depth_first'
6
+ autoload :BreathFirst, 'algorithmable/graphs/traversals/breath_first'
7
+
8
+ def traverse_with_depth_first(graph, source)
9
+ DepthFirst.new(graph, source)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,44 @@
1
+ module Algorithmable
2
+ module Graphs
3
+ module Traversals
4
+ class DepthFirst
5
+ include Algorithmable::Graphs::Traversals::Errors
6
+
7
+ def initialize(graph, source)
8
+ @visited = []
9
+ @edge_to = []
10
+ @source = source
11
+ traverse graph, source
12
+ end
13
+
14
+ def visited?(vertex)
15
+ @visited[vertex]
16
+ end
17
+
18
+ def path_to(vertex)
19
+ fail UnvisitedVertexError, vertex unless visited?(vertex)
20
+ path = []
21
+ finish = vertex
22
+ while finish != @source
23
+ path.push finish
24
+ finish = @edge_to[finish]
25
+ end
26
+ path.push @source
27
+ path
28
+ end
29
+
30
+ private
31
+
32
+ def traverse(graph, vertex)
33
+ @visited[vertex] = true
34
+ graph.adjacency(vertex).each do |neighbour_vertex|
35
+ unless visited? neighbour_vertex
36
+ @edge_to[neighbour_vertex] = vertex
37
+ traverse graph, neighbour_vertex
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ module Algorithmable
2
+ module Graphs
3
+ module Traversals
4
+ module Errors
5
+ TraversalError = Class.new RuntimeError
6
+ UnvisitedVertexError = Class.new TraversalError
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,26 @@
1
+ module Algorithmable
2
+ module Graphs
3
+ class Undirected
4
+ attr_reader :vertices, :edges
5
+
6
+ def initialize(vertices = 0)
7
+ @vertices = vertices
8
+ @edges = 0
9
+ @adj = []
10
+ @vertices.times { |i| @adj[i] = [] }
11
+ end
12
+
13
+ def add_edge(left_vertex, right_vertex)
14
+ @adj[left_vertex] ||= []
15
+ @adj[right_vertex] ||= []
16
+ @adj[left_vertex].push right_vertex
17
+ @adj[right_vertex].push left_vertex
18
+ @edges = @edges.next
19
+ end
20
+
21
+ def adjacency(vertex)
22
+ @adj[vertex]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module Algorithmable
2
+ module Sort
3
+ autoload :Merge, 'algorithmable/sort/merge'
4
+ autoload :Bubble, 'algorithmable/sort/bubble'
5
+
6
+ class << self
7
+ def merge(collection)
8
+ Merge.sort(collection)
9
+ end
10
+
11
+ def bubble(collection)
12
+ Bubble.sort(collection)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ module Algorithmable
2
+ module Sort
3
+ class Bubble
4
+ def self.sort(collection)
5
+ new.sort(collection)
6
+ end
7
+
8
+ def sort(collection)
9
+ loop do
10
+ swapped = false
11
+ (collection.size.pred).times do |index|
12
+ if (collection[index] <=> collection[index.next]) == 1
13
+ collection = swap(index, collection)
14
+ swapped = true
15
+ end
16
+ end
17
+ break unless swapped
18
+ end
19
+ collection
20
+ end
21
+
22
+ private
23
+
24
+ def swap(index, collection)
25
+ current = collection[index]
26
+ collection[index] = collection[index.next]
27
+ collection[index.next] = current
28
+ collection
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module Algorithmable
2
+ module Sort
3
+ class Merge
4
+ def self.sort(collection)
5
+ new.sort(collection)
6
+ end
7
+
8
+ def sort(collection)
9
+ return collection if collection.size <= 1
10
+ mid = collection.size / 2
11
+ left = collection[0...mid]
12
+ right = collection[mid...collection.size]
13
+ merge(sort(left), sort(right))
14
+ end
15
+
16
+ private
17
+
18
+ def merge(left, right)
19
+ sorted = []
20
+ until left.empty? || right.empty?
21
+ left.first <= right.first ? sorted << left.shift : sorted << right.shift
22
+ end
23
+ sorted + left + right
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Algorithmable
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algorithmable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Lishtaba
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2015-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,6 +140,14 @@ files:
140
140
  - algorithmable.gemspec
141
141
  - cucumber.yml
142
142
  - lib/algorithmable.rb
143
+ - lib/algorithmable/graphs.rb
144
+ - lib/algorithmable/graphs/traversals.rb
145
+ - lib/algorithmable/graphs/traversals/depth_first.rb
146
+ - lib/algorithmable/graphs/traversals/errors.rb
147
+ - lib/algorithmable/graphs/undirected.rb
148
+ - lib/algorithmable/sort.rb
149
+ - lib/algorithmable/sort/bubble.rb
150
+ - lib/algorithmable/sort/merge.rb
143
151
  - lib/algorithmable/version.rb
144
152
  - script/console
145
153
  homepage: ''