algorithmable 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0f425945316b488e5f78067168f54dd56415392
4
- data.tar.gz: c9dd9353cf0883de156a9860472d18233c227ea2
3
+ metadata.gz: 23dff715f8d8ae5e91a4e738ec6fe52f9da12fe7
4
+ data.tar.gz: dbfa9eb077c85ba9957f10edbb99513e793ca495
5
5
  SHA512:
6
- metadata.gz: 6fd0bb3ef1d1f3fe956aaa0fce478fcd1dc474d54b02ea86619802873ab620d181f3e49c7fefaf86c805fdd917f69e00d5a2e6f50f5971d97afb2462a91d62af
7
- data.tar.gz: 5bdbf61cfb48f980c4d3d3721b7fc48a3f2c6aa26a249753b27bae965e83b41886855cbdae5e5b5904cbc621a3ee03456b826ee0e9345c1b59e338340dee923e
6
+ metadata.gz: d5a1aedb5222032d612c3037a91c94c303a1f4cfe955d752b97d6e4543d210f444fc5d01a203fe60c91dd969f4ed453a8e7bc5055264906bbbc062fc51f6ab4d
7
+ data.tar.gz: 784476ad38e96e828f2360f6272ab4d7b2b9d176446183257592d9595cdf769c85f59994a6ce23437537acd9ad1620f7c02b299fea43fcda3a7a648916d4ec6d
data/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  # Algorithmable
4
4
 
5
- Useful algorithms such as Sorting Strategies, Searches and Data Structures such as symbol tables, binary search tree BST, red black tree RBT,
6
- hash tables, bag, linked list, deque, queue FIFO, stack LIFO, graphs & Graph Traversal strategies for everyday usage.
5
+ Useful algorithms such as Sorting Strategies, Searches and Data Structures such as Priority Queues, Symbol Tables, Binary Search Tree BST, Red Black tree RBT,
6
+ Hash Tables, Bag, Linked List, Deque, Queue (FIFO), Stack (LIFO), Graphs & Graph Traversal strategies for everyday usage.
7
7
 
8
8
  Algorithms and Data Structures implemented using Ruby, for fun.
9
9
 
@@ -17,6 +17,8 @@ Library also contains solutions for several puzzles solved by implemented algori
17
17
  - Queue (FIFO)
18
18
  - Stack (LIFO)
19
19
  - Ordered Symbol Table BST. Implementation using Unbalanced Binary Search Tree.
20
+ - Minimum Priority Queue
21
+ - Maximum Priority Queue
20
22
 
21
23
  ## Graphs
22
24
 
@@ -35,11 +37,16 @@ Library also contains solutions for several puzzles solved by implemented algori
35
37
 
36
38
  - Bubble sort
37
39
  - Merge sort
