rbutils 0.0.2 → 0.0.3

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NmNmZGQ1NjQ4OTljNjdhZmUyNzliNWVjMjZlZjc4ZGY0MTIzOTU0NA==
4
+ YmVmYmZkNjEwODUxNjE2ZTQwYWIzYTAyN2Q2MzkzYjcwNjk2YWViZA==
5
5
  data.tar.gz: !binary |-
6
- MDgxZWZmNTEyZGYwOTIyZjE4MzRiYjBhM2YyNzFkMjhkYzM2N2I1Mg==
6
+ OTBiZTYyYzE5YjBiZmNhNjFmOTBiNGJhMzlkMDAxODYxMDYxNGJkMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGJlYWYzOTMyMTNiZDc3ZDhkODIyMjZhNWUwZDI4MjkzMmRmM2E0Yjg1MmM3
10
- YzJkMWI0NWFmZmRkYzlmMjI2N2IwZmQ2MGExMmIyYTIwMzJkYjBiYWVhYmQ0
11
- ODFmNDkzZDk5MDVmM2I5ZDRlMjQ5ZDM3OWE0Nzc2OWFlZmY3NDA=
9
+ MmNiYTVmNGNiMjRjNGY4NjlmMjFmYmM2YWNhN2M1ODlhNDVkY2UyN2I4NDg5
10
+ Y2RlMTQ3NTY1YWYwNmUzODNiMDYzODk3MjYyYTlhZjQ5ZDVjYzYxYmY1NDhh
11
+ Zjc4NWE3Y2MzYjU3YzNhNTFkYzYwZTliY2Y2NmU1YmRmY2RmMjk=
12
12
  data.tar.gz: !binary |-
13
- ZWExMTIyYjM2MTFlMTVjMWRlN2U3Y2NhMTRlY2RmOGFiMjhhMWQ4MzEzMTZj
14
- ODE4M2FhY2ZkZTA5NDYxZjc4MGFhMDJhNjBkYmVkNGMwMDZmMzU4NDI2MDk0
15
- NjM0OTEyNjlhYzFjZGUzN2ZmZjMxZjRiOGVlM2I1YTNiYTIyMmE=
13
+ ZjgxMjYyNjViM2Q1MDhhOWYyNzBjYTY3Y2JkMWM1YzZkNzYyYWYwNWRkNWFj
14
+ NjU3NzFkODliY2RjMTE2NGRiN2I4ZWVhZGUyOTRmMzBlZjEzOTkxYWE0MmRk
15
+ YTc2ZDUzNjgzOGQ4ZDU5NmI4NzZmYjE0NjI5MjBjMjhhZDMzNTY=
@@ -0,0 +1,45 @@
1
+ require_relative 'Graph'
2
+
3
+ class BFS < Graph
4
+ def initialize(graph, source)
5
+ @graph = graph
6
+ @node = source
7
+ @visited = []
8
+ @edge_to = {}
9
+
10
+ bfs(source)
11
+ end
12
+
13
+ def bfs(node)
14
+ #we have to use a queue for BFS
15
+ queue = []
16
+ #initially push the node to the queue
17
+ queue << node
18
+ #mark node as visited
19
+ @visited << node
20
+
21
+ #while there are elements in the wueue,
22
+ #continue adding from adjacent nodes
23
+ while queue.any?
24
+ curr = queue.shift
25
+ curr.adjacents.each do |adj|
26
+ next if @visited.include?(adj)
27
+ queue << adj
28
+ @visited << adj
29
+ @edge_to[adj] = curr
30
+ end
31
+ end
32
+ end
33
+
34
+ #returns the path to the node after
35
+ #bfs has completed
36
+ def path_to(node)
37
+ return unless @visited.include?(node)
38
+ path = []
39
+ while(node != @node) do
40
+ path.unshift(node)
41
+ node = @edge_to[node]
42
+ end
43
+ path.unshift(@node)
44
+ end
45
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'Graph'
2
+
3
+ class DFS < Graph
4
+ def initialize(graph, source)
5
+ @graph = graph
6
+ @source = source
7
+ @visited = []
8
+ @edge_to = {}
9
+
10
+ #perform dfs
11
+ dfs(source)
12
+ end
13
+
14
+ def dfs(node)
15
+ #mark node as visited
16
+ @visited << node
17
+
18
+ #take each adjacent node
19
+ node.adjacents.each do |adj|
20
+ next if @visited.include?(adj)
21
+ #perform dfs
22
+ dfs(adj)
23
+ #check edges
24
+ @edge_to[adj] = node
25
+ end
26
+ end
27
+
28
+ def path_to(node)
29
+ return unless @visited.include?(node)
30
+ path = []
31
+ curr = node
32
+
33
+ #get the paths from the specified node to the
34
+ #source
35
+ while(curr != @source) do
36
+ path.unshift(curr)
37
+ curr = @edge_to[curr]
38
+ end
39
+ path.unshift(@source)
40
+ end
41
+ end
@@ -0,0 +1,67 @@
1
+ require_relative "priorityqueue"
2
+
3
+ class Dijkstra < Graph
4
+
5
+ #source node and graph needed
6
+ def initialize(graph, source)
7
+
8
+ #init variables
9
+ @graph = graph
10
+ @source = source
11
+ @path = {}
12
+ @dist_to = {}
13
+ @queue = PriorityQueue.new
14
+
15
+ #compute the shortest path
16
+ computeSP
17
+ end
18
+
19
+ def path_to(node)
20
+ path = []
21
+ while node != @source
22
+ #add node to the front of the path
23
+ path.unshift(node)
24
+ node = @path[node]
25
+ end
26
+ #add the source at the beginning
27
+ #invariant: source is at the front
28
+ # and remaining nodes in the path are behind it
29
+ # (reverse order)
30
+ path.unshift(@source)
31
+ end
32
+
33
+ #Computer the shortest path source the source and
34
+ #Every other node
35
+ def computeSP
36
+ @graph.nodes.each do |node|
37
+ @dist_to[node]=Float::INFINITY
38
+ end
39
+ @dist_to[@source] = 0
40
+
41
+ #node and it's distance to source (source -> source = 0)
42
+ @queue.insert(@source, 0)
43
+
44
+ #while elements exist in the queue
45
+ while @queue.any?
46
+ #check the lowest distance (closest)
47
+ close = @queue.remove_min
48
+ #check each adjacent edge
49
+ close.adjacent_edges.each do |adjacent|
50
+ relax(adjacent)
51
+ end
52
+ end
53
+ end
54
+
55
+ # Check if the shortest known path is still the shortest path
56
+ # That is, are there any shorter paths along this edge?
57
+ def relax(edge)
58
+ #If the distance currently is lower than the added distance,
59
+ #Don't increase it.
60
+ return if @dist_to[edge.dest] <= @dist_to[edge.source] + edge.weight
61
+
62
+ #Update the distance and add it back to the queue
63
+ @dist_to[edge.dest] = @dist_to[edge.source] + edge.weight
64
+ @path[edge.dest] = edge.source
65
+ @queue.insert(edge.dest, @dist_to[edge.dest])
66
+ end
67
+ end
@@ -0,0 +1,19 @@
1
+ class Edge
2
+ attr_accessor :source
3
+ attr_accessor :dest
4
+ attr_accessor :weight
5
+
6
+ def initialize(source, dest, weight)
7
+ @source = source
8
+ @dest = dest
9
+ @weight = weight
10
+ end
11
+
12
+ def <=>(other)
13
+ self.weight <=> other.weight
14
+ end
15
+
16
+ def to_str
17
+ "#{source.to_str} => #{dest.to_str}, weight is #{weight}"
18
+ end
19
+ end
@@ -0,0 +1,67 @@
1
+ require 'set'
2
+
3
+ class Graph
4
+ attr_accessor :nodes
5
+ attr_accessor :edges
6
+ attr_accessor :connections
7
+
8
+ def initialize
9
+ @nodes = Set.new
10
+ @edges = Array.new
11
+ @connections = Hash.new
12
+ end
13
+
14
+ def add_node(node)
15
+ nodes << node
16
+ node.graph = self
17
+ end
18
+
19
+ #adds an edge dest the graph
20
+ def add_edge(source, dest, weight)
21
+ edges << Edge.new(source, dest, weight)
22
+ end
23
+
24
+ #for BFS and DFS
25
+ def add_adjacency(n1, n2)
26
+ n1.adjacents << n2
27
+ n2.adjacents << n1
28
+ end
29
+
30
+ #connects two nodes in the graph
31
+ def connect node1, node2
32
+ if !Set.new([node1, node2]).subset? @nodes
33
+ raise BadNodeInput, 'The graph does not have either ' + node1 + ' or ' + node2
34
+ end
35
+ @connections[node1] ||= Array.new
36
+ @connections[node1].push node2
37
+ unless node1.eql? node2
38
+ @connections[node2] ||= Array.new
39
+ @connections[node2].push node1
40
+ end
41
+ end
42
+
43
+ #disconnects two nodes in the graph
44
+ def disconnect node1, node2
45
+ if !nodes.include?(node1) || !nodes.include?(node2)
46
+ raise NodeContainsException, 'The graph does not contain either ' + node1 + ' or ' + node2
47
+ end
48
+ @connections[node1].delete node2
49
+ @connections[node2].delete node1
50
+ end
51
+
52
+ #returns the degree of the specified node
53
+ def degree node
54
+ d = 0
55
+ if (@connections[node].is_a? (Array)) && nodes.include?(node)
56
+ d = @connections[node].count
57
+ if @connections[node].include?(node)
58
+ d += 1
59
+ end
60
+ end
61
+ return d
62
+ end
63
+
64
+ def adjacents node
65
+ @connections[node]
66
+ end
67
+ end
@@ -0,0 +1,18 @@
1
+ class Node
2
+ attr_accessor :name
3
+ attr_accessor :graph
4
+ attr_accessor :adjacents
5
+
6
+ def initialize(name)
7
+ @name = name
8
+ @adjacents = Set.new
9
+ end
10
+
11
+ def adjacent_edges
12
+ graph.edges.select{|e| e.source == self}
13
+ end
14
+
15
+ def to_str
16
+ @name
17
+ end
18
+ end
@@ -0,0 +1,22 @@
1
+ class PriorityQueue
2
+ def initialize
3
+ @queue = {}
4
+ end
5
+
6
+ def any?
7
+ return @queue.any?
8
+ end
9
+
10
+ def insert(key, value)
11
+ @queue[key] = value
12
+ @queue.sort_by {|_key, value| value }
13
+ end
14
+
15
+ def remove_max
16
+ @queue.shift.last
17
+ end
18
+
19
+ def remove_min
20
+ @queue.shift.first
21
+ end
22
+ end
@@ -1,4 +1,4 @@
1
- class neuralnetwork
1
+ class NeuralNetwork
2
2
 
