1 / 3

HW#3: Due Nov 8 23:59

HW#3: Due Nov 8 23:59. NOTE. Submit both hardcopy and softcopy. Verify the max_heapify(int x[],int i,int h_size) b y using CBMC x[] is the array containing a max-heap i is the index to the node that violates the max-heap property h_size is a total number of nodes in the max-heap:

akamu
Download Presentation

HW#3: Due Nov 8 23:59

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HW#3: Due Nov8 23:59 NOTE. Submit both hardcopy and softcopy. Verify the max_heapify(int x[],int i,int h_size) by using CBMC • x[] is the array containing a max-heap • i is the index to the node that violates the max-heap property • h_size is a total number of nodes in the max-heap: Assumptions 1. The right and left sub-treesof node i are max heaps, but that x[i]may be smaller than its children 2. the max heap has less than 8 elements 3. For the sake of avoiding the state explosion, you do not have to check if all elements of the subtree rooted at node iexist in the subtree after max_heapify(). To do list: • Describe your environment model in detail • Describe your assertion check routine in detail • Describe run-time parameters of CBMC • Report verification results (i.e., time, memory, assert violation, size of generated SAT formula, etc) • (Extra point) Assume that the max heap has less than 128 elements • Use different loop bounds for differernt loop using –unwindset • Ex. cbmc --unwind 7 --unwindset c::f.0:128,c::main.0:128 max-heap.c

  2. A max heap is a heap data structure created using a binary tree. It can be seen as a binary tree with two additional constraints: • The shape property: the tree is a complete binary tree; that is, all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right. • The max-heap property: each node is greater than or equal to each of its children according to a comparison predicate defined for the data structure. Max heap can be implemented using an array as follows (note that array index starts from 1):

  3. int main(){ int i; max_heapify(a,2,H_SIZE); for (i=1;i<=H_SIZE;i++) printf("%d ",a[i]); return 0; } /* Output: 16 14 10 8 7 9 3 2 4 1 */ max_heapify(a,2,10) /* Example code */ #include<stdio.h> #define MAX 16 #define H_SIZE 10 #define parent(i)(i/2) #define left(i) (2*i) #define right(i)(2*i+1) /* Ignore the first 0, since max heapcontents start at index 1 */ int a[MAX] = {0,16,4,10,14,7,9,3,2,8,1,}; void max_heapify(int x[],int i,int h_size){ int largest, tmp; int l=left(i); int r=right(i); if (l<=h_size && x[l]>x[i]) largest=l; else largest=i; if(r<=h_size && x[r]>x[largest]) largest=r; if (largest!=i) { tmp=x[i]; x[i]=x[largest]; x[largest]=tmp; max_heapify(x,largest,h_size); } } 1 16 3 2 4 10 6 7 4 5 14 7 9 3 8 9 10 2 8 1 1 16 3 2 14 10 6 7 4 5 4 7 9 3 8 9 10 2 8 1 1 16 3 2 14 10 6 7 4 5 8 7 9 3 8 9 10 2 4 1

More Related