Plan
School of Computing and Information Systems COMP90038 Algorithms and Complexity Tutorial Week 4
13–17 August 2018
We provide more exercises than we can cover in the tutorial, so that you have more to practice on in your own time.
The exercises
15. For each of the following pairs f, g, determine whether f(n) ∈ O(g(n)), or g(n) ∈ O(f(n)), or both:
(b) f(n) = n2 +n√n and g(n) = n2 +n (d) f(n) = n+logn and g(n) = √n
(f) f(n) = (logn)2 and g(n) = 2+logn
16. Show the steps of selection sort, when given the keys S, O, R, T, X, A, M, P, L, E.
17. One possible way of representing a polynomial
p(x) = anxn +an−1xn−1 +···+a1x+a0 is as an array A of length n + 1, with A[i] holding the coefficient ai.
(a) Design a brute-force algorithm for computing the value of p(x) at a given point x. Express this as a function Peval(A,n,x) where A is the array of coefficients, n is the degree of the polynomial, and x is the point for which we want the value of p.
(b) If your algorithm is Θ(n2), try to find a linear algorithm.
(c) Is it possible to find an algorithm that solves the problem in sub-linear time?
(a) f(n) = (n2 +1−n2)/2 and g(n) = 2n (c) f(n) = nlogn and g(n) = n√n
4
(e) f(n) = 4nlogn+n and g(n) = (n2 −n)/2
18. Trace the brute-force string search algorithm on the following input: The path p is ‘needle’, and the text t is ‘there_need_not_be_any’. How many comparisons (successful and unsuc- cessful) are made?
19. Assume we have a text consisting of one million zeros. For each of these patterns, determine how many character comparisons the brute-force string matching algorithm will make:
(a) 010001 (b) 000101 (c) 011101
20. Give an example of a text of length n and a pattern of length m, which together constitute a worst-case scenario for the brute-force string matching algorithm. How many character comparisons, as a function of n and m, will be made for the worst-case example. What is the value of m (the length of the pattern) that maximises this function? i.e. What is the worst case pattern length?
21. The assignment problem asks how to best assign n jobs to n contractors who have put in bids for each job. An instance of this problem is an n × n cost matrix C, with C[i, j] specifying what it will cost to have contractor i do job j. The aim is to minimise the total cost. More formally, we want to find a permutation ⟨j1, j2, . . . jn⟩ of ⟨1, 2, . . . , n⟩ such that ∑ni=1 C[i, ji] is minimized. Use brute force to solve the following instance:
Job1 Job2 Job3 Job4
Contractor 1 Contractor 2 Contractor 3 Contractor 4
9278 6437 5818 7694
22. Give an instance of the assignment problem for which the smallest item C[i,j] of its cost matrix is not included in its solution.