$24.99
Assignment 5 – Divide and Conquer
Answer the questions in the boxes provided on the question sheets. If you run out of room for an answer, add a page to the end of the document.
Name: Zhuyan Xu Wisc id:
Divide and Conquer
1. Erickson, Jeff. Algorithms (p.49, q. 6). Use recursion trees to solve each of the following recurrences.
(a) C(n) = 2C(n/4) + n2
Assignment 5 – Divide and Conquer
However, the only way you can access these values is through queries to the databases. In a single query, you can specify a value k to one of the two databases, and the chosen database will return the kth smallest value that it contains. Since queries are expensive, you would like to compute the median using as few queries as possible.
Give an algorithm that finds the median value using at most O(logn) queries.
3. Kleinberg, Jon. Algorithm Design (p. 246, q. 2). Recall the problem of finding the number of inversions. As in the text, we are given a sequence of n numbers a1,...,an, which we assume are all distinct, and we define an inversion to be a pair i < j such that ai > aj. We motivated the problem of counting inversions as a good measure of how different two orderings are. However, this measure is very sensitive. Let’s call a pair a significant inversion if i < j and ai > 2aj. Give an O(nlogn) algorithm to count the number of significant inversions between two orderings.
4. Kleinberg, Jon. Algorithm Design (p. 246, q. 3). You’re consulting for a bank that’s concerned about fraud detection. They have a collection of n bank cards that they’ve confiscated, suspecting them of being used in fraud.
It’s difficult to read the account number off a bank card directly, but the bank has an "equivalence tester" that takes two bank cards and determines whether they correspond to the same account.
Their question is the following: among the collection of n cards, is there a set of more than of them that all correspond to the same account? Assume that the only feasible operations you can do with the cards are to pick two of them and plug them in to the equivalence tester. Show how to decide the answer to their question with only O(nlogn) invocations of the equivalence tester.
merge anti countMajority
hpn.zhtA.iswithmajority Tim仳e put list A A
m My typeofMAcards lagerthan以县Me outputIf 刐majority1 returno1f⽉
api.mergemgA.diustn inCtomajorityC ofathilsl nWea ismyiteminA Ǚ
2 If MMcompareAt NChmywiotheveryuntcardsin yak⼆countConningMajoritylIButfithalfby the bio In fyihbmergeCont HiAz ah appearance
h4 saIfme的forbsiisMBitsizelcj.reegetkiretuIntmmnflc.my.my return a
5. Implement the optimal algorithm for inversion counting in either C, C++, C#, Java, or Python. Beelseelseifreturn c my
efficient and implement it in O(nlogn) time, where n is the number of elements in the ranking.
The input will start with an positive integer, giving the number of instances that follow. For each instance, there will be a positive integer, giving the number of elements in the ranking. A sample input is the following:
e
2
5
5 4 3 2 1
4
1 5 9 8
The sample input has two instances. The first instance has 5 elements and the second has 4. For each instance, your program should output the number of inversions on a separate line. Each output line should be terminated by a newline. The correct output to the sample input would be:
10
1
Page 3 of 3
⼋
merge Li hi lineage 2 sorted army mergeSortL
indentDoiii.addedriflbinie ithgthH.es h L 0 bin
whileif inredtteurnrunt三erehsleingth idenzih.bg hhii LIbin l.bgN
l
i.nergesrnlindenii2ti笑i.点im算蕊
dseifindenzl h.bgth hlinden q h
addzitmei thecmfnyof
if add2 mergeSon is
olnhgngiolilres.appendlhlindenlyiindenlttiehe.ru
append hlinden2
index2 t i
8 7 23 5 4 61
ooioolo_olo.to