程序代写代做代考 C go algorithm graph Algorithms Tutorial 1 Solutions

Algorithms Tutorial 1 Solutions
1. You are given an array S of n integers and another integer x.
(a) Describe an O(nlogn) algorithm (in the sense of the worst case perfor- mance) that determines whether or not there exist two elements in S whose sum is exactly x.
(b) Describe an algorithm that accomplishes the same task, but runs in O(n) expected (i.e., average) time.
Solution: Note that brute force does not work here, because it runs in O(n2) time.
(a) First, we sort the array – we can do this in O(n log n) in the worst case, for example, using Merge Sort. A couple of approaches from here:
• For each element a in the array, we can check if there exists an element x − a also in the array in O(log n) time using binary search. The only special case is if a = x−a (i.e. x = 2a), where we just need to check the two elements adjacent to a in the sorted array to see if another a exists. Hence, we take at most O(log n) time for each element, so this part is also O(n log n) time in the worst case, giving an O(n log n) algorithm.
• Alternatively again, you add the smallest and the largest elements of the array. If the sum exceeds x no solution can exist involving the largest element; if the sum is smaller than x then no solution can exist involving the smallest element. Thus, if this sum is not equal to x you can eliminate one element. After at most n − 1 many such steps you will either find a solution or will eliminate all elements thus verifying no such elements exist.
1

(b) We take a similar approach as in (a), except using a hash map (hash table) to check if elements exist in the array: each insertion and lookup takes O(1) expected time.
The following approaches correspond to the approaches in (a):
• At index i, we assume A[1..i − 1] is already stored in the hash map. Then we check if x − A[i] is in the hash map in O(1), then insert A[i] into the hash map, also in O(1).
• Alternatively, we hash all elements of A and then go through elements of A again, this time for each element a checking in O(1) time if x − a is in in the hash table. If 2a = x, we also check if at least 2 copies of a appear in the corresponding slot of the hash table.
2. Given two arrays of n integers, design an algorithm that finds out in O(n log (n)) steps if the two arrays have an element in common. (Microsoft interview question)
Solution: Go through all the elements of one of the arrays and for each element do a binary search in the other array to see if this element also appears there.
3. Given an array A[1..100] which contains all natural numbers between 1 and 99, design an algorithm that runs in O(n) and returns the duplicated value. (Microsoft interview question)
Solution: A simple solution: Initialise an array B of length 99; go through A placing each element i into the slot B[i] and see which slot would get 2 elements. A fancier solution: just add up all elements of A and subtract from such a sum the sum of all numbers from 1 to 99, i.e., subtract 100 × 99/2 = 50 × 99 = 4950 to get which number appears twice.
4. Assume you are given two arrays A and B, each containing n distinct positive numbers and the equation x8 − x4y4 = y6 + x2y2 + 10 . Design an algorithm which runs in time O(nlogn) which finds if A contains a value for x and B contains a value for y that satisfy the equation.
Solution: Solution: write the equation in the form x8 =x4y4 +y6 +x2y2 +10;
now note that the right hand side is monotonic in y (actually, in both x and y, but monotonicity in y is important here.)
2

sort array B in time n log n; go through all elements of A; for each element x ∈ A do binary search to see if there is a y satisfying the equation;
this is possible because the right hand side is monotonic in y and B is sorted.
if one element y produced a value of the right hand side bigger than the value of x8 then all values larger than such y will also produce a value of the right hand side exceeding x8.
5. You’re given an array of n integers, and must answer a series of n queries, each of the form: “how many elements of the array have value between L and R?”, where L and R are integers. Design an O(n log n) algorithm that answers all of these queries.
Solution: We first sort the array in O(n log n), using Merge Sort. For each query, we can binary search to find the index of the:
• First element with value no less than L; and
• First element with value strictly greater than R.
The difference between these indices is the answer to the query. Each binary search takes O(log n) so the algorithm runs in O(n log n) overall. Note that if your binary search hits L you have to see if the preceding element is smaller than L; if it is also equal to L, you have to continue the binary search (go- ing towards the smaller elements) until you find the first element equal to L. Similar observation applies if your binary search hits R.
6. Assume you have an array of 2n distinct integers. Find the largest and the smallest number using 3n − 2 comparisons only.
Solution: Note that the brute force does not work – if you take the first two elements A[1] and A[2], compare them and set m = min{A[1], A[2]} and M = max{A[1], A[2]}, you made 1 comparison and are left with 2n−2 elements each of which has to be compared both with m and M to see if they need to be revised, which in total gives you 2(2n − 2) + 1 = 4n − 3 comparisons. Instead, we first form n pairs and compare the two elements of each pair, putting the smaller into a new array S and the larger into a new array L. Note that the smallest element must be in S and the largest in L and we have made n comparisons. We now use the brute force on the two arrays which takes n − 1 comparisons in each array. In total this takes n+n−1+n−1 = 3n−2 comparisons.
3

