64
Contest Algorithms January 2016 3. Collections 1 Contest Algorithms: 3. Collections

Contest Algorithms January 2016 3. Collections 1Contest Algorithms: 3. Collections

Embed Size (px)

Citation preview

1

Contest AlgorithmsJanuary 2016

3. Collections

Contest Algorithms: 3. Collections

Contest Algorithms: 3. Collections 2

1. Class / Interface Libraries2. Collection, Collections3. List: ArrayList, LinkedList4. Stack5. Queue: Radix sort, Deque, PriorityQueue6. Sets: TreeSet, HashSet7. Maps: TessMap, HashMap8. BitSet, Sieve of Eratosthenes

Overview

Contest Algorithms: 3. Collections 3

Collection

Stack

Map

SortedMap

NavigableMap

List Queue Set

Deque SortedSet

NavigableSet

Vector

HashTable

Dictionary

EnumMap

IdentityHashMap WeakHashMap

HashMap

LinkedHashMap

TreeMap

ArrayList

LinkedList

PriorityQueue

ArrayDeque

LinkedHashSet

HashSet

TreeSet

interfaces and classes in the java.util package. Interfaces are in blue.

1. Class/ Interface Libraries

ArraysCollections

BitSet

Kinds of Collections

Collection--a group of objects, called elements Set--An unordered collection with no duplicates

SortedSet--An ordered collection with no duplicates

List--an ordered collection, duplicates are allowedMap--a collection that maps keys to values

SortedMap--a collection ordered by the keys

Note that there are two distinct hierarchies

Contest Algorithms: 3. Collections 5

CollectionSummary

Contest Algorithms: 3. Collections 6

Map Summary

Contest Algorithms: 3. Collections 7

Another Summary

Contest Algorithms: 3. Collections 8

Yes Yes

A collection class takes an object type as a parameter: Collection<E> List<E> Stack<E> Set<E>

A map takes two object type parameters: Map<K,V>

Generics

20-9

2. Collection<E>

20-10

boolean isEmpty ( )

int size ( )

boolean contains (Object obj)

boolean add (E obj)

boolean remove (E obj)

Iterator<E> iterator ( )

// ... other methods

Iterator “For Each” Loop

20-11

Collection<String> words = new ArrayList<String>(); ...

