You are on page 1of 8

Do you Know?

Set 10 (page 40)


Name: _________________________________________________ Period: _______ The source code for the AbstractGrid class is in Appendix D. 1. Where is the isValid method specified? The method is mentioned in the Grid Interface. Which classes provide an implementation of this method? . The BoundedGrid and UnboundedGrid implement this method. 2. Which AbstractGrid methods call the isValid method? getValidAdjacentLocations() calls isvalid. Other methods then call from the former methods which means in turn, they also somewhat call the isValid() method. Why dont the other methods need to call it? The other methods call getValidAdjacentLocation() where its been already established. Which methods of the Grid interface are called in the getNeighbors method? It calls the grid methods and getOccupiedAdjacentLocations() methods. Which classes provide implementations of these methods? BoundedGrid and UnboundedGrid implement the getOccupiiedAdjacentLocations() method.

3. Why must the get method, which returns an object of type E, be used in the getEmptyAdjacentLocations method when this method returns locations, not objects of type E? The get() method returns the object. The getEmptyAdjacentLocations() calls the get() method and tests for null. Get() is only way to check if present or not.
2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

4. What would be the effect of replacing the constant Location.HALF_RIGHT with Location.RIGHT in the two places where it occurs in the getValidAdjacentLocations method? The amount of valid adjacent locations will decrease.

2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

Do you Know? Set 11 (page 41)

Name: _________________________________________________ Period: _______ The source code for the BoundedGrid class is in Appendix D. 1. What ensures that a grid has at least one valid location? If there arent any valid location, then an illegal argument will be put up. 2. How is the number of columns in the grid determined by the getNumCols method? The getNumCols() method will return the length column wise. What assumption about the grid makes this possible? This is assuming the grid has such a finite size. It would need to be bounded. 3. What are the requirements for a Location to be valid in a BoundedGrid? The location has to have row values greater than or equal to 0 and less than the number of rows in the grid. The column has to be positive and less than max. In the next four questions, let r = number of rows, c = number of columns, and n = number of occupied locations. 4. What type is returned by the getOccupiedLocations method? An arraylist is return by the getOccupiedLocations() method. What is the time complexity (Big-Oh) for this method? Explain. Accessing a two dimensional array, O(1). 5. What type is returned by the get method? The type returned for the get method is E, which is whatever type is stored in the occupantArray What parameter is needed? It requires a location object. What is the time complexity (Big-Oh) for this method? Explain.
2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

Accessing a two dimensional array will give you O(1); 6. What conditions may cause an exception to be thrown by the put method? If the location where an object is to be added is not in the grid (invalid location), an IllegalArgumentException will be thrown. If the object sent to the put method is null, a NullPointerException will be thrown. What is the time complexity (Big-Oh) for this method? Explain. The time complexity for the put method is O(1) 7. What type is returned by the remove method? The generic type E: whatever type is actually stored in the BoundedGrid object. What happens when an attempt is made to remove an item from an empty location? If an attempt is made to remove an item from an empty location, null is stored in the location and null is returned. It is not an error to call the Grid classs remove method on a location that is empty. What is the time complexity (Big-Oh) for this method? Explain. The time complexity for the remove method is O(1). 8. Based on the answers to questions 4, 5, 6, and 7, would you consider this an efficient implementation? Justify your answer.

Overall, yes. The only method that is inefficient is the getOccupiedLocations method, O(r * c). The other methods (put, get, and remove) are O(1). A BoundedGrid stores more than just its occupants. It also stores null values in the unoccupied locations. A bounded grid that only stored the occupants, while still offering O(1) access to the occupants, would be a better implementation. A HashMap could implement the BoundedGrid this way

2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

Do you Know? Set 12 (page 42)