7. Assume that you have an array of 2n distinct integers. Find the largest and the second largest number using only 2n + n − 2 comparisons.
Solution: See the figure below. On the figure we see a complete binary tree
with 2n leaves and 2n − 1 internal nodes and of depth n. Place all the numbers at the leaves, compare each pair and “promote” the larger element (shown in red) to the next level and proceed in such a way till you reach the root of the tree, which will contain the largest element. Clearly, each internal node is a result of one comparison and there are 2n − 1 many nodes thus also the same number of comparisons so far. Now just note that the second largest element must be among the black nodes which were compared with the largest element along the way – all elements underneath them must be smaller or equal to the elements shown in black. There are n many such elements so finding the largest among them will take n − 1 comparisons by brute force. In total this is exactly 2n + n − 2 many comparisons.
8. You are at a party attended by n people (not including yourself), and you suspect that there might be a celebrity present. A celebrity is someone known by everyone, but does not know anyone except themselves. You may assume everyone knows themselves. Your task is to work out if there is a celebrity present, and if so, which of the n people present is a celebrity. To do so, you can ask a person X if they know another person Y (where you choose X and Y when asking the question).
(a) Show that your task can always be accomplished by asking no more than 3n − 3 such questions, even in the worst case.
4

(b) Show that your task can always be accomplished by asking no more than 3n − ⌊log2 n⌋ − 3 such questions, even in the worst case.
Solution: Assume the people are numbered 1 to n.
(a) We observe that at most one person can be a celebrity. We proceed as follows. First, we find a single candidate, i.e., the only person person who could be a celebrity. Initially our candidate c is person 1. Then, for each personifrom2ton,weaskifcknowsi. Ifcdoes,thenccannotbea celebrity (for they know someone else) and i is our new candidate. If c doesn’t, then i can’t be a celebrity and c remains our candidate. Thus, after asking n−1 questions we can establish that only the final c is possibly a celebrity.
It is possible that c also isn’t a celebrity, so we must verify that they are. To do this, we need to ask n − 1 questions of the form “does j know c?”; if the answer is always yes, c still could be a celebrity (otherwise he is not and we conclude there is no celebrity at the party); then we ask n − 1 questions of the form “does c know j?” and if the answer is always “no” we have found a celebrity, otherwise no celebrity is present. Hence, our algorithm uses n − 1 + 2(n − 1) = 3n − 3 questions, even in the worst case. Note also that 3n − 4 questions suffice, because we can reuse the answer to at least one question the potential celebrity was asked during the initial search for a potential celebrity.
(b) We arrange n people present as leaves of a balanced full tree, i.e., a tree in which every node has either 2 or 0 children and the depth of the tree is as small as possible. To do that compute m = ⌊log2 n⌋ and construct a perfect binary three with 2m ≤ n leaves. If 2m < n add two children to each of the leftmost n − 2m leaves of such a perfect binary tree. In this way you obtain 2(n−2m)+(2m −(n−2m)) = 2n−2m+1 +2m −n+2m = n leaves exactly, but each leaf now has its pair, and the depth of each leaf is at least ⌊log2 n⌋. For each pair we ask if, say, the left child knows the right child and, depending on the answer as in (a) we promote the potential celebrity one level closer to the root. It will again take n − 1 questions to determine a potential celebrity, but during the verification step we can save ⌊log2 n⌋ questions (one on each level) because we can reuse answers obtained along the path that the potential celebrity traversed through the tree. Thus, 3n − 3 − ⌊log2 n⌋ questions suffice. 5 Figure1: Heren=9;thus,m=⌊log2n⌋=3andweaddtwochildrenton−2m = 9 − 23 = 1 leaf of a perfect binary tree with 8 leaves. Thus obtained tree has 9 leaves. Potential celebrity is in red. Note that we do not have to repeat 3 questions represented by the green arrows. 9. Given n numbers x1, . . . , xn where each xi is a real number in the interval [0, 1], devise an algorithm that runs in linear time that outputs a permutation of the n numbers, say y1, . . . , yn, such that 􏰀ni=2|yi − yi−1| < 2. Hint: this is easy to do in O(n log n) time: just sort the sequence in ascending order. In this case, 􏰀ni=2|yi − yi−1| = 􏰀ni=2(yi − yi−1) = yn − y1 ≤ 1 − 0 = 1. Here |yi − yi−1| = yi − yi−1 because all the differences are non-negative, and all the terms in the sum except the first and the last one cancel out. To solve this problem, one might think about tweaking the BucketSort algorithm. Solution: Split the interval [0,1] into n equal buckets. Assign to each xi its bucket number b(xi) = ⌊nxi⌋. Why is this the bucket where xi belongs? xi isinthebucketkifandonlyifk ≤xi M then similarly M cannot be found in the rightmost column because all element there are larger than M(1,n), thus the last column can be ignored. In both cases the sum of the width and height of the search table is reduced by 1. We continue in this manner until either M is found or it is ascertained that it does not occur in the table. Since the initial sum of the height and the width of the table is 2n the algorithm takes O(n) many steps.
12. Suppose that you are taking care of n kids, who took their shoes off. You have to take the kids out and it is your task to make sure that each kid is wearing a pair of shoes of the right size (not necessarily their own, but one of the same size). Allyoucandoistotrytoputapairofshoesonakid,andseeifthey fit, or are too large or too small; you are NOT allowed to compare a shoe with another shoe or a foot with another foot. Describe an algorithm whose expected number of shoe trials is O(n log n) which properly fits shoes on every kid.
Solution: This is done by a “double QuickSort” as follows. Pick a shoe and use it as a pivot to split the kids into three groups: those for whom the shoe was too large, those who fit the shoe and those for whom the shoe was too small. Then pick a kid for whom the shoe was a fit and let him try all the shoes, splitting them in three groups as well: shoes that are too small, shoes that fit him and the shoes which were too large for him. Continue this process with the first group of kids and first group of shoes and then also the third group of shoes with the third group of kids.
13. You are conducting an election among a class of n students. Each student casts precisely one vote by writing their name, and that of their chosen classmate on a single piece of paper. However, the students have forgotten to specify the order of names on each piece of paper – for instance, “Alice Bob” could mean Alice voted for Bob, or Bob voted for Alice!
(a) Show how you can still uniquely determine how many votes each student received.
8