3
3
  attr_accessor :inputs,
4
4
  :n_inputs,
@@ -0,0 +1,13 @@
1
+ #Bubble sort implementation in Ruby
2
+ public
3
+ def bubblesort(array)
4
+ x = array.clone
5
+ x.each_index do |i|
6
+ (x.length-i-1).times do |j|
7
+ if (x[j+1] < x[j])
8
+ x[j], x[j+1] = x[j+1], x[j]
9
+ end
10
+ end
11
+ end
12
+ return x
13
+ end
@@ -0,0 +1,29 @@
1
+ #Quick Sort implementation in Ruby
2
+ public
3
+ def quicksort(array, l, r)
4
+ if r == -1
5
+ r = array.length - 1
6
+ end
7
+
8
+ if l == -1
9
+ l = 0
10
+ end
11
+
12
+ if (r - l) < 1
13
+ return array
14
+ else
15
+ pivot = array[l]
16
+ i = l+1
17
+ for j in l+1..r
18
+ if array[j] < pivot
19
+ array[j], array[i] = array[i], array[j]
20
+ i += 1
21
+ end
22
+ end
23
+
24
+ array[l], array[i-1] = array[i-1], array[l]
25
+ array = quicksort(array, l, i-1)
26
+ array = quicksort(array, i, r)
27
+ return array
28
+ end
29
+ end
data/lib/rbutils.rb CHANGED
@@ -1,25 +1,11 @@
1
- #this needs to be changed to conform to gemspec standards
2
- # it needs to allow for user interface with the different classes provided.
3
- class RUtils
4
- def test_neuralnet()
5
- learning_rate = 10 #|rate|
6
- momentum_rate = 1 #|momentum|
1
+ require_relative 'rbutils/neuralnet/neuralnetwork'
2
+ require_relative 'rbutils/graph/bfs'
3
+ require_relative 'rbutils/graph/dfs'
4
+ require_relative 'rbutils/graph/dijkstra'
5
+ require_relative 'rbutils/graph/edge'
6
+ require_relative 'rbutils/graph/graph'
7
+ require_relative 'rbutils/graph/node'
8
+ require_relative 'rbutils/graph/priorityqueue'
9
+ require_relative 'rbutils/sort/bubblesort'
10
+ require_relative 'rbutils/sort/quicksort'
7
11
 
