algorithmable 0.3.0 → 0.4.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: faa2cfc1ad0200b15adfbfa778a72b89d605e15f
4
- data.tar.gz: 4b4a5326da1e136fb0513e35a29bae2b1171dfe0
3
+ metadata.gz: b0dedcc11bcfa5814e48d25ba1090a2495e187b1
4
+ data.tar.gz: 79d772b02883f4550b8abdc3b48afe7c60e96d89
5
5
  SHA512:
6
- metadata.gz: 2a79e522c0901adb9fbbc434eb0dc760556ba27d932f8036d124ad6bce9e4145f77c4a1cae96c2dd5f806add3b5f5a94f7580d8597a79f96c65c05a7b8f37d7c
7
- data.tar.gz: c22a35a53cf69acd26b10f5c127da3f6bc5e92ab053c795fa74877410c91b2bbcafc3b484fe9fa226ce10bf61093fab3851389ecb92e757259170bea0bd64603
6
+ metadata.gz: 5e8f1d7d60d1bcba3a293c9860516a9bf42d1ac847e2bd7f532f9b33a649b45e89125d1e645c8735d16b6b77d6a0f659da36adbd8d133a22b64b2c9b1529deb9
7
+ data.tar.gz: b47c85b9d228aef4ebea3d03fe5eefa4a2c41951eb94438b902835dadf1c437270a58dd125c0b1c5ed6e1d78e4e42098ea47dc8fe1c27844b6bd6c9ae7571f7a
@@ -0,0 +1,52 @@
1
+ require 'thread'
2
+ module Algorithmable
3
+ module Graphs
4
+ module Traversals
5
+ class BreadthFirst
6
+ include Algorithmable::Graphs::Traversals::Errors
7
+
8
+ def initialize(graph, source)
9
+ vertices_size = graph.vertices.size
10
+ @visited = Array.new(vertices_size - 1)
11
+ @edge_to = Array.new(vertices_size - 1)
12
+ @source = source
13
+ traverse graph, source
14
+ end
15
+
16
+ def visited?(vertex)
17
+ @visited[vertex]
18
+ end
19
+
20
+ def path_to(vertex)
21
+ fail UnvisitedVertexError, vertex unless visited?(vertex)
22
+ path = []
23
+ finish = vertex
24
+ while finish != @source
25
+ path.push finish
26
+ finish = @edge_to[finish]
27
+ end
28
+ path.push @source
29
+ path
30
+ end
31
+
32
+ private
33
+
34
+ def traverse(graph, vertex)
35
+ queue = ::Queue.new
36
+ @visited[vertex] = true
37
+ queue.enq vertex
38
+ until queue.empty?
39
+ next_vertex = queue.deq
40
+ graph.adjacency(next_vertex).each do |neighbour_vertex|
41
+ unless visited? neighbour_vertex
42
+ @edge_to[neighbour_vertex] = next_vertex
43
+ @visited[neighbour_vertex] = true
44
+ queue.enq neighbour_vertex
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -3,11 +3,15 @@ module Algorithmable
3
3
  module Traversals
4
4
  autoload :Errors, 'algorithmable/graphs/traversals/errors'
5
5
  autoload :DepthFirst, 'algorithmable/graphs/traversals/depth_first'
6
- autoload :BreathFirst, 'algorithmable/graphs/traversals/breath_first'
6
+ autoload :BreadthFirst, 'algorithmable/graphs/traversals/breadth_first'
7
7
 
8
8
  def traverse_with_depth_first(graph, source)
9
9
  DepthFirst.new(graph, source)
10
10
  end
11
+
12
+ def traverse_with_breadth_first(graph, source)
13
+ BreadthFirst.new(graph, source)
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -21,6 +21,27 @@ module Algorithmable
21
21
  def adjacency(vertex)
22
22
  @adj[vertex]
23
23
  end
24
+
25
+ def valid_vertex?(vertex)
26
+ !(0 > vertex || vertex >= @vertices)
27
+ end
28
+
29
+ def degree(vertex)
30
+ raise "Vertex #{vertex} is not valid." unless valid_vertex?(vertex)
31
+ adjacency(vertex).size
32
+ end
33
+
34
+ def to_s
35
+ data = ''
36
+ @vertices.times do |vertex|
37
+ data += "( #{vertex} => "
38
+ @adj[vertex].each do |neighbor|
39
+ data += "#{neighbor} "
40
+ end
41
+ data += ') '
42
+ end
43
+ "#<#{self.class} @vertices=#{@vertices} @edges=#{@edges} @data=#{data}>"
44
+ end
24
45
  end
25
46
  end
26
47
  end
@@ -1,3 +1,3 @@
1
1
  module Algorithmable
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.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.3.0
4
+ version: 0.4.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-29 00:00:00.000000000 Z
11
+ date: 2015-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,6 +142,7 @@ files:
142
142
  - lib/algorithmable.rb
143
143
  - lib/algorithmable/graphs.rb
144
144
  - lib/algorithmable/graphs/traversals.rb
145
+ - lib/algorithmable/graphs/traversals/breadth_first.rb
145
146
  - lib/algorithmable/graphs/traversals/depth_first.rb
146
147
  - lib/algorithmable/graphs/traversals/errors.rb
147
148
  - lib/algorithmable/graphs/undirected.rb