代做15-122: Principles of Imperative Computation — Spring 2022 Final Exam代做Java程序

15-122: Principles of Imperative Computation — Spring 2022

Final Exam

Tuesday 6th December, 2022

1 Is this a Tree? [C0] (35 points)

This question is about binary trees containing integer data, defined as follows:

typedef struct tree_node tree;

struct tree_node {

int data;

tree* left;

tree* right;

};

As usual, the empty tree is represented by the NULL pointer. The data in a tree are not neces-sarily ordered, and there may be duplicates.

Task 1.1

To warm up, write a recursive C0 function that returns the number of elements in a tree.

You may assume that the tree contains fewer than int_max() elements.

int tree_size(tree* T)

//@ensures \result >= 0;

{

}

The function tree_size above impose no requirement on its input! In particular, the tree T could be some horrible pointer mess like the following:

In class, we saw a most naive representation invariant on trees that did not consider the pointer structure. In the next few tasks, we will implement a specification function

bool is_tree(tree* T);

that returns true on well-formed trees, like the ones seen in class as well as the ones rooted at node 5 and at node 10 in the above example, but rejects (i.e., returns false on) ill-formed trees (like the ones rooted at node 3 or at node 7).

We will do so by traversing the tree (i.e., visiting each of its nodes) and, along the way, storing the pointers we encounter in a hash set. As we examine a new pointer, we first check if it is in the set, adding it only if we never saw it before.

Task 1.2

The next two tasks ask you to flesh out this approach in two specific situation. For each, state what the function should do (in term of its return value and/or how it manipulates the hash set, as appropriate) and why.

a. What should is_tree do when called on a pointer that is not in the set?

It should

because

b. What should is_tree do when called on a pointer that is already in the set?

It should

because

c. What should is_tree do when called on a NULL pointer?

It should

because

Recall that a hash set is a set library implemented using hash tables. Here is the interface of a semi-generic C0 self-resizing hash set library, as discussed in class and the lecture notes:

/******** Client interface ********/

// typedef _______ elem;

bool elem_equiv(elem x, elem y);

int elem_hash(elem x);

/******** Library interface ********/

// typedef ______* hset_t;

hset_t hset_new(int capacity)

/*@requires capacity > 0; @*/

/*@ensures \result != NULL; @*/ ;

bool hset_contains(hset_t H, elem x)

/*@requires H != NULL; @*/ ;

void hset_add(hset_t H, elem x)

/*@requires H != NULL; @*/

/*@ensures hset_contains(H, x); @*/ ;

Task 1.3

As a client of this C0 library, define the type elem of the data we will want to insert in the hash set so that we can use it to check if a tree has a valid pointer structure.

typedef                                                                                elem;

Task 1.4

Define the function elem_equiv that determines when two set elements are the same.

bool elem_equiv(elem e1, elem e2)

//@requires e1 != NULL && e2 != NULL;

{

}

Task 1.5

The hash function we will use returns the (integer) value at the root of its input tree:

int elem_hash(elem T)

//@requires T != NULL;

{

return T->data;

}

Assume the hash table underlying the hash set library implementation uses separate chaining to resolve collisions. If it contains n entries and the table has capacity m, can we count on all chains having length about n/m? Why or why not?

⃝   Yes                  ⃝ No            ecause

Task 1.6

Complete the recursive function has_good_pointers, to be called within is_tree, which checks if the tree T has a valid pointer structure with the help of the hash set H, used to store pointers that have already been visited. Include contracts as needed. (You may not need all lines provided.)

bool has_good_pointers(tree* T, hset_t H)

//@                                           ;

{

if (                                  ) return                       ;

if (                                  ) return                       ;

}

Task 1.7

Write the function is_tree that checks that its argument has a valid pointer structure, using has_good_pointers as a helper function.

bool is_tree(tree* T) {

}

Task 1.8

Assume is_tree(T) returns true. What is the asymptotic runtime complexity of this call in terms of the number n of elements in the tree in the best and worst case? As you answer this question, consider the functions elem_equiv and elem_hash as implemented earlier.

Best case:             O(          ) when

Worst case:           O(          ) when

2 A Heap for Every Occasion [C1] (55 points)

Consider a priority queue implementation based on heaps as seen in class. The heaps on this page contain characters, where a character that comes later in the alphabet (like ’Z’) is considered to have higher priority than a character that comes earlier (like ’A’).

Task 2.1

