-
Notifications
You must be signed in to change notification settings - Fork 4
/
PriorityQueue.m
62 lines (46 loc) · 1.92 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
classdef PriorityQueue < handle
% Priorigy queue data structure for the USC algorithm
properties
valueList;
indexList;
topindex;
topvalue;
end
methods
function thePQueue = PriorityQueue(n)
thePQueue.valueList = sparse(1,n);
thePQueue.indexList =[];
thePQueue.topindex=0;
thePQueue.topvalue=+inf;
end
function push(thePQueue, index, value)
[index1,IA1] = setdiff(index, thePQueue.indexList);
thePQueue.valueList(index1)=value(IA1);
thePQueue.indexList = union(thePQueue.indexList,index1);
[index2,IA2] = intersect(index, thePQueue.indexList);
thePQueue.valueList(index2)=min(value(IA2),thePQueue.valueList(index2));
if min(value)<thePQueue.topvalue
[thePQueue.topvalue,I]=min(value);
thePQueue.topindex = index(I);
end
end
function [minindex,minvalue] = pop(thePQueue)
minindex = thePQueue.topindex;
minvalue = thePQueue.topvalue;
thePQueue.valueList(thePQueue.topindex)=0;
thePQueue.indexList = setdiff(thePQueue.indexList,thePQueue.topindex);
[~,J,V]=find(thePQueue.valueList);
if ~isempty(J)
[thePQueue.topvalue,jindex] = min(V);
thePQueue.topindex=J(jindex);
else
thePQueue.topindex=0;
thePQueue.topvalue=+inf;
end
end
function flagIsEmpty = isEmpty(thePQueue)
[~,J,V]=find(thePQueue.valueList);
flagIsEmpty = isempty(J);
end
end
end