代做CSCI 4041 Algorithms and Data Structures - Spring 2025 Midterm Exam帮做R编程

CSCI 4041 Algorithms and Data Structures - Spring 2025

Midterm Exam

The following Algorithms and Data Structures may be useful for answering questions..

Algorithms:

bubble_sort(A)

# Sorts by repeatedly swapping adjacent items until they are correct.

selection_sort(A)

# Sorts by finding the minimum value for each subarray.

insertion_sort(A)

# Sorts list by repeatedly inserting into a sorted list.

merge_sort(A)

# Sorts list using divide and conquer

heap_sort(A)

# Sorts by moving the top element of a max heap to the end of the array.

quicksort(A)

# Sorts by partitioning by a pivot and recursively sorting each partition.

counting_sort(A)

# Sorts by counting the times a key appears.

radix_sort(A, sortAlgorithm)

# Sorts array by sorting digits from least significant to most significant.

bucket_sort(A)

# Sorts by putting keys in bucket lists and sorting the lists.

Data Structures:

Heap(compareFunction):

# Maintains a min / max heap depending on a compareFunction.

Heap.extract_max()

# Returns and removes the top item on the heap and maintains the heap prpoerty.

Heap.size()

# Returns the size of the heap.

Heap.build_heap(A)

# Builds a heap from an array.

Heap.insert(item)

# Inserts an item into the heap in correct location.

Problem 1: Runtime (50 points)

Theory (15 points):

(3 points each) True/False - Check the box.

1. □ True □ False If f(n) = Θ(n 2 ), then f(n) = O(n 3 ) and n = O(f(n))

2. □ True □ False lg (n!) = O(n lg n).

3. □ True □ False n sin n = Θ(n).

4. □ True □ False n lg n = Ω( √ n).

5. □ True □ False log3 n + lg n 3 = Θ(log7 n 3 ).

Sorts (15 points):

(3 points each) Short Answer. Write the name of the sort you would use to solve each problem. Refer to Page 2 for a list of sorts.

6. Sort a list of text strings (each of length d) in linear time.

7. Sort sensor data that is uniformly distributed in linear time.

8. Sort a small array stably and in place.

9. Sort a large array of data stably in Θ(n lg n) time.

10. Sort random numbers in-place in Θ(n lg n) time.

Master Theorem (20 points):

Calculate the runtime for the following algorithm using the Master Theorem (if possible). If you cannot use the Master Theorem, explain why it cannot be used.

def blur_edges(A, p, r):

if p >= r

return

q = ⌊(r − p)/4⌋

blur_edges(A, p, p + q)

blur_edges(A, r-q, r)

for i = 2 to r-p:

A[i-1] = (A[i] + A[i-1])/2

Recall:

• log24 = 2

• log42 = 0.5

• a =

• b =

• f(n) =

• n logba =

• Case (1,2,3 or None):

• Regulatory Condition? (if applicable)

• Runtime:

Problem 2: Correctness (50 points)

The following algorithm takes heap A, and returns the maximum path from the node at index index to a leaf node. The path is returned as a list of indexes:

// MAX - PATH (...) calculates the maximum path in heap A

// from the index to a leaf . Returns a list of indexes .

MAX - PATH (A , index )

1. if ( index > A . heap_size )

2. return []

3. L = MAX - PATH (A , LEFT ( index ))

4. R = MAX - PATH (A , RIGHT ( index ))

5. sum_L = SUM - PATH (A , L ) + A [ index ]

6. sum_R = SUM - PATH (A , R ) + A [ index ]

7. P = L

8. if sum_L < sum_R

9. P = R

10. P . append ( index )

11. return P

// SUM - PATH (...) returns the sum of items in heap A

// for a list of indexes in a path P

SUM - PATH (A , P )

1. n = P . length

1. sum = 0

2. for i = 1 to n - 1

3. sum = sum + A [ P [ i ]]

4. return sum + A [ P [ n ]]

Answer the following questions:

(a) Describe a loop invariant that would help prove the correctness of SUM-PATH(A, P).

(b) State the loop invariant before and after the k th iteration. What does this mean about the state of the loop before the k + 1 iteration?

(c) Prove: MAX-PATH(A, index) is correct. (You may assume SUM-PATH(A, P) is correct.)

Problem 3: Sorting Application (50 points)

Consider the following scenario:

Your company specializes in analyzing remote sensor data. One of your customers would like to monitor the real-time statistics from a sensor. This involves repeatedly calculating the median from a list of measurements that are passed in via array A, and correspond to a particular time (e.g. A = [0.3, 2.0, 10.2, ...] at 110.34 minutes). To accomplish this task, you must meet the following requirements:

• Calculate the median as fast as possible without allocating memory. The median is the middle value of a sorted list.

• Measurements are not guaranteed to arrive in the correct order. For example, an early time measurement may arrive after a late time measurement. Therefore, we will use a heap to efficiently sort as measurements arrive.

• You will need to implement the following functions:

– INIT-HEAP() - Creates a heap that stores medians for efficient time ordering.

– CALC-MEDIAN(time, A, H) - Calculates the median of list A adds it to the heap H.

– GET-NEXT-MEDIAN(last time, H) - Returns the measurement with lowest time step greater than last time.

• You may use the following comparison function if needed.

min - time - measurement (A , B )

return A . time < B . time

Complete the following:

Note:

• You may call or use any of the algorithms or data structures listed on Page 2.

• You may write your code in psuedocode or any other language.

• Don’t worry too much about syntax. As long as we understand your algorithm, it is sufficient.

(a) Initialize the Medians array or data structure:

(b) Write the CALC-MEDIAN(time, A, H) algorithm described above.

(c) Write the GET-NEXT-MEDIAN(last time, H) algorithm described above.

(d) Briefly and informally describe the worst-case runtime for the following:

• INIT-MEDIANS()

• CALC-MEDIAN(...)

• GET-NEXT-MEDIAN(...)



热门主题

课程名

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
站长地图