networkr 0.0.1 → 0.0.2

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: 43e672ad355d6a2b7535dcea503b62266cc29d8c
4
- data.tar.gz: 054fb810a76cc380e31f6c51a184d798538106d5
3
+ metadata.gz: 7edd7e7378f063576f1ec7eb1f99c5249e87e414
4
+ data.tar.gz: e5a82633408291f00db0c0d722b8f3048779c89b
5
5
  SHA512:
6
- metadata.gz: 3f623b282a654a2ec6b18d4e8ab4cf2e05c2839c82c48da0e830ddfbc0cc8c2ad5c916ad609fd5835a189364df528e5f0a558e4e2d463681fcc6565c14193795
7
- data.tar.gz: f9dbe521e03f73414c12b03fc7d7ab7550a98318f26b03f19283af502e59636a224d4d2854ebbbf51a14088390ad33268b301f916adaad385668c9e3f9853cac
6
+ metadata.gz: 60fe3c7de61c2229be0498da555d48becb4a150d083d788f39f6f280deae06389cd4150d5258a61bba5e78cf0e46a2af2d8a8b55daf8d36a861eb04e052c18a6
7
+ data.tar.gz: 50b22e9e6ab999cc4f569fc84e59a19076f842fc207366afb07702671526e99c96994e778b976c24af43e4bdfc220cc60183a557565268169188341e4771f894
@@ -0,0 +1,10 @@
1
+ lib/networkr.rb 43923214b5cfdb4b79e5956d3cb0f45eeb2b7e6f
2
+ lib/utils/heap.rb a495d1e614cf0c11895f1036968f1ef059cbf837
3
+ lib/networkr/version.rb 3ea15170a76704b53f3b9174590913bccfbe19c8
4
+ lib/networkr/graphs/graph.rb 0dccca2beba586bb30958644434f06fb4933651d
5
+ lib/networkr/graphs/digraph.rb 6e5f9be871d96dcb4d4a285ee099a83c9ac3b360
6
+ lib/networkr/algorithms/prim.rb abbee9c7c2868dcb04ad2b8a613efc4c58d7c218
7
+ lib/networkr/graphs/multigraph.rb 383153b0c94aa98545f40da23ccb65910fa7e9bc
8
+ lib/networkr/algorithms/karger.rb 2b95c1a6da1f27603330848b6bae3af76bf17ddd
9
+ lib/networkr/algorithms/kosaraju.rb 30203afd643e3bb67341fc928a30338ae592f577
10
+ lib/networkr/algorithms/dijkstra.rb c8493d7c3ec640e6e1bf96d154eab8a02c23f317
File without changes
Binary file
Binary file
Binary file
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- networkr (0.0.1)
4
+ networkr (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Networkr
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/networkr`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Networkr is a Ruby gem inspired by the Python package NetworkX. It includes basic functionality for the creation, manipulation, and analysis of graphs.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Graphs supported include undirected single-edge graphs (weighted or unweighted), directed single-edge graphs (weighted or unweighted), and undirected multi-edge graphs (weighted or unweighted).
6
+
7
+ Algorithms available include Dijkstra's shortest paths, Karger's minimum cut, Kosaraju's strongly connected components, and Prim's minimum spanning tree.
6
8
 
7
9
  ## Installation
8
10
 
@@ -22,19 +24,154 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ ### Graphs
28
+
29
+ All `Networkr` graphs store nodes, an adjacency list, and optional attributes.
30
+
31
+ - `Networkr::Graph` is the base class for `Networkr::DiGraph` and `Networkr::MultiGraph`. `Networkr::Graph` graphs hold undirected edges. Self loops are allowed but parallel edges are not.
32
+
33
+ - `Networkr::DiGraph` graphs hold directed edges. Self loops are allowed but parallel edges are not.
34
+
35
+ - `Networkr::MultiGraph` graphs hold undirected edges. Self loops and parallel edges are allowed.
36
+
37
+ **Graphs**
38
+
39
+ Create an empty `Networkr::Graph`:
40
+
41
+ ```ruby
42
+ g = Networkr::Graph.new
43
+ ```
44
+
45
+ Create an empty `Networkr::Graph` with an attribute:
46
+ ```ruby
47
+ g = Networkr::Graph.new(name: "users")
48
+ g.graph
49
+ { name: "users" }
50
+ ```
51
+
52
+ `Networkr` graphs can be grown in several ways.
26
53
 
27
- ## Development
54
+ **Nodes**
28
55
 
29
- 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.
56
+ Nodes can be any Ruby object and can hold optional attributes.
57
+
58
+ Add a node:
59
+
60
+ ```ruby
61
+ g.add_node(1)
62
+ ```
63
+
64
+ Add nodes with an attributes:
65
+
66
+ ```ruby
67
+ g.add_node(1, username: "janedoe")
68
+ g.add_node(3, username: "johndoe")
69
+ g.nodes[1]
70
+ { username: "janedoe" }
71
+ ```
72
+
73
+ Add/update node attributes using `#add_node` or `g.nodes`:
74
+
75
+ ```ruby
76
+ g.nodes[1][:score] = 10
77
+ g.nodes[1].delete(:score) # remove attribute
78
+ g.nodes
79
+ { 1 => { username: "janedoe" }, 3 => { username: "johndoe" } }
80
+ ```
81
+
82
+ **Edges**
83
+
84
+ Edges are represented in an adjacency list can also hold optional attributes. If an edge connects nodes not yet in the graph, the nodes are added automatically. There are no errors when adding nodes or edges that already exist.
85
+
86
+ Add an edge:
87
+
88
+ ```ruby
89
+ g.add_edge(1, 2)
90
+ ```
91
+
92
+ Add/update edge attributes using `#add_edge` or `g.adj`:
93
+
94
+ ```ruby
95
+ g.add_edge(1, 2, weight: 10)
96
+ g.adj[1][2][:weight] = 4
97
+ g.adj[1][2]
98
+ { weight: 4 }
99
+ ```
100
+
101
+ Other methods that can be called on `Networkr` graphs:
102
+
103
+ - `#remove_edge(u, v)` removes edge `u`-`v`
104
+
105
+ - `#children_of(node)` returns hash containing children of `node`
106
+
107
+ - `#has_node?(node)` returns `true` if graph contains node `node`
108
+
109
+ - `#has_edge?(u, v)` returns `true` if graph contains edge `u`-`v`
110
+
111
+ - `#length` returns number of nodes in graph
112
+
113
+ - `#clear` clears graph
114
+
115
+ - `#deep_copy` returns deep copy of graph
116
+
117
+ - `#is_directed?` returns boolean
118
+
119
+ - `#is_multigraph?` returns boolean
120
+
121
+ - `#to_s` returns string representation of graph
122
+
123
+ ### Algorithms
124
+
125
+ **Shortest Paths**
126
+
127
+ `Networkr::dijkstra` computes the shortest paths to every node in a weighted, undirected graph from a single source node. Weights must be non-negative.
128
+
129
+ ```ruby
130
+ g = Networkr::Graph.new
131
+ ...
132
+ Networkr::dijkstra(g, source_node) #returns a hash where keys are destination nodes and values are distances
133
+ ```
134
+
135
+ **Minimum Spanning Tree**
136
+
137
+ `Networkr::prim` finds the minimum spanning tree of a weighted, undirected graph. Weights can be negative. `source_node` should be a random node in the graph.
138
+
139
+ ```ruby
140
+ g = Networkr::Graph.new
141
+ ...
142
+ Networkr::prim(g, source_node) #returns a Networkr::Graph graph
143
+ ```
144
+
145
+ **Minimum Cut**
146
+
147
+ `Networkr::karger` finds a cut that crosses the minimum number of edges in a connected multi-edge graph. Karger's algorithm is a random algorithm and is not guaranteed to generate the correct output. However, if run |V|^2 log |V| times, its success probability is very high.
148
+
149
+ Use `Networkr::karger` to run the algorithm once. Use `Networkr::karger_repeated` to run it |V|^2 log |V| times.
150
+
151
+ ```ruby
152
+ g = Networkr::MultiGraph.new
153
+ ...
154
+ Networkr::karger_repeated(g) #returns the minimum number of edges crossed by a cut
155
+ ```
156
+
157
+ **Strongly Connected Components**
158
+
159
+ `Networkr::kosaraju_scc_sizes` and `Networkr::kosaraju_num_scc` find the strongly connected components in a directed graph.
160
+
161
+ ```ruby
162
+ g = Networkr::DiGraph.new
163
+ g_rev = Networkr::DiGraph.new #reversed graph of g
164
+ ...
165
+ Networkr::kosaraju_scc_sizes(g, g_rev) #returns the sizes of strongly connected components in descending order
166
+ Neworkr::kosaraju_num_scc(g, g_rev) #returns the number of strongly connected components
167
+ ```
30
168
 
31
- 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).
169
+ ## Future Directions
32
170
 
