abuelo 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +2 -2
- data/Gemfile.lock +1 -1
- data/README.md +35 -9
- data/abuelo.gemspec +0 -1
- data/lib/abuelo.rb +1 -0
- data/lib/abuelo/algorithms/dijkstra.rb +96 -0
- data/lib/abuelo/exceptions/exceptions.rb +3 -0
- data/lib/abuelo/graph.rb +1 -1
- data/lib/abuelo/version.rb +1 -1
- data/spec/algorithms/dijkstra_spec.rb +252 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 353ed08a9040bffa3feb4b0849ac570528d34e14
|
4
|
+
data.tar.gz: 7a851f16545babb62022b33316b08cf2bd26fa08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cfa8bf9e255e57572166984c86fd06dddd04dd8e16ae0c4757cb2da285ba23a534e2499ffe282bd564d342c85c77c20d05b6f845fc259698a29317d7b706553
|
7
|
+
data.tar.gz: a31822a057e438e778c25b5fc305367bbb8f82793aa654bb78d61c1b088b11924552d3c25ce780e16df3ef41942ae326bea84a63b6a8cdf49d0b934972a7c92c
|
data/CHANGELOG.md
CHANGED
data/CODE_OF_CONDUCT.md
CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
|
|
55
55
|
## Enforcement
|
56
56
|
|
57
57
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at [
|
58
|
+
reported by contacting the project team at [cache.zero+abuelo@mailbox.org]. All
|
59
59
|
complaints will be reviewed and investigated and will result in a response that
|
60
60
|
is deemed necessary and appropriate to the circumstances. The project team is
|
61
61
|
obligated to maintain confidentiality with regard to the reporter of an incident.
|
@@ -71,4 +71,4 @@ This Code of Conduct is adapted from the [Contributor Covenant][homepage], versi
|
|
71
71
|
available at [http://contributor-covenant.org/version/1/4][version]
|
72
72
|
|
73
73
|
[homepage]: http://contributor-covenant.org
|
74
|
-
[version]: http://contributor-covenant.org/version/1/4/
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Abuelo
|
2
2
|
[![Build Status](https://travis-ci.org/dirkholzapfel/abuelo.svg?branch=master)](https://travis-ci.org/dirkholzapfel/abuelo)
|
3
|
+
[![ProjectTalk](http://www.projecttalk.io/images/gh_badge-3e578a9f437f841de7446bab9a49d103.svg?vsn=d)] (http://www.projecttalk.io/boards/dirkholzapfel%2Fabuelo?utm_campaign=gh-badge&utm_medium=badge&utm_source=github)
|
4
|
+
[![Join the chat at https://gitter.im/dirkholzapfel/abuelo](https://badges.gitter.im/dirkholzapfel/abuelo.svg)](https://gitter.im/dirkholzapfel/abuelo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
3
5
|
|
4
6
|
Abuelo is a graph theory library written in Ruby that allows you to build a representation of a graph.
|
5
7
|
|
@@ -87,9 +89,9 @@ Be aware that you have to provide all symmetric edges in an adjacency matrix for
|
|
87
89
|
|
88
90
|
```ruby
|
89
91
|
adjacency_matrix = <<-matrix
|
90
|
-
|
91
|
-
42
|
92
|
-
|
92
|
+
0 42 0
|
93
|
+
42 0 23
|
94
|
+
0 23 0
|
93
95
|
matrix
|
94
96
|
|
95
97
|
# The above matrix corresponds to this internal representation
|
@@ -108,9 +110,9 @@ graph.find_edge(node_1, node_2).weight # => 42
|
|
108
110
|
#### Directed graphs
|
109
111
|
```ruby
|
110
112
|
adjacency_matrix = <<-matrix
|
111
|
-
0 42
|
112
|
-
0
|
113
|
-
0
|
113
|
+
0 42 0
|
114
|
+
0 0 23
|
115
|
+
0 0 0
|
114
116
|
matrix
|
115
117
|
|
116
118
|
# The above matrix corresponds to this internal representation
|
@@ -123,16 +125,40 @@ adjacency_matrix = <<-matrix
|
|
123
125
|
|
124
126
|
graph = Abuelo::Graph.new(adjacency_matrix: adjacency_matrix, directed: true)
|
125
127
|
```
|
128
|
+
## Algorithms
|
129
|
+
### Dijkstra
|
130
|
+
This is the example from https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
131
|
+
|
132
|
+
<a title="By Ibmua (Work by uploader.) [Public domain], via Wikimedia Commons" href="https://commons.wikimedia.org/wiki/File%3ADijkstra_Animation.gif"><img width="256" alt="Dijkstra Animation" src="https://upload.wikimedia.org/wikipedia/commons/5/57/Dijkstra_Animation.gif"/></a>
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
adjacency_matrix = <<-matrix
|
136
|
+
0 7 9 0 0 14
|
137
|
+
7 0 10 15 0 0
|
138
|
+
9 10 0 11 0 2
|
139
|
+
0 15 11 0 6 0
|
140
|
+
0 0 0 6 0 9
|
141
|
+
14 0 2 0 9 0
|
142
|
+
matrix
|
143
|
+
|
144
|
+
graph = Abuelo::Graph.new(adjacency_matrix: adjacency_matrix)
|
145
|
+
start_node = graph.find_node_by_name('node 1')
|
146
|
+
node_5 = graph.find_node_by_name('node 5')
|
147
|
+
|
148
|
+
dijkstra = Abuelo::Algorithms::Dijkstra.new(graph, start_node)
|
149
|
+
dijkstra.shortest_distance_to(node_5) # => 20
|
150
|
+
dijkstra.shortest_path_to(node_5).map(&:to_s) # => ['node 1', 'node 3', 'node 6', 'node 5']
|
151
|
+
```
|
126
152
|
|
127
153
|
## Documentation
|
128
|
-
[YARD](http://yardoc.org) documentation is available at [rubydoc](http://www.rubydoc.info/
|
154
|
+
[YARD](http://yardoc.org) documentation is available at [rubydoc](http://www.rubydoc.info/github/dirkholzapfel/abuelo).
|
129
155
|
|
130
156
|
## Future
|
131
157
|
* Implement graph algorithms
|
132
158
|
* [Kruskal's algorithm](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm)
|
133
159
|
* [Prim's algorithm](https://en.wikipedia.org/wiki/Prim%27s_algorithm)
|
134
|
-
* [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
|
135
160
|
* Implement visualization of graph
|
161
|
+
* Add priority queue to Dijkstra's algorithm implementation
|
136
162
|
|
137
163
|
## Installation
|
138
164
|
Abuelo is a gem which you can install with:
|
@@ -146,7 +172,7 @@ gem 'abuelo'
|
|
146
172
|
```
|
147
173
|
|
148
174
|
## Credits
|
149
|
-
Dirk Holzapfel
|
175
|
+
Dirk Holzapfel ([@cachezero](https://twitter.com/cachezero))
|
150
176
|
|
151
177
|
[cachezero.net](http://cachezero.net)
|
152
178
|
|
data/abuelo.gemspec
CHANGED
data/lib/abuelo.rb
CHANGED
@@ -0,0 +1,96 @@
|
|
1
|
+
module Abuelo
|
2
|
+
module Algorithms
|
3
|
+
#
|
4
|
+
# Class Dijkstra implements Dijkstra's algorithm for finding shortest paths between
|
5
|
+
# nodes in a graph.
|
6
|
+
#
|
7
|
+
# See https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
8
|
+
#
|
9
|
+
# Possible improvement: use a priority queue to improve runtime.
|
10
|
+
#
|
11
|
+
# @author Dirk Holzapfel <cache.zero@mailbox.org>
|
12
|
+
#
|
13
|
+
class Dijkstra
|
14
|
+
#
|
15
|
+
# @param [Abuelo::Graph] graph
|
16
|
+
# @param [Abuelo::Node] start_node
|
17
|
+
#
|
18
|
+
# @raise [Abuelo::Exceptions::NoNodeError] if the start_node is
|
19
|
+
# not a Abuelo::Node
|
20
|
+
#
|
21
|
+
def initialize(graph, start_node)
|
22
|
+
if !start_node.is_a?(Abuelo::Node)
|
23
|
+
raise Abuelo::Exceptions::NoNodeError.new('start_node is not a Abuelo::Node')
|
24
|
+
end
|
25
|
+
|
26
|
+
@graph = graph
|
27
|
+
@start_node = start_node
|
28
|
+
@distances = {}
|
29
|
+
@previous_nodes = {}
|
30
|
+
init
|
31
|
+
process
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# Calculates the shortest distance from the start_node to the given node.
|
36
|
+
#
|
37
|
+
# @param [Abuelo::Node] node
|
38
|
+
#
|
39
|
+
# @return [Numeric, nil] the distance to the node or nil if node is unreachable
|
40
|
+
#
|
41
|
+
def shortest_distance_to(node)
|
42
|
+
@distances[node]
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Calculates the shortest path from the start_node to the given node.
|
47
|
+
#
|
48
|
+
# @param [Abuelo::Node] node
|
49
|
+
#
|
50
|
+
# @return [Array<Abuelo::Node>, nil] list of nodes from start_node to the given node
|
51
|
+
# that are a possible solution for the shortest path. Nil if node is unreachable.
|
52
|
+
#
|
53
|
+
def shortest_path_to(node)
|
54
|
+
return nil if @previous_nodes[node].nil?
|
55
|
+
|
56
|
+
nodes = [node]
|
57
|
+
while previous_node = @previous_nodes[nodes[0]] do
|
58
|
+
nodes.unshift(previous_node)
|
59
|
+
end
|
60
|
+
|
61
|
+
nodes
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def init
|
68
|
+
@graph.nodes.each do |node|
|
69
|
+
@distances[node] = Float::INFINITY
|
70
|
+
@previous_nodes[node] = nil
|
71
|
+
end
|
72
|
+
|
73
|
+
@distances[@start_node] = 0
|
74
|
+
end
|
75
|
+
|
76
|
+
def process
|
77
|
+
unvisited_nodes = @graph.nodes
|
78
|
+
|
79
|
+
until unvisited_nodes.empty? do
|
80
|
+
node = unvisited_nodes.min_by { |node| @distances[node] }
|
81
|
+
unvisited_nodes.delete(node)
|
82
|
+
|
83
|
+
unvisited_neighbours = unvisited_nodes & node.neighbours
|
84
|
+
unvisited_neighbours.each do |neighbour|
|
85
|
+
new_edge_weight = @graph.find_edge(node, neighbour).weight
|
86
|
+
alternative_distance = @distances[node] + new_edge_weight
|
87
|
+
if alternative_distance < @distances[neighbour]
|
88
|
+
@distances[neighbour] = alternative_distance
|
89
|
+
@previous_nodes[neighbour] = node
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/abuelo/graph.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Abuelo
|
2
2
|
#
|
3
|
-
# Class Graph provides a representation of a
|
3
|
+
# Class Graph provides a representation of a graph.
|
4
4
|
#
|
5
5
|
# A graph consists of nodes (= vertices, points) and edges (= lines, arcs).
|
6
6
|
# The graph may be undirected or directed. For the sake of simplicity Abuelo sticks
|
data/lib/abuelo/version.rb
CHANGED
@@ -0,0 +1,252 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Abuelo::Algorithms::Dijkstra do
|
4
|
+
describe '#initialize(graph, start_node)' do
|
5
|
+
it 'raises an error if the start_node is not a Abuelo::Node' do
|
6
|
+
expect do
|
7
|
+
described_class.new(Abuelo::Graph.new, 'foo')
|
8
|
+
end.to raise_error(Abuelo::Exceptions::NoNodeError)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'undirected graph' do
|
13
|
+
# This is the example from https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
|
14
|
+
adjacency_matrix = <<-matrix
|
15
|
+
0 7 9 0 0 14
|
16
|
+
7 0 10 15 0 0
|
17
|
+
9 10 0 11 0 2
|
18
|
+
0 15 11 0 6 0
|
19
|
+
0 0 0 6 0 9
|
20
|
+
14 0 2 0 9 0
|
21
|
+
matrix
|
22
|
+
|
23
|
+
let(:undirected_graph) do
|
24
|
+
Abuelo::Graph.new(adjacency_matrix: adjacency_matrix)
|
25
|
+
end
|
26
|
+
let(:start_node) { undirected_graph.find_node_by_name('node 1') }
|
27
|
+
let(:dijkstra) { described_class.new(undirected_graph, start_node) }
|
28
|
+
|
29
|
+
describe '#shortest_distance_to(node)' do
|
30
|
+
it 'is 0 for the start node' do
|
31
|
+
expect(dijkstra.shortest_distance_to(start_node)).to eq 0
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'is Float::INFINITY for an unreachable node' do
|
35
|
+
unreachable_node = Abuelo::Node.new('unreachable node')
|
36
|
+
undirected_graph.add_node(unreachable_node)
|
37
|
+
expect(dijkstra.shortest_distance_to(unreachable_node)).to eq Float::INFINITY
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'is nil when the node is not part of the graph' do
|
41
|
+
unknown_node = Abuelo::Node.new('foo')
|
42
|
+
expect(dijkstra.shortest_distance_to(unknown_node)).to be nil
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'wikipedia example' do
|
46
|
+
it 'is 0 for node 1' do
|
47
|
+
node = undirected_graph.find_node_by_name('node 1')
|
48
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 0
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'is 7 for node 2' do
|
52
|
+
node = undirected_graph.find_node_by_name('node 2')
|
53
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 7
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'is 9 for node 3' do
|
57
|
+
node = undirected_graph.find_node_by_name('node 3')
|
58
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 9
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'is 20 for node 4' do
|
62
|
+
node = undirected_graph.find_node_by_name('node 4')
|
63
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 20
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'is 20 for node 5' do
|
67
|
+
node = undirected_graph.find_node_by_name('node 5')
|
68
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 20
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'is 11 for node 6' do
|
72
|
+
node = undirected_graph.find_node_by_name('node 6')
|
73
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 11
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#shortest_path_to(node)' do
|
79
|
+
it 'is nil for the start node' do
|
80
|
+
expect(dijkstra.shortest_path_to(start_node)).to be nil
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'is nil for an unreachable node' do
|
84
|
+
unreachable_node = Abuelo::Node.new('unreachable node')
|
85
|
+
undirected_graph.add_node(unreachable_node)
|
86
|
+
expect(dijkstra.shortest_path_to(unreachable_node)).to be nil
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'is nil when the node is not part of the graph' do
|
90
|
+
unknown_node = Abuelo::Node.new('foo')
|
91
|
+
expect(dijkstra.shortest_path_to(unknown_node)).to be nil
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'wikipedia example' do
|
95
|
+
it 'solves the problem for node 1' do
|
96
|
+
node = undirected_graph.find_node_by_name('node 1')
|
97
|
+
expect(dijkstra.shortest_path_to(node)).to be nil
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'solves the problem for node 2' do
|
101
|
+
node = undirected_graph.find_node_by_name('node 2')
|
102
|
+
shortest_path = ['node 1', 'node 2']
|
103
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'solves the problem for node 3' do
|
107
|
+
node = undirected_graph.find_node_by_name('node 3')
|
108
|
+
shortest_path = ['node 1', 'node 3']
|
109
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'solves the problem for node 4' do
|
113
|
+
node = undirected_graph.find_node_by_name('node 4')
|
114
|
+
shortest_path = ['node 1', 'node 3', 'node 4']
|
115
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'solves the problem for node 5' do
|
119
|
+
node = undirected_graph.find_node_by_name('node 5')
|
120
|
+
shortest_path = ['node 1', 'node 3', 'node 6', 'node 5']
|
121
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'solves the problem for node 6' do
|
125
|
+
node = undirected_graph.find_node_by_name('node 6')
|
126
|
+
shortest_path = ['node 1', 'node 3', 'node 6']
|
127
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'directed graph' do
|
134
|
+
adjacency_matrix = <<-matrix
|
135
|
+
0 7 0 0 0 0
|
136
|
+
0 0 10 15 0 0
|
137
|
+
9 0 0 11 0 2
|
138
|
+
0 0 11 0 6 0
|
139
|
+
0 0 0 0 0 0
|
140
|
+
14 0 0 0 9 0
|
141
|
+
matrix
|
142
|
+
|
143
|
+
let(:directed_graph) do
|
144
|
+
Abuelo::Graph.new(adjacency_matrix: adjacency_matrix, directed: true)
|
145
|
+
end
|
146
|
+
let(:start_node) { directed_graph.find_node_by_name('node 1') }
|
147
|
+
let(:dijkstra) { described_class.new(directed_graph, start_node) }
|
148
|
+
|
149
|
+
describe '#shortest_distance_to(node)' do
|
150
|
+
it 'is 0 for the start node' do
|
151
|
+
expect(dijkstra.shortest_distance_to(start_node)).to eq 0
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'is Float::INFINITY for an unreachable node' do
|
155
|
+
unreachable_node = Abuelo::Node.new('unreachable node')
|
156
|
+
directed_graph.add_node(unreachable_node)
|
157
|
+
expect(dijkstra.shortest_distance_to(unreachable_node)).to eq Float::INFINITY
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'is nil when the node is not part of the graph' do
|
161
|
+
unknown_node = Abuelo::Node.new('foo')
|
162
|
+
expect(dijkstra.shortest_distance_to(unknown_node)).to be nil
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'example' do
|
166
|
+
it 'is 0 for node 1' do
|
167
|
+
node = directed_graph.find_node_by_name('node 1')
|
168
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 0
|
169
|
+
end
|
170
|
+
|
171
|
+
it 'is 7 for node 2' do
|
172
|
+
node = directed_graph.find_node_by_name('node 2')
|
173
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 7
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'is 9 for node 3' do
|
177
|
+
node = directed_graph.find_node_by_name('node 3')
|
178
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 17
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'is 20 for node 4' do
|
182
|
+
node = directed_graph.find_node_by_name('node 4')
|
183
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 22
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'is 20 for node 5' do
|
187
|
+
node = directed_graph.find_node_by_name('node 5')
|
188
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 28
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'is 11 for node 6' do
|
192
|
+
node = directed_graph.find_node_by_name('node 6')
|
193
|
+
expect(dijkstra.shortest_distance_to(node)).to eq 19
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe '#shortest_path_to(node)' do
|
199
|
+
it 'is nil for the start node' do
|
200
|
+
expect(dijkstra.shortest_path_to(start_node)).to be nil
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'is nil for an unreachable node' do
|
204
|
+
unreachable_node = Abuelo::Node.new('unreachable node')
|
205
|
+
directed_graph.add_node(unreachable_node)
|
206
|
+
expect(dijkstra.shortest_path_to(unreachable_node)).to be nil
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'is nil when the node is not part of the graph' do
|
210
|
+
unknown_node = Abuelo::Node.new('foo')
|
211
|
+
expect(dijkstra.shortest_path_to(unknown_node)).to be nil
|
212
|
+
end
|
213
|
+
|
214
|
+
context 'example' do
|
215
|
+
it 'solves the problem for node 1' do
|
216
|
+
node = directed_graph.find_node_by_name('node 1')
|
217
|
+
expect(dijkstra.shortest_path_to(node)).to be nil
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'solves the problem for node 2' do
|
221
|
+
node = directed_graph.find_node_by_name('node 2')
|
222
|
+
shortest_path = ['node 1', 'node 2']
|
223
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'solves the problem for node 3' do
|
227
|
+
node = directed_graph.find_node_by_name('node 3')
|
228
|
+
shortest_path = ['node 1', 'node 2', 'node 3']
|
229
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'solves the problem for node 4' do
|
233
|
+
node = directed_graph.find_node_by_name('node 4')
|
234
|
+
shortest_path = ['node 1', 'node 2', 'node 4']
|
235
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'solves the problem for node 5' do
|
239
|
+
node = directed_graph.find_node_by_name('node 5')
|
240
|
+
shortest_path = ['node 1', 'node 2', 'node 3', 'node 6', 'node 5']
|
241
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'solves the problem for node 6' do
|
245
|
+
node = directed_graph.find_node_by_name('node 6')
|
246
|
+
shortest_path = ['node 1', 'node 2', 'node 3', 'node 6']
|
247
|
+
expect(dijkstra.shortest_path_to(node).map(&:to_s)).to eq shortest_path
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abuelo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dirk Holzapfel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,11 +83,13 @@ files:
|
|
83
83
|
- Rakefile
|
84
84
|
- abuelo.gemspec
|
85
85
|
- lib/abuelo.rb
|
86
|
+
- lib/abuelo/algorithms/dijkstra.rb
|
86
87
|
- lib/abuelo/edge.rb
|
87
88
|
- lib/abuelo/exceptions/exceptions.rb
|
88
89
|
- lib/abuelo/graph.rb
|
89
90
|
- lib/abuelo/node.rb
|
90
91
|
- lib/abuelo/version.rb
|
92
|
+
- spec/algorithms/dijkstra_spec.rb
|
91
93
|
- spec/edge_spec.rb
|
92
94
|
- spec/graph_spec.rb
|
93
95
|
- spec/node_spec.rb
|
@@ -117,6 +119,7 @@ signing_key:
|
|
117
119
|
specification_version: 4
|
118
120
|
summary: Abuelo
|
119
121
|
test_files:
|
122
|
+
- spec/algorithms/dijkstra_spec.rb
|
120
123
|
- spec/edge_spec.rb
|
121
124
|
- spec/graph_spec.rb
|
122
125
|
- spec/node_spec.rb
|