Name: _________________________________________________ Period: _______ The source code for the UnboundedGrid class is in Appendix D. 1. Which method(s) must the Location class implement so that an instance of HashMap can be used for the map? What is required of that method(s)? Location class must have the hashCode and equals method. The hashcode must return the same value for two spots and are true via equals method. What would be required of the Location class if a TreeMap were used instead? Treemap requires keys of the map to be comparable. Does Location satisfy these requirements for HashMap and TreeMap? Location works out for both of them. 2. Why are the checks for null included in the get, put, and remove methods? The UnboundedGrid uses a Hashmap to hold item.s The isValid() method returns true all the time then. For a map object, null is perfectly legal as a key but not as a location. So, the UnboundedGrid methods put, get, and remove has to check for the location parameter and throw up an exception whenever a null is seen. Why are no such checks included in the corresponding methods for the BoundedGrid? The isValid() method is used before using a location to get the array in BoundedGrid. If location ends up null, an exception will be thrown out. 3. What is the average time complexity (Big-Oh) for the three methods: get, put, and remove? Explain. The average time complexity for the get, put, and remove is O(1) What would it be if a TreeMap were used instead of a HashMap? Explain. The average time would be O(log n) where n is the number of occupied locations.

2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

4. How would the behavior of this class differ, aside from time complexity, if a TreeMap were used instead of a HashMap? Explain. Most of the time, the getOccupiedLocations method will return the occupants in a different order. Keys (locations) in a HashMap are placed in a hash table based on an index that is calculated by using the keys hashCode and the size of the table. The order in a key is visited when the keySet is traveled depends on where it is located in the hash table. Could a map implementation be used for a bounded grid? Yes it can. What advantage, if any, would the map implementation have over the two-dimensional array implementation? Explain. If a hashmap was implemented for the bounded grid, the average time for getOccupiedLocations would be O(n), n being the number of items in grid. What advantage, if any, would the two-dimensional array implementation that is used by the BoundedGrid class have over a map implementation? Explain. The map implementation would need more memory. A two dimensional array would store the items; the locations combine the row and column as indices.

2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

Exercises Part 5 (page 41)


Name: _________________________________________________ Period: _______
1. Create a class SparseBoundedGrid that uses a sparse array implementation. There are

two possible implementations: a. Create the sparse array using an array of linked lists. Each entry in the array is a linked list of SparseGridNodes, or is null if that row is empty. Each linked list entry will be a SparseGridNode which holds a grid occupant, a column index, and the next SparseGridNode. OR b. Create the sparse array using an array list of linked lists. Each entry in the array list is a LinkedList<OccupantInCol>. Each linked list entry will be an OccupantInCol which holds a grid occupant and a column index. For a grid with r rows and c columns, the sparse array has length r. Each of the linked lists has maximum length c. Why is this a more time-efficient implementation than BoundedGrid? If a program had a large grid and always uses the getOccupiedLocations() method, the time complexity will be O(r+n), r being the rows, n being the number of items. The time complexity for the BoundedGrid implementation is O(r*c), r being again the rows and this time c being columns. 2. Consider using a HashMap or TreeMap to implement the SparseBoundedGrid. How could you use the UnboundedGrid class to accomplish this task? Which methods of UnboundedGrid could be used without change? Most of the code can be used like getOccupiedLocations(), get(), remove(), etc. Fill in the following chart to compare the expected Big-Oh efficiencies for each implementation of the SparseBoundedGrid. Let r = number of rows, c = number of columns, and n = number of occupied locations
Methods getNeighbors SparseGridNode LinkedList HashMap TreeMap

O(c)

O(c)

O(1)

O(log n)

2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

getEmptyAdjacentLocations getOccupiedAdjacentLocations getOccupiedLocations get put remove

O(c) O(c) O(r+n) O(c) O(c) O(c)

O(C) O(c) O(r+n) O(c) O(c) O(c)

O(1) O(1) O(n) O(1) O(1) O(1)

O(log n) O(log n) O(n) O(log n) O(log n) O(log n)

3. Consider an implementation of an unbounded grid in which all valid locations have non-

negative row and column values. The constructor allocates a 16 x 16 array. When a call is made to the put method with a row or column index that is outside the current array bounds, double both array bounds until they are large enough, construct a new square array with those bounds, and place the existing occupants into the new array. Implement the methods specified by the Grid interface using this data structure. What is the Big-Oh efficiency of the get method? Explain. O(1), just use the graph above What is the efficiency of the put method when the row and column index values are within the current array bounds? Explain. It will be O(1) because same as above What is the efficiency when the array needs to be resized? Explain. The efficiency will be O(x*a) where x is the new amount of rows and a the number of columns.

2007 The College Board. All rights reserved. Visit apcentral.collegeboard.com (for AP professionals) and www.collegeboard.com/apstudents (for AP students and parents).

You might also like