graphsrb 0.1.0

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.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +11 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +7 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.lock +37 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +338 -0
  11. data/Rakefile +6 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/examples/ApproxAlgs/TSP/tsp.rb +45 -0
  15. data/examples/ApproxAlgs/VertexCover/main.rb +34 -0
  16. data/examples/ApproxAlgs/VertexCover/vertex_cover.rb +31 -0
  17. data/examples/Apts/artc_points.rb +59 -0
  18. data/examples/Apts/main.rb +51 -0
  19. data/examples/BackEdges/backedges.rb +37 -0
  20. data/examples/BackEdges/main.rb +41 -0
  21. data/examples/BipartMatching/bipart_matching.rb +34 -0
  22. data/examples/BipartMatching/main.rb +58 -0
  23. data/examples/EdgeConnectivity/econ.rb +31 -0
  24. data/examples/EdgeConnectivity/main.rb +34 -0
  25. data/examples/Euler/euler_tour.rb +36 -0
  26. data/examples/Euler/main.rb +19 -0
  27. data/examples/FundCircuits/fund_circuits.rb +49 -0
  28. data/examples/FundCircuits/main.rb +34 -0
  29. data/examples/FundCutsets/fund_cutsets.rb +50 -0
  30. data/examples/FundCutsets/main.rb +26 -0
  31. data/examples/MaxFlow/edmonds_karp.rb +91 -0
  32. data/examples/MaxFlow/main.rb +79 -0
  33. data/examples/PrimMST/main.rb +46 -0
  34. data/examples/PrimMST/prim.rb +82 -0
  35. data/examples/ProbAlgs/LargeCustset/large_cutset.rb +59 -0
  36. data/examples/ProbAlgs/LargeCustset/main.rb +45 -0
  37. data/graphsrb.gemspec +39 -0
  38. data/lib/graphsrb.rb +11 -0
  39. data/lib/graphsrb/adjacency_list.rb +78 -0
  40. data/lib/graphsrb/base_graph.rb +167 -0
  41. data/lib/graphsrb/diedge.rb +6 -0
  42. data/lib/graphsrb/digraph.rb +93 -0
  43. data/lib/graphsrb/edge.rb +34 -0
  44. data/lib/graphsrb/exceptions.rb +5 -0
  45. data/lib/graphsrb/graph.rb +46 -0
  46. data/lib/graphsrb/node.rb +21 -0
  47. data/lib/graphsrb/version.rb +3 -0
  48. data/lib/graphsrb/vertex.rb +32 -0
  49. metadata +149 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 715975b29b455568139a81746acda9989fbf5739d7321419749337f6b55f1dc9
