int main() { int N = 0; int i = 0; int cmd = 0; int cur = 0; freopen("heap.in", "rt", stdin); freopen("heap.out", "wt", stdout); scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d", &cmd); switch (cmd) { case 0: scanf("%d", &cur); heap_add(cur); break; case 1: assert(size > 0); printf("%d\n", heap_extract_max()); break; } } //fcloseall(); return 0; }
/****************************************************************************** * MAIN ******************************************************************************/ int main(int argc, char * argv[]) { int A[MAX_NUM_NR]; char * filename_unsorted = get_filename(argc, argv); char * filename_sorted = get_sorted_filename(filename_unsorted); int n = get_A(A, MAX_NUM_NR, filename_unsorted); print_A(A, n, "original:"); heap_sort(A, n); print_A(A, n, "sorted:"); assert(verify_A(A, n, filename_sorted)); /* priority queue */ int heapsize = n; build_max_heap(A, heapsize); ______________________________(A, n, heapsize, "===============\\nafter build_max_heap\\n==============="); printf("heap_maximum: %d\n", heap_maximum(A, heapsize)); printf("heap_extract_max: %d\n", heap_extract_max(A, &heapsize)); print_A(A, heapsize, "after heap_extract_max"); ______________________________(A, n, heapsize, "===============\\nafter heap_extract_max\\n==============="); heap_increase_key(A, heapsize, 3, 88); print_A(A, heapsize, "after heap_increase_key"); ______________________________(A, n, heapsize, "===============\\nafter heap_increase_key\\n==============="); max_heap_insert(A, n, &heapsize, 97); print_A(A, heapsize, "after max_heap_insert"); ______________________________(A, n, heapsize, "===============\\nafter max_heap_insert\\n==============="); return 0; }
void main(void) { int i; int arr[] = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7}; int *heap_size = malloc(sizeof(int)); *heap_size = sizeof(arr) / sizeof(int) - 1; HEAP A = {arr, heap_size}; build_max_heap(A); heap_increase_key(A, 8, 15); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n%d\n", *(A.heap_size)); heap_extract_max(A); printf("%d\n", *(A.heap_size)); printf("\n"); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); A = max_heap_insert(A, 16); printf("%d\n", *(A.heap_size)); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); A = heap_delete(A, 3); for (i = 0; i <= *(A.heap_size); i++) printf("%d\t", A.arr[i]); printf("\n"); }
int main(){ int i, size = 9; int t[] = {63, 73, 25, 19, 44, 2, 59, 38, 10}; printf("Array before creating priority queue:"); for (i = 0; i < size; i++) printf("%d\t",t[i]); build_maxheap(t,size-1); printf("\nNewly built Priority queue : "); for (i = 0; i < size; i++) printf("%d\t",t[i]); i = heap_extract_max(t, size-1); size--; printf("\nExtract maximum:%d\n",i); printf("Priority queue after extract maximum: "); for (i = 0; i < size; i++) printf("%d\t",t[i]); printf("\nEnter a number to be inserted into the priority queue:"); scanf("%d", &i); max_heap_insert(t, i, size-1); size++; printf("Priority queue after insert: "); for (i = 0; i < size; i++) printf("%d\t",t[i]); printf("\n"); return 0; }
// Pop a plan location from the queue plan_cell_t *plan_pop(plan_t *plan) { if(heap_empty(plan->heap)) return(NULL); else return(heap_extract_max(plan->heap)); }
void heap_free(heap_t* h) { if(h->free_fn) { while(!heap_empty(h)) (*h->free_fn)(heap_extract_max(h)); } free(h->data); free(h->A); free(h); }
void connect_enforce(struct vtx_data **graph, /* data structure for graph */ int nvtxs, /* number of vertices in full graph */ int using_ewgts, /* are edge weights being used? */ int * assignment, /* set number of each vtx (length n) */ double * goal, /* desired sizes for each set */ int nsets_tot, /* total number sets created */ int * total_move, /* total number of vertices moved */ int * max_move /* largest connected component moved */ ) { struct vtx_data **subgraph; /* data structure for domain graph */ int subnvtxs; /* number of vertices in a domain */ int subnedges; /* number of edges in a domain */ struct heap * heap; /* data structure for sorting set sizes */ int * heap_map; /* pointers from sets to heap locations */ int * list_ptrs; /* header of vtx list for each domain */ int * setlists; /* linked list of vtxs for each domain */ int * vtxlist; /* space for breadth first search list */ int * comp_lists; /* list of vtxs in each connected comp */ int * clist_ptrs; /* pointers to heads of comp_lists */ int * subsets; /* list of active domains (all of them) */ int * subsets2; /* list of active domains (all of them) */ double * set_size; /* weighted sizes of different partitions */ double size; /* size of subset being moved to new domain */ int * bndy_list; /* list of domains adjacent to component */ double * bndy_size; /* size of these boundaries */ double * comp_size; /* sizes of different connected components */ double comp_size_max; /* size of largest connected component */ int comp_max_index = 0; /* which component is largest? */ int * glob2loc; /* global to domain renumbering */ int * loc2glob; /* domain to global renumbering */ int * degree; /* number of neighbors of a vertex */ int * comp_flag; /* component number for each vtx */ double ewgt; /* edge weight */ int nbndy; /* number of sets adjecent to component */ int domain; /* which subdomain I'm working on */ int new_domain; /* subdomain to move some vertices to */ double max_bndy; /* max connectivity to other domain */ int ncomps; /* number of connected components */ int change; /* how many vertices change set? */ int max_change; /* largest subset moved together */ int vtx; /* vertex in a connected component */ int set; /* set a neighboring vertex is in */ int i, j, k, l; /* loop counters */ double heap_extract_max(); int make_maps(), find_comps(); void make_setlists(), make_subgraph(), remake_graph(); void heap_build(), heap_update_val(); change = 0; max_change = 0; /* Allocate space & initialize some values. */ set_size = smalloc(nsets_tot * sizeof(double)); bndy_size = smalloc(nsets_tot * sizeof(double)); bndy_list = smalloc(nsets_tot * sizeof(int)); setlists = smalloc((nvtxs + 1) * sizeof(int)); list_ptrs = smalloc(nsets_tot * sizeof(int)); glob2loc = smalloc((nvtxs + 1) * sizeof(int)); loc2glob = smalloc((nvtxs + 1) * sizeof(int)); subsets = smalloc(nsets_tot * sizeof(int)); heap = (struct heap *)smalloc((nsets_tot + 1) * sizeof(struct heap)); heap_map = smalloc(nsets_tot * sizeof(int)); for (i = 0; i < nsets_tot; i++) { set_size[i] = 0; bndy_list[i] = 0; bndy_size[i] = 0; subsets[i] = i; } for (i = 1; i <= nvtxs; i++) { set_size[assignment[i]] += graph[i]->vwgt; } for (i = 0; i < nsets_tot; i++) { heap[i + 1].tag = i; heap[i + 1].val = set_size[i] - goal[i]; } make_setlists(setlists, list_ptrs, nsets_tot, subsets, assignment, NULL, nvtxs, TRUE); heap_build(heap, nsets_tot, heap_map); for (i = 0; i < nsets_tot; i++) { /* Find largest remaining set to work on next */ size = heap_extract_max(heap, nsets_tot - i, &domain, heap_map); /* Construct subdomain graph. */ subnvtxs = make_maps(setlists, list_ptrs, domain, glob2loc, loc2glob); if (subnvtxs > 1) { subgraph = (struct vtx_data **)smalloc((subnvtxs + 1) * sizeof(struct vtx_data *)); degree = smalloc((subnvtxs + 1) * sizeof(int)); make_subgraph(graph, subgraph, subnvtxs, &subnedges, assignment, domain, glob2loc, loc2glob, degree, using_ewgts); /* Find connected components. */ comp_flag = smalloc((subnvtxs + 1) * sizeof(int)); vtxlist = smalloc(subnvtxs * sizeof(int)); ncomps = find_comps(subgraph, subnvtxs, comp_flag, vtxlist); sfree(vtxlist); /* Restore original graph */ remake_graph(subgraph, subnvtxs, loc2glob, degree, using_ewgts); sfree(degree); sfree(subgraph); if (ncomps > 1) { /* Figure out sizes of components */ comp_size = smalloc(ncomps * sizeof(double)); for (j = 0; j < ncomps; j++) { comp_size[j] = 0; } for (j = 1; j <= subnvtxs; j++) { comp_size[comp_flag[j]] += graph[loc2glob[j]]->vwgt; } comp_size_max = 0; for (j = 0; j < ncomps; j++) { if (comp_size[j] > comp_size_max) { comp_size_max = comp_size[j]; comp_max_index = j; } } for (j = 0; j < ncomps; j++) { if (j != comp_max_index) { change += comp_size[j]; if (comp_size[j] > max_change) max_change = comp_size[j]; } } sfree(comp_size); /* Make data structures for traversing components */ comp_lists = smalloc((subnvtxs + 1) * sizeof(int)); clist_ptrs = smalloc(ncomps * sizeof(int)); if (ncomps > nsets_tot) { subsets2 = smalloc(ncomps * sizeof(int)); for (j = 0; j < ncomps; j++) subsets2[j] = j; } else subsets2 = subsets; make_setlists(comp_lists, clist_ptrs, ncomps, subsets2, comp_flag, NULL, subnvtxs, TRUE); if (ncomps > nsets_tot) { sfree(subsets2); } /* Move all but the largest component. */ ewgt = 1; for (j = 0; j < ncomps; j++) if (j != comp_max_index) { /* Figure out to which other domain it is most connected. */ nbndy = 0; k = clist_ptrs[j]; while (k != 0) { vtx = loc2glob[k]; for (l = 1; l <= graph[vtx]->nedges; l++) { set = assignment[graph[vtx]->edges[l]]; if (set != domain) { if (bndy_size[set] == 0) bndy_list[nbndy++] = set; if (using_ewgts) ewgt = graph[vtx]->ewgts[l]; bndy_size[set] += ewgt; } } k = comp_lists[k]; } /* Select a new domain. */ /* Instead of just big boundary, penalize too-large sets. */ /* Could be more aggressive to improve balance. */ max_bndy = 0; new_domain = -1; for (k = 0; k < nbndy; k++) { l = bndy_list[k]; if (bndy_size[l] * goal[l] / (set_size[l] + 1) > max_bndy) { new_domain = bndy_list[k]; max_bndy = bndy_size[l] * goal[l] / (set_size[l] + 1); } } if (new_domain == -1) { printf("Error in connect_enforce: new_domain = -1. Disconnected graph?\n"); new_domain = domain; } /* Clear bndy_size array */ for (k = 0; k < nbndy; k++) bndy_size[bndy_list[k]] = 0; k = clist_ptrs[j]; size = 0; while (k != 0) { vtx = loc2glob[k]; assignment[vtx] = new_domain; size += graph[vtx]->vwgt; /* Finally, update setlists and list_ptrs */ /* Note: current domain setlist now bad, but not used again */ setlists[vtx] = list_ptrs[new_domain]; list_ptrs[new_domain] = vtx; k = comp_lists[k]; } /* printf("Updating set %d (from %d) to size %g\n", new_domain, domain, set_size[new_domain] + size - goal[new_domain]); */ if (heap_map[new_domain] > 0) { set_size[new_domain] += size; heap_update_val(heap, heap_map[new_domain], set_size[new_domain] - goal[new_domain], heap_map); } } sfree(clist_ptrs); sfree(comp_lists); } sfree(comp_flag); } } sfree(heap_map); sfree(heap); sfree(subsets); sfree(loc2glob); sfree(glob2loc); sfree(list_ptrs); sfree(setlists); sfree(bndy_list); sfree(bndy_size); sfree(set_size); *total_move = change; *max_move = max_change; }
int main(int argc, char const *argv[]) { // int tid; /* thread id */ struct timeval tt; float e; long tsec, tusec; node max_local, sub_local1, sub_local2; /* initialize global state */ TOTALRESULT = 0.0; TOTALERROR = 0.0; int iter = 1; /* retrieve the start computing time */ gettimeofday(&tt, 0); tsec = tt.tv_sec; tusec = tt.tv_usec; /* create the binary heap */ binary_heap h; heap_initialize(&h, 2*ITMAX); /* give the binary heap a root node */ node root = node_create(0, 1); rule(smooth, &root); update(root, addition); heap_insert(&h, root); // heap_print(h); #ifdef _OPENMP /* set the number of threads */ omp_set_num_threads(8); #pragma omp parallel shared(h, iter, TOTALRESULT, TOTALERROR) private(max_local, sub_local1, sub_local2) #endif { while ((TOTALERROR > ABSREQ) && (iter < ITMAX)) { #ifdef _OPENMP #pragma omp critical (retrive_max) #endif { /* this section should be a critical section, * because each time only one thread can access * the binary heap */ #ifdef _OPENMP // printf("Current thread %d is handling the heap, and iteration is %d.\n", // omp_get_thread_num(), iter); #endif max_local = heap_extract_max(&h); // if (max_local.result == 0.0) continue; // node_print(max_local); } /* divide the max error region into two small regions */ sub_local1 = node_create(max_local.start, (max_local.start + max_local.end) / 2.0); sub_local2 = node_create((max_local.start + max_local.end) / 2.0, max_local.end); #ifdef _OPENMP #pragma omp critical (state_update) #endif { /* critical section, only one thread can access * the global state each time */ update(max_local, subtraction); } /* apply the function, update the result and error */ rule(smooth, &sub_local1); rule(smooth, &sub_local2); #ifdef _OPENMP #pragma omp critical (heap_update) #endif { /* critical section, only one thread can access * the global state, binary heap * and variable iter each time */ update(sub_local1, addition); update(sub_local2, addition); heap_insert(&h, sub_local1); heap_insert(&h, sub_local2); iter++; } } } // heap_print(h); /* get the current time, representing the end computing time */ gettimeofday(&tt, 0); e = (float) (tt.tv_sec - tsec) + (float) (tt.tv_usec - tusec) / 1000000; printf("It took %g seconds to get the final result %.15f\n", e, TOTALRESULT); heap_finalize(&h); printf("total error is: %.40f, and number of iteration is: %d\n", TOTALERROR, iter); return 0; }
int main() { printf("----------------------Heap---------------------\n"); printf("\t0. max_heapify(heap_t*, int)\n"); printf("\t1. build_max_heap(heap_t*)\n"); printf("\t2. heap_extract_max(heap_t*)\n"); printf("\t3. heap_sort(heap_t*)\n"); printf("\t4. heap_increase_key(heap_t*, int, int)\n"); printf("\t5. heap_insert(heap_t*, int)\n"); printf("Please select (-1 to quit): "); int select; scanf("%d", &select); if(select == -1) { exit(0); } while(select != -1) { heap_t heap; int length; int heap_size; printf("Now create a heap\n"); printf("Enter heap length: "); scanf("%d",&length); printf("Enter heap size: "); scanf("%d",&heap_size); printf("Enter elements: \n"); int items[length]; for(int i = 0; i < heap_size; i++) { scanf("%d",&items[i]); } heap.length = length; heap.heap_size = heap_size; heap.elements = items; switch(select) { case 0: printf("Please enter index you want to heapify: "); int index; scanf("%d",&index); max_heapify(&heap, index); break; case 1: build_max_heap(&heap); break; case 2: build_max_heap(&heap); heap_extract_max(&heap); break; case 3: build_max_heap(&heap); heap_sort(&heap); break; case 4: build_max_heap(&heap); printf("Please enter index you want to increase: "); int i = getchar(); printf("Please enter key: "); int key = getchar(); heap_increase_key(&heap, i, key); break; case 5: build_max_heap(&heap); printf("Please enter elements you want to insert: "); int element = getchar(); heap_insert(&heap,element); break; default: printf("Num error."); } for(int i = 0; i < heap.length; i++) { printf("%d ", heap.elements[i]); } printf("\n\n"); printf("\t0. max_heapify(heap_t*, int)\n"); printf("\t1. build_max_heap(heap_t*)\n"); printf("\t2. heap_extract_max(heap_t*)\n"); printf("\t3. heap_sort(heap_t*)\n"); printf("\t4. heap_increase_key(heap_t*, int, int)\n"); printf("\t5. heap_insert(heap_t*, int)\n"); printf("Please select (-1 to quit): "); scanf("%d",&select); } printf("----------------------End---------------------\n"); return 0; }