38
- - Heapsort ( Binary Heap )
40
+ - Heaport ( Binary Heap )
41
+
42
+ ## Algorithms
43
+
44
+ - Levenshtein Distance algorithm (http://www.levenshtein.net)
45
+ - Lawrence Philips' Metaphone Algorithm (http://aspell.net/metaphone)
39
46
 
40
47
  # Usage
41
48
 
42
- It is always better to check unit tests in order to understand how things going on.
49
+ It is always better to check unit tests in order to understand how things going on.
43
50
  Most of the components are encapsulated through the factory methods, so the are could be mixed to other objects.
44
51
 
45
52
  ## Installation
@@ -65,4 +72,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/rlisht
65
72
  ## License
66
73
 
67
74
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
68
-
@@ -0,0 +1,87 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module Heap
4
+ class Imp
5
+ attr_reader :size
6
+
7
+ def initialize(collection = [], &block)
8
+ @comparison_function = block || -> (this, other) { (this <=> other) < 0 }
9
+ @queue = []
10
+ @size = 0
11
+ collection.each do |item|
12
+ enqueue item
13
+ end
14
+ end
15
+
16
+ def empty?
17
+ !@size.nonzero?
18
+ end
19
+
20
+ def enqueue(key)
21
+ @size = @size.next
22
+ @queue[@size] = key
23
+ swim @size
24
+ maintain_heap_invariant!
25
+ end
26
+
27
+ def dequeue
28
+ return if empty?
29
+ maximum = @queue[1]
30
+ exchange 1, @size
31
+ @size = @size.pred
32
+ sink 1
33
+ @queue[@size.next] = nil
34
+ maintain_heap_invariant!
35
+ maximum
36
+ end
37
+
38
+ def peek
39
+ return if empty?
40
+ @queue[1]
41
+ end
42
+
43
+ private
44
+
45
+ def exchange(this, other)
46
+ swap = @queue[this]
47
+ @queue[this] = @queue[other]
48
+ @queue[other] = swap
49
+ end
50
+
51
+ def maintain_heap_invariant!
52
+ fail "Heap is not invariant now: #{inspect}!" unless assert_heap_invariant(1)
53
+ end
54
+
55
+ def assert_heap_invariant(index)
56
+ return true if index > @size
57
+ left = 2 * index
58
+ right = 2 * index + 1
59
+ return false if left <= @size && agreed(index, left)
60
+ return false if right <= @size && agreed(index, right)
61
+ assert_heap_invariant(left) && assert_heap_invariant(right)
62
+ end
63
+
64
+ def swim(index)
65
+ while index > 1 && agreed(index / 2, index)
66
+ exchange(index, index / 2)
67
+ index /= 2
68
+ end
69
+ end
70
+
71
+ def agreed(this, that)
72
+ @comparison_function.call(@queue[this], @queue[that])
73
+ end
74
+
75
+ def sink(index)
76
+ while 2 * index <= @size
77
+ temp_index = 2 * index
78
+ temp_index = temp_index.next if temp_index < @size && agreed(temp_index, temp_index.next)
79
+ break unless agreed(index, temp_index)
80
+ exchange(index, temp_index)
81
+ index = temp_index
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,13 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module Heap
4
+ class Max < Imp
5
+ def initialize(collection = [])
6
+ super(collection) do |this, other|
7
+ (this <=> other) < 0
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module Heap
4
+ class Min < Imp
5
+ def initialize(collection = [])
6
+ super(collection) do |this, other|
7
+ (this <=> other) > 0
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Algorithmable
2
+ module DataStructs
3
+ module Heap
4
+ autoload :Imp, 'algorithmable/data_structs/heap/imp'
5
+ autoload :Max, 'algorithmable/data_structs/heap/max'
6
+ autoload :Min, 'algorithmable/data_structs/heap/min'
7
+ private_constant :Imp
8
+ end
9
+ end
10
+ end
@@ -6,6 +6,7 @@ module Algorithmable
6
6
  autoload :Queue, 'algorithmable/data_structs/queue'
7
7
  autoload :Stack, 'algorithmable/data_structs/stack'
8
8
  autoload :OrderedSymbolTable, 'algorithmable/data_structs/ordered_symbol_table'
9
+ autoload :Heap, 'algorithmable/data_structs/heap'
9
10
 
10
11
  def new_bag
11
12
  Bag.new
@@ -26,5 +27,17 @@ module Algorithmable
26
27
  def new_ordered_symbol_table(key_type, value_type)
27
28
  OrderedSymbolTable.new(key_type, value_type)
28
29
  end
30
+
31
+ def new_max_priority_heap(collection = [])
32
+ Heap::Max.new(collection)
33
+ end
34
+
35
+ alias_method :new_max_priority_queue, :new_max_priority_heap
36
+
37
+ def new_min_priority_heap(collection = [])
38
+ Heap::Min.new(collection)
39
+ end
40
+
41
+ alias_method :new_min_priority_queue, :new_min_priority_heap
29
42
  end
30
43
  end
@@ -1,22 +1,20 @@
1
1
  module Algorithmable
2
2
  module Sort
3
3
  class BinaryHeap
4
- def self.sort(collection)
5
- new.sort(collection)
6
- end
4
+ include Algorithmable::DataStructs
7
5
 
8
- def sort(collection)
9
- fail NotImplementedError, collection
6
+ def self.sort(collection)
7
+ new(collection).sort
10
8
  end
11
9
 
12
- private
13
-
14
- def swim(node)
15
- fail NotImplementedError, node
10
+ def initialize(collection = [])
11
+ @heap = new_min_priority_heap collection
16
12
  end
17
13
 
18
- def sink(node)
19
- fail NotImplementedError, node
14
+ def sort
15
+ [].tap do |result|
16
+ result << @heap.dequeue until @heap.empty?
17
+ end
20
18
  end
21
19
  end
22
20
  end
@@ -12,6 +12,10 @@ module Algorithmable
12
12
  def bubble(collection)
13
13
  Bubble.sort(collection)
14
14
  end
15
+
16
+ def binary_heap(collection)
17
+ BinaryHeap.sort(collection)
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -1,3 +1,3 @@
1
1
  module Algorithmable
2
- VERSION = '0.10.0'
2
+ VERSION = '0.11.0'
3
3
  end
data/lib/algorithmable.rb CHANGED
@@ -11,6 +11,7 @@ module Algorithmable
11
11
  autoload :DataStructs, 'algorithmable/data_structs'
12
12
  autoload :Graphs, 'algorithmable/graphs'
13
13
  autoload :Puzzles, 'algorithmable/puzzles'
14
+ autoload :LevenshteinDistance, 'algorithmable/levenshtein_distance'
14
15
 
15
16
  class << self
16
17
  def logger
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.10.0
4
+ version: 0.11.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: 2016-01-10 00:00:00.000000000 Z
11
+ date: 2016-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,6 +143,10 @@ files:
143
143
  - lib/algorithmable/data_structs.rb
144
144
  - lib/algorithmable/data_structs/bag.rb
145
145
  - lib/algorithmable/data_structs/deque.rb
146
+ - lib/algorithmable/data_structs/heap.rb
147
+ - lib/algorithmable/data_structs/heap/imp.rb
148
+ - lib/algorithmable/data_structs/heap/max.rb
149
+ - lib/algorithmable/data_structs/heap/min.rb
146
150
  - lib/algorithmable/data_structs/linked_list.rb
147
151
  - lib/algorithmable/data_structs/ordered_symbol_table.rb
148
152
  - lib/algorithmable/data_structs/queue.rb