I want to implement a Haskell Dijkstra algorithm from scratch. For this, I wrote a binary heap on a tree .

When writing the algorithm itself, a problem arose: it is necessary for the neighbors of this vertex to update the keys inside the heap .

How can it be done quickly enough in a functional language without looking through the whole heap ?

  • Well, here programmingpraxis.com/2011/01/04/dijkstras-algorithm use Data.Map ... (if I understood the question correctly) - alexlz
  • @alexlz, yes, I saw this code, as the priority queue there is just a list used, this is not the best solution, I want to use a bunch - dima
  • What is a bunch? And what does "just a list" mean? - alexlz
  • one
    @alexlz heap is [heap] [1], usually binary used for Dijkstra. The point is that the extremum is extracted for O (1), and in the list for O (N). Although there are cases where the heap loses (since it must be rebuilt after extraction) ps In Cormen’s book, it is used in pyramid sorting (where it is well described), you can also look at [Yandex School] [2] the 3rd lecture (about the heap) and 13 (Dijkstra "+" and "-" of the selected structure) [1]: ru.wikipedia.org/wiki/… [2]: compscicenter.ru/program/course/algorithms2011Fall - rasmisha
  • one
    @alexlz suffering? what are you talking about? if you are about what I answered, then I can ask why these questions were? And I don’t feel sorry for the minutes of writing (especially this is just a comment), can anyone discover the really interesting lectures of Yandex - rasmisha

1 answer 1

Need a structure supporting the following:

  • add new value for O (logN)
  • find out the minimum element for O (1)
  • remove the minimum element for O (logN)
  • change the value of the item for O (logN)

In addition to the standard heap, we will store the array valueToIndex [v] = index, in which for each vertex v, a pointer / index per element is stored in the heap, where the time value for this vertex is stored.

When you want to change the value at some vertex:

  1. get the link to the element in the heap using the valueToIndex array
  2. change the value
  3. because for dekstra value can only decrease, it is necessary to push up this element by recursion (no more logN lifts)

Also, in all standard heap functions, when changing the location of elements, it is necessary to update the values ​​of the valueToIndex [] array.

If you implement a bunch of non-arrays, then you need to keep a reference to the ancestor.

If some moment is not clear, then sign for more.