33
- ## TODO
34
- more robust tests
35
- use heap
36
- more flexibility
37
- add algorithms
171
+ - Implement algorithms with more efficient data structures
172
+ - Add more robust tests
173
+ - Allow algorithms to take more than one kind of graph and produce more than one kind of result
174
+ - Add more algorithms
38
175
 
39
176
  ## Contributing
40
177
 
@@ -0,0 +1,870 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Class: BinaryMinHeap
8
+
9
+ &mdash; Documentation by YARD 0.9.5
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "BinaryMinHeap";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index (B)</a> &raquo;
40
+
41
+
42
+ <span class="title">BinaryMinHeap</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <iframe id="search_frame" src="class_list.html"></iframe>
63
+
64
+ <div id="content"><h1>Class: BinaryMinHeap
65
+
66
+
67
+
68
+ </h1>
69
+ <div class="box_info">
70
+
71
+ <dl>
72
+ <dt>Inherits:</dt>
73
+ <dd>
74
+ <span class="inheritName">Object</span>
75
+
76
+ <ul class="fullTree">
77
+ <li>Object</li>
78
+
79
+ <li class="next">BinaryMinHeap</li>
80
+
81
+ </ul>
82
+ <a href="#" class="inheritanceTree">show all</a>
83
+
84
+ </dd>
85
+ </dl>
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+ <dl>
98
+ <dt>Defined in:</dt>
99
+ <dd>lib/utils/heap.rb</dd>
100
+ </dl>
101
+
102
+ </div>
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+ <h2>
113
+ Class Method Summary
114
+ <small><a href="#" class="summary_toggle">collapse</a></small>
115
+ </h2>
116
+
117
+ <ul class="summary">
118
+
119
+ <li class="public ">
120
+ <span class="summary_signature">
121
+
122
+ <a href="#child_indices-class_method" title="child_indices (class method)">.<strong>child_indices</strong>(len, parent_index) &#x21d2; Object </a>
123
+
124
+
125
+
126
+ </span>
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+ <span class="summary_desc"><div class='inline'></div></span>
137
+
138
+ </li>
139
+
140
+
141
+ <li class="public ">
142
+ <span class="summary_signature">
143
+
144
+ <a href="#heapify_down-class_method" title="heapify_down (class method)">.<strong>heapify_down</strong>(array, index_map, parent_idx, len = array.length, &amp;prc) &#x21d2; Object </a>
145
+
146
+
147
+
148
+ </span>
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+ <span class="summary_desc"><div class='inline'></div></span>
159
+
160
+ </li>
161
+
162
+
163
+ <li class="public ">
164
+ <span class="summary_signature">
165
+
166
+ <a href="#heapify_up-class_method" title="heapify_up (class method)">.<strong>heapify_up</strong>(array, child_idx, len = array.length, &amp;prc) &#x21d2; Object </a>
167
+
168
+
169
+
170
+ </span>
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+
180
+ <span class="summary_desc"><div class='inline'></div></span>
181
+
182
+ </li>
183
+
184
+
185
+ <li class="public ">
186
+ <span class="summary_signature">
187
+
188
+ <a href="#parent_index-class_method" title="parent_index (class method)">.<strong>parent_index</strong>(child_index) &#x21d2; Object </a>
189
+
190
+
191
+
192
+ </span>
193
+
194
+
195
+
196
+
197
+
198
+
199
+
200
+
201
+
202
+ <span class="summary_desc"><div class='inline'></div></span>
203
+
204
+ </li>
205
+
206
+
207
+ <li class="public ">
208
+ <span class="summary_signature">
209
+
210
+ <a href="#swap%21-class_method" title="swap! (class method)">.<strong>swap!</strong>(array, index_map, i1, i2) &#x21d2; Object </a>
211
+
212
+
213
+
214
+ </span>
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+ <span class="summary_desc"><div class='inline'></div></span>
225
+
226
+ </li>
227
+
228
+
229
+ </ul>
230
+
231
+ <h2>
232
+ Instance Method Summary
233
+ <small><a href="#" class="summary_toggle">collapse</a></small>
234
+ </h2>
235
+
236
+ <ul class="summary">
237
+
238
+ <li class="public ">
239
+ <span class="summary_signature">
240
+
241
+ <a href="#count-instance_method" title="#count (instance method)">#<strong>count</strong> &#x21d2; Object </a>
242
+
243
+
244
+
245
+ </span>
246
+
247
+
248
+
249
+
250
+
251
+
252
+
253
+
254
+
255
+ <span class="summary_desc"><div class='inline'></div></span>
256
+
257
+ </li>
258
+
259
+
260
+ <li class="public ">
261
+ <span class="summary_signature">
262
+
263
+ <a href="#initialize-instance_method" title="#initialize (instance method)">#<strong>initialize</strong>(&amp;prc) &#x21d2; BinaryMinHeap </a>
264
+
265
+
266
+
267
+ </span>
268
+
269
+
270
+ <span class="note title constructor">constructor</span>
271
+
272
+
273
+
274
+
275
+
276
+
277
+
278
+
279
+ <span class="summary_desc"><div class='inline'>
280
+ <p>A new instance of BinaryMinHeap.</p>
281
+ </div></span>
282
+
283
+ </li>
284
+
285
+
286
+ <li class="public ">
287
+ <span class="summary_signature">
288
+
289
+ <a href="#peek-instance_method" title="#peek (instance method)">#<strong>peek</strong> &#x21d2; Object </a>
290
+
291
+
292
+
293
+ </span>
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+ <span class="summary_desc"><div class='inline'></div></span>
304
+
305
+ </li>
306
+
307
+
308
+ <li class="public ">
309
+ <span class="summary_signature">
310
+
311
+ <a href="#pop-instance_method" title="#pop (instance method)">#<strong>pop</strong> &#x21d2; Object </a>
312
+
313
+
314
+
315
+ </span>
316
+
317
+
318
+
319
+
320
+
321
+
322
+
323
+
324
+
325
+ <span class="summary_desc"><div class='inline'></div></span>
326
+
327
+ </li>
328
+
329
+
330
+ <li class="public ">
331
+ <span class="summary_signature">
332
+
333
+ <a href="#push-instance_method" title="#push (instance method)">#<strong>push</strong>(val) &#x21d2; Object </a>
334
+
335
+
336
+
337
+ </span>
338
+
339
+
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+ <span class="summary_desc"><div class='inline'></div></span>
348
+
349
+ </li>
350
+
351
+
352
+ <li class="public ">
353
+ <span class="summary_signature">
354
+
355
+ <a href="#reduce%21-instance_method" title="#reduce! (instance method)">#<strong>reduce!</strong>(val) &#x21d2; Object </a>
356
+
357
+
358
+
359
+ </span>
360
+
361
+
362
+
363
+
364
+
365
+
366
+
367
+
368
+
369
+ <span class="summary_desc"><div class='inline'></div></span>
370
+
371
+ </li>
372
+
373
+
374
+ </ul>
375
+
376
+
377
+ <div id="constructor_details" class="method_details_list">
378
+ <h2>Constructor Details</h2>
379
+
380
+ <div class="method_details first">
381
+ <h3 class="signature first" id="initialize-instance_method">
382
+
383
+ #<strong>initialize</strong>(&amp;prc) &#x21d2; <tt><span class='object_link'><a href="" title="BinaryMinHeap (class)">BinaryMinHeap</a></span></tt>
384
+
385
+
386
+
387
+
388
+
389
+ </h3><div class="docstring">
390
+ <div class="discussion">
391
+
392
+ <p>Returns a new instance of BinaryMinHeap</p>
393
+
394
+
395
+ </div>
396
+ </div>
397
+ <div class="tags">
398
+
399
+
400
+ </div><table class="source_code">
401
+ <tr>
402
+ <td>
403
+ <pre class="lines">
404
+
405
+
406
+ 2
407
+ 3
408
+ 4
409
+ 5
410
+ 6</pre>
411
+ </td>
412
+ <td>
413
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 2</span>
414
+
415
+ <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='op'>&amp;</span><span class='id identifier rubyid_prc'>prc</span><span class='rparen'>)</span>
416
+ <span class='ivar'>@store</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
417
+ <span class='ivar'>@index_map</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span>
418
+ <span class='ivar'>@prc</span> <span class='op'>=</span> <span class='id identifier rubyid_prc'>prc</span> <span class='op'>||</span> <span class='const'>Proc</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_b'>b</span> <span class='rbrace'>}</span>
419
+ <span class='kw'>end</span></pre>
420
+ </td>
421
+ </tr>
422
+ </table>
423
+ </div>
424
+
425
+ </div>
426
+
427
+
428
+ <div id="class_method_details" class="method_details_list">
429
+ <h2>Class Method Details</h2>
430
+
431
+
432
+ <div class="method_details first">
433
+ <h3 class="signature first" id="child_indices-class_method">
434
+
435
+ .<strong>child_indices</strong>(len, parent_index) &#x21d2; <tt>Object</tt>
436
+
437
+
438
+
439
+
440
+
441
+ </h3><table class="source_code">
442
+ <tr>
443
+ <td>
444
+ <pre class="lines">
445
+
446
+
447
+ 52
448
+ 53
449
+ 54</pre>
450
+ </td>
451
+ <td>
452
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 52</span>
453
+
454
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_child_indices'>child_indices</span><span class='lparen'>(</span><span class='id identifier rubyid_len'>len</span><span class='comma'>,</span> <span class='id identifier rubyid_parent_index'>parent_index</span><span class='rparen'>)</span>
455
+ <span class='lbracket'>[</span><span class='int'>2</span> <span class='op'>*</span> <span class='id identifier rubyid_parent_index'>parent_index</span> <span class='op'>+</span> <span class='int'>1</span><span class='comma'>,</span> <span class='int'>2</span> <span class='op'>*</span> <span class='id identifier rubyid_parent_index'>parent_index</span> <span class='op'>+</span> <span class='int'>2</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_select'>select</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_idx'>idx</span><span class='op'>|</span> <span class='id identifier rubyid_idx'>idx</span> <span class='op'>&lt;</span> <span class='id identifier rubyid_len'>len</span> <span class='rbrace'>}</span>
456
+ <span class='kw'>end</span></pre>
457
+ </td>
458
+ </tr>
459
+ </table>
460
+ </div>
461
+
462
+ <div class="method_details ">
463
+ <h3 class="signature " id="heapify_down-class_method">
464
+
465
+ .<strong>heapify_down</strong>(array, index_map, parent_idx, len = array.length, &amp;prc) &#x21d2; <tt>Object</tt>
466
+
467
+
468
+
469
+
470
+
471
+ </h3><table class="source_code">
472
+ <tr>
473
+ <td>
474
+ <pre class="lines">
475
+
476
+
477
+ 64
478
+ 65
479
+ 66
480
+ 67
481
+ 68
482
+ 69
483
+ 70
484
+ 71
485
+ 72
486
+ 73
487
+ 74
488
+ 75
489
+ 76
490
+ 77
491
+ 78
492
+ 79
493
+ 80
494
+ 81
495
+ 82
496
+ 83
497
+ 84
498
+ 85
499
+ 86
500
+ 87
501
+ 88
502
+ 89
503
+ 90
504
+ 91
505
+ 92</pre>
506
+ </td>
507
+ <td>
508
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 64</span>
509
+
510
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_heapify_down'>heapify_down</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='comma'>,</span> <span class='id identifier rubyid_index_map'>index_map</span><span class='comma'>,</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span><span class='comma'>,</span> <span class='id identifier rubyid_len'>len</span> <span class='op'>=</span> <span class='id identifier rubyid_array'>array</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_prc'>prc</span><span class='rparen'>)</span>
511
+ <span class='id identifier rubyid_prc'>prc</span> <span class='op'>||=</span> <span class='const'>Proc</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_b'>b</span> <span class='rbrace'>}</span>
512
+
513
+ <span class='kw'>while</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span> <span class='op'>&lt;=</span> <span class='id identifier rubyid_parent_index'>parent_index</span><span class='lparen'>(</span><span class='id identifier rubyid_len'>len</span> <span class='op'>-</span> <span class='int'>1</span><span class='rparen'>)</span>
514
+ <span class='id identifier rubyid_children'>children</span> <span class='op'>=</span> <span class='id identifier rubyid_child_indices'>child_indices</span><span class='lparen'>(</span><span class='id identifier rubyid_len'>len</span><span class='comma'>,</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span><span class='rparen'>)</span>
515
+
516
+ <span class='kw'>if</span> <span class='id identifier rubyid_children'>children</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span> <span class='op'>==</span> <span class='int'>1</span>
517
+ <span class='id identifier rubyid_smallest_child_idx'>smallest_child_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_children'>children</span><span class='lbracket'>[</span><span class='int'>0</span><span class='rbracket'>]</span>
518
+ <span class='kw'>else</span>
519
+ <span class='id identifier rubyid_child1_idx'>child1_idx</span><span class='comma'>,</span> <span class='id identifier rubyid_child2_idx'>child2_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_children'>children</span><span class='lbracket'>[</span><span class='int'>0</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_children'>children</span><span class='lbracket'>[</span><span class='int'>1</span><span class='rbracket'>]</span>
520
+
521
+ <span class='kw'>if</span> <span class='id identifier rubyid_prc'>prc</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_child1_idx'>child1_idx</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_child2_idx'>child2_idx</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='op'>&lt;=</span> <span class='int'>0</span>
522
+ <span class='id identifier rubyid_smallest_child_idx'>smallest_child_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_child1_idx'>child1_idx</span>
523
+ <span class='kw'>else</span>
524
+ <span class='id identifier rubyid_smallest_child_idx'>smallest_child_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_child2_idx'>child2_idx</span>
525
+ <span class='kw'>end</span>
526
+ <span class='kw'>end</span>
527
+
528
+ <span class='kw'>if</span> <span class='id identifier rubyid_prc'>prc</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_parent_idx'>parent_idx</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_smallest_child_idx'>smallest_child_idx</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='op'>&gt;</span> <span class='int'>0</span>
529
+ <span class='id identifier rubyid_swap!'>swap!</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='comma'>,</span> <span class='id identifier rubyid_index_map'>index_map</span><span class='comma'>,</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span><span class='comma'>,</span> <span class='id identifier rubyid_smallest_child_idx'>smallest_child_idx</span><span class='rparen'>)</span>
530
+ <span class='id identifier rubyid_parent_idx'>parent_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_smallest_child_idx'>smallest_child_idx</span>
531
+ <span class='kw'>else</span>
532
+ <span class='kw'>break</span>
533
+ <span class='kw'>end</span>
534
+
535
+ <span class='kw'>end</span>
536
+
537
+ <span class='id identifier rubyid_array'>array</span>
538
+ <span class='kw'>end</span></pre>
539
+ </td>
540
+ </tr>
541
+ </table>
542
+ </div>
543
+
544
+ <div class="method_details ">
545
+ <h3 class="signature " id="heapify_up-class_method">
546
+
547
+ .<strong>heapify_up</strong>(array, child_idx, len = array.length, &amp;prc) &#x21d2; <tt>Object</tt>
548
+
549
+
550
+
551
+
552
+
553
+ </h3><table class="source_code">
554
+ <tr>
555
+ <td>
556
+ <pre class="lines">
557
+
558
+
559
+ 94
560
+ 95
561
+ 96
562
+ 97
563
+ 98
564
+ 99
565
+ 100
566
+ 101
567
+ 102
568
+ 103
569
+ 104
570
+ 105
571
+ 106
572
+ 107
573
+ 108
574
+ 109
575
+ 110</pre>
576
+ </td>
577
+ <td>
578
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 94</span>
579
+
580
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_heapify_up'>heapify_up</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='comma'>,</span> <span class='id identifier rubyid_child_idx'>child_idx</span><span class='comma'>,</span> <span class='id identifier rubyid_len'>len</span> <span class='op'>=</span> <span class='id identifier rubyid_array'>array</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_prc'>prc</span><span class='rparen'>)</span>
581
+ <span class='id identifier rubyid_prc'>prc</span> <span class='op'>||=</span> <span class='const'>Proc</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_a'>a</span><span class='comma'>,</span> <span class='id identifier rubyid_b'>b</span><span class='op'>|</span> <span class='id identifier rubyid_a'>a</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_b'>b</span> <span class='rbrace'>}</span>
582
+
583
+ <span class='kw'>while</span> <span class='id identifier rubyid_child_idx'>child_idx</span> <span class='op'>&gt;</span> <span class='int'>0</span>
584
+ <span class='id identifier rubyid_parent_idx'>parent_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_parent_index'>parent_index</span><span class='lparen'>(</span><span class='id identifier rubyid_child_idx'>child_idx</span><span class='rparen'>)</span>
585
+
586
+ <span class='kw'>if</span> <span class='id identifier rubyid_prc'>prc</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_parent_idx'>parent_idx</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_child_idx'>child_idx</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='op'>&gt;</span> <span class='int'>0</span>
587
+ <span class='id identifier rubyid_swap!'>swap!</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='comma'>,</span> <span class='id identifier rubyid_index_map'>index_map</span><span class='comma'>,</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span><span class='comma'>,</span> <span class='id identifier rubyid_child_idx'>child_idx</span><span class='rparen'>)</span>
588
+ <span class='id identifier rubyid_child_idx'>child_idx</span> <span class='op'>=</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span>
589
+ <span class='kw'>else</span>
590
+ <span class='kw'>break</span>
591
+ <span class='kw'>end</span>
592
+
593
+ <span class='kw'>end</span>
594
+
595
+ <span class='id identifier rubyid_array'>array</span>
596
+ <span class='kw'>end</span></pre>
597
+ </td>
598
+ </tr>
599
+ </table>
600
+ </div>
601
+
602
+ <div class="method_details ">
603
+ <h3 class="signature " id="parent_index-class_method">
604
+
605
+ .<strong>parent_index</strong>(child_index) &#x21d2; <tt>Object</tt>
606
+
607
+
608
+
609
+
610
+
611
+ </h3><table class="source_code">
612
+ <tr>
613
+ <td>
614
+ <pre class="lines">
615
+
616
+
617
+ 56
618
+ 57
619
+ 58
620
+ 59
621
+ 60
622
+ 61
623
+ 62</pre>
624
+ </td>
625
+ <td>
626
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 56</span>
627
+
628
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_parent_index'>parent_index</span><span class='lparen'>(</span><span class='id identifier rubyid_child_index'>child_index</span><span class='rparen'>)</span>
629
+ <span class='kw'>if</span> <span class='id identifier rubyid_child_index'>child_index</span> <span class='op'>==</span> <span class='int'>0</span>
630
+ <span class='kw'>return</span> <span class='kw'>nil</span>
631
+ <span class='kw'>else</span>
632
+ <span class='lparen'>(</span><span class='id identifier rubyid_child_index'>child_index</span> <span class='op'>-</span> <span class='int'>1</span><span class='rparen'>)</span> <span class='op'>/</span> <span class='int'>2</span>
633
+ <span class='kw'>end</span>
634
+ <span class='kw'>end</span></pre>
635
+ </td>
636
+ </tr>
637
+ </table>
638
+ </div>
639
+
640
+ <div class="method_details ">
641
+ <h3 class="signature " id="swap!-class_method">
642
+
643
+ .<strong>swap!</strong>(array, index_map, i1, i2) &#x21d2; <tt>Object</tt>
644
+
645
+
646
+
647
+
648
+
649
+ </h3><table class="source_code">
650
+ <tr>
651
+ <td>
652
+ <pre class="lines">
653
+
654
+
655
+ 47
656
+ 48
657
+ 49
658
+ 50</pre>
659
+ </td>
660
+ <td>
661
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 47</span>
662
+
663
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_swap!'>swap!</span><span class='lparen'>(</span><span class='id identifier rubyid_array'>array</span><span class='comma'>,</span> <span class='id identifier rubyid_index_map'>index_map</span><span class='comma'>,</span> <span class='id identifier rubyid_i1'>i1</span><span class='comma'>,</span> <span class='id identifier rubyid_i2'>i2</span><span class='rparen'>)</span>
664
+ <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_i1'>i1</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_i2'>i2</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_i2'>i2</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_array'>array</span><span class='lbracket'>[</span><span class='id identifier rubyid_i1'>i1</span><span class='rbracket'>]</span>
665
+ <span class='id identifier rubyid_index_map'>index_map</span><span class='lbracket'>[</span><span class='id identifier rubyid_parent_val'>parent_val</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_index_map'>index_map</span><span class='lbracket'>[</span><span class='id identifier rubyid_child_val'>child_val</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_child_idx'>child_idx</span><span class='comma'>,</span> <span class='id identifier rubyid_parent_idx'>parent_idx</span>
666
+ <span class='kw'>end</span></pre>
667
+ </td>
668
+ </tr>
669
+ </table>
670
+ </div>
671
+
672
+ </div>
673
+
674
+ <div id="instance_method_details" class="method_details_list">
675
+ <h2>Instance Method Details</h2>
676
+
677
+
678
+ <div class="method_details first">
679
+ <h3 class="signature first" id="count-instance_method">
680
+
681
+ #<strong>count</strong> &#x21d2; <tt>Object</tt>
682
+
683
+
684
+
685
+
686
+
687
+ </h3><table class="source_code">
688
+ <tr>
689
+ <td>
690
+ <pre class="lines">
691
+
692
+
693
+ 8
694
+ 9
695
+ 10</pre>
696
+ </td>
697
+ <td>
698
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 8</span>
699
+
700
+ <span class='kw'>def</span> <span class='id identifier rubyid_count'>count</span>
701
+ <span class='ivar'>@store</span><span class='period'>.</span><span class='id identifier rubyid_length'>length</span>
702
+ <span class='kw'>end</span></pre>
703
+ </td>
704
+ </tr>
705
+ </table>
706
+ </div>
707
+
708
+ <div class="method_details ">
709
+ <h3 class="signature " id="peek-instance_method">
710
+
711
+ #<strong>peek</strong> &#x21d2; <tt>Object</tt>
712
+
713
+
714
+
715
+
716
+
717
+ </h3><table class="source_code">
718
+ <tr>
719
+ <td>
720
+ <pre class="lines">
721
+
722
+
723
+ 27
724
+ 28
725
+ 29
726
+ 30</pre>
727
+ </td>
728
+ <td>
729
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 27</span>
730
+
731
+ <span class='kw'>def</span> <span class='id identifier rubyid_peek'>peek</span>
732
+ <span class='kw'>return</span> <span class='kw'>nil</span> <span class='kw'>if</span> <span class='id identifier rubyid_count'>count</span> <span class='op'>==</span> <span class='int'>0</span>
733
+ <span class='ivar'>@store</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span>
734
+ <span class='kw'>end</span></pre>
735
+ </td>
736
+ </tr>
737
+ </table>
738
+ </div>
739
+
740
+ <div class="method_details ">
741
+ <h3 class="signature " id="pop-instance_method">
742
+
743
+ #<strong>pop</strong> &#x21d2; <tt>Object</tt>
744
+
745
+
746
+
747
+
748
+
749
+ </h3><table class="source_code">
750
+ <tr>
751
+ <td>
752
+ <pre class="lines">
753
+
754
+
755
+ 12
756
+ 13
757
+ 14
758
+ 15
759
+ 16
760
+ 17
761
+ 18
762
+ 19
763
+ 20
764
+ 21
765
+ 22
766
+ 23
767
+ 24
768
+ 25</pre>
769
+ </td>
770
+ <td>
771
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 12</span>
772
+
773
+ <span class='kw'>def</span> <span class='id identifier rubyid_pop'>pop</span>
774
+ <span class='kw'>return</span> <span class='kw'>nil</span> <span class='kw'>if</span> <span class='id identifier rubyid_count'>count</span> <span class='op'>==</span> <span class='int'>0</span>
775
+
776
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_swap!'>swap!</span><span class='lparen'>(</span><span class='ivar'>@store</span><span class='comma'>,</span> <span class='ivar'>@index_map</span><span class='comma'>,</span> <span class='int'>0</span><span class='comma'>,</span> <span class='id identifier rubyid_count'>count</span> <span class='op'>-</span> <span class='int'>1</span><span class='rparen'>)</span>
777
+
778
+ <span class='id identifier rubyid_val'>val</span> <span class='op'>=</span> <span class='ivar'>@store</span><span class='period'>.</span><span class='id identifier rubyid_pop'>pop</span>
779
+ <span class='ivar'>@index_map</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span><span class='lparen'>(</span><span class='id identifier rubyid_val'>val</span><span class='rparen'>)</span>
780
+
781
+ <span class='kw'>unless</span> <span class='id identifier rubyid_count'>count</span> <span class='op'>==</span> <span class='int'>0</span>
782
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_heapify_down'>heapify_down</span><span class='lparen'>(</span><span class='ivar'>@store</span><span class='comma'>,</span> <span class='ivar'>@index_map</span><span class='comma'>,</span> <span class='int'>0</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_prc'>prc</span><span class='rparen'>)</span>
783
+ <span class='kw'>end</span>
784
+
785
+ <span class='id identifier rubyid_val'>val</span>
786
+ <span class='kw'>end</span></pre>
787
+ </td>
788
+ </tr>
789
+ </table>
790
+ </div>
791
+
792
+ <div class="method_details ">
793
+ <h3 class="signature " id="push-instance_method">
794
+
795
+ #<strong>push</strong>(val) &#x21d2; <tt>Object</tt>
796
+
797
+
798
+
799
+
800
+
801
+ </h3><table class="source_code">
802
+ <tr>
803
+ <td>
804
+ <pre class="lines">
805
+
806
+
807
+ 32
808
+ 33
809
+ 34
810
+ 35
811
+ 36</pre>
812
+ </td>
813
+ <td>
814
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 32</span>
815
+
816
+ <span class='kw'>def</span> <span class='id identifier rubyid_push'>push</span><span class='lparen'>(</span><span class='id identifier rubyid_val'>val</span><span class='rparen'>)</span>
817
+ <span class='ivar'>@store</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_val'>val</span>
818
+ <span class='ivar'>@index_map</span><span class='lbracket'>[</span><span class='id identifier rubyid_val'>val</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_count'>count</span> <span class='op'>-</span> <span class='int'>1</span>
819
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_heapify_up'>heapify_up</span><span class='lparen'>(</span><span class='ivar'>@store</span><span class='comma'>,</span> <span class='ivar'>@index_map</span><span class='comma'>,</span> <span class='id identifier rubyid_count'>count</span> <span class='op'>-</span> <span class='int'>1</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_prc'>prc</span><span class='rparen'>)</span>
820
+ <span class='kw'>end</span></pre>
821
+ </td>
822
+ </tr>
823
+ </table>
824
+ </div>
825
+
826
+ <div class="method_details ">
827
+ <h3 class="signature " id="reduce!-instance_method">
828
+
829
+ #<strong>reduce!</strong>(val) &#x21d2; <tt>Object</tt>
830
+
831
+
832
+
833
+
834
+
835
+ </h3><table class="source_code">
836
+ <tr>
837
+ <td>
838
+ <pre class="lines">
839
+
840
+
841
+ 38
842
+ 39
843
+ 40
844
+ 41</pre>
845
+ </td>
846
+ <td>
847
+ <pre class="code"><span class="info file"># File 'lib/utils/heap.rb', line 38</span>
848
+
849
+ <span class='kw'>def</span> <span class='id identifier rubyid_reduce!'>reduce!</span><span class='lparen'>(</span><span class='id identifier rubyid_val'>val</span><span class='rparen'>)</span>
850
+ <span class='id identifier rubyid_index'>index</span> <span class='op'>=</span> <span class='ivar'>@index_map</span><span class='lbracket'>[</span><span class='id identifier rubyid_val'>val</span><span class='rbracket'>]</span>
851
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_heapify_up'>heapify_up</span><span class='lparen'>(</span><span class='ivar'>@store</span><span class='comma'>,</span> <span class='ivar'>@index_map</span><span class='comma'>,</span> <span class='id identifier rubyid_index'>index</span><span class='comma'>,</span> <span class='op'>&amp;</span><span class='id identifier rubyid_prc'>prc</span><span class='rparen'>)</span>
852
+ <span class='kw'>end</span></pre>
853
+ </td>
854
+ </tr>
855
+ </table>
856
+ </div>
857
+
858
+ </div>
859
+
860
+ </div>
861
+
862
+ <div id="footer">
863
+ Generated on Sun Jul 24 17:23:15 2016 by
864
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
865
+ 0.9.5 (ruby-2.1.2).
866
+ </div>
867
+
868
+ </div>
869
+ </body>
870
+ </html>