/// <summary> /// Swaps two Nodes of array /// </summary> /// <param name="x">index of 1st Node</param> /// <param name="y">index of 2nd Node</param> private void Swap(int x, int y) { HeapNode temp = Heap_array[x]; Heap_array[x] = Heap_array[y]; Heap_array[y] = temp; }
public MinHeap(Graph graph) { this.graph = graph; Capacity = this.graph.Size; Heap_array = new HeapNode[Capacity]; Heap_size = 0; }
/// <summary> /// Insert Node [vertex, score] in heap_array /// Time complexity [log n] where n- size of graph /// </summary> /// <param name="vtx">vertex</param> public void InsertKey(int vtx) { Heap_size++; int i = Heap_size - 1; //add Node to the end of heap array Heap_array[i] = new HeapNode(vtx, graph.Marks[vtx]); //insert it in right(according to min-heap) position //so that any it's child node has bigger Mark //Bubble-Upping node while (i != 0 && Heap_array[Parent(i)].Key > Heap_array[i].Key) { Swap(i, Parent(i)); i = Parent(i); } }