s = 0; i = 1; while (s < n) { s = s + i; i = i + 1; }
Algorithm: textSearch (text, pattern) Input: two char arrays text and pattern of lengths n and m 1. for i=0 to n-1 2. if recursiveMatch (i, 0, m-1) 3. return true 4. endif 5. endfor 6. return false Algorithm: recursiveMatch (i, p, q) Input: See if there's a match at position i in text with the chars in the pattern between positions p and q 1. // Base cases ... not shown // Find the middle index. 2. m = (p+q)/2 3. If both the left half and right half of the range [p,q] match, then return true otherwise return false
Rectangles can be specified in many ways. We will use the following convention: the top-left and bottom-right corners. If you know these two points, then the two points uniquely determine a rectangle.
For example, consider these two sets of rectangles in the region specified by (0,100) and (100,0):
Now, determining whether two rectangles intersect requires some clarification:
To implement:
IntPair ip = new IntPair (5, 12);Of course, you will be doing this in a loop and will be creating instances to add to the array that you will finally return in the method findIntersections.
import edu.gwu.algtest.*; import edu.gwu.util.*; import edu.gwu.geometry.*; import java.util.*;
Here's how it would work:
Here are the rectangles depicted in the region [(0,100), (100,0)]:
Algorithm: findIntersections (rectSet1, rectSet2) Input: two sets of rectangles containing n and m rectangles respectively // First, place data rectangles in rectSet1 into tree. 1. makeFilterTree (rectSet1) // Now scan rectangles in second set and query against tree. 2 for i = 0 to m // Get list of intersections from tree. 3. LinkedList intersectionSet = filterTreeSearch (rectSet2[i]); 4. if intersectionSet not empty 5. numIntersections += intersectionSet.size(); 6. Place intersections in intersectionSet into list of intersections; 7. endif 8. endfor 9. return all intersections;
Submission: