MA214 Algorithms and Data Structures 2023/24 Exercises 4Python

Java Python MA214 Algorithms and Data Structures

2023/24

Exercises 4

(Master Theorem, Heap Sort, Binary Search)

Exercise 4.1. Master Theorem                                                               3 pts

Use the Master Theorem to find solutions to the following recurrences. Make sure that you state and justify which case of the Master Theorem you use.

(a) T(n) = 3 · T(n/4) + n log2 (n)

(b) T(n) = 3 · T(n/2) + n

(c) T(n) = 3 · T(n/3) + n/2

Exercise 4.2. Heap Sort                                                                           1 pts

In the lectures last week, we saw the Heap Sort algorithm, which sorts a list in place and in worst-case time O(n · log2 (n)). Using the figures from the lecture as a model, illustrate the operation of BuildMaxHeap on the list

A = [5, 3, 17, 10, 84, 19, 6, 22, 9] .

Exercise 4.3. Randomised algorithms                                                    3 pts

Suppose your are given a list A of even length, len(A) = n, and you know that n/2 of the entries are equal to x, and the other n/2 entries are equal to y = x. You want to find x and y.

A simple deterministic algorithm (not relying on randomisation) for this problem that is guaranteed to find x and y proceeds as follows. First it sets x = A[0]. It then goes through the elements at positions i = 2, . . . , n − 1. As soon as A[i] = x, the algorithm sets y = A[i] and outputs x and y.

(a) Implement this algorithm in Python, and analyse i MA214 Algorithms and Data Structures 2023/24 Exercises 4Python ts running time using big Θ- notation.

Now consider the following randomised algorithm for this problem. Repeatedly draw a random entry (with replacement, so you may draw the same entry more than once) until you have seen two distinct numbers for the first time.

(b) Implement this algorithm in Python.

(c) What is the expected number of random draws performed by this algorithm?

(d) Use your answer to (c) to analyse the expected running time of this algorithm using big O-notation.

(e) Is this a Monte Carlo or a Las Vegas algorithm?

Hint: For part (c) it may be useful to consider the random variable Y which counts the number of random draws, and compute the probability that Pr[Y > k].

Exercise 4.4. Binary Search                                                                   3 pts

Given an arbitrary list, an algorithm searching for a specific target value in the list cannot do much better than scanning the list elements one by one, which requires Ω(n) time in the worst case. On the other hand, if the algorithm is guaranteed to be given a sorted list (say, in non-decreasing order), it is possible to do drastically better. An algorithm, with a single comparison, can eliminate half of the entries of the list as possible locations. This is the idea underlying the “Binary Search” algorithm.

(a) Implement a recursive Python function that searches for value x in the list A, which is sorted in non-decreasing order. Your function should return an index i, where A [i]=x (or the special Python value None if no such i exists) and have time com-plexity O(log n), where n is the length of the input list. (Do not use slicing, as it is a linear-time operation.)

(b) Implement an iterative version of the binary search in Python, BinarySearch(A,x), where A, x, and the time complexity are the same as above.

(c) Give a loop invariant for your algorithm in part (b). Prove the correctness of that algorithm using your loop invariant         

  • 20
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值