last class. to organize data to be able to perform operations on those data efficiently. data...

74
Last Class

Upload: egbert-price

Post on 27-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Last Class

• To organize data to be able to perform operations on those data efficiently.

Data Structure

• Could be implemented in many different ways.

•The efficiency depend on the particular application.

• It is a step-by-step procedure for solving a problem in a finite amount of time.

Algorithm

• Its running time depend on the input size and on the input data

•The best case is the input that cause the lower running time and the worst case the input that cause the biggest running time.

• By inspecting the pseudo code, we count the number of primitive operation to analyze the running time.

Algorithm

• We compare two algorithm by their asymptotic behaviour of their running time when the size of the input grow.

Asymptotic Behaviour

•Big Oh define an asymptotically upper bound

•Big Omega define an asymptotically lower bound

• A function f(n) is Θ(g(n)) if it is both big Oh and big Omega.

Comming up

The Java Collections Framework

Generic algorithms that work on one or more interfaces -regardless of implementation

The JCF consists of:•Interfaces

The basic data types and their operations( Set, List, Map,...)

•ImplementationsClasses that implement interfacesDifferent implementations have different performance( tradeoffs )

•Algorithms

Java generics

Many classes, interfaces, and methods are genericThey work on a variety of data types

Each instance works for a specific data typeArrayList<String> a = new ArrayList<String>();a.add("Hello");a.add("World!");System.out.println(a);

ArrayList<Date> b = new ArrayList<Date>();b.add(new Date(0));b.add(new Date());System.out.println(b);

The Java Collections Framework

( Interfaces )

Collection Map

Set List Queue

SortedSet

SortedMap

Two hierarchies:• Collection:

− stores individual objects1 generic parameter (type of object stored)

• Map:− stores key/value pairs2 generic parameters (type of key and type of value)

Collection

• A group of objects− no particular order− possibly with duplicates

• Basic operations− size, isEmpty, contains, add, remove, iterator

• Bulk operations− containsAll, addAll, removeAll, retainAll, clear

• Array operations− toArray

11

1. Collection<String> names = new ArrayList<String>();

2. names.add("sheldon");3. names.add("penny");4. System.out.println(names);5. System.out.println(names.size()); // 26. System.out.println(names.contains("leonard")); //

false7. String[] newNames =

{"howard","raj","wil","leslie","penny"};8. for (String s : newNames ) names.add(s);9. names.addAll(Arrays.asList(newNames));10. System.out.println(names.size()); // ?

System.out.println(names); // ?

Collection (example)

12

// an example of the Collection iteration idiompublic static void prettyPrintStrings(Collection<String> c) { Iterator<String> i = c.iterator(); while (i.hasNext()) { System.out.print("**" + i.next() + "**"); } System.out.println();}

Iterators

All Collections have iterators− An iterator allows for listing the elements of a collection one by one

13

// an example of a method that operates on generic collectionspublic static <T> void prettyPrint(Collection<T> c) { Iterator<T> i = c.iterator(); while (i.hasNext()) { System.out.print("**" + i.next() + "**"); } System.out.println();}

Generic (example)

If we don't care what kind of objects are in a Collection we can write a method that operates on generic collections

Collection Summary

• Sets, Lists, and Queues treat these differently

• For a Collection we cannot know

−what happens to duplicate values−what order an iterator reports the elements in

Set

• All elements are unique− An element only occurs once in a set - even if added multiple times

• No order is associated with the elements− The order of elements in an iteration is unspecified

• Supports the same operations as Collection− size, isEmpty, contains, add, remove, iterator, containsAll, addAll, removeAll, retainAll, clear, toArray

A mathematical set

16

// Print the elements in a without printing any element twice

public static <T> void printWithoutDuplicates(T[] a) { Set<T> s = new HashSet<T>(); for (T x : a) { if (!s.contains(x)) { s.add(x); System.out.println(x); } }}

Set (example)

In what order are the elements printed?

17

// Print the elements in c without printing any element twice