Given the following array with char values representing a heap, draw the corresponding tree representation and circle every parent-child relation where the heap ordering invari-ant is not satisfied. This heap has 12 elements.

Task 2.2

Given the following heap, draw the tree we get after we add ’J’ to it. Your tree should satisfy the heap invariants.

Task 2.3

Given the following heap, draw the tree that we get after we remove the maximum element. Your tree should satisfy the heap invariants.

In the remaining tasks of this question, you will be the client of a priority queue library with the same interface seen in class, extended with the function pq_size which returns the number of elements in a priority queue. This interface is reported on page 30 of this exam. This priority queue library may not be implemented using heaps. All the code you will need to write in this question is in C1.

Task 2.4

There is an easy way to sort an array if we have a priority queue: insert all its elements into the priority queue and then empty out the priority queue back into the array. This last phase will retrieve the inserted elements in order.

Complete the code for the function sort that uses a priority function cmp to sort the input array A from highest to lowest priority. The specification function is_sorted(A,lo,hi,cmp) checks if the array segment A[lo,hi) is indeed sorted in this manner. The specification function ge_seg_pq(A,lo,hi, Q, cmp) checks that every element in the array segment A[lo,hi) has priority greater than or equal to the priority of every element in the priority queue Q according to cmp. The missing loop invariant on line 19 should be valid and it should allow you to prove the correctness of this code (in a later task).

1 void sort(void*[] A, int n, has_higher_priority_fn* cmp)

2 //@requires n == \length(A);

3 //@requires cmp != NULL;

4 //@ensures is_sorted(A, 0, n, cmp);

5 {

6 pq_t Q =                               ;

7

8 // Store the elements in the priority queue

9

10

11

12

13

14

15

16 // Retrieve the sorted elements and put them back in the array

17 for (int i = 0; i < n; i++)

18

19 //@loop_invariant                                   ;

20 //@loop_invariant ge_seg_pq(A, 0, i, Q, cmp);

21 //@loop_invariant is_sorted(A, 0, i, cmp);

22 {

23 //@assert !pq_empty(Q);

24

25 void* x =                             ;

26 A[i] = x;

27 }

28 }

Task 2.5

Assume that the function cmp runs in constant time. What is the worst-case asymptotic complexity of the call sort(A,n,cmp)?

O(                       )

Task 2.6

Assume that you have already shown that the loop invariants on lines 19–21 are valid and that the loop terminates. Prove that this function is correct, i.e., that the postcondition holds when it returns. (You may not need all the lines provided.)

To show:

a                                     by

b                                     by

c                                      by

d                                      by

e                                      by

f                                       by

Task 2.7

But, let’s prove nonetheless that the loop invariant on line 21 is preserved by an arbitrary iteration of the loop. As you do so, you may assume that the other loop invariants for this loop are valid. You may use other specification functions from the arrayutil library by passing cmp as an additional parameter (for example, gt_seg(x, A,lo,hi, cmp) to indicate that x has higher priority than every element in A[lo,hi) according to cmp). (You may not need all the lines provided.)

Assume:

To show:

a                                 by

b                                 by

c                                 by

d                                 by

e                                 by

f                                  by

g                                  by

Task 2.8

We know how to return the element with highest priority out of a priority queue. Now, let’s find the element with the k th highest priority (if k is 1, it returns an element with the highest priority). Complete the implementation of the function k_priority(Q,k) that returns the k th priority element in priority queue Q. On return, Q should contain the same elements as when the function was called. Hint: as you look for the k th priority element, you will need to store other elements somewhere.

elem k_priority(pq_t Q, int k)

//@requires Q != NULL && !pq_empty(Q);

//@requires 1 <= k && k <= pq_size(Q);

{

// Temporary space

;

// Get to the k-th element

// Restore Q

return

;

}

Task 2.9

The median of a collection Q is the element m in Q so that half of the other elements have priority higher than or equal to m’s and half have lower or equal priority. If there are an even number of elements in Q, the half with lower priority will have one more element. Complete the function median that computes the median element of a priority queue Q according to this definition, where “larger” means “having higher priority”.

elem median(pq_t Q)

//@requires Q != NULL && !pq_empty(Q);

{

return

;

}

Task 2.10

Assume that the priority function stored in the priority queue Q runs in constant time and that Q contains n elements. What is the worst-case asymptotic complexity of the call median(Q)?

O(                                     )




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图