4
+ data.tar.gz: fe3af651185b5409c570413d30388a776997ad67486759000b4ea8be55ce932e
5
+ SHA512:
6
+ metadata.gz: f5e54f587c0dc3447d2fe4d5de32dd91b142ad4b08b3a01ec74dc514902fd35b0e0823945c166af98ec1a1aa7d745d99cdd511ed8fa8e34e1b4aa327cf879c3b
7
+ data.tar.gz: a072ae99482311dd0282cc36937826188f233d160f93466e2ff22b4fba86082ca586734b744e73ce054e6bce1f188868731a18946e9dd6fc4c469999d138f6e0
Binary file
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.5
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in graphsrb.gemspec
6
+ gemspec
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ graphsrb (0.1.0)
5
+ json (~> 1.8)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ json (1.8.6)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ graphsrb!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,338 @@
1
+ # Graphsrb
2
+
3
+ Graphsrb allows to create simple directed and undirected graphs. By default weight of an edge is equal to one. Basic operations allows easily implement graph algorithms.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'graphsrb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install graphsrb
20
+
21
+ ## Usage
22
+ ```
23
+ require 'graphsrb'
24
+ include Graphsrb
25
+ ```
26
+ ### Creating graphs
27
+ Each vertex of a graph must have a unique id (a positive integer). Each edge is characterized by a pair of vertices `initial_vertex` and `terminal_vertex`.
28
+ ```
29
+ # (undirected) complete graph on 3 vertices
30
+ graph = Graph.new(edges:[[1,2],[2,3],[3,1]])
31
+ graph.vertex_count # => 3
32
+ graph.edge_count # => 3
33
+ graph.edge?(1,3) # => true
34
+ graph.edge?(3,1) # => true
35
+
36
+ ```
37
+ ```
38
+ # directed graph on 3 vertices
39
+ graph = Digraph.new(edges:[[1,2],[2,3],[3,1]])
40
+ graph.vertex_count # => 3
41
+ graph.edge_count # => 3
42
+ graph.edge?(3,1) # => false
43
+ graph.edge?(1,3) # => false
44
+ ```
45
+ ### Basic operations
46
+ #### Undirected graphs
47
+ ```
48
+ graph = Graph.new
49
+ graph.add_edge(1,2)
50
+ v1 = Vertex.new(1)
51
+ v2 = Vertex.new(2)
52
+ edge1 = graph.edge(v1, v2)
53
+ edge1.initial_vertex.id # => 1
54
+ edge1.terminal_vertex.id # => 2
55
+ edge2 = graph.edge(v2, v1)
56
+ edge1 == edge2 # => true
57
+ ```
58
+
59
+ ```
60
+ # create a graph by specifying weight of each edge
61
+ graph = Graph.new(edges:[[1,2,1],[2,3,2],[3,1,3],[1,4,-1]])
62
+ v1 = Vertex.new(1)
63
+ v2 = Vertex.new(2)
64
+ graph.edge(v1, v2).weight # => 1
65
+
66
+ graph.edges.each do |edge|
67
+ puts edge
68
+ end
69
+ # output
70
+ # (1, 2, weight:1)
71
+ # (1, 4, weight:-1)
72
+ # (2, 3, weight:2)
73
+ # (3, 1, weight:3)
74
+
75
+ graph.vertices.each do |v|
76
+ puts "deg(#{v}) = #{graph.degree(v)}"
77
+ end
78
+ # output
79
+ # deg(1) = 3
80
+ # deg(2) = 2
81
+ # deg(3) = 2
82
+ # deg(4) = 1
83
+
84
+ graph.adjacent_vertices(v).each do |v|
85
+ puts v
86
+ end
87
+ # output
88
+ # 2
89
+ # 4
90
+ # 3
91
+ ```
92
+
93
+ ```
94
+ graph = Graph.new(edges:[[1,2,1],[2,3,2],[3,1,3],[1,4,-1]])
95
+ v1 = Vertex.new(1)
96
+ v3 = Vertex.new(3)
97
+ graph.remove_edge(v1, v3)
98
+ graph.edges.each do |edge|
99
+ puts edge
100
+ end
101
+ # output
102
+ # (1, 2, weight:1)
103
+ # (1, 4, weight:-1)
104
+ # (2, 3, weight:2)
105
+
106
+ graph.add_edge(1,3, weight:3)
107
+ graph.edges.each do |edge|
108
+ puts edge
109
+ end
110
+ # output
111
+ # (1, 2, weight:1)
112
+ # (1, 4, weight:-1)
113
+ # (1, 3, weight:3)
114
+ # (2, 3, weight:2)
115
+
116
+ graph.remove_vertex(v1)
117
+ graph.vertex_count # => 3
118
+ graph.edge_count # => 1
119
+ graph.edge?(2,3) # => true
120
+ graph.edge?(1,3) # => false
121
+ ```
122
+
123
+ ```
124
+ graph = Graph.new(edges:[[1,2,1],[2,3,2],[3,1,3],[1,4,-1]])
125
+ v1 = Vertex.new(1)
126
+ v3 = Vertex.new(3)
127
+ graph.edge(v1, v3).weight # => 3
128
+ graph.update_weight(v1, v3, 5)
129
+ graph.edge(v1, v3).weight # => 5
130
+ # increase the weight of the edge (v1,v3) by 2 units
131
+ graph.increase_weight(v1, v3, 2)
132
+ graph.edge(v1, v3).weight # => 7
133
+ ```
134
+
135
+ #### Directed graphs
136
+ ```
137
+ # unlike undirected graphs digraph have directed edges
138
+ e1 = DiEdge.new(1,2)
139
+ e2 = DiEdge.new(2,1)
140
+ e1 == e2 # => false
141
+ ```
142
+ ```
143
+ graph = Digraph.new
144
+ graph.add_edge(1,2)
145
+ v1 = Vertex.new(1)
146
+ v2 = Vertex.new(2)
147
+ edge1 = graph.edge(v1, v2)
148
+ edge1.initial_vertex.id # => 1
149
+ edge1.terminal_vertex.id # => 2
150
+ edge2 = graph.edge(v2, v1) #= nil
151
+ ```
152
+ ```
153
+ graph = Digraph.new(edges:[[1,2,1],[2,3,2],[3,1,3],[1,4,-1]])
154
+ v1 = Vertex.new(1)
155
+
156
+ graph.indegree(v1) # => 1
157
+ graph.outdegree(v1) # => 2
158
+
159
+ graph.outgoing_edges(v1).each do |edge|
160
+ puts edge
161
+ end
162
+ # output
163
+ # (1, 2, weight:1)
164
+ # (1, 4, weight:-1)
165
+
166
+ graph.incoming_edges(v1).each do |edge|
167
+ puts edge
168
+ end
169
+ # output
170
+ # (3, 1, weight:3)
171
+ ```
172
+ ### Implementation of Algorithms
173
+ #### Backedges
174
+ ```
175
+ require 'graphsrb'
176
+ include Graphsrb
177
+
178
+ module BackEdges
179
+ def self.run(graph)
180
+ init(graph)
181
+ @graph.vertices.each do |v|
182
+ dfs(v) if @dfi[v.id] == 0
183
+ end
184
+ @forest
185
+ end
186
+
187
+
188
+ def self.init(graph)
189
+ @graph = graph
190
+ @forest = []
191
+ @index = 1
192
+ @dfi = []
193
+
194
+ @graph.vertices.each do |v|
195
+ @dfi[v.id] = 0
196
+ end
197
+ end
198
+
199
+ def self.dfs(v)
200
+ @dfi[v.id] = @index
201
+ @index += 1
202
+
203
+ vertices = @graph.adjacent_vertices(v)
204
+ vertices.each do |u|
205
+ if @dfi[u.id] == 0
206
+ @forest << @graph.edge(v, u)
207
+ dfs(u)
208
+ end
209
+ end
210
+ end
211
+ end
212
+
213
+ edges = [
214
+ [1,2],[1,3],[1,4],[1,5],[1,6],[1,7],[1,8],
215
+ [2,5],[2,6],[2,8],
216
+ [3,4],[3,7],
217
+ [9,10],[9,13],
218
+ [10,11],[10,12],[10,13],
219
+ [11,12]
220
+ ]
221
+ forest = BackEdges.run(Graph.new(edges:edges))
222
+ bedges = graph.edges - forest
223
+ puts "Back edges:"
224
+ bedges.each{|edge| puts edge}
225
+ ```
226
+ #### Max-flow (Edmonds-Karp algorithm)
227
+ ```
228
+ require 'graphsrb'
229
+ include Graphsrb
230
+
231
+ module EdmondsKarp
232
+ MAX_INT = (2**(0.size * 8 -2) -1)
233
+
234
+ # Given undirected graph, source, and target
235
+ # computes maximum flow value between the source and target
236
+ def self.run(graph, s, t)
237
+ init(graph)
238
+ max_flow = 0
239
+ while true
240
+ min_val, vertices = find_path(s,t)
241
+ break if min_val.nil?
242
+ update_flows(vertices, min_val)
243
+ max_flow += min_val
244
+ end
245
+ max_flow
246
+ end
247
+
248
+ def self.init(graph)
249
+ @graph = graph
250
+
251
+ @flow = []
252
+ @graph.vertices.each{|v| @flow[v.id] = []}
253
+
254
+ @graph.vertices.each do |v|
255
+ @graph.adjacent_vertices(v).each do |u|
256
+ @flow[v.id][u.id] = 0
257
+ end
258
+ end
259
+ end
260
+
261
+ def self.find_path(s,t)
262
+ parent = []
263
+ queue = [s]
264
+ visited = []
265
+
266
+ parent[s.id] = nil
267
+ visited[s.id] = true
268
+
269
+ max_id = @graph.vertices.map{|v| v.id}.max
270
+ pushed_amount = Array.new(max_id, MAX_INT)
271
+
272
+ while !queue.empty?
273
+ v = queue.shift
274
+ break if v == t
275
+ vertices = @graph.adjacent_vertices(v)
276
+
277
+ vertices.each do |u|
278
+ cap = @graph.edge(v,u).weight
279
+ flow = @flow[v.id][u.id]
280
+ if !visited[u.id] && (cap - flow > 0)
281
+ queue.push(u)
282
+ visited[u.id] = true
283
+ parent[u.id] = v
284
+ pushed_amount[u.id] = [pushed_amount[v.id], cap - flow].min
285
+ end
286
+ end
287
+ end
288
+
289
+ return [nil, nil] if v != t
290
+
291
+ path = [t]
292
+ v = parent[t.id]
293
+ while true
294
+ path << v
295
+ break if v == s
296
+ v = parent[v.id]
297
+ end
298
+
299
+ [pushed_amount[t.id], path.reverse]
300
+ end
301
+
302
+ def self.update_flows(vertices, min_val)
303
+ (0...vertices.size-1).each do |i|
304
+ u,v = vertices[i], vertices[i+1]
305
+ @flow[u.id][v.id] += min_val
306
+ end
307
+ end
308
+
309
+ def self.print_flow
310
+ @graph.vertices.each do |v|
311
+ @graph.adjacent_vertices(v).each do |u|
312
+ cap = @graph.edge(v,u).weight
313
+ puts "(#{v.id})==#{@flow[v.id][u.id]}/#{cap}==>(#{u.id})"
314
+ end
315
+ end
316
+ puts "-"*45
317
+ end
318
+ end
319
+ ```
320
+ ** See examples (folder) for other implementations. **
321
+
322
+ ## Development
323
+
324
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
325
+
326
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
327
+
328
+ ## Contributing
329
+
330
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/graphsrb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
331
+
332
+ ## License
333
+
334
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
335
+
336
+ ## Code of Conduct
337
+
338
+ Everyone interacting in the Graphsrb project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/graphsrb/blob/master/CODE_OF_CONDUCT.md).