public static <T> void printWithoutDups(Collection<T> c) { Set<T> s = new HashSet<T>(); for (T x : c) { if (!s.contains(x)) { s.add(x); System.out.println(x); } }}

Set (example for a Collection)

18

// Removes duplicate elements from an array// Extra array positions are set to nullpublic static <T> void removeDuplicates(T[] a) { Set<T> s = new HashSet<T>(); for (T x : a) { s.add(x); } s.toArray(a);}

Set (example)

In what order do elements appear in the output array?

// Removes duplicate elements from an array// Extra array positions are set to nullpublic static <T> void removeDuplicates(T[] a) {…}

19

• Implement the following function• Input: an array a• Output: print each element of a that occurs

exactly once– Don't print any element not in a– Don't print any element that occurs more than once

in a

In-Class Exercise

20

// Sort a and remove any duplicate elements// Fill extra positions with nullpublic static <T> void sortAndRemoveDups(T[] a) { SortedSet<T> s = new TreeSet<T>(); for (T x : a) s.add(x); s.toArray(a);}

SortedSet•Just like Set, but elements are in sorted order

•We can use the natural ordering or define a Comparable object (more on this later)

An iterator outputs the elements in sorted order

21

SortedSet<String> snames = new TreeSet<String>();String[] names = hlprsw {"sheldon”,"penny“,"howard","raj","wil","leslie","penny"};snames.addAll(names);System.out.println(snames);System.out.println(snames.subSet("leslie", "sheldon"));System.out.println(snames.headSet("penny"));System.out.println(snames.tailSet("penny"));

SortedSet• A SortedSet allows for range view

− subSet(from,to), headSet(to), tailSet(from)− first(), last()

22

// Return a list of possible completions for a string spublic void findCompletions(SortedSet<String> words, String s) { SortedSet<String> ts = words.tailSet(s); Iterator<String> i = ts.iterator(); String w; while (i.hasNext() && (w = i.next()).startsWith(s)) System.out.println(w);}

SortedSet• SortedSet supports searches for elements not in the set

− s.headSet(x).last() is the largest value less than x− s.tailSet(x).first() is the smallest value greater than or equal to x

Set Summary• Elements are unique.

• No order is associated with the elements.• Support the same operation that Collection

SortedSet Summary• Like Set, but elements are in sorted order

• Support range view operations

• supports searches for elements not in the set

List

• Represents a sequence of elements− Elements are stored in a specific order− Elements can occur more than once

• Positional access− get(i), set(i,x), add(i,x), add(x), remove(i), addAll(i,c)

• Searching and iteration− indexOf(o), lastIndexOf(o), listIterator(), listIterator(i)

• Range view−subList(i, j)

25

List<String> l = new ArrayList<String>();String[] bus = {"Apple","Google","IBM","Microsoft","Oracle"};for (String s : bus) l.add(s);System.out.println(l);l.add("Yahoo"); // appendSystem.out.println(l);l.add(1, "Cognos"); // add as second elementSystem.out.println(l);l.set(1, "Cisco"); // replace second elementSystem.out.println(l.get(0) + " " + l.get(1) + " " + l.get(2));System.out.println(l.subList(0,3));

List (example)

26

ListIterator<String> it = l.listIterator();while (it.hasNext()) System.out.print(it.next() + " ");System.out.println();while (it.hasPrevious()) System.out.print(it.previous() + " ");

ListIterator (example)• ListIterators can iterate forward and backward

− Forward: hasNext(), next()

− Backward: hasPrevious, previous()

List Summary

• Like arrays−Store a sequence of elements in a particular order−Can be accessed by position−Elements can be modified

• Not Like arrays−Elements can be added at the end−Elements can be inserted in the middle and front

• Performance−An implementation can be fast at one or the other, but not both (more later)

28

String[] bus = {"Apple","Google","IBM","Microsoft","Oracle"};List<String> l2 = Arrays.asList(bus);System.out.println(l2);System.out.println(bus[1]); // "Google"l2.set(1, "Hewlett-Packard");System.out.println(l2);System.out.println(bus[1]); // "Hewlett-Packard"

Arrays as Lists• An array can be made into a list in one line

− Arrays.asList

− Set methods modify the original array

List Algorithms

• A number of algorithms are implemented that apply to lists

−Collections.sort, Collections.shuffle, −Collections.reverse, Collections.rotate, −Collections.swap, Collections.replaceAll, −Collections.fill, Collections.copy, −Collections.binarySearch, −Collections.indexOfSublist, −Collections.lastIndexOfSublist

• We will use these in examples later

Queue• A queue stores elements for processing

− Usually elements come out in the same order they went in (FIFO)− Add an element to the end of the queue and remove (or look at) an element at the front of the queue

• Two versions of each method:

Throws Exceptions Returns Special Value

Insert add(e) offer(e)Remove remove() poll()Examine element() peek()

31

// An implementation of Unix 'tail' command// writes the last n lines of r onto w

public static void tail(BufferedReader r, PrintWriter w, int n) { String line; Queue<String> q = new LinkedList<String>(); while ((line = r.readLine()) != null) { q.add(line); if (q.size() > n) q.remove(); } while (!q.isEmpty()) { w.println(q.remove()); }}

Queue (example)

Collection Exercises

• Write a function that takes a Collection of Strings and returns a List of strings in the collection whose length is greater than 5.

• Write a function that takes a Collection of Strings and prints them in sorted order, eliminating duplicates

• Write a function that takes a Collection of Strings, selects one at random and returns it

Maps

• A Map associates keys of type K onto values of type V

−Similar to an array/list except that elements of type K are used for indexing, rather than integers

• Basic operations−put(k,v), get(k), remove(k), containsKey(k), containsValue(v), size(), isEmpty()

• Collection views−keySet(), values(), entrySet()

34

Map<String,String> m = new HashMap<String,String>();m.put("GOOG", "Google Inc.");m.put("AAPL", "Apple Inc.");m.put("MSFT", "Microsoft Corporation");m.put("ORCL", "Oracle Corporation");m.put("CSCO", "Cisco Systems, Inc.");System.out.println("Stock symbol GOOG is for " + m.get("GOOG"));System.out.println("Stock symbol PLCS is for " + m.get("PLCS"));System.out.println("Symbols: " + m.keySet());System.out.println("Companies: " + m.values());System.out.println("Mappings: " + m.entrySet());

Map (example) - NASDAQ

Maps

• The keys in a map are all unique,

• values may not be unique

• To store multiple values for each key, associate a collection with each key

−e.g. Map<String,ArrayList<String>>

36

Map<String,Collection<String>> m2 = new HashMap<String,Collection<String>>();m2.put("Shakespeare", new ArrayList<String>());m2.get("Shakespeare").add("Romeo and Juliet");m2.get("Shakespeare").add("Macbeth");m2.put("Bacon", new ArrayList<String>());m2.get("Bacon").add("New Atlantis");m2.get("Bacon").add("The Comedy of Errors(?)");System.out.println("William Shakespeare wrote: " + m2.get("Shakespeare"));System.out.println("Sir Francis Bacon wrote: " + m2.get("Bacon"));

Storing Multiple Values per Key

In-Class Exercises

• Implement the following function.

Input: an array a

Output: print each element of a that occurs at most 10 times

− Don't print any element not in a− Don't print any element that occurs more than 10 times

SortedMap

• Identical to a Map, except that keys are stored in sorted order

• Operations that use order

− subMap(from,to),− headMap(to),− tailMap(from),− firstKey(),− lastKey()

Which Collection to use (not exact)

• Associating keys with values?− Yes (Map): Exact search only?

− No (Collection): Elements are ordered?

o Yes: Mapo No: Sorted Map

− Yes: Sorted Order?o Yes: SortedSet or SortedMapo No: List

− No: Sorted?o Yes: SortedSet or SortedMapo No: Set or Map

Mutable Objects and Collections

• Objects stored in Sets or used as keys in Maps should not be changed

− The object should not be modified in a way that changes the results of equals() or hashCode() − Ideally, they should be immutable (unmodifiable)

• Objects stored in SortedSets and used as keys in SortedMaps should not be changed in a way that modifies their order

− The result of compareTo() should not change while the object is stored.

41

Set<Person> s = new Set<Person>();Person p = new Person("John", "Doe");s.add(p);p.changeLastName("Smith"); // Don't do this!

Don’t do this

• p gets stored in s at a location that is defined by the contents (firstName and lastName) of p

• Changing the contents (lastName) of p means that p is no longer stored at the correct location in s.

equals, compareTo, and hashCode

• The equals() method can be overridden for any object

− In this case, you must also override the hashCode() method to guarantee that:

− a.compareTo(b) < 0 if a < b− a.compareTo(b) > 0 if a > b− a.compareTo(b) = 0 if a = b

o If a.equals(b) then a.hashCode() = b.hashCode()

• Objects stored in SortedSets must implement the

Comparable interface

Comparators

• To use something other than default ordering we can define a Comparator

• An object that implements the Comparator<T> interface implements the compare(t1,t2) method that compares two elements of class T

• Most SortedSet implementations allow for the specification of a Comparator during initialization

44

public class ReverseStringComparator implements Comparator<String> { public int compare(String a, String b) { int r = a.compareTo(b); if (r < 0) return 1; if (r > 0) return -1; return 0; }}...SortedSet<String> r = new TreeSet<String>(new ReverseStringComparator());

Comparator (example)

Summary: JCF Interfaces

• Sets for representing mathematical sets−Unordered −No duplicates

• Lists for representing sequences−Order matters−Positional operators

• Maps for mapping keys onto values−keys form a Set

• Sorted versions of Set and Map−allows searching for elements/keys that are not present

In-Class Exercises

• Write an application that reads a text file and parses it into words

− The application counts the number of occurrences of each word

− The top 50 words, ordered in decreasing order by number of occurrences, are printed

47

InterfaceImplementation Technique

Hash table

Array Tree Linked list

Hash table + Linked list

Set HashSet TreeSet LinkedHashSet

Sorted Set TreeSet

List ArrayList LinkedList

Queue PriorityQueue LinkedList

Map HashMap TreeMap LinkedHashMap

Sorted Map

TreeMap

Summary of Implementations

Collection MapSet

List

Queue

SortedSet SortedMap

48

• HashSet and LinkedHashSet are both fast– add(x), remove(x), contains(x), size(), isEmpty() all

execute in constant time on average

Set: HashSet or LinkedHashSet

• LinkedHashSet remembers order elements are added– iteration reproduces this order

49

LinkedHashSet example

// Print the elements in T without printing any element// twice, in order of first occurrence in apublic static <T> void printInOrderWithoutDups(T[] a) { Set<T> s = new LinkedHashSet<T>(); for (T x : a) { s.add(x); } for (T x : s) { System.out.println(x); }}

50

• HashMap and LinkedHashMap are both fast– put(k,v), get(k), remove(k), containsKey(k), size(),

isEmpty() all execute in constant time on average– containsValue(v) is slow

Map: HashMap or LinkedHashMap

• LinkedHashMap remembers order keys are added– keySet() is a LinkedHashSet

51

• ArrayList– represents a list as an array– lookups are fast but modifications are slow– fast random-access (get(i), set(i,x))–additions and removals can be slow

ArrayList or LinkedList

• LinkedList– represents a list as a doubly-linked list– lookups are slow but modifications are fast– random-access can be slow (get(i), set(i,x))–additions and removals are fast (constant time)

52

• A list represented as an array–Usually the array has room for more elements

ArrayList

• get(i), and set(i,x) are as fast as reading and writing a value in an array

• add(x) is usually fast–translates to set(size(),x);

b c d ea

b c d e xa

53

• add(i,x) is slow when i << size()–elements have to be shifted to make room for x–requires moving size(x) - i array values