(b) Hence, explain how you can determine which students did not receive any votes. Can you determine who these students voted for?
(c) Suppose every student received at least one vote. What is the maximum possible number of votes received by any student? Justify your answer.
(d) Using parts (b) and (c), or otherwise, design an algorithm that constructs a list of votes of the form “X voted for Y ” consistent with the pieces of paper. Specifically, each piece of paper should match up with precisely one of these votes. If multiple such lists exist, produce any. An O(n2) algorithm earns partial credit, but you should aim for an O(n) algorithm.
Hint: first, use part (c) to consider how you would solve it in the case where every student received at least one vote. Then, apply part (b).
Solution: We can immediately resolve any pieces of paper where a student’s name appears twice: they must have voted for themselves.
(a) If a student’s name appears on x pieces of paper, then the student received x − 1 votes since each student voted precisely once.
(b) If a student did not receive any votes, their name only appears on precisely one piece of paper. The name of the other student is who they voted for.
(c) If every student received at least one vote, then at least n distinct pieces of paper are required to correspond to these votes. There are no more pieces of paper to be distributed, so every student received exactly one vote. Hence, each student also received a maximum of one vote.
(d) Suppose every student received at least one vote. Then, by (c), every stu- dent received exactly one vote. By considering the votes as an undirected graph (where each student is a vertex and every vote is an edge between two students), or otherwise, we can see that every student appears on pre- cisely two pieces of paper. This corresponds to a set of disjoint cycle graphs where students are vertices and pieces of paper are edges between students.
Pick any student s appearing on two pieces of paper, and arbitrarily choose one of their pieces of paper as their vote. Suppose they voted for t. We are now left with a single choice for t’s vote. We can repeatedly follow these pieces of paper until we arrive back to s. We then repeat with another student appearing on two pieces of paper until all votes have been resolved. We can do this in O(n) altogether, for instance using a (simplified) Depth- First Search (DFS).
9

