Starting from:

$34.99

CS577 Assignment 6- Divide and Conquer 2 Solution

Assignment 6 – Divide and Conquer 2
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: Zhuoyan Xu Wisc id:
Divide and Conquer
1. Kleinberg, Jon. Algorithm Design (p. 248, q. 5) Hidden surface removal is a problem in computer graphics where you identify objects that are completely hidden behind other objects, so that your renderer can skip over them. This is a common graphical optimization.
Give an algorithm that takes n lines as input and in O(nlogn) time returns all the ones that are visible.

Sorting requires 0 nhyn framing comparison requires014
tool04kg4
2. In class, we considered a divide and conquer algorithm for finding the closest pair of points in a plane. Recall that this algorithm runs in O(nlogn) time. Let’s consider two variations on this problem:
(a) First consider the problem of searching for the closest pair of points in 3-dimensional space. Show how you could extend the single plane closest pairs algorithm to find closest pairs in 3D space. Your solution should still achieve O(nlogn) run time.
Solution: we have Px Py Pa us sorted xyt values 04h9m
find a hyperplane x k divides space into A1lefthalfof 171 AlrightbitofPx
Thenthendivedesyseparateehosedlemmaoinncorrespondinglecty utreanddrividedoythispositions7 valuesrecursivelyarotoundassignedhyperplanwe hepointsreecheekmmininfdlqf.ggpointsd p lolpinydirtrtl
then trial complexity 0 nigh
(b) Now consider the problem of searching for the closest pair of points on the surface of a sphere (distances measured by the shortest path across the surface). Explain how your algorithm from part a can be used to find the closest pair of points on the sphere as well.
Solution: we usethe same algorithm we amsiderpointy onsurphore as a sahsetof larger 3Dspace we modify our distancemetricdlxo.nu to distance on surphare Then we usethe same algorithm
(c) Finally, one way to approximate the surface of a sphere is to take a plane and “wrap” at the edges, so a point at x coordinate 0 and y coordinate MAX is the same as x coordinate 0 and y coordinate MIN. Similarly, the left and right edges of the plane wrap around. Show how you could extend the single plane closest pairs algorithm to find closest pairs in this space.
Solution: We treat surface on surphere as a plane Then we implement regular chest pair algorithm on this singleplane
Then for checking thepoints in 5 subsetofpoints
planewithinintotofseLt swe Walsoe apaddingddpointsthewithedgeinus8e theofedgeothersideofof
points we copythepoints at tower f edgeofplaneand add to
upperof planet Then we notonlycheekpairs cross dividerbentcheek pairsacross edge
Then the forming algorithm is the same
3. Erickson, Jeff. Algorithms (p. 58, q. 25 d and e) Prove that the following algorithm computes gcd(x,y) the greatest common divisor of x and y, and show its worst-case running time.
BINARYGCD(x,y): 2 Thenwe show correctness Sincethealgorithmreturn
if x = y: correctresultandalgorithmtermates in LtgXT11 Ugg 1 return x is correct
else if x and y are both even: Thenthealgorithm return 2*BINARYGCD(x/2,y/2)
else if x is even: 3 Worst running time return BINARYGCD(x/2,y) when XY In worst cos
else if y is even: The recurrison willstop return BINARYGCD(x,y/2) itstop at XYn else if x > y:
return BINARYGCD( (x-y)/2,y ) The running time is 0 hogXtheyy else return BINARYGCD( x, (y-x)/2 )
Solution: firstweshow invariant consider kisthegreatestcommondivisorof
Xandy XL thenk Y x ybotheven thenKiserenandtyris
thenKisoddthenany oddYiseven wehavesomeores
thenadXxCisisDCDisevenoddofoddxyweyyEishaveiscoddDIEEoddECAsYtXiIsisydirisablewsetfeorheodrYefsGodCDdt vXiiKYcfecversIXddYYGCDlaThenXY13adiissECDwdivsYeehaistiblexvofybyexxCDdisyai ofEIYcis1yladivd41ithensiblethenbytheydAvihecVBreveesamersa G
since
XisoddYisoddxcy wehavesameresult
4. Here we explore the structure of some different recursion trees than the previous homework.
(a) Asymptotically solve the following recurrence for A(n) for n 1.
with base case A(1) = 1


(b) Asymptotically solve the following recurrence for B(n) for n 1.
with base case B(1) = 1

(c) Asymptotically solve the following recurrence for C(n) for n 0.
with base case C(0) = 0

(d) Let d > 3 be some arbitrary constant. Then solve the following recurrence for D(x) where x 0.
with base case D(0) = 0
5. Implement a solution in either C, C++, C#, Java, or Python to the following problem.
Suppose you are given two sets ofset {q1,q2,...,qn} on the lineqi. Your goal is to develop an algorithm to determine how many pairs of thesey = 1n. Create a set ofpoints, one set {pn1,pline segments by connecting each point2,...,pn} on the line y = 0 and the otherpi to the corresponding point
line segments intersect. Your algorithm should take the 2n points as input, and return the number of intersections. Using divide-and-conquer, you should be able to develop an algorithm that runs in O(nlogn) time.
Hint: What does this problem have in common with the problem of counting inversions in a list?
Input should be read in from stdin. The first line will be the number of instances. For each instance, the first line will contain the number of pairs of points (n). The next n lines each contain the location of a point qi on the top line. Followed by the final n lines of the instance each containing the location of the corresponding point pi on the bottom line. For the example shown in Fig 1, the input is properly
Figure 1: An example for the line intersection problem where the answer is 4
Constraints:
•• 1For each point, its location n  106 x is a positive integer such that 1  x  106
bottomAlistPr is indext1ho ihndexROph merge 14hmergebyoroderderr tht het hmergeSoµlengthrtL merge R him

Lgyp iref indq Eextresp4lengtht 88addsindex2falsehlength F l Lh Lqbibninblength l l t whileretrtuurneresaresp h 4 mergeSiri
addsfalse hi h mergeSorci
if indaddsex1 truh lengthe 7 returnmerge1hhi else if index2 hlength88 4 index1 q h index2
adds time thecomplexityof
if ladder mergesortis
rindexefapptxtenditheinde11x resp append112,4indexi 0Intogn a01M
else ragappend1hlinda4 respappend hlindexa
index2 1
Sample Test Cases:

expected output: 4
7

More products