ArrayList (Cont'd)

b c d ea

b c d exa

add(1,x)

• remove(i) is slow when i << size()–elements have to be shifted.–requires moving size(x) – i – 1 array values

ArrayList (Cont'd)

b c d ea

b c d exa

remove(1)

55

• Fast when–a lot of random access is needed–additions and removals are at or near the end of the list.

ArrayList summary

• Provides a fast implementation of–an array that can grow and shrink if needed–a stack•push(x) → add(x)•pop() → remove(size()-1);

• Implements a List as a doubly-linked list–each node stores a reference to the next and previous element–has a pointer to the first and last elements

LinkedList

a b c d e f

57

• get(i), set(i,x) are fast when i is small

LinkedList(Cont'd)

a b c d e fget(0)

a b c d e fget(1)

• add(i,x) are fast when i is small–find list node i-1–modify next/previous at node i-1, i, and new node for x

LinkedList(Cont'd)

a b c d e f

add(1,x)

a b c d e f

x

• remove(i) is fast when i is small–find node i - 1–modify next/previous at nodes i-1 and

i+1

a b c d e f

remove(1)

a b c d e f

LinkedList(Cont'd)

• add(i,x), add(x), remove(i), get(i), set(i,x) are fast when i is large–if i > size() / 2 then we traverse the list backwards

LinkedList(Cont'd)

a b c d e f

remove(4)

a b c d e f

• add(i,x), remove(i), get(i), set(i,x) must traverse–min{i, size()-i-1} elements

• Fast when i ~ 0 or i ~ size()

• Slow when i >> 0 and i << size()

LinkedList(Cont'd)

a b y z

l.get(l.size()/2)

m...

..

.

• LinkedLists are good for implementing

LinkedList(Cont'd)

−Stacks

−Queues

−Dequeues

63

• The ListIterator interface provides

–hasNext(), next(), hasPrevious(), previous(), nextIndex(), previousIndex() •These are fast (constant-time) for both ArrayList and LinkedList

–remove(), set(x), add(x)

•These are fast for LinkedList

•These can be slow for ArrayList

ListIterators

• ArrayList–Fast to get to the location you want–Slow to insert or remove at that location•except at the end (back).

LinkedList versus ArrayListsummary

• LinkedList–Slow to get to the location you want•except at the front and back;•or if you have an iterator at that location

–Fast to insert or remove at that location

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void frontGets(List<Integer> l, int n) { for (int i = 0; i < n; i++) { l.get(0); }}

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void backGets(List<Integer> l, int n) { for (int i = 0; i < n; i++) { l.get(l.size()-1); }}

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void randomGets(List<Integer> l, int n) { Random gen = new Random(); for (int i = 0; i < n; i++) { l.get(gen.nextInt(l.size())); }}

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void insertAtBack(List<Integer> l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); }}

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void insertAtFront(List<Integer> l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(0, new Integer(i)); }}

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void insertInMiddle(List<Integer> l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); } for (int i = 0; i < n; i++) { l.add(n/2+i, new Integer(i)); }}

• Among ArrayList or LinkedList which type of implementation make this code faster?

Excercises

public static void insertInMiddle2(List<Integer> l, int n) { l.clear(); for (int i = 0; i < n; i++) { l.add(new Integer(i)); } ListIterator<Integer> li = l.listIterator(n/2); for (int i = 0; i < n; i++) { li.add(new Integer(i)); }}

• Unordered implementation of Set and Map–HashSet and HashMap

summary

• SortedSet and SortedMap implementations–TreeSet and TreeMap

• Implementations of Set and Map that maintain insertion order–LinkedHashSet and LinkedHashMap

• List implementions–ArrayList: fast random access–LinkedList: fast insertion at an easy-to-get-to location

73

InterfaceImplementation Technique

Hash table

Array Tree Linked list

Hash table + Linked list

Set HashSet TreeSet LinkedHashSet

Sorted Set TreeSet

List ArrayList LinkedList

Queue PriorityQueue LinkedList

Map HashMap TreeMap LinkedHashMap

Sorted Map

TreeMap

Summary of Implementations

Collection MapSet

List

Queue

SortedSet SortedMap

Next Class

Array Based List Implementations