Upload
sreedhar-aakula
View
221
Download
0
Embed Size (px)
Citation preview
8/4/2019 10. Sets & Maps
1/72
ADSA: Sets&Maps/10 1
241-423 Advanced Data Structures
and Algorithms
Objectives implement sets and maps
Semester 2, 2010-2011
10. Sets and Maps
8/4/2019 10. Sets & Maps
2/72
ADSA: Sets&Maps/10 2
Contents1. Set and Map Interfaces
2. The TreeSet Collection
3. A Spell Checker
4. Four Set Operators5. Ordered Set Operators
6. Maps
7. The TreeMap Collection8. Map Collection Views
9. Create a Concordance
8/4/2019 10. Sets & Maps
3/72
ADSA: Sets&Maps/10 3
1. Set and Map Interfaces
The dashed lines indicate that the class implementsthe interface.
The Map interface is separate, since if defines
methods not relevant to general collections.
8/4/2019 10. Sets & Maps
4/72
ADSA: Sets&Maps/10 4
The Set Interface
Set implements the Collection interface,
requiring each element to be unique.
8/4/2019 10. Sets & Maps
5/72
ADSA: Sets&Maps/10 5
The Map Interface
A map stores an element as a key-value pair
the first field is a key which uniquely identifies the
pair
the second field is a value (an object)
8/4/2019 10. Sets & Maps
6/72
ADSA: Sets&Maps/10 6
A map of String-Integer pairs to denote the
number of students in each of department:
Uses the key to access the value field of a pair.
e.g. deptMap.get("English") returns 117
8/4/2019 10. Sets & Maps
7/72ADSA: Sets&Maps/10 7
2. The TreeSet Collection
TreeSet is implemented by a Binary Search
Tree (BST).
Since BSTs store elements in order, it
implements the OrderedSet interface
it extends the Set interface, adding first() and
last().
8/4/2019 10. Sets & Maps
8/72ADSA: Sets&Maps/10 8
class TreeSet implements OrderedSet ds.util
Constructor
TreeSet()
Creates an empty ordered set whose elements have a specified type that
must implement the Comparable interface
Methods
T first()
Returns the minimum value of the elements in the set
T last()
Returns the maximum value of the elements in the set
String toString()
Returns a string containing a comma separated ordered list of elementsenclosed in brackets.
and others from the
Set interface
8/4/2019 10. Sets & Maps
9/72ADSA: Sets&Maps/10 9
3. A Spell Checker
A set is an ideal data structure for the
implementation of a simple spelling checker.
a dictionary setis constructed from the file
"dict.txt" which contains about 25,000 words
Each word in the input is checked against the
dictionary set. If the word is not in the set, then
the word is assumed to be misspelled.
8/4/2019 10. Sets & Maps
10/72ADSA: Sets&Maps/10 10
public static void spellChecker(String filename, Scanner keyIn)
{
// sets for the dictionary and misspelled words
TreeSet dictionary = new TreeSet();
TreeSet misspelledWords = new TreeSet();
Scanner dictFile=null, docFile=null;
try
{
// dictionary, document, input streams
dictFile = new Scanner(new FileReader("dict.txt"));docFile = new Scanner(new FileReader(filename));
}
catch(FileNotFoundException e){
System.err.println(e);
System.exit(1);
}
// insert each word from file "dict.txt" into set
while(dictFile.hasNext()) {
String word = dictFile.next();
dictionary.add(word);
}
:
8/4/2019 10. Sets & Maps
11/72ADSA: Sets&Maps/10 11
while(docFile.hasNext()) {
String word = docFile.next(); // get word from input
if (!dictionary.contains(word)) { // lookup word
System.out.println(word);
System.out.print(" 'a'(add) 'i'(ignore) " +
"'m'(misspelled) ");
String response = keyIn.next();
if (response.charAt(0) == 'a') // add to dict
dictionary.add(word);
else if (response.charAt(0) == 'm') // add to misspells
misspelledWords.add(word);
}}
System.out.println("\nMisspelled words: " +
misspelledWords);
} // end of spellChecker()
8/4/2019 10. Sets & Maps
12/72ADSA: Sets&Maps/10 12
UseSpellCheckerimport java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import ds.util.TreeSet;
public class UseSpellChecker{
public static void main(String[] args)
{
Scanner keyIn = new Scanner( System.in);
System.out.print("Enter file to spell check: ");
String fileName = keyIn.next();
spellChecker(fileName, keyIn);
}
// spellchecker() method goes here
} // end of UseSpellChecker class
8/4/2019 10. Sets & Maps
13/72ADSA: Sets&Maps/10 13
Files
8/4/2019 10. Sets & Maps
14/72
ADSA: Sets&Maps/10 14
Execution
8/4/2019 10. Sets & Maps
15/72
ADSA: Sets&Maps/10 15
4. Four Set Operators
8/4/2019 10. Sets & Maps
16/72
ADSA: Sets&Maps/10 16
Implementation of Set Ops
public static Set setOp(Set setA, Set setB)
{
Set returnSet;
/* allocate concrete collection for returnSet:a TreeSet or HashSet object (see slide 3)
depending on the input argument type */
if (setA instanceof OrderedSet)
returnSet = new TreeSet();
elsereturnSet = new HashSet();
. . .
return returnSet;
}
General code, required
by all the set operators
8/4/2019 10. Sets & Maps
17/72
ADSA: Sets&Maps/10 17
public static Set union(Set setA, Set setB)
{
Set setUnion;
// allocate concrete collection object for setUnion
. . .
// use iterator to add elements from setA
Iterator iterA = setA.iterator();
while (iterA.hasNext())
setUnion.add(iterA.next());
// use iterator to add non-duplicate elems from setB
Iterator iterB = setB.iterator();
while (iterB.hasNext())setUnion.add(iterB.next());
return setUnion;
} // end of union()
8/4/2019 10. Sets & Maps
18/72
ADSA: Sets&Maps/10 18
public static Set intersection(Set setA, Set setB)
{
Set setIntersection;
T item;
// allocate concrete collection object for setIntersection
. . .
/* scan elements in setA and check whetherthey are also elements in setB */
Iterator iterA = setA.iterator();
while (iterA.hasNext()) {
item = iterA.next();
if (setB.contains(item)) // is item in both sets?
setIntersection.add(item);}
return setIntersection;
} // end of intersection()
8/4/2019 10. Sets & Maps
19/72
ADSA: Sets&Maps/10 19
public static Set difference(Set setA, Set setB)
{
Set setDifference;
T item;
// allocate concrete collection object for setDifference
. . .
/* scan elements in setA and check whether
they are not in setB */
Iterator iterA = setA.iterator();
while (iterA.hasNext()) {
item = iterA.next();
if (!setB.contains(item)) // is A item not in B?setDifference.add(item);
}
return setDifference;
} // end of difference()
8/4/2019 10. Sets & Maps
20/72
ADSA: Sets&Maps/10 20
public static boolean subset(Set setA, Set setB)
// is setA a subset of setB?
{
return ( intersection(setA, setB).size() == setA.size() );
// is all of setA in the intersection with setB?
}
setBsetA
8/4/2019 10. Sets & Maps
21/72
ADSA: Sets&Maps/10 21
4.1. UsingSets
import java.io.*;import java.util.Scanner;
import ds.util.Sets;
import ds.util.TreeSet;
import ds.util.Set;
public class UsingSets
{
public static void main(String[] args)
{
// declare ordered sets for current and new accountsSet oldAcct = new TreeSet();
Set currAcct = new TreeSet();
:
8/4/2019 10. Sets & Maps
22/72
ADSA: Sets&Maps/10 22
// input names from file into the set
try{
readAccounts("oldAcct.txt", oldAcct);
readAccounts("currAcct.txt", currAcct);
}
catch(IOException e)
{ System.err.println("Cannot open account files");System.exit(1);
}
// use set union to determine all accounts to update
Set processAcct = Sets.union(currAcct, oldAcct);
:
8/4/2019 10. Sets & Maps
23/72
ADSA: Sets&Maps/10 23
// determine carryover accounts using intersectionSet carryOverAcct =
Sets.intersection(currAcct, oldAcct);
// find new and obsolete accounts using difference
Set newAcct = Sets.difference(currAcct, oldAcct);
Set obsoleteAcct = Sets.difference(oldAcct, currAcct);
// list elements in sets
System.out.println("Old Accounts: " + oldAcct);
System.out.println("Current Accounts: " + currAcct);
System.out.println("Process Accounts: " +processAcct);
System.out.println("New Accounts: " + newAcct);System.out.println("Carryover Accounts: " +carryOverAcct);
System.out.println("Obsolete Accounts: " +obsoleteAcct);
} // end of main()
8/4/2019 10. Sets & Maps
24/72
ADSA: Sets&Maps/10 24
public static void readAccounts(String filename,Set t) throws IOException
{
Scanner sc = new Scanner(new FileReader(filename));
// input the set of current accounts
while(sc.hasNext())t.add( sc.next() ); // add an account name
} // end of readAccounts()
} // end of UseSets class
8/4/2019 10. Sets & Maps
25/72
ADSA: Sets&Maps/10 25
Execution
8/4/2019 10. Sets & Maps
26/72
ADSA: Sets&Maps/10 26
5. Ordered Set Operators
If a set is ordered, the set operators can be
implemented by using an iterator that scans the
set faster.
TreeSet uses ordered sets because it
implements the OrderedSet interface
see slide 3
8/4/2019 10. Sets & Maps
27/72
ADSA: Sets&Maps/10 27
Ordered Set Intersection
Ordered set intersection uses iterators to make
apairwise scan of the elements in the two sets.
at each step, a comparison is made between twoelements in the two sets
if a match is found, then the element is copied to
the intersection set
8/4/2019 10. Sets & Maps
28/72
ADSA: Sets&Maps/10 28
8/4/2019 10. Sets & Maps
29/72
ADSA: Sets&Maps/10 29
advance()
private static T advance(Iterator iter)
/* if more elements remain, return the
next value; otherwise return null */
{
T value = null;
if (iter.hasNext())
value = iter.next();
return value;
}
A support method for
orderedIntersection()
8/4/2019 10. Sets & Maps
30/72
ADSA: Sets&Maps/10 30
orderedIntersection()
public static
8/4/2019 10. Sets & Maps
31/72
ADSA: Sets&Maps/10 31
// move forward as long as we have not
// reached the end of either set
while (lhsValue != null && rhsValue != null) {
if (lhsValue.compareTo(rhsValue) < 0)
lhsValue = advance(lhsIter); // next value in lhs
else if (rhsValue.compareTo(lhsValue) < 0)
rhsValue = advance(rhsIter); // next value in rhs
else { // lhsValue == rhsValue
setIntersection.add(lhsValue); // add to inter
lhsValue = advance(lhsIter); // next lhs
rhsValue = advance(rhsIter); // next rhs
}
}
return setIntersection;
} // end oforderedIntersection()
8/4/2019 10. Sets & Maps
32/72
ADSA: Sets&Maps/10 32
Complexity of orderdIntersection()
Assume that nlhs and nrhs are the number of
elements in setA and setB.
Each iteration of the loop makes one or two
comparisons
assume these occur in O(1) running time
8/4/2019 10. Sets & Maps
33/72
ADSA: Sets&Maps/10 33
We must make at most nlhs + nrhs
comparisons, so the algorithm has
worst-case running time O(nlhs + nrhs).
So the algorithm is linearwith respect to
the total number of elements in the two sets.
8/4/2019 10. Sets & Maps
34/72
ADSA: Sets&Maps/10 34
6. Maps
A map stores data in key-value pairs. A key acts like an index to locate the
corresponding value in the map
a map is also called an associative array
8/4/2019 10. Sets & Maps
35/72
ADSA: Sets&Maps/10 35
interface MAP (partial) ds.utilvoid clear()
Removes all mappings from this map.
boole
an
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
boole
an
isEmpty()
Returns true if this map contains no key-value mappings.V remove(Object key)
Removes the mapping for this key from this map if present. Returns the previous
value associated with specified key, or null if there was no mapping for key.
int size()
Returns the number of key-value mappings in this map.
Access/Update MethodsK get(Object key)
Returns the value to which this map maps the specified key or null if the map
contains no mapping for this key.
Vput(K key, V value)
Associates the specified value with the specified key in this map. Returns the
previous value associated with key, or null if there was no mapping for key.
The Map Interface
V
K
K
K
8/4/2019 10. Sets & Maps
36/72
ADSA: Sets&Maps/10 36
size(), isEmpty(), and clear() are like those in
the Collection interface.
A map does not have an iterator to scan its
elements
instead, keySet() and entrySet() return the keys and
the entries in a map as a set (see later)
8/4/2019 10. Sets & Maps
37/72
ADSA: Sets&Maps/10 37
7. The TreeMap Collection
TreeMap is an ordered collection that
accesses elements in ascending order of its
keys.
The class implements the OrderedMap
interface (see slide 3) firstKey() and lastKey() return the values
corresponding to the minimum and maximum
keys
8/4/2019 10. Sets & Maps
38/72
8/4/2019 10. Sets & Maps
39/72
ADSA: Sets&Maps/10 39
Brief TreeMap Example
// arrays for class names and their enrollment (student nos)
String[] className = {"ECON 101","CS 173","ENGL 25"};
int[] enrollment = {85, 14, 30};
// create a TreeMap object
TreeMap tm = new TreeMap();for(int i = 0; i < 3; i++)
tm.put(className[i], enrollment[i]);
8/4/2019 10. Sets & Maps
40/72
ADSA: Sets&Maps/10 40
7.1. Student Working Hours Map
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import ds.util.TreeMap;
import ds.time.Time24;
public class CalcHours
{
public static void main(String[] args)
{
TreeMap workMap =new TreeMap();
:
the key-value pair is
student name and
hours worked
8/4/2019 10. Sets & Maps
41/72
ADSA: Sets&Maps/10 41
/* access the student info file:
each line consists of a student name and
their work time in hours and mins
*/
Scanner fin = null;try { // load student info
fin = new Scanner(new FileReader("studwk.txt"));
}
catch (FileNotFoundException e) {
System.err.println("Cannot open "\"studwk.dat\"");
System.exit(1);}
:
8/4/2019 10. Sets & Maps
42/72
ADSA: Sets&Maps/10 42
// input names and times
while (fin.hasNext()) {
String studName = fin.next();
// get hours and minutes from the input line
int hours = fin.nextInt();
int mins = fin.nextInt();
Time24 workTime = new Time24(hours,mins);
// access entry for the student name
Time24 timeValue = workMap.get(studName);
:
8/4/2019 10. Sets & Maps
43/72
ADSA: Sets&Maps/10 43
if (timeValue == null) // add new entryworkMap.put(studName, workTime);
else { // update existing entry
timeValue.addTime(hours*60 + mins);
workMap.put(studName, timeValue);
}
}
// display the work Map
System.out.println("Student-Time: " + workMap);
} // end of main()
} // end of CalcHours class
8/4/2019 10. Sets & Maps
44/72
ADSA: Sets&Maps/10 44
Execution
8/4/2019 10. Sets & Maps
45/72
ADSA: Sets&Maps/10 45
7.2. A Software Map
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import ds.util.TreeMap;
import ds.util.TreeSet;
public class MakeSoftMap
{
public static void main(String[] args)
{TreeMap softwareMap =
new TreeMap();
:
the key-value pair is
company name and
a set of software names
8/4/2019 10. Sets & Maps
46/72
ADSA: Sets&Maps/10 46
/* access the product info file:
each line consists of a company name and
software product name, separated by a tab
*/
Scanner fin = null;
try { // load product info
fin = new Scanner(new FileReader("product.txt"));
fin.useDelimiter("[\t\n\r]+");
}
catch (FileNotFoundException e){
System.err.println("Cannot open "\"product.txt\"");
System.exit(1);
}
:
8/4/2019 10. Sets & Maps
47/72
ADSA: Sets&Maps/10 47
while(fin.hasNext()){
String company = fin.next();
String product = fin.next();
// look for software set for the company
TreeSet prodSet = softwareMap.get(company);
// if no entry found, then create empty software set
if (prodSet == null)prodSet = new TreeSet();
prodSet.add(product); // add product name to set
softwareMap.put(company, prodSet); // add entry
}
// display contents of software Map
System.out.println(softwareMap);
} // end of main()
} // end of MakeSoftMap class
8/4/2019 10. Sets & Maps
48/72
ADSA: Sets&Maps/10 48
Execution
8/4/2019 10. Sets & Maps
49/72
ADSA: Sets&Maps/10 49
8. Map Collection Views
A map does not have an iteratorfor accessing
its elements.
Instead we can use two collection views, which
are sets that act on the original map
the keySet collection view
the entrySet collection view
The original map is sometimes called the
backing collection.
8/4/2019 10. Sets & Maps
50/72
ADSA: Sets&Maps/10 50
In Map, keySet() returns a set of map keys.
This set is a collection view for the map.
Set keys = peopleMap.keySet();
8.1. The keySet Collection View
8/4/2019 10. Sets & Maps
51/72
ADSA: Sets&Maps/10 51
Deletion
Deleting a key from the set removes the
corresponding entry from the map.
8/4/2019 10. Sets & Maps
52/72
ADSA: Sets&Maps/10 52
The Set interface defines an add() operation,
but this makes no sense for a keySet view:
add() would have to add a key-value pair to thebacking collection, but what value should be
added?
Instead add() throws an
UnsupportedOperationException in a keySetview
Insertion
8/4/2019 10. Sets & Maps
53/72
ADSA: Sets&Maps/10 53
8.2. The entrySet Collection View
In Map, entrySet() returns a view called an
entry set, is a set of key-value entries.
The entries implement the Map.Entry interface
an interface inside the Map interface
Set< Map.Entry > entriesSet =peopleMap.entrySet();
8/4/2019 10. Sets & Maps
54/72
ADSA: Sets&Maps/10 54
entriesSet is a set of Map.Entry objects from
the peopleMap.
Operations on the set affect the map.
interface MAP.ENTRY ds.util.Map
K getKey()
Returns the key corresponding to this entry..
V getValue()
Returns the value corresponding to this entry..
V setValue(V value)
Replaces the value corresponding to this entry with the specified value. Returns
the old value corresponding to the entry
8/4/2019 10. Sets & Maps
55/72
ADSA: Sets&Maps/10 55
confTimeMap holds conference activities andtheir times.
TreeMap confTimeMap =
new TreeMap();
confTimeMap.put("Session 1", new Time24(9,30));
confTimeMap.put("Session 2", new Time24(14,00));
confTimeMap.put("Lunch", new Time24(12,0));
confTimeMap.put("Dinner", new Time24(17,30));
Entry Set View Example
confTimeMap
8/4/2019 10. Sets & Maps
56/72
ADSA: Sets&Maps/10 56
// create an entry set for confTimeMap
Set< Map.Entry > entries =
confTimeMap.entrySet();
entries set
8/4/2019 10. Sets & Maps
57/72
ADSA: Sets&Maps/10 57
8.3. Scanning a Map's Entries
A Map.Entry set can be used to define an
iteratorfor scanning the map entries.
The iterator can access an entry's key and
access/update an entry's value.
The iterator remove() method removes anentry from the map
8/4/2019 10. Sets & Maps
58/72
ADSA: Sets&Maps/10 58
Entry Set Iterators
An entry set iterator provides a way to scan the
entries in a map.
The iterator references a Map.Entry element in
the map
the programmer can use Map.Entry methods to
access the map components
8/4/2019 10. Sets & Maps
59/72
ADSA: Sets&Maps/10 59
// create an entry set for confTimeMap
Set< Map.Entry > entries =
confTimeMap.entrySet();
// create an iterator for the entry set
Iterator< Map.Entry > iter =entries.iterator();
Examples
entries set
iter
iterator
continued
8/4/2019 10. Sets & Maps
60/72
ADSA: Sets&Maps/10 60
map entriesset
entriesiterator
changes will affect
original map
8/4/2019 10. Sets & Maps
61/72
ADSA: Sets&Maps/10 61
Delay all activities by 30 minutes.
// scan map entries
while (iter.hasNext()) {
Map.Entry me = iter.next();// get next Map.Entry object
Time24 t = me.getValue();
t.addTime(30); // add 30 mins
me.setValue(t); // update map entry
}
8/4/2019 10. Sets & Maps
62/72
ADSA: Sets&Maps/10 62
for (Map.Entry i : entries) {
String activity = (String)i.getKey();
if (activity.indexOf("Session") != -1) //is activity a session?
System.out.println("Activity " + activity +" Starting time " + i.getValue());
}
Activity Session 1 Starting time 10:00
Activity Session 2 Starting time 14:30
List session starting time.
8/4/2019 10. Sets & Maps
63/72
ADSA: Sets&Maps/10 63
9. Create a Concordance
A concordance is a list of all the unique
words from a file along with the line
numbers where the words appear.
We implement the concordance as a
TreeMap.
8/4/2019 10. Sets & Maps
64/72
ADSA: Sets&Maps/10 64
Input File
8/4/2019 10. Sets & Maps
65/72
8/4/2019 10. Sets & Maps
66/72
ADSA: Sets&Maps/10 66
import java.io.*;
import java.util.regex.*;
import java.util.StringTokenizer;
import java.util.Scanner;import ds.util.*;
public class CreateConcordance
{
private static Pattern identifierPattern =
Pattern.compile("[a-zA-Z][a-zA-Z0-9]*");
public static void main(String[] args) throws IOException
{
System.out.print("Enter the file name: ");
Scanner keyIn = new Scanner(System.in);
String filename = keyIn.nextLine();
System.out.println();
concordance(filename); // create concordance
} // end of main()
8/4/2019 10. Sets & Maps
67/72
ADSA: Sets&Maps/10 67
public static void concordance(String filename)
throws IOException
{
// create the concordance treemap
TreeMap concordanceMap =
new TreeMap();
// create scanner to input from document file
Scanner fin = new Scanner(new FileReader(filename));:
the key-value pair is
identifier name and
a set of line numbers
8/4/2019 10. Sets & Maps
68/72
ADSA: Sets&Maps/10 68
// read the file a line at a timeint lineNumber = 0;
while(fin.hasNext()) {
String inputLine = fin.nextLine(); // read a line
lineNumber++;
// create matcher to find identifiers in line
Matcher matcher = identifierPattern.matcher(inputLine);
// extract identifiers until end of line
while (matcher.find()) {
String ident = inputLine.substring(
matcher.start(), matcher.end());
:
8/4/2019 10. Sets & Maps
69/72
ADSA: Sets&Maps/10 69
// find line number set for the identifier
TreeSet lineNums =
concordanceMap.get(ident);
if (lineNums == null) // no set; make one
lineNums = new TreeSet();
// add line number to set
lineNums.add(lineNumber);concordanceMap.put(ident, lineNums);
}
}
// output the concordance
writeConcordance(concordanceMap);
} // end of concordance()
8/4/2019 10. Sets & Maps
70/72
ADSA: Sets&Maps/10 70
public static void writeConcordance(
TreeMap map){
// create entry view for the map
Set entries =
map.entrySet();
// create iterator over the entry view
Iterator iter =entries.iterator();
while (iter.hasNext()) {
Map.Entry e = iter.next();
System.out.print( e.getKey() ); // output key
// pad output to 12 characters using blanks
if (e.getKey().length() < 12)
for (int i=0;i < 12 - (e.getKey().length()); i++)
System.out.print(' ');
:
8/4/2019 10. Sets & Maps
71/72
ADSA: Sets&Maps/10 71
// extract map value as a TreeSetTreeSet lineNumberSet = e.getValue();
// display identifier and line numbers
System.out.print(formatInt(4, lineNumberSet.size()) +
": ");
// iterate over TreeSet of line numbers
Iterator setIter = lineNumberSet.iterator();
while (setIter.hasNext())
System.out.print( setIter.next() + " ");
System.out.println();
}System.out.println();
}
8/4/2019 10. Sets & Maps
72/72
private static String formatInt(int w, int n)
/* returns a formatted string with integer n
right-justified in a field of w spaces;
used to line up output in concordance
*/{ . . . }
} // end of CreateConcordance class