Now we combine this with part (b) to obtain an algorithm for the general case. We repeatedly check if a student has no votes (by counting votes) and resolve their vote. Once we reach a point where this is no longer possible, we know every student received at least one vote, and use the algorithm above.
This can be done in O(n2) by repeatedly taking O(n) to identify a student who has no votes, or more cleverly in O(n) as follows. We keep a count, for each student, how many pieces of paper they appear on and maintain a queue of students who appear on only one vote. We can initially populate this queue in O(n). Then, we repeatedly process the front student of the queue by removing their vote. Note that this only changes the vote count of the person they voted for, so we simply decrease their count. If their count reaches 1, we push them onto the queue. Hence, we process each student, updating counts and the queue in O(1) so this step is O(n) as well, giving an O(n) algorithm.
14. There are N teams in the local cricket competition and you happen to have N friends that keenly follow it. Each friend supports some subset (possibly all, or none) of the N teams. Not being the sporty type – but wanting to fit in nonetheless – you must decide for yourself some subset of teams (possibly all, or none) to support. You don’t want to be branded a copycat, so your subset must not be identical to anyone else’s. The trouble is, you don’t know which friends support which teams, so you can ask your friends some questions of the form “Does friend A support team B?” (you choose A and B before asking each question). Design an algorithm that determines a suitable subset of teams for you to support and asks as few questions as possible in doing so.
Solution: Suppose your friends are numbered 1 to N and the teams are also numbered 1 to N. Then, for each i, ask friend i if they support team i. If they do, we choose not to support them and if they don’t, we do support them. Clearly, this subset of teams is different to all of our friends’, and it uses N queries, which is the minimal possible for any deterministic solution (we must have some information about each friend).
15. You are given an array A consisting of 2n − 1 integers. Design an algorithm which finds all of the n possible sums of n consecutive elements of A and which
10

runs in time O(n). Thus, you have to find the values of all of the sums S[1] = A[1] + A[2] + . . . + A[n − 1] + A[n];
S[2] = A[2] + A[3] + . . . + A[n] + A[n + 1]; . . .
S[n] = A[n] + A[n + 1] + . . . + A[2n − 2] + A[2n − 1], and your algorithm should run in time O(n).
Solution: Here are two possible solutions:
• We can compute S[1] in O(n) by simply iterating. Then, for each i ≥ 2, we have that S[i] = S[i−1]−A[i−1]+A[n+i−1], so we can compute each subsequent S[i] in O(1) time each, giving an O(n) algorithm.
• We can first compute the array C[0..2n − 1] where C[i] = A[1] + A[2] + · · ·+A[2n−1]. We have that C[0] = 0, and for i ≥ 1, C[i] = C[i−1]+A[i]. Hence, we compute C in O(n). Then S[i] = C[i+n−1]−C[i−1], so we can compute all the S values in O(n).
16. You are a fisherman, trying to catch fish with a net that is W meters wide. Using your advanced technology, you know that the positions of all N fish in the sea can be represented as integers on a number line. There may be more than one fish at the same location.
To catch the fish, you will cast your net at position x, and will catch all fish with positions between x and x + W , inclusive. Given N , W and an array X[1..N] denoting the positions of fish in the sea, give an O(NlogN) algorithm to find the maximum number of fish you can catch by casting your net once. For example, if N = 7,W = 3 and X = [1,11,4,10,6,7,7], then the most fish you can catch is 4: by placing your net at x = 4, you will catch one fish at position 4, one fish at position 6 and two fish at position 7.
Solution: First, sort the array (e.g. using MergeSort) in O(N log N ) time. We know that it optimal to cast our net starting from the same position as a fish. We can use a “two pointers” approach: we keep variables L and R, so the fish in positions [L..R] of the array are those in our net.
Initially L = 1, and in O(N) time we find the largest R so that the Rth fish in sorted order is still in our net. We then repeatedly increment L, and respectively repeatedly increment R to include all fish that fit within the net starting at the Lth fish. At each stage, we know we can catch R − L + 1 fish, so we take the maximum among these.
11

