-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
207 lines (172 loc) · 6.94 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
= Depq - Double-Ended Priority Queue
depq is double-ended stable priority queue with priority update operation implemented using implicit heap.
== Features
* queue - you can insert and delete values
* priority - you can get a value with minimum priority
* double-ended - you can get a value with maximum priority too
* stable - you will get the value inserted first with minimum/maximum priority
* priority update - you can change the priority of inserted values (usable for Dijkstra's shortest path algorithm and various graph algorithms)
* implicit heap - compact heap representation using array. most operations are O(log n) at worst
* several utility methods: nlargest, nsmallest, merge, astar_search
== Install
gem install depq
== Links
* ((<source repository on github|URL:http://github.com/akr/depq>))
* ((<depq on rubygems.org|URL:http://rubygems.org/gems/depq>))
== Introduction
=== Simple Insertion/Deletion
You can insert values into a Depq object.
You can delete the values from the object from ascending/descending order.
delete_min deletes the minimum value.
It is used for ascending order.
q = Depq.new
q.insert "durian"
q.insert "banana"
p q.delete_min #=> "banana"
q.insert "orange"
q.insert "apple"
q.insert "melon"
p q.delete_min #=> "apple"
p q.delete_min #=> "durian"
p q.delete_min #=> "melon"
p q.delete_min #=> "orange"
p q.delete_min #=> nil
delete_max is similar to delete_min except it deletes maximum element
instead of minimum.
It is used for descending order.
=== The Order
The order is defined by the priorities corresponds to the values and
comparison operator specified for the queue.
q = Depq.new(:casecmp) # use casecmp instead of <=>.
q.insert 1, "Foo" # specify the priority for 1 as "Foo"
q.insert 2, "bar"
q.insert 3, "Baz"
p q.delete_min #=> 2 # "bar" is minimum
p q.delete_min #=> 3
p q.delete_min #=> 1 # "Foo" is maximum
p q.delete_min #=> nil
If there are multiple values with same priority, subpriority is used to compare them.
subpriority is an integer which can be specified by 3rd argument of insert.
If it is not specified, total number of inserted elements is used.
So Depq is "stable" which means that the element inserted first is deleted first.
q = Depq.new
q.insert "a", 1 # "a", "c" and "e" has same priority: 1
q.insert "b", 0 # "b", "d" and "f" has same priority: 0
q.insert "c", 1
q.insert "d", 0
q.insert "e", 1
q.insert "f", 0
p q.delete_min #=> "b" first element with priority 0
p q.delete_min #=> "d"
p q.delete_min #=> "f" last element with priority 0
p q.delete_min #=> "a" first element with priority 1
p q.delete_min #=> "c"
p q.delete_min #=> "e" last element with priority 1
delete_max is also stable.
This means delete_max deletes the element with maximum priority with "minimum" subpriority.
q = Depq.new
q.insert "a", 1 # "a", "c" and "e" has same priority: 1
q.insert "b", 0 # "b", "d" and "f" has same priority: 0
q.insert "c", 1
q.insert "d", 0
q.insert "e", 1
q.insert "f", 0
p q.delete_max #=> "a" first element with priority 1
p q.delete_max #=> "c"
p q.delete_max #=> "e" last element with priority 1
p q.delete_max #=> "b" first element with priority 0
p q.delete_max #=> "d"
p q.delete_max #=> "f" last element with priority 0
=== Update Element
An inserted element can be modified and/or deleted.
The element to be modified is specified by Depq::Locator object.
It is returned by insert, find_min_locator, etc.
q = Depq.new
d = q.insert "durian", 1
m = q.insert "mangosteen", 2
c = q.insert "cherry", 3
p m #=> #<Depq::Locator: "mangosteen":2>
p m.value #=> "mangosteen"
p m.priority #=> 2
p q.find_min #=> "durian"
p q.find_min_locator #=> #<Depq::Locator: "durian":1>
m.update("mangosteen", 0)
p q.find_min #=> "mangosteen"
p q.find_min_locator #=> #<Depq::Locator: "mangosteen":0>
q.delete_element d
p q.delete_min #=> "mangosteen"
p q.delete_min #=> "cherry"
p q.delete_min #=> nil
For example, this feature can be used for graph algorithms
such as Dijkstra's shortest path finding algorithm,
A* search algorithm, etc.
def dijkstra_shortest_path(start, edges)
h = {}
edges.each {|v1, v2, w|
(h[v1] ||= []) << [v2, w]
}
h.default = []
q = Depq.new
visited = {start => q.insert([start], 0)}
until q.empty?
path, w1 = q.delete_min_priority
v1 = path.last
h[v1].each {|v2, w2|
if !visited[v2]
visited[v2] = q.insert(path+[v2], w1 + w2)
elsif w1 + w2 < visited[v2].priority
visited[v2].update(path+[v2], w1 + w2) # update val/prio
end
}
end
result = []
visited.each_value {|loc|
result << [loc.value, loc.priority]
}
result
end
E = [
['A', 'B', 2],
['A', 'C', 4],
['B', 'C', 1],
['C', 'B', 2],
['B', 'D', 3],
['C', 'D', 1],
]
p dijkstra_shortest_path('A', E)
#=> [[["A"], 0],
# [["A", "B"], 2],
# [["A", "B", "C"], 3],
# [["A", "B", "C", "D"], 4]]
== Internal Heap Algorithm
Depq uses min-heap, max-heap or interval-heap internally.
When delete_min is used, min-heap is constructed.
When delete_max is used, max-heap is constructed.
When delete_min and delete_max is used, interval-heap is constructed.
== Author
Tanaka Akira <[email protected]>
== License
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(The modified BSD licence)