Download presentation
Presentation is loading. Please wait.
1
CS100J Lecture 13 Previous Lecture Java constructs
for( initialized-declaration; … ) throw Programming Concepts Multiple class implementations Use of an array for maintaining a list of items Exception handling This Lecture Additional material on the use of arrays and alternative implementations of a class The use of auxiliary private methods Searching CS 100 Lecture 13
2
Motivating Example In many applications, we need to maintain a list of natural numbers, where each natural number may occur more than once. Such a list is called a “multi-set” of natural numbers. For example, the set of grades obtained on an exam is such a multi-set. Specification of class NatMultiset //Construct NatMultiset of items <= maxValue. public NatMultiset(int maxValue) // Insert item into multiset. public void insert(int item) // Is item in multiset? public boolean isElement(int item) // Delete item from multiset. public void delete(int item) // Multiplicity of item in multiset. public int multiplicity(int item) CS 100 Lecture 13
3
Frequencies of Grades, revisited
int grade; // the grade being processed. // Let histogram be represented by a NatMultiset. NatMultiset hist = new NatMultiset(100000); /* “Process” grades until (but not including) a stopping signal of -1. */ grade = in.readInt(); while (grade != -1 ) { hist.insert(grade); } /* Print histogram. */ System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100000; grade++ ) System.out.println( grade + ” ” + hist.multiplicity(grade) ); CS 100 Lecture 13
4
Motivation for a Third Implementation
As before: Grades are in the range ,000. The are only 350 students in the class. However: It is very likely that the number of distinct grades will be quite small. Shortcomings of second implementation: Wasted time searching for all duplicate elements. Arbitrary upper bound on maxSize The application still wastes time iterating over 100,001 different grades, most of which have multiplicity 0. CS 100 Lecture 13
5
Third Implementation of natMultset
Representation Invariant: The distinct multiset elements are list[0..currentSize-1], where currentSize <= maxSize, and the multiplicity of element list[i] is freq[i]. currentSize maxSize list freq currentSize maxSize . . . ? ? ? ? ? ? . . . ? ? ? ? ? public class NatMultiset { private int currentSize; private int maxSize; private int[] list; private int[] freq; . . . } CS 100 Lecture 13
6
Constructor NatMultiset
/* Construct a NatMultiset for up to maxElements elements in the range 0 through maxValue. */ public NatMultiset( int maxValue ) { maxSize = _____________________; list = new int[maxSize+1]; freq = new int[maxSize]; currentSize = 0; } Running time: proportional to maxSize CS 100 Lecture 13
7
Auxiliary Method search
/* Return the index of the leftmost occurrence of item in list, or currentSize if item isn't in list. */ private int search(int item) { list[currentSize] = item; int k = 0; while (list[k] != item) k++; return k; } Running time: Worst case proportional to currentSize CS 100 Lecture 13
8
Method insert Running time: Worst case proportional to currentSize
/* Insert item into the multiset; throw RuntimeException if out of space. */ public void insert(int item) { int k = search(item); if (k != currentSize) /* item is already in multiset. */ freq[k]++; else if ( currentSize!=maxSize ){ /* First occurrence of item. */ list[currentSize] = item; freq[currentSize] = 1; currentSize++; } else throw new RuntimeException( "NatMultiset too large for bounds” ); Running time: Worst case proportional to currentSize CS 100 Lecture 13
9
Method isElement Running time: Worst case proportional to currentSize
/* Return true if the multiset contains 1 or more instances of item. */ public boolean isElement(int item) { return search(item) != currentSize; } Running time: Worst case proportional to currentSize CS 100 Lecture 13
10
Method delete Running time: Worst case proportional to currentSize
/* Delete one instance of item from the multiset, if there is one. */ public void delete(int item) { int k = search(item); if ( k != currentSize ) if (freq[k] > 1) freq[k]--; else { /* Last occurrence of item. */ currentSize--; list[k] = list[currentSize]; freq[k] = freq[currentSize]; } Running time: Worst case proportional to currentSize CS 100 Lecture 13
11
Method multiplicity /* Return the number of occurrences of item in the multiset. */ public int multiplicity(int item) { int k = search(item); if (k != currentSize) return freq[k]; else return 0; } Running time: Worst case proportional to currentSize CS 100 Lecture 13
12
Removing arbitrary size limitations
Recall the arbitrary initialization of maxSize. Plan: instead of aborting execution when there is no more room, double the size of the arrays when needed. CS 100 Lecture 13
13
Doubling the Capacity /* Double maxSize and the lengths of both list and freq. */ private void stretch() { int newSize = 2 * maxSize; // Allocate new arrays. int[] new_list = new int[newSize+1]; int[] new_freq = new int[newSize]; // Copy old arrays to new arrays. for (int k = 0; k < currentSize; k++) new_list[k] = list[k]; new_freq[k] = freq[k]; } // Adopt new arrays. list = new_list; freq = new_freq; maxSize = newSize; CS 100 Lecture 13
14
Method insert, revisited
/* Insert item into the multiset; throw RuntimeException if out of space. */ public void insert(int item) { int k = search(item); if (k != currentSize) /* item is already in multiset. */ freq[k]++; else { /* First occurrence of item. */ if (currentSize == maxSize) stretch(); list[currentSize] = item; freq[currentSize] = 1; currentSize++; } Running time: Worst case proportional to currentSize CS 100 Lecture 13
15
Major Shortcoming The interface of NatMultset forces us to iterate through entire range of value. /* Print histogram. */ System.out.println(”Grade Frequency”); for ( grade = 0; grade<=100000; grade++ ) System.out.println( grade + ” ” + hist.multiplicity(grade) ); Need to redesign the interface to support more efficient retrieval of elements in the multiset. CS 100 Lecture 13
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.