java - Dijkstra with a heap. How to update the heap after relaxation? -
i trying implement dijkstra algorithm.
foreach distance d d = infinity d[source] = 0 create_heap_based_on_distances(); while(true) bestedge = heap[0] remove_minimum_from_heap //it heap[0] foreach adjacency of bestedge if (weight + bestedge_distance < current_distance) { current_distance = weight + bestedge_distance // have update heap, how can that? } if (heap_empty) break
so, in relaxation, how can update heap, have correct order? don't have heap's index node in step. mean have create new array nodes[edge] = heapindex
, heap's index node? seems inefficient need update insert_to_heap
, remove_minimum
functions.
c or java code okay.
does mean have create new array nodes[edge] = heapindex, heap's index node?
yes.
but seems inefficient need update insert_to_heap, remove_minimum functions.
array updates cheap, both in theory , in practice, , have few of per heap operation, not inefficient @ all. also, memory usage of such array cheap compared storage cost of graph data structure.