for (String word : words) { // process word > } A “for each” loop replaces the

iterator technique

Bulk operations

boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear( );

return true if the object was modified

Array operations

Object[ ] toArray( ); creates a new array of Objects

Object[ ] toArray(Object a[ ]); provide the array that is filled

Examples: Object[ ] a = coll.toArray(); String[ ] a = (String[ ]) coll.toArray(new String[0]);

The Collections class

Not the same as the Collection interface

Static utility methods to operate on Collection objects

Collections class methods

Binary searchSortingCopyingMin/MaxReverseShuffle Frequency / disjointSynchronization

UnmodifiableSingletons FillConstants for empty…

List Set Map

Etc.

see UseCollections.java

3. List<E>

20-16

«interface»

Collection

«interface»

List // All Collection<E> methods, plus:

E get (int i)

E set (int i, E obj)

void add (int i, E obj)

E remove (int i)

int indexOf (E obj)

int lastIndexOf(E obj)

List subList(int from, int to)

additional positionalaccess andsearching methods

Represents a list as a dynamic array.

Provides faster random access to the elements

Implements all the methods of List<E>

ArrayList

20-17

«interface»

List

ArrayList LinkedList

a1 a2 an-1...a0

see UseArrayList.java

Contest Algorithms: 3. Collections 18

ArrayList<Integer> v = new ArrayList<Integer>(); v.add(10); v.add(7); v.add(2); v.add(15); v.add(4);

// sort ascending Collections.sort(v); System.out.println(v); // [2, 4, 7, 10, 15]

int pos = Collections.binarySearch(v, 7); // 2

Code

Represents a list as a doubly-linked list

Implements all the methods of List<E> Faster insertions and deletions

LinkedList

20-19

a0 a1 a2 an-1...

«interface»

List

ArrayList LinkedList

and Queue, Deque

Additional methods specific to LinkedList:

void addFirst (E obj)

void addLast (E obj)

E getFirst ( )

E getLast ( )

E removeFirst ( )

E removeLast ( )

see UseLinkedList.java

Implements a list as a doubly-linked list

No random access to the elements — needs to traverse the list to get to the i-th element

+Inserting and removing elements is done by rearranging the links — no shifting

+Nodes are allocated and released as necessary

Implements a list as an array

+Provides random access to the elements

- Inserting and removing elements requires shifting of subsequent elements

- Needs to be resized when runs out of space

ArrayList vs. LinkedList

20-21

20-22

ArrayList LinkedList

get(i) and set(i, obj) O(1) O(n)

add(i, obj) and remove(i) O(n) O(n)

add(0, obj) O(n) O(1)

add(obj) O(1) O(1)

contains(obj) O(n) O(n)

20-23

Works well for an ArrayList O(n);inefficient for a LinkedList O(n2)

for (int i = 0; i < list.size(); i++) { Object x = list.get (i); ... }

for (Object x : list) { ... }

Work well for both an ArrayList and a LinkedList O(n)

Contest Algorithms: 3. Collections 24

CheckPali.java uses a list to check if the letter of an input string are a paalindrome

Code see CheckPali.java

4. Stack<E>

20-25

boolean isEmpty ( )

E push (E obj)

E pop ( )

E peek ( )

Returns the top element without removing it from the stack

see UseStackQueue.java

Contest Algorithms: 3. Collections 26

ConvertInteger.java uses a stack to convert a decimal into any base between 2 and 16:

Code see ConvertInteger.java

27

5. Queue<E>boolean offer (E e)

Insert e at rear of queue; return true if workedE remove ()

Remove and return front entry; exception if noneE poll ()

Remove and return front entry; null if noneE peek ()

Return front entry without removing; null if noneE element ()

Return front entry without removing; exception if none

LinkedList

«interface»

Queue

see UseStackQueue.java

and List, Deque

28

Part of the Collection hierarchy, so ...Offers many other methods, including:

add size isEmpty iterator

Radix Sort

• Radix sort uses the digits in each array element to sort the array.

• The array elements are passed to ten queues (index 0 to 9) using each element's digit as the index.

• Elements are copied from the queues back to the original array, in partially sorted order.

Radix Sort ExampleArray: [91, 6, 85, 15, 92, 35, 30, 22, 39]

After Pass 0: [30, 91, 92, 22, 85, 15, 35, 6, 39]

After Pass 1: [6, 15, 22, 30, 35, 39, 85, 91, 92]

use the unitsdigit in each element(100 digit)

use the tensdigit in each element(101 digit)

partiallysorted

fullysorted

• The i-th pass distributes the array elements into one of the 10 queues by looking at the digit in each element corresponding to the power 10i. in pass 0, look at units digit (100) in pass 1, look at tens digit (101)

• Radix sort must carry out d passes, one for each digit in the largest element e.g. if the largest element is 92, then d = 2 passes;

if largest is142, then d == 3 passes

Contest Algorithms: 3. Collections 32

private static final int NUM_VALS = 50;

public static void main(String[] args) { int[] arr = new int[NUM_VALS]; // initialize array with random numbers in range 0 - 99999 Random rnd = new Random(); for (int i = 0; i < NUM_VALS; i++) arr[i] = rnd.nextInt(100000);

// apply the radix sort and output the sorted array radixSort(arr, 5); displayArray(arr); } // end of main()

Codesee UseRadixSort.java

Contest Algorithms: 3. Collections 33

public static void radixSort(int[] arr, int d) { // an arraylist of 10 empty queues ArrayList<LinkedList<Integer>> qs = new ArrayList<>(); for (int i=0;i < 10; i++) qs.add( new LinkedList<Integer>());

// the current digit is found by dividing by 10^power int power = 1; for (int i=0;i < d;i++) { distribute(arr, qs, power); collect(qs, arr); power *= 10; } } // end of radixSort()

Contest Algorithms: 3. Collections 34

private static void distribute(int[] arr, ArrayList<LinkedList<Integer>> qs, int power) { // insert each element into the right queue for (int i=0; i < arr.length; i++) qs.get((arr[i] / power) % 10).add(arr[i]); }

private static void collect(ArrayList<LinkedList<Integer>> qs, int[] arr) // gather elements from the queues and copy back to the array { int i=0; for (LinkedList<Integer> q : qs) while (!q.isEmpty()) arr[i++] = q.remove(); }

Execution

Radix Sort EfficiencyThe runtime efficiency of radixSort() is O(radix*n) where

the list has n elements and the biggest element has radix digits a fast linear sorting algorithm

But radixSort() uses a lot of memory it needs one queue for each digit

If we are sorting strings, then we will need 1 queue for each different letter (62+ queues)

Contest Algorithms: 3. Collections 37

Deque is a queue where you can insert and remove elements from both ends short for Double Ended Queue

ArrayDeque stores its elements in a dynamic 'circular' array always faster than LinkedList for random access less node creation (on average) than LinkedList,

so a bit faster for insertion

Deque ("deck")«interface»

Queue

«interface»

Deque

ArrayDeque

see UseStackQueue.java

Items are processed in order of priority,NOT in order of addition.

The same methods as in Queue: isEmpty, add, remove, peek add and remove run in O(log n) time; peek runs in O(1)

time

Works with Comparable objects or takes a comparator as a parameter; see code

Priority Queues

20-38

PriorityQueue

«interface»

Queue

Contest Algorithms: 3. Collections 39

public class Pair< X, Y > { private X x; private Y y;

public Pair(X x, Y y) { this.x = x; this.y = y; }

public X getX() { return x; }

public Y getY() { return y; }

public void setX(X el) { x = el; }

public void setY(Y el) { y = el; }

public String toString() { return "(" + x + ", " + y + ")"; }} // end of Pair class

Codesee Pair.java

Contest Algorithms: 3. Collections 40

PriorityQueue< Pair<Integer,String>> pq = new PriorityQueue< Pair<Integer,String>>(7, // initial capacity new Comparator< Pair<Integer,String>>() { public int compare( Pair<Integer,String> i, Pair<Integer,String> j) { return j.getX() - i.getX(); } // order based on the x in Pair, largest first. });

// enter these 7 money-name pairspq.offer( new Pair<Integer, String>(100, "john") ); // inserting is O(log n)pq.offer( new Pair<Integer, String>(10, "billy") );pq.offer( new Pair<Integer, String>(20, "andy") );pq.offer( new Pair<Integer, String>(100, "steven") );pq.offer( new Pair<Integer, String>(70, "felix") );pq.offer( new Pair<Integer, String>(2000, "grace") );pq.offer( new Pair<Integer, String>(70, "martin") ); :

see UsePriorityQueue.java

Contest Algorithms: 3. Collections 41

/* priority queue will arrange items based on the first x in Pair, largest first. If first keys tie, then the second key is used, largest first. */for (Pair<Integer,String> p : pq) System.out.println(p);

// print out the top 3 with most moneyPair<Integer, String> result = pq.poll(); /* O(1) to access the top / max element + O(log n) removal of the top and repair of the structure */System.out.println(result.getY() + " has $" + result.getX()); // prints grace has $2000

result = pq.poll();System.out.println(result.getY() + " has $" + result.getX()); // prints steven has $100

result = pq.poll();System.out.println(result.getY() + " has $" + result.getX()); // prints john has $100

(2000, grace)(100, steven)(100, john)(10, billy)(70, felix)(20, andy)(70, martin)

A set is a collection without duplicate valuesWhat is a “duplicate” depends on the implementationDesigned for finding a value quickly

6. Sets

20-42

«interface»

Collection

TreeSet HashSet

«interface»

Set

Works with Comparable objects (or takes a comparator as a parameter)

contains(), add(), and remove() run in O(log n) time for-each returns elements in ascending orderMethods: headset, tailSet, subset() use this ordering

TreeSet<E>

20-43

TreeSet HashSet

«interface»

Set

see UseMapSet.java

Contest Algorithms: 3. Collections 44

public class TreeSetWithComparator { public static void main(String a[]) { TreeSet<String> ts = new TreeSet<String>(new StringComp()); ts.add("RED"); ts.add("ORANGE"); ts.add("BLUE"); ts.add("GREEN"); System.out.println(ts); for(String s : ts) System.out.println(" " + s); }} class StringComp implements Comparator<String>{ public int compare(String s1, String s2) { return s1.compareTo(s2); }}

Code

> java TreeSetWithComparator[BLUE, GREEN, ORANGE, RED] BLUE GREEN ORANGE RED

see TreeSetWithComparator.java

Works with objects with hashCode() and equals() Implements a set as a hash table contains(), add(), and remove() run in O(1) time

faster than TreeSet for-each returns elements in no particular order

HashSet<E>

20-45

TreeSet HashSet

«interface»

Set

7. MapsA 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

TreeMap HashMap

«interface»

Map

Map<K, V> Methods

20-47

TreeMap HashMap

«interface»

Map

boolean isEmpty ( )

int size ( )

V get (K key)

V put (K key, V value)

V remove (K key)

boolean containsKey (K key)

Set<K> keySet ( )

Map.Entry<K, V> entrySet()collection views;replace iteration

Map Collection Views

A map does not have an iterator/for-each for accessing its elements.

Instead we can use collection views, which are sets that act on the original map the keySet collection view

a set of key entries the entrySet collection view

a set of key-value entries

A view is backed by the original Map, so any changes done to the keyset / entrySet will affect the Map as well.

Contest Algorithms: 3. Collections 49

System.out.println( userScores.keySet()); // prints keys in ascending order if using TreeSet

// iterate through the map using entry set;// accessed in ascending key order if using TreeSetfor(Map.Entry<String,Integer> entry : userScores.entrySet()) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.println(" " + key + " => " + value); }

Codesee UseMapSet.java

Takes a Comparator as a parameter also works with keys thatimplement Comparable

containsKey(), get(), and put() run in O(log n) time for-each on entrySet returns elements in ascending

order by key submap() uses this ordering

TreeMap<K,V>

20-50

TreeMap HashSet

«interface»

Map

Contest Algorithms: 3. Collections 51

SortedMap subMap(int fromKey, int toKey) returns a portion of the TreeMap whose keys range from

fromKey (inclusive) to toKey (exclusive)

The SortedMap returned by this method is backed by the original TreeMap any changes made to SortedMap will change the original

TreeMap as well

TreeMap.submap()

Contest Algorithms: 3. Collections 52

// display data between ["f".."m") // ('felix' is included, martin' is excluded)SortedMap<String, Integer> res = userScores.subMap("f", "m");

System.out.println(res.keySet()); // prints [felix, grace, john]System.out.println(res.values()); // prints [82, 75, 78]

Codesee UseMapSet.java

Works with key objects with hashCode() and equals() Implements the key set as a hash table containsKey(), get(), and put() run in O(1) time

faster than TreeMap for-each on entrySet returns elements in no particular

order

HashMap<K,V>

20-53

TreeMap HashMap

«interface»

Map

8. BitSetAn alternative to boolean[], with many useful methods:

BitSet(int nbits) constructor, all nbits bits are false initiallyvoid set(int) set a bit at index pos; may dynamically resize

setboolean get(int) get bit status at index posvoid clear(int) clear a bit (set to false) at index posvoid or(BitSet) compute logical-or of two bitsetsvoid and(BitSet)void xor(BitSet)String toString(): nice list of comma-separated on-positionsBoolean equals(Object): test if two bitsets are the same

Contest Algorithms: 3. Collections 55

BitSet bs1 = new BitSet(20); bs1.set(1); bs1.set(4);

BitSet bs2 = new BitSet(20); bs2.set(4); bs2.set(5);

System.out.println("bits 1 = " + bs1); System.out.println("bits 2 = " + bs2);

if(bs1.equals(bs2)) System.out.println ("bits1 == bits2\n"); else System.out.println ("bits1 ! = bits2\n");

// logically AND the first two BitSets bs1.and(bs2); System.out.println("ANDing bits1 and bits2");

System.out.println("bits1 = " + bs1.toString());

Code

> java UseBitSetBits 1 = {1, 4}Bits 2 = {4, 5}bits1 ! = bits2

ANDing bits1 and bits2bits1 = {4}

see UseBitSet.java

Contest Algorithms: 3. Collections 56

BitSet uses a single bit to a boolean value.  Internally the bits are managed inside a long[]But BitSet only supports int keys, so is limited to 232-1

bits

BitSet is much more space efficient than a boolean[] since Java uses an entire byte to store each element in the boolean array! http://chrononsystems.com/blog/

hidden-evils-of-javas-byte-array-byte

BitSet Implementation

Contest Algorithms: 3. Collections 57

A graph showing the space used by boolean[] and BitSet for 100,000 elements:

Contest Algorithms: 3. Collections 58

The process is a systematic way of selecting values we know are prime and crossing out values we know must be composite.

Create a list of every number from 2 up to as big as we want (e.g to 29)

1. Cross out all multiples of 2:

The Sieve of Eratosthenes

Contest Algorithms: 3. Collections 59

Go to next uncrossed number == 3. Cross out all multiples of 3:

Go to next uncrossed number == 5. Cross out all multiples of 5:

Contest Algorithms: 3. Collections 60

Continue until the end of the list is reached:

The uncrossed numbers are the primes from 2 to 29.

Contest Algorithms: 3. Collections 61

Represent the list as a Bitset

Start by setting all the bits to true, and then sieve by setting bit multiples to false.

The advantages of this approach is that the BitSet can represent a very large number sequence, and is quite fast too.

Bitset Version of Sieve see SieveBitSet.java

Contest Algorithms: 3. Collections 62

BitSet sieve = new BitSet(1024); int size = sieve.size();

// set all bits from 2 to 1023 to true for (int i = 2; i < size; i++) sieve.set(i); // to true

// start sieving int finalBit = (int) Math.sqrt(size); // can stop at √n, not n for (int i = 2; i < finalBit; i++) { if (sieve.get(i)) { for (int j = 2 * i; j < size; j += i) sieve.clear(j); // to false } } :

Contest Algorithms: 3. Collections 63

// display prime numbers from 2 to 1023 int counter = 0; System.out.println("--------- Primes from 2 to 1023 ---------"); for (int i = 2; i < size; i++) { if (sieve.get(i)) { System.out.printf("%4d", i); System.out.print(++counter % 7 == 0 ? "\n" : " "); } }

Contest Algorithms: 3. Collections 64