8
- neural_net = NeuralNetwork.new(:inputs => 2, :hidden_nodes => 10, :output_nodes => 1)
9
-
10
- 10000.times do |i|
11
- neural_net.train([0, 0], [0], learning_rate, momentum_rate)
12
- neural_net.train([0, 1], [0], learning_rate, momentum_rate)
13
- neural_net.train([1, 0], [0], learning_rate, momentum_rate)
14
- neural_net.train([1, 1], [1], learning_rate, momentum_rate)
15
- end
16
-
17
- puts "[0, 0] -> " + neural_net.predict([0, 0]).to_s
18
- puts "[1, 0] -> " + neural_net.predict([1, 0]).to_s
19
- puts "[0, 1] -> " + neural_net.predict([0, 1]).to_s
20
- puts "[1, 1] -> " + neural_net.predict([1, 1]).to_s
21
- puts "done."
22
- end
23
- end
24
-
25
- require 'rutils/neuralnet/neuralnetwork'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - manan
@@ -10,18 +10,26 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-06-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Utilities for enhanced functionality. THIS GEM IS CURRENTLY NOT COMPLETE-
14
- PLEASE DO NOT DOWNLOAD. Source is at https://github.com/mananshah99/rbutils if you
15
- would like to use some of the functionality pre-release.
13
+ description: ! 'Utilities for enhanced functionality. Source is at https://github.com/mananshah99/rbutils. '
16
14
  email: manan.shah.777@gmail.com
17
15
  executables: []
18
16
  extensions: []
19
17
  extra_rdoc_files: []
20
18
  files:
21
19
  - lib/rbutils.rb
20
+ - lib/rbutils/graph/bfs.rb
21
+ - lib/rbutils/graph/dfs.rb
22
+ - lib/rbutils/graph/dijkstra.rb
23
+ - lib/rbutils/graph/edge.rb
24
+ - lib/rbutils/graph/graph.rb
25
+ - lib/rbutils/graph/node.rb
26
+ - lib/rbutils/graph/priorityqueue.rb
22
27
  - lib/rbutils/neuralnet/neuralnetwork.rb
23
- homepage:
24
- licenses: []
28
+ - lib/rbutils/sort/bubblesort.rb
29
+ - lib/rbutils/sort/quicksort.rb
30
+ homepage: https://rubygems.org/gems/rbutils
31
+ licenses:
32
+ - Apache
25
33
  metadata: {}
26
34
  post_install_message:
27
35
  rdoc_options: []
@@ -42,5 +50,5 @@ rubyforge_project:
42
50
  rubygems_version: 2.3.0
43
51
  signing_key:
44
52
  specification_version: 4
45
- summary: Utilities for Ruby.
53
+ summary: Multifaceted utilities for Ruby.
46
54
  test_files: []