// This function inserts array elements to heap and then calls // buildHeap for heap property among nodes void populateMinHeap( MinHeap* minHeap, ListNode* *array, int n ) { for( int i = 0; i < n; ++i ) minHeap->array[ minHeap->count++ ].head = array[i]; buildMinHeap( minHeap ); }
void dijkstra(ALGraph G,int s,int d[],int pi[],int Q[]) { //Q[]是最小优先队列,Q[1..n]中存放的是图顶点标号,Q[0]中存放堆的大小 //优先队列中有key的概念,这里key可以从d[]中取得。比如说,Q[2]的大小(key)为 d[ Q[2] ] initSingleSource(G,s,d,pi); //1、初始化结点工作 //2、初始化队列 Q[0] = G.vexnum; int i=1; for(;i<=Q[0];i++) { Q[i] = i-1; } Q[1] = s; Q[s+1] = 0; int u; int v; while(Q[0]!=0) { buildMinHeap(Q,d); //3.1、建立最小堆 u = extractMin(Q,d); //3.2、从最小队列中,抽取最小结点 ArcNode* arcNodePt = G.vertices[u].firstarc; while(arcNodePt!=NULL) { v = arcNodePt->adjvex; relax(u,v,G,d,pi); //4、松弛操作。 arcNodePt = arcNodePt->nextarc; } } }
// MinHeap constructor MinHeap::MinHeap(HuffmanNode **nodes, int length) { heapSize = length; this->heapNodes = new HuffmanNode *[length]; for (int i = 0; i < length; i++) this->heapNodes[i] = nodes[i]; buildMinHeap(); }
void dijkstra(int start,int size) { minHeap* h = (minHeap*)malloc(sizeof(minHeap)); h->size = 0; h->array[0] = unused; dist[start] = 0; int count = 0; preDist[start] = 0; for (int i = 1; i <= size; i++) { push(h, i); } while (!isEmpty(h)) { int top = pop(h); printf("------------------------------------------------------\n"); printf("S[%d] : d[%c] = %d\n", count,printVertex(top), dist[top]); count++; printf("------------------------------------------------------\n"); for (int i = 1; i <= size; i++) { if (graph[top-1][i-1]!=inf) { //연결 if (dist[i] > dist[top] + graph[top-1][i-1]) { dist[i] = dist[top] + graph[top-1][i-1]; buildMinHeap(h); } } } printDist(h); for(int i=1; i<=size; i++){ preDist[i] = dist[i]; } } }
// Creates a min heap of capacity equal to size and inserts all character of // data[] in min heap. Initially size of min heap is equal to capacity struct MinHeap* createAndBuildMinHeap(char data[], int freq[], int size) { struct MinHeap* minHeap = createMinHeap(size); for (int i = 0; i < size; ++i) minHeap->array[i] = newNode(data[i], freq[i]); minHeap->size = size; buildMinHeap(minHeap); return minHeap; }
int pop(minHeap* h) { if (isEmpty(h))return 0; int top = h->array[1]; swap(&h->array[1],&h->array[h->size] ); h->array[h->size] = inf; h->size--; buildMinHeap(h); return top; }
void heapSort(int * array, size_t length) { int * array_copy = malloc(sizeof(int) * length); for (size_t i = 0; i < length; ++i) { array_copy[i] = array[i]; } Heap heap = {array_copy, length}; buildMinHeap(&heap); for (size_t i = 0; i < length; ++i) {; array[i] = popHeap(&heap); } free(array_copy); }
int main() { initializeHeap(heap); initializeValues(); buildMinHeap(); printValues(); //printf("min: %d \n",heapExtractMin()); //heapDecreaseKey(2,0); //minHeapInsert(0); //printValues(); return 0; }
void heapsort(int length) { int temp,i; buildMinHeap(length); int size=length; for(i=length-1;i>=1;i--) { temp=array[0]; array[0]=array[i]; array[i]=temp; size=size-1; minHeapify(0,size); } }
int main() { int ch,a[100], p, n, i, pos; printf("\nEnter the number of elements "); scanf("%d",&n); numm=n; for(i=1;i<=n;i++) scanf("%d",&a[i]); buildMinHeap(a, numm); printf("\n1.See\n2.extract\n3.insert\n4.update\n5.exit "); while(1) { printf("\nEnter ur choice "); scanf("%d",&ch); switch(ch) { case 1: see(a, numm); break; case 2: printf("\nthe min element was %d \n",extractMin(a)); break; case 3: printf("\nEnter node to insert "); scanf("%d",&i); insert(a,i); break; case 4: printf("\nEnter the index to update "); scanf("%d",&p); printf("nEnter the node key value "); scanf("%d",&i); update(a, p, i); break; case 5: return (0); } } return 0; }
// Inserts a word to heap, the function handles the 3 cases explained above void insertInMinHeap( MinHeap* minHeap, TrieNode** root, const char* word ) { // Case 1: the word is already present in minHeap if( (*root)->indexMinHeap != -1 ) { ++( minHeap->array[ (*root)->indexMinHeap ]. frequency ); // percolate down minHeapify( minHeap, (*root)->indexMinHeap ); } // Case 2: Word is not present and heap is not full else if( minHeap->count < minHeap->capacity ) { int count = minHeap->count; minHeap->array[ count ]. frequency = (*root)->frequency; minHeap->array[ count ]. word = new char [strlen( word ) + 1]; strcpy( minHeap->array[ count ]. word, word ); minHeap->array[ count ]. root = *root; (*root)->indexMinHeap = minHeap->count; ++( minHeap->count ); buildMinHeap( minHeap ); } // Case 3: Word is not present and heap is full. And frequency of word // is more than root. The root is the least frequent word in heap, // replace root with new word else if ( (*root)->frequency > minHeap->array[0]. frequency ) { minHeap->array[ 0 ]. root->indexMinHeap = -1; minHeap->array[ 0 ]. root = *root; minHeap->array[ 0 ]. root->indexMinHeap = 0; minHeap->array[ 0 ]. frequency = (*root)->frequency; // delete previously allocated memoory and delete [] minHeap->array[ 0 ]. word; minHeap->array[ 0 ]. word = new char [strlen( word ) + 1]; strcpy( minHeap->array[ 0 ]. word, word ); minHeapify ( minHeap, 0 ); } }
// Creates a min heap of capacity equal to size and inserts all character of // data[] in min heap. Initially size of min heap is equal to capacity struct MinHeap* createAndBuildMinHeap(uint16 freq[], uint16 size) { struct MinHeap* minHeap = createMinHeap(size); if (minHeap == NULL) { hError |= (1 << 3); return NULL; } for (uint16 i = 0; i < size; ++i) { minHeap->array[i] = newNode((uint8)(i & 0xFF), freq[i]); if (minHeap->array[i] == NULL) { hError |= (1 << 4); return NULL; } } minHeap->size = size; buildMinHeap(minHeap); return minHeap; }
int kthLargest(int a[], int size, int k) { int minHeap[k]; int i; for(i = 0; i < k; i++) minHeap[i] = a[i]; buildMinHeap(minHeap, k); for(i = k; i < size; i++) { if(a[i] > minHeap[0]) { minHeap[0] = a[i]; minHeapify(minHeap, k, 0); } } return minHeap[0]; }
/* Driver program to test above functions*/ int main() { printf("Hello World\n"); int *try; try=(int *)malloc(sizeof(int)*10); try[0]=4; try[1]=1; try[2]=3; try[3]=2; try[4]=16; try[5]=9; try[6]=10; try[7]=14; try[8]=8; try[9]=7; struct heap* H1; H1=initHeap(H1,try,10,10); //heapify(H1,2); printHeap(H1); H1=buildMinHeap(H1); printHeap(H1); heapSort(H1); //int max=heapExtractMax(H1); //printf("\nmax:%d\n",max); printHeap(H1); H1=minHeapInsert(H1,5); printHeap(H1); return 0; }
int main(int argc, char* argv[]) { //memory allocation could be better. could allocate as needed later in processing? could make loop to keep from //hand coding all the following steps. //initialize big matrix to store vectors if (!vorticities) { vorticities = (double *)malloc(3 * nx * ny * nz * sizeof(double)); } if (!vorticities) { fprintf(stderr, "Out of memory: malloc(vorticities) failed.\n"); exit(1); } if ((unsigned int)vorticities % 8) { fprintf(stderr, "Vorticities array is not qword aligned.\n"); exit(1); } numDist = (localLevel*2+1); //cube this for number to add. if (!distances) { distances = (double *)malloc(numDist*numDist*numDist * nx * ny * nz * sizeof(double)); } if (!distances) { fprintf(stderr, "Out of memory: malloc(distances) failed.\n"); exit(1); } if ((unsigned int)distances % 8) { fprintf(stderr, "Distances array is not qword aligned.\n"); exit(1); } if (!alphaSegs) { alphaSegs = (int *)malloc(3 * (numDist*numDist*numDist-1) * nx * ny * nz * sizeof(int)); } if (!alphaSegs) { fprintf(stderr, "Out of memory: malloc(alphaSegs) failed.\n"); exit(1); } if ((unsigned int)alphaSegs % 8) { fprintf(stderr, "alphaSegs array is not qword aligned.\n"); exit(1); } if (!alphaDists) { alphaDists = (double *)malloc( ((numDist*numDist*numDist-1) * nx * ny * nz * sizeof(double))/2); } if (!alphaDists) { fprintf(stderr, "Out of memory: malloc(alphaDists) failed.\n"); exit(1); } if ((unsigned int)alphaDists % 8) { fprintf(stderr, "alphaDists array is not qword aligned.\n"); exit(1); } if (!unionSets) { unionSets = (Parent *)malloc( nx * ny * nz * sizeof(Parent)); } if (!unionSets) { fprintf(stderr, "Out of memory: malloc(unionSets) failed.\n"); exit(1); } if ((unsigned int)unionSets % 8) { fprintf(stderr, "unionSets array is not qword aligned.\n"); exit(1); } if (!graphNodes) { graphNodes = (Node *)malloc( nx * ny * nz * sizeof(Node)); } if (!graphNodes) { fprintf(stderr, "Out of memory: malloc(graphNodes) failed.\n"); exit(1); } if ((unsigned int)graphNodes % 8) { fprintf(stderr, "graphNodes array is not qword aligned.\n"); exit(1); } if (argc >= 3) { maxAlpha = atof(argv[3]); numComponents = atoi(argv[4]); } //hardcoded file input FILE *inputFile = fopen(argv[1], "r"); FILE *outputFile = fopen(argv[2], "w"); readVorticitiesFile(vorticities, inputFile); fclose(inputFile); //calculate distances from nearby points to other nearby points calcAppAniDel(vorticities, distances); //average the distances calculated int sizeHeap = averageStoreDistances(distances, alphaSegs, alphaDists, vorticities); //free this memory now. free(vorticities); free(distances); //put the distances into a min-heap in-place buildMinHeap(alphaDists, alphaSegs, sizeHeap); MakeSets(unionSets); //initialize InitGraph(graphNodes); //now we call the big function that builds cycles--need vorticities to determine orientation buildCycles(alphaDists, alphaSegs, sizeHeap, unionSets, graphNodes, outputFile, numComponents); fclose(outputFile); free(alphaDists); free(alphaSegs); deleteGraph(graphNodes); free(unionSets); }
MinHeap(std::vector<Node> elements) : heapArray(elements) { buildMinHeap(); };
void push(minHeap* h, int x) { h->array[++h->size]= x; buildMinHeap(h); }