graph-rb 0.1.0 → 0.1.1
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 +4 -4
- data/CHANGELOG.md +32 -0
- data/Gemfile.lock +1 -1
- data/README.md +11 -4
- data/lib/graph.rb +33 -16
- data/lib/graph/edge.rb +9 -3
- data/lib/graph/version.rb +1 -1
- data/lib/graph/vertex.rb +8 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 211e4392a9c71a1a017a86abb884ad207dc3ae87fdfe7a280c47a809c70ea28e
|
4
|
+
data.tar.gz: cc14689cb908518d23f66c7743542246f24b5336417b89cb8e963e1c485f73b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afe5678f0f1ad8539d155705a6136ce138dc2ce8c2c12c523bddf500ed9f7b759d8e014ad895635b04fee4e52d7b674aba70163dd1cbe3d7316395760ee646ab
|
7
|
+
data.tar.gz: fc3db12719a15d5714c834e60970b1a643663ebc13db5bddf360f0a987e128a63c5e8bf520c6cac6f3a60dce1fc78be563d4c7033c28e398c546102fdc2765d4
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [Unreleased]
|
8
|
+
|
9
|
+
## [0.1.1] - 2019-04-26
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
- `Graph::Edge` have a new attribue named `weight`
|
14
|
+
- `Graph::Vertex` and `Graph::Edge` have a new method named `to_h`
|
15
|
+
|
16
|
+
### Changed
|
17
|
+
|
18
|
+
- `Graph::Edge` attribute `ukey` rename to `key`
|
19
|
+
- `Graph::Vertex` attribute `uid` rename to `key`
|
20
|
+
- `Graph#add`, `Graph#delete` and `Graph#index_of` now identify vertex or edge by attribute `key`
|
21
|
+
|
22
|
+
## [0.1.0] - 2019-04-26
|
23
|
+
|
24
|
+
### Added
|
25
|
+
|
26
|
+
- class `Graph`
|
27
|
+
- class `Graph::Vertex`
|
28
|
+
- class `Graph::Edge`
|
29
|
+
|
30
|
+
[Unreleased]: https://github.com/jk195417/graph-rb/compare/v0.1.1...HEAD
|
31
|
+
[0.1.1]: https://github.com/jk195417/graph-rb/compare/v0.1.0...v0.1.1
|
32
|
+
[0.1.0]: https://github.com/jk195417/graph-rb/releases/tag/v0.1.0
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Graph
|
2
2
|
|
3
|
-
A ruby gem for graph, data structure.
|
3
|
+
A ruby gem for graph, data structure. https://rubygems.org/gems/graph-rb
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -29,9 +29,16 @@ edge1 = Graph::Edge.new(vertex1, vertex2)
|
|
29
29
|
edge2 = Graph::Edge.new(vertex1, vertex3)
|
30
30
|
edge3 = Graph::Edge.new(vertex2, vertex3)
|
31
31
|
|
32
|
-
|
33
|
-
graph
|
34
|
-
graph
|
32
|
+
# add vertex or edge to graph
|
33
|
+
graph << vertex1
|
34
|
+
graph << edge3
|
35
|
+
|
36
|
+
# delete vertex or edge to graph
|
37
|
+
graph.delete edge3
|
38
|
+
graph.delete vertex1
|
39
|
+
|
40
|
+
# show graph in a hash
|
41
|
+
graph.to_h
|
35
42
|
```
|
36
43
|
|
37
44
|
## Development
|
data/lib/graph.rb
CHANGED
@@ -15,6 +15,11 @@ class Graph
|
|
15
15
|
return include_edge?(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Edge)
|
16
16
|
end
|
17
17
|
|
18
|
+
def index_of(vertex_or_edge)
|
19
|
+
return index_of_vertex(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Vertex)
|
20
|
+
return index_of_edge(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Edge)
|
21
|
+
end
|
22
|
+
|
18
23
|
def add(vertex_or_edge)
|
19
24
|
return add_vertex(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Vertex)
|
20
25
|
return add_edge(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Edge)
|
@@ -26,15 +31,14 @@ class Graph
|
|
26
31
|
end
|
27
32
|
|
28
33
|
def to_h
|
29
|
-
|
30
|
-
|
31
|
-
edges: []
|
32
|
-
}
|
34
|
+
# O(vertices.size) + O(edges.size)
|
35
|
+
result = {}
|
33
36
|
@vertices.each do |vertex|
|
34
|
-
result[
|
37
|
+
result[vertex.key] = vertex.to_h
|
35
38
|
end
|
36
39
|
@edges.each do |edge|
|
37
|
-
result[:edges] <<
|
40
|
+
result[edge.from.key][:edges] << edge.to_h
|
41
|
+
result[edge.to.key][:edges] << edge.to_h
|
38
42
|
end
|
39
43
|
result
|
40
44
|
end
|
@@ -49,8 +53,16 @@ class Graph
|
|
49
53
|
@edges.include? edge
|
50
54
|
end
|
51
55
|
|
56
|
+
def index_of_vertex(target)
|
57
|
+
@vertices.index { |vertex| vertex.key == target.key }
|
58
|
+
end
|
59
|
+
|
60
|
+
def index_of_edge(target)
|
61
|
+
@edges.index { |edge| edge.key == target.key }
|
62
|
+
end
|
63
|
+
|
52
64
|
def add_vertex(vertex)
|
53
|
-
return false if
|
65
|
+
return false if index_of_vertex(vertex)
|
54
66
|
|
55
67
|
vertex.send(:add_to, self)
|
56
68
|
@vertices << vertex
|
@@ -58,30 +70,35 @@ class Graph
|
|
58
70
|
end
|
59
71
|
|
60
72
|
def add_edge(edge)
|
61
|
-
return false if
|
73
|
+
return false if index_of_edge(edge)
|
62
74
|
|
63
|
-
edge.send(:add_to, self)
|
64
75
|
add_vertex(edge.from)
|
65
76
|
add_vertex(edge.to)
|
77
|
+
edge.send(:add_to, self)
|
66
78
|
@edges << edge
|
67
79
|
self
|
68
80
|
end
|
69
81
|
|
70
82
|
def delete_vertex(vertex)
|
71
|
-
|
83
|
+
idx = index_of_vertex vertex
|
84
|
+
return false unless idx
|
85
|
+
|
72
86
|
edges.each do |edge|
|
73
|
-
delete_edge(edge) if (edge.from == vertex) || (edge.to == vertex)
|
87
|
+
delete_edge(edge) if (edge.from.key == vertex.key) || (edge.to.key == vertex.key)
|
74
88
|
end
|
75
89
|
vertex.send(:add_to, nil)
|
76
|
-
@vertices.
|
90
|
+
@vertices.delete_at idx
|
77
91
|
self
|
78
92
|
end
|
79
93
|
|
80
94
|
def delete_edge(edge)
|
81
|
-
|
82
|
-
|
95
|
+
idx = index_of_edge edge
|
96
|
+
return false unless idx
|
97
|
+
|
83
98
|
edge.send(:add_to, nil)
|
84
|
-
@edges.
|
99
|
+
@edges.delete_at idx
|
85
100
|
self
|
86
101
|
end
|
87
|
-
|
102
|
+
|
103
|
+
alias << add
|
104
|
+
end
|
data/lib/graph/edge.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
class Graph::Edge
|
2
|
-
attr_reader :
|
2
|
+
attr_reader :key, :graph, :from, :to
|
3
|
+
attr_accessor :weight
|
3
4
|
|
4
|
-
def initialize(from, to)
|
5
|
+
def initialize(from, to, weight: 1)
|
5
6
|
@from = from
|
6
7
|
@to = to
|
7
|
-
@
|
8
|
+
@key = "from_#{from.key}_to_#{to.key}"
|
9
|
+
@weight = weight
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_h
|
13
|
+
{ from: from.key, to: to.key, weight: weight }
|
8
14
|
end
|
9
15
|
|
10
16
|
private
|
data/lib/graph/version.rb
CHANGED
data/lib/graph/vertex.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
class Graph::Vertex
|
2
|
+
attr_reader :key, :graph
|
2
3
|
attr_accessor :attachment
|
3
|
-
|
4
|
+
|
4
5
|
@@autoincrement_counter = 0
|
5
6
|
|
6
|
-
def initialize(attachment
|
7
|
-
|
8
|
-
@uid = uid
|
7
|
+
def initialize(key: nil, attachment: nil)
|
8
|
+
@key = key || (@@autoincrement_counter += 1)
|
9
9
|
@attachment = attachment
|
10
10
|
end
|
11
11
|
|
12
|
+
def to_h
|
13
|
+
{ edges: [], attachment: attachment.to_h }
|
14
|
+
end
|
15
|
+
|
12
16
|
private
|
13
17
|
|
14
18
|
def add_to(graph)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graph-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 楊竑昕
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- CODE_OF_CONDUCT.md
|
66
67
|
- Gemfile
|
67
68
|
- Gemfile.lock
|