VOID HeapIncreaseValue(PHeap Heap, ULONG HeapIndex, HEAP_VAL_T Inc) { if (Inc == 0) return; Heap->Entries[HeapIndex]->Value += Inc; #ifdef MIN_HEAP HeapSiftDown(Heap, HeapIndex); #else HeapSiftUp(Heap, HeapIndex); #endif }
VOID HeapZeroValue(PHeap Heap, ULONG HeapIndex) { if (Heap->Entries[HeapIndex]->Value) { Heap->Entries[HeapIndex]->Value = 0; #ifdef MIN_HEAP HeapSiftUp(Heap, HeapIndex); #else HeapSiftDown(Heap, HeapIndex); #endif } }
void HeapDelete(LKH::LKHAlg::Node * N,LKH::LKHAlg *Alg) { int Loc = N->Loc; if (!Loc) return; Alg->Heap[Loc] = Alg->Heap[(*HeapCount)--]; Alg->Heap[Loc]->Loc = Loc; if (Alg->Heap[Loc]->Rank > N->Rank) HeapSiftDown(Alg->Heap[Loc],Alg); else HeapSiftUp(Alg->Heap[Loc],Alg); N->Loc = 0; }
VOID HeapDecreaseValue(PHeap Heap, ULONG HeapIndex, HEAP_VAL_T Dec) { if (Heap->Entries[HeapIndex]->Value == 0 || Dec == 0) return; if (Heap->Entries[HeapIndex]->Value > Dec) Heap->Entries[HeapIndex]->Value -= Dec; else Heap->Entries[HeapIndex]->Value = 0; #ifdef MIN_HEAP HeapSiftUp(Heap, HeapIndex); #else HeapSiftDown(Heap, HeapIndex); #endif }
VOID HeapDelete(PHeap Heap, ULONG HeapIndex) { PHeapEntry* h = Heap->Entries; if (Heap->Used == 0) return; Heap->Used--; HEAP_ENTRY_SWAP(h, HeapIndex, Heap->Used); if (HEAP_ENTRY_COMPARE(h, HeapIndex, Heap->Used)) HeapSiftDown(Heap, HeapIndex); else HeapSiftUp(Heap, HeapIndex); _HFREE(Heap->Entries[Heap->Used]); Heap->Entries[Heap->Used] = NULL; }
BOOLEAN HeapInsert(PHeap Heap, HEAP_DAT_T *pData, HEAP_VAL_T Value) { PHeapEntry entry = NULL; if (Heap->Used == Heap->Size) return FALSE; entry = (PHeapEntry)_HMALLOC(sizeof(HeapEntry)); if (entry == NULL) return FALSE; entry->Value = Value; entry->pData = pData; pData->HeapIndex = Heap->Used; Heap->Used++; Heap->Entries[pData->HeapIndex] = entry; HeapSiftUp(Heap, pData->HeapIndex); return TRUE; }
void HeapInsert(LKH::LKHAlg::Node * N,LKH::LKHAlg *Alg) { HeapLazyInsert(N,Alg); HeapSiftUp(N,Alg); }
void LKH::LKHAlg::MinimumSpanningTree(int Sparse) { Node *Blue; /* Points to the last node included in the tree */ Node *NextBlue = 0; /* Points to the provisional next node to be included */ Node *N; Candidate *NBlue; int d; Blue = N = FirstNode; Blue->Dad = 0; /* The root of the tree has no father */ if (Sparse && Blue->CandidateSet) { /* The graph is sparse */ /* Insert all nodes in the heap */ Blue->Loc = 0; /* A blue node is not in the heap */ while ((N = N->Suc) != FirstNode) { N->Dad = Blue; N->Cost = N->Rank = INT_MAX; HeapLazyInsert(N,this); } /* Update all neighbors to the blue node */ for (NBlue = Blue->CandidateSet; (N = NBlue->To); NBlue++) { if (FixedOrCommon(Blue, N)) { N->Dad = Blue; N->Cost = NBlue->Cost + Blue->Pi + N->Pi; N->Rank = INT_MIN; HeapSiftUp(N,this); } else if (!Blue->FixedTo2 && !N->FixedTo2) { N->Dad = Blue; N->Cost = N->Rank = NBlue->Cost + Blue->Pi + N->Pi; HeapSiftUp(N,this); } } /* Loop as long as there are more nodes to include in the tree */ while ((NextBlue = HeapDeleteMin(this))) { Follow(NextBlue, Blue); Blue = NextBlue; /* Update all neighbors to the blue node */ for (NBlue = Blue->CandidateSet; (N = NBlue->To); NBlue++) { if (!N->Loc) continue; if (FixedOrCommon(Blue, N)) { N->Dad = Blue; N->Cost = NBlue->Cost + Blue->Pi + N->Pi; N->Rank = INT_MIN; HeapSiftUp(N,this); } else if (!Blue->FixedTo2 && !N->FixedTo2 && (d = NBlue->Cost + Blue->Pi + N->Pi) < N->Cost) { N->Dad = Blue; N->Cost = N->Rank = d; HeapSiftUp(N,this); } } } } else { /* The graph is dense */ while ((N = N->Suc) != FirstNode) N->Cost = INT_MAX; /* Loop as long as there a more nodes to include in the tree */ while ((N = Blue->Suc) != FirstNode) { int Min = INT_MAX; /* Update all non-blue nodes (the successors of Blue in the list) */ do { if (FixedOrCommon(Blue, N)) { N->Dad = Blue; N->Cost = (this->*D)(Blue, N); NextBlue = N; Min = INT_MIN; } else { if (!Blue->FixedTo2 && !N->FixedTo2 && !Forbidden(Blue, N) && (!c || (this->*c)(Blue, N) < N->Cost) && (d = (this->*D)(Blue, N)) < N->Cost) { N->Cost = d; N->Dad = Blue; } if (N->Cost < Min) { Min = N->Cost; NextBlue = N; } } } while ((N = N->Suc) != FirstNode); Follow(NextBlue, Blue); Blue = NextBlue; } } }