Boost C++ Libraries: Ticket #11151: Bug in update method for fibonacci heap. https://svn.boost.org/trac10/ticket/11151 <p> There appears to be a bug in the update method for handles in the fibonacci heap. Calling update with a handle is supposed to rearrange the corresponding element in the heap. However, it does not properly increase the priority in the fibonacci heap. The pairing heap does not suffer from this problem. </p> <p> The following example shows the problem. I made my original problem as minimal as possible. I was unable to create a smaller example from scratch. The example finds the minimum path from the topleft (0,0) of a matrix to the bottom right, using djikstra's algorithm. </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;limits&gt; #include &lt;boost/heap/fibonacci_heap.hpp&gt; #include &lt;boost/heap/pairing_heap.hpp&gt; using namespace boost::heap; struct VertexRef; struct Position { int x; int y; explicit Position(int ax, int ay) : x(ax), y(ay) {}; }; struct Vertex { int cost; int dist; Position position; fibonacci_heap&lt;VertexRef&gt;::handle_type handle; explicit Vertex(Position pos, int c) : cost{c}, position{pos} {}; }; struct MatrixGrid { explicit MatrixGrid(int w, int h) : m_width{w}, m_height{h} { unsigned int size = w * h; this-&gt;vertices.reserve(size); } void putVertex(const Vertex&amp; vertex) { this-&gt;vertices[indexForPos(vertex.position)] = vertex; } Vertex&amp; getVertex(Position pos) { return this-&gt;vertices[indexForPos(pos)]; } template&lt;typename Func&gt; void withNeighbours(Vertex&amp; vertex, Func f) { Position pos(vertex.position); if (pos.x &gt; 0) f(getVertex(Position(pos.x-1, pos.y))); if (pos.y &gt; 0) f(getVertex(Position(pos.x, pos.y-1))); if (pos.x &lt; m_width-1) f(getVertex(Position(pos.x+1, pos.y))); if (pos.y &lt; m_height-1) f(getVertex(Position(pos.x, pos.y+1))); } int width() const { return m_width; } int height() const { return m_height; } private: int m_width; int m_height; std::vector&lt;Vertex&gt; vertices{}; unsigned int indexForPos(const Position&amp; pos) { assert(pos.x &lt; m_width &amp;&amp; pos.y &lt; m_height &amp;&amp; pos.x &gt;= 0 &amp;&amp; pos.y &gt;= 0); return pos.y * m_width + pos.x; } }; struct VertexRef { Vertex&amp; vertex; bool operator&lt;(const VertexRef&amp; rhs) const { return vertex.dist &gt; rhs.vertex.dist; } VertexRef(Vertex&amp; aVertex) : vertex(aVertex) {} }; MatrixGrid get_predictable_matrix(int width, int height) { auto grid = MatrixGrid(width, height); for (int y=0; y &lt; height; ++y) { for (int x=0; x &lt; width; ++x) { Position pos(x, y); grid.putVertex(Vertex(pos, y*width+x)); } } return grid; } void run_dijkstra(MatrixGrid&amp; grid, const Position&amp; startPosition) { auto queue = fibonacci_heap&lt;VertexRef&gt;(); int maxDist = std::numeric_limits&lt;int&gt;::max(); //Set initial node properly { for (int y=0; y &lt; grid.height(); ++y) { for (int x=0; x &lt; grid.width(); x++) { auto&amp; vertex = grid.getVertex(Position(x, y)); if (x == startPosition.x &amp;&amp; y == startPosition.y) { vertex.dist = vertex.cost; } else { vertex.dist = maxDist; } auto handle = queue.push(vertex); vertex.handle = handle; } } } while (!queue.empty()) { auto&amp; current = queue.top().vertex; auto checkNeighbour = [&amp;queue, &amp;current](Vertex&amp; neighbour) { //dist is max int.. Can overflow int64_t candidateDist = static_cast&lt;int64_t&gt;(current.dist) + static_cast&lt;int64_t&gt;(neighbour.cost); if (candidateDist &lt; static_cast&lt;int64_t&gt;(neighbour.dist)) { neighbour.dist = candidateDist; queue.increase(neighbour.handle); } }; grid.withNeighbours(current, checkNeighbour); queue.pop(); } } int main(int argc, char const *argv[]) { auto grid = get_predictable_matrix(10, 10); Position start(0, 0); Position end(9, 9); run_dijkstra(grid, start); std::cout &lt;&lt; "Final Dist: " &lt;&lt; grid.getVertex(end).dist &lt;&lt; std::endl; /* Fibonacci(increase) = 576 Fibonacci(update) = 585 Pairing(increase) = 576 Pairing(update) = 576 */ return 0; } </pre><p> Using a pairing heap for the program, the answer is correct regardless whether using increase or update in djikstra method. However, by using fibonacci heap, only the increase call is correct. Update acts the same as if decrease or not calling it at all. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11151 Trac 1.4.3 timblechmann Sun, 05 Apr 2015 00:23:47 GMT <link>https://svn.boost.org/trac10/ticket/11151#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11151#comment:1</guid> <description> <p> i can see the same result, but would need a test case which is easier to debug. would you be able to provide one? thnx </p> </description> <category>Ticket</category> </item> <item> <author>Søren Enevoldsen <senevo10@…></author> <pubDate>Sun, 05 Apr 2015 17:27:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11151#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11151#comment:2</guid> <description> <p> I've been unable in several attempts to reproduce the problem. There needs to be a lot of boilerplate to get the indirect referencing of values, such that other values than the current (top()) can be changed. </p> <p> I have, however, found a possible related problem, that appears when entries "move past each other" in priority. In this case it is possible I did something wrong; maybe missed some requirement for elements? The following example have an array of numbers. I indirectly add increasing values (+5 each) to the heap. The highest value, also the "rightmost", gets picked first. Before removing it, I increase the value to the left and update the heap. I tested for 4 different scenarios: </p> <p> Use Update and increment by 5: Works correct. Use Increase and increment by 5: Works correct. Use Update and increment by 15: Correct sum. But wrong reason. The entries in the heap have been corrupted. Use Increase and increment by 15: Core dump! </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/heap/fibonacci_heap.hpp&gt; #include &lt;boost/heap/pairing_heap.hpp&gt; using namespace boost::heap; struct EntryLocation; typedef fibonacci_heap&lt;EntryLocation&gt; HeapType; struct Entry { int num; HeapType::handle_type handle; Entry() : num(0) {} Entry(int number) : num(number) {} bool operator&lt;(const Entry&amp; rhs) const { return num &lt; rhs.num; } }; struct EntryLocation { int index; std::vector&lt;Entry&gt;* entries; EntryLocation() = delete; EntryLocation(int i, std::vector&lt;Entry&gt;* e) : index(i), entries(e) {} Entry&amp; getEntry() const { return entries-&gt;at(index); } bool operator&lt;(const EntryLocation&amp; rhs) const { return getEntry() &lt; rhs.getEntry(); } }; int main(int argc, char const *argv[]) { int size = 5; auto heap = HeapType(); std::vector&lt;Entry&gt; entries(size); //Create entries for (int i=0; i &lt; size; i++) { entries[i] = Entry(i * 10); EntryLocation location(i, &amp;entries); auto handle = heap.push(location); entries[i].handle = handle; } //Do weird sum int sum = 0; while (!heap.empty()) { auto&amp; location = heap.top(); auto&amp; entry = location.getEntry(); sum += entry.num; std::cout &lt;&lt; "Index: " &lt;&lt; location.index &lt;&lt; " has value: " &lt;&lt; entry.num &lt;&lt; std::endl; //Modify entry to the "left" if (location.index &gt; 0) { auto&amp; leftEntry = entries[location.index - 1]; leftEntry.num += 5; // &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; HERE. Try changing to 15. heap.update(leftEntry.handle); } heap.pop(); } std::cout &lt;&lt; sum &lt;&lt; std::endl; return 0; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>timblechmann</dc:creator> <pubDate>Sun, 05 Apr 2015 20:39:15 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11151#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11151#comment:3</guid> <description> <p> the second one is a user bug: you are modifying an invalid handle </p> </description> <category>Ticket</category> </item> <item> <author>Søren Enevoldsen <senevo10@…></author> <pubDate>Sun, 05 Apr 2015 21:04:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11151#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11151#comment:4</guid> <description> <p> What makes a handle invalid? Can you not modify handles after the corresponding element is removed from the heap? It is the user's responsibility to wrap his elements, with perhaps a boolean flag, to detect when the elements have been removed from the heap, to avoid using a invalid handle. From my first case I thought this was not a problem since I do it there, but now I realize that due to the way dijkstra works, it appears I only touch "live" handles. </p> <p> If that is the case then i'm unsure how to produce a simpler case expressing the first actual bug. My attempts with simple cases that only manipulate the priority of the top node works fine. The bug seems to only manifest itself when there is a significant amount of data, like my 10x10 matrix, and when the priority is changed for elements other than the current (top) one. Do you have any ideas on what kind of algorithmic problem that has these properties but is simpler to write a test case for? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>timblechmann</dc:creator> <pubDate>Sun, 05 Apr 2015 21:41:21 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/11151#comment:5 https://svn.boost.org/trac10/ticket/11151#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> a handle will be invalidated once the object has been removed (pop()) from the heap, as some internal data structures are destroyed. </p> <p> fwiw, i've been able to fix the issue. </p> Ticket