-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.m
172 lines (115 loc) · 4.31 KB
/
PriorityQueue.m
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
classdef PriorityQueue < handle
properties (SetAccess = private)
numElements;
priorityList;
valueList;
end
methods (Access = public)
function obj = PriorityQueue()
obj.numElements = 0;
obj.priorityList = {};
obj.valueList = {};
end
function insert(obj, value, priority)
% increase the size of the array if full
if obj.numElements > 0 && obj.numElements + 1 > numel( obj.priorityList )
% double the size of the array and copy stuff
obj.priorityList = cat(1, obj.priorityList, cell(obj.numElements, 1));
obj.valueList = cat(1, obj.valueList, cell(obj.numElements, 1));
end
obj.numElements = obj.numElements + 1;
obj.priorityList{ obj.numElements } = priority;
obj.valueList{ obj.numElements } = value;
obj.swim(obj.numElements);
end
function [value, priority] = pop( obj )
if obj.isEmpty()
error( 'called pop() on an empty priority queue' );
end
priority = obj.priorityList{1};
value = obj.valueList{1};
obj.exch(1, obj.numElements);
obj.numElements = obj.numElements - 1;
obj.sink(1);
obj.priorityList{ obj.numElements + 1 } = [];
obj.valueList{ obj.numElements + 1 } = [];
% halve the size of the arrays if they get one-quarter full
if obj.numElements > 0 && obj.numElements == floor( numel( obj.priorityList ) / 4 )
obj.priorityList( 2 * obj.numElements + 1 : end ) = [];
obj.valueList( 2 * obj.numElements + 1 : end ) = [];
end
end
function [flagEmpty] = isEmpty( obj )
flagEmpty = (obj.numElements == 0);
end
function [contain] = contains( obj, value )
contain = 0;
for i = 1 : size(obj.valueList,1)
if compareElement( obj.valueList{i} , value ) == 0
contain = 1;
return;
end
end
end
function [qSize] = size( obj )
qSize = obj.numElements;
end
function [value, priority] = peek( obj )
if obj.isEmpty()
error( 'requested max() of an empty priority queue' );
end
priority = obj.priorityList{1};
value = obj.valueList{1};
end
end
methods (Access = private)
function swim(obj, elPos)
while elPos > 1 && obj.compare(floor(elPos / 2), elPos)
obj.exch(floor(elPos / 2), elPos);
elPos = floor(elPos / 2);
end
end
function sink(obj, elPos)
while 2 * elPos <= obj.numElements
j = 2 * elPos;
if j < obj.numElements && obj.compare(j, j+1)
j = j + 1;
end
if ~obj.compare(elPos, j)
break;
end
obj.exch(elPos, j);
elPos = j;
end
end
function [blnCmpResult] = compare(obj, e1, e2)
blnCmpResult = (obj.priorityList{e1} > obj.priorityList{e2});
if obj.priorityList{e1} == obj.priorityList{e2}
compareResult = compareElement(obj.valueList{e1} , obj.valueList{e2});
if compareResult > 0
blnCmpResult = 1;
end
end
end
function exch(obj, e1, e2 )
temp = obj.priorityList{e1};
obj.priorityList{e1} = obj.priorityList{e2};
obj.priorityList{e2} = temp;
temp = obj.valueList{e1};
obj.valueList{e1} = obj.valueList{e2};
obj.valueList{e2} = temp;
end
end
end
function ret = compareElement(a, b)
ret = size(a, 2) - size(b, 2);
for i = 1 : min(size(a, 2), size(b, 2))
if a(i) < b(i)
ret = -1;
return;
elseif a(i) > b(i)
ret = 1;
return;
end
end
end