6 * $Date: 2012-07-03 09:54:22 +0200 (Tue, 03 Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of a base class for planar representations
11 * of graphs and cluster graphs.
13 * \author Hoi-Ming Wong
16 * This file is part of the Open Graph Drawing Framework (OGDF).
20 * See README.txt in the root directory of the OGDF installation for details.
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * Version 2 or 3 as published by the Free Software Foundation;
26 * see the file LICENSE.txt included in the packaging of this file
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public
37 * License along with this program; if not, write to the Free
38 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
39 * Boston, MA 02110-1301, USA.
41 * \see http://www.gnu.org/copyleft/gpl.html
42 ***************************************************************/
50 #ifndef OGDF_MIN_PRIORITY_QUEUE_H
51 #define OGDF_MIN_PRIORITY_QUEUE_H
53 #include<ogdf/basic/Array.h>
59 template<class Score
, class X
>
62 template<class T1
, class T2
> friend class MinPriorityQueue
;
67 int pos
; // the position in the heapElement array
71 HeapElement(const HeapElement
<Score
, X
> &orig
): v(orig
.v
), x(orig
.x
), pos(orig
.pos
) {}
72 HeapElement(Score vt
, X xt
) : v(vt
), x(xt
) {}
75 Score
score_value() const { return v
; }
77 X
element() const { return x
;}
83 // follow the pseudo code of "introduction to algorithms"
84 template <class Score
, class X
>
85 class MinPriorityQueue
{
89 HeapElement
<Score
, X
> **heapElements
;
91 int number
; // the number of elements in the Queue
94 void swap(int pos1
, int pos2
) {
95 HeapElement
<Score
, X
> *tmp
= heapElements
[pos1
];
96 heapElements
[pos1
] = heapElements
[pos2
];
97 heapElements
[pos2
] = tmp
;
98 // update the position
99 heapElements
[pos2
]->pos
= pos2
;
100 heapElements
[pos1
]->pos
= pos1
;
103 void minHeapify(int pos
) {
104 int l
= getLeft(pos
);
105 int r
= getRight(pos
);
107 if (l
<= number
&& heapElements
[l
]->score_value() < heapElements
[pos
]->score_value() )
111 if (r
<= number
&& heapElements
[r
]->score_value() < heapElements
[smallest
]->score_value())
113 if (smallest
!= pos
) {
115 minHeapify(smallest
);
119 int getParent(int pos
) const {return pos
/2;}
120 int getLeft(int pos
) const {return pos
*2;}
121 int getRight(int pos
) const {return pos
*2+1;}
126 // contructor, only fixed size
127 MinPriorityQueue(int _size
) : number(0), s(_size
) {
128 heapElements
= new HeapElement
<Score
, X
>* [_size
+1]; // allocate
129 for (int i
= 0; i
< s
+1; ++i
) {
135 ~MinPriorityQueue() {
136 for (int i
= 0; i
< s
+1; ++i
) {
137 if (heapElements
[i
] != 0) {
138 delete heapElements
[i
];
142 delete [] heapElements
;
146 bool empty() const { return number
== 0; }
147 int count() const { return number
; }
148 int size() const { return s
; }
150 //return the Object with the min. score
151 const X
& getMin() const {
152 return heapElements
[1]->element();
157 void decreasePriority(const HeapElement
<Score
, X
> *elem
, // handle to the element
158 Score sc
// new score, muss be smaller then the old score
162 if (heapElements
[i
]->score_value() < sc
)
163 throw "New key is greater than current key.";
164 heapElements
[i
]->v
= sc
;
165 while (i
> 1 && (heapElements
[getParent(i
)]->score_value() > heapElements
[i
]->score_value()) ) {
166 swap(i
, getParent(i
));
172 // return a handle to the new inserted element
173 const HeapElement
<Score
, X
> * insert(const HeapElement
<Score
, X
> &elem
) {
174 HeapElement
<Score
, X
> *h_elem
= new HeapElement
<Score
, X
>(elem
); // make a copy
176 OGDF_ASSERT(number
<= s
);
177 h_elem
->pos
= number
; // store the position
178 heapElements
[number
] = h_elem
;
180 while ( (i
> 1) && ( heapElements
[getParent(i
)]->score_value() > heapElements
[i
]->score_value()) ) {
181 swap(i
, getParent(i
));
188 // return the smallest element and remove it from the queue
189 HeapElement
<Score
, X
> pop() {
191 throw "Heap underflow error!";
192 HeapElement
<Score
, X
> obj
= *heapElements
[1];
193 HeapElement
<Score
, X
> * p
= heapElements
[1];
198 OGDF_ASSERT(number
+1 <= s
);
199 heapElements
[number
+1] = 0;
205 /*********************************************************************************/
208 cout
<< "\nHeap Array: \n";
209 for (int i
= 0; i
< s
+1; i
++) {
210 HeapElement
<Score
, X
> *obj
= heapElements
[i
];
212 cout
<< "score: " << obj
->score_value() << "; elem: " << obj
->element() << "; index "<< i
<< "; pos: " << obj
->pos
<< endl
<< flush
;
214 cout
<< "index: " << i
<< " value: null;" << endl
<< flush
;