Since L and R are only ever incremented, and they are each incremented at most N times, the algorithm is O(N).
17. Your army consists of a line of N giants, each with a certain height. You must designate precisely L ≤ N of them to be leaders. Leaders must be spaced out across the line; specifically, every pair of leaders must have at least K ≥ 0 giants standing in between them. Given N,L,K and the heights H[1..N] of the giants in the order that they stand in the line as input, find the maximum height of the shortest leader among all valid choices of L leaders. We call this the optimisation version of the problem.
For instance, suppose N = 10,L = 3,K = 2 and H = [1,10,4,2,3,7,12,8,7,2]. Then among the 10 giants, you must choose 3 leaders so that each pair of leaders has at least 2 giants standing in between them. The best choice of leaders has heights 10, 7 and 7, with the shortest leader having height 7. This is the best possible for this case.
(a) In the decision version of this problem, we are given an additional integer T as input. Our task is to decide if there exists some valid choice of leaders satisfying the constraints whose shortest leader has height no less than T . Give an algorithm that solves the decision version of this problem in O(N) time.
(b) Hence, show that you can solve the optimisation version of this problem in O(N logN) time.
Solution:
(a) Notice that for the decision variant, we only care for each giant whether its height is at least T, or less than T: the actual value doesn’t matter. Call a giant eligible if their height as at least T .
We sweep from left to right, taking the first eligible giant we can, then skip- ping the next K giants and repeating. We return true if the total number of giants we obtain from this process is at least L, or false otherwise.
This algorithm is clearly O(N).
(b) Observe that the optimisation problem corresponds to finding the largest
value of T for which the answer to the decision problem is true.
Suppose our decision algorithm returns true for some T. Then clearly it will return true for all smaller values of T as well: since every giant that is eligible for this T will also be eligible for smaller T. Hence, we can say that our decision problem is monotonic in T .
12

Thus, we can use binary search to work out the maximum value of T where our decision problem returns true. Note that it suffices to check only heights of giants as candidate answers: the answer won’t change between them. Thus, we can sort our heights in O(N log N ) and binary search over these values, deciding whether to go higher or lower based on a run of our decision problem. Since there are O(log N ) iterations in the binary search, each taking O(N ) to resolve, our algorithm is O(N log N ) overall.
18. Read the review material from the class website on asymptotic notation and ba- sic properties of logarithms, pages 38-44 and then determine if f(n) = Ω(g(n)), f(n) = O(g(n)) or f(n) = Θ(g(n)) for the following pairs. Justify your an- swers.
You might find the following inequality useful: if f(n),g(n),c > 0 then
f(n) < c g(n) if and only if log f(n) < log c + log g(n). Also remember that O(f(n)) does not define a linear ordering; for some f,g neither f = O(g) nor g = O(f). Solution: (a) Using log(ab) = b log a we obtain log2(nlog2 n) + 2 log2 n = log2 n · log2(n) + 2 log2 n = (log2(n))2 + 2 log2 n = Θ((log2(n))2) because 2 log2 n grows much slower than (log2(n))2. (b) We want to show that n100 = O(2n/100), which means that we have to show that n100 < c2n/100 for some positive c and all sufficiently large n. But, since the log function is monotonically increasing, this will hold just in case logn100 c 2 log2n for
some c and all sufficiently large n. By the same argument as in the
previous case it is enough to show that log √n = log n1/2 > log c + √
log2 2 log2 n = logc+􏰟log2 n. Again taking c = 1, it is enough to show that 1/2 log n > 􏰟log2 n which clearly holds for all sufficiently large n.
(d) We again wish to show that n1.001 = Ω(n log n), i.e., that n1.001 > cn log n for some c and all sufficiently large n. Since n > 0 we can divide both sides by n, so we have to show that n0.001 > clogn. We again take c = 1 and show that n0.001 > log n for all sufficiently large n, which is equivalent to showing that log n/n0.001 < 1 for sufficiently large n. To this end we use the L’Hoˆpital’s to compute the limit log n n→∞ n0.001 (log n)′ n→∞ (n0.001)′ 1 = lim n n→∞ 0.001n0.001−1 lim = lim 11 n = lim n = lim n→∞ 0.001 1 · n0.001 n→∞ 0.001 · n0.001 Since lim log n n→∞ n0.001 = 0. = 0 then, for sufficiently large n we will have log n < 1. (e) Justnotethat(1+sinπn/2)/2cycles,withoneperiodequalto{1/2,1,1/2,0}. Thus, for all n = 4k+1 we have (1+sinπn/2)/2 = 1 and for all n = 4k+3 we have (1 + sin πn/2)/2 = 0. Thus for any fixed constant c > 0 for all
n = 4k+1 eventually n(1+sinπn/2)/2 = n > c√n, and for all n = 4k+3
we have n(1+sin πn/2)/2 = n0 = 1 and so n(1+sin πn/2)/2 = 1 < c√n. Thus, neither f(n) = O(g(n)) nor f(n) = Ω(g(n)). 19. Determine the asymptotic growth rate of the solutions to the following recur- rences. If possible, you can use the Master Theorem, if not, find another way of solving it. (a) T(n)=2T(n/2)+n(2+sinn) (b) T(n)=2T(n/2)+√n+logn n0.001 (c) T(n)=8T(n/2)+nlogn 14 (d) T(n)=T(n−1)+n Solution: (a) Notethatinthiscasea=2andb=2sonlogba =nlog22 =n. Ontheother hand, f(n) = n(2+sinn) = Θ(n) because 1 ≤ n(2+sinn) ≤ 3. Thus, the second case of the Master Theorem applies and we get T (n) = Θ(n log n). (b) Again, nlogb a = n. On the other hand, we have log n = O(√n) and so √n + log n = Θ(√n). This implies √n = n.5 = O(n0.9) = O(nlogb a−.1) so the first case of the Master Theorem applies and we obtain T(n) = Θ(nlogb a) = Θ(n). (c) We have nlogb a = nlog2 8 = n3. Thus f(n) = nlog n = Ω(n4). Consequently, f(n) = Ω(nlogb a+1). To be able to use the third case of the Master Theorem, we have to show that for some 0 < c < 1 the following holds: af(n/b) = 8f(n/2) < cf(n) which in our case translates to 􏰛 n 􏰜log(n/2) log n 82 16 then 8 􏰗 n 􏰘log(n/2) < 1/2nlog n and the condition is satisfied 2 with c = 1/2 and all n > 16.
(d) Note that for every k we have T(k) = T(k − 1) + k. So just unwind the
recurrence to get
T (n) = T (n − 1) + n = T (n − 2) + (n − 1) + n = T (n − 3) + (n − 2) + (n − 1) + n = . . .
= T (1) + (n − (n − 2)) + (n − (n − 3)) + … + (n − 1) + n = T (1) + (2 + 3 + 4 + . . . + n) = T (1) + n(n + 1) − 1
2
= Θ(n2);
20. Assume that you are given an array A containing 2n numbers. The only oper-
ation that you can perform is make a query if element A[i] is equal to element 15

A[j], 1 ≤ i,j ≤ 2n. Your task is to determine if there is a number which appears in A at least n times using an algorithm which runs in linear time.
Warning and a Hint: a tricky one. The reasoning resembles a little bit the reasoning used in the celebrity problem: try comparing them in pairs and first find one or at most two possible candidates and then count how many times they appear.
Solution: Split all elements of A into pairs and compare the numbers in each pair. If the numbers are different throw both away. Note that in this way we can throw away at most one copy of the number X appearing n times (if there is such) so in whatever is left X will also have number of copies equal half of total leftover elements. If the numbers in a pair are equal set them into two separate piles A and B. Now note that A has the same elements as B you can trow away B and A will still have the same property that half of its elements are equal just in case this was true of the original pile.
16