Contest Algorithms January 2016 3 Collections Contest Algorithms

  • Slides: 64
Download presentation
Contest Algorithms January 2016 3. Collections Contest Algorithms: 3. Collections 1

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

Overview § 1. Class / Interface Libraries § 2. Collection, Collections § 3. List:

Overview § 1. Class / Interface Libraries § 2. Collection, Collections § 3. List: Array. List, Linked. List § 4. Stack § 5. Queue: Radix sort, Deque, Priority. Queue § 6. Sets: Tree. Set, Hash. Set § 7. Maps: Tess. Map, Hash. Map § 8. Bit. Set, Sieve of Eratosthenes Contest Algorithms: 3. Collections 2

1. Class/ Interface Libraries Map Sorted. Map List Collection Queue Set Dictionary Deque Navigable.

1. Class/ Interface Libraries Map Sorted. Map List Collection Queue Set Dictionary Deque Navigable. Map Navigable. Set Tree. Map Hash. Table Hash. Map Bit. Set Enum. Map Array. List Weak. Hash. Map Arrays Contest Algorithms: 3. Collections Tree. Set Hash. Set Linked. List Linked. Hash. Set Stack Collections Array. Deque Priority. Queue Linked. Hash. Map Vector Identity. Hash. Map Sorted. Set interfaces and classes in the java. util package. Interfaces are in blue. 3

Kinds of Collections § Collection--a group of objects, called elements § Set--An unordered collection

Kinds of Collections § Collection--a group of objects, called elements § Set--An unordered collection with no duplicates § Sorted. Set--An ordered collection with no duplicates § List--an ordered collection, duplicates are allowed § Map--a collection that maps keys to values § Sorted. Map--a collection ordered by the keys § Note that there are two distinct hierarchies

Collection Summary Contest Algorithms: 3. Collections 5

Collection Summary Contest Algorithms: 3. Collections 5

Map Summary Contest Algorithms: 3. Collections 6

Map Summary Contest Algorithms: 3. Collections 6

Another Summary Contest Algorithms: 3. Collections 7

Another Summary Contest Algorithms: 3. Collections 7

Yes Contest Algorithms: 3. Collections Yes 8

Yes Contest Algorithms: 3. Collections Yes 8

Generics § A collection class takes an object type as a parameter: § Collection<E>

Generics § 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> 20 -9

2. Collection<E> boolean is. Empty ( ) int size ( ) boolean contains (Object

2. Collection<E> boolean is. Empty ( ) int size ( ) boolean contains (Object obj) boolean add (E obj) boolean remove (E obj) Iterator<E> iterator ( ) // . . . other methods 20 -10

Iterator “For Each” Loop Collection<String> words = new Array. List<String>(); . . . for

Iterator “For Each” Loop Collection<String> words = new Array. List<String>(); . . . for (String word : words) { // process word > } A “for each” loop replaces the iterator technique 20 -11

Bulk operations boolean contains. All(Collection c); add. All(Collection c); remove. All(Collection c); retain. All(Collection

Bulk operations boolean contains. All(Collection c); add. All(Collection c); remove. All(Collection c); retain. All(Collection c); void clear( ); § return true if the object was modified

Array operations § Object[ ] to. Array( ); § creates a new array of

Array operations § Object[ ] to. Array( ); § creates a new array of Objects § Object[ ] to. Array(Object a[ ]); § provide the array that is filled § Examples: Object[ ] a = coll. to. Array(); String[ ] a = (String[ ]) coll. to. Array(new String[0]);

The Collections class § Not the same as the Collection interface § Static utility

The Collections class § Not the same as the Collection interface § Static utility methods to operate on Collection objects

Collections class methods § Binary search § Sorting § Copying § Min/Max § Reverse

Collections class methods § Binary search § Sorting § Copying § Min/Max § Reverse § Shuffle § Frequency / disjoint § Synchronization § Unmodifiable § Singletons § Fill § Constants for empty… § List § Set § Map § Etc. see Use. Collections. java

3. List<E> «interface» Collection // All Collection<E> methods, plus: E get (int i) E

3. List<E> «interface» Collection // 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 index. Of (E obj) int last. Index. Of(E obj) List sub. List(int from, int to) «interface» List additional positional access and searching methods 20 -16

 «interface» Array. List Linked. List § Represents a list as a dynamic array.

«interface» Array. List Linked. List § Represents a list as a dynamic array. § Provides faster random access to the elements § Implements all the methods of List<E> see Use. Array. List. java a 0 a 1 a 2 . . . an-1 20 -17

Code Array. List<Integer> v = new Array. List<Integer>(); v. add(10); v. add(7); v. add(2);

Code Array. List<Integer> v = new Array. List<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. binary. Search(v, 7); Contest Algorithms: 3. Collections // 2 18

 «interface» Linked. List and Queue, Deque Array. List Linked. List § Represents a

«interface» Linked. List and Queue, Deque Array. List Linked. List § Represents a list as a doubly-linked list a 0 a 1 a 2 . . . an-1 § Implements all the methods of List<E> § Faster insertions and deletions 20 -19

§ Additional methods specific to Linked. List: void add. First (E obj) void add.

§ Additional methods specific to Linked. List: void add. First (E obj) void add. Last (E obj) E get. First ( ) E get. Last ( ) E remove. First ( ) E remove. Last ( ) see Use. Linked. List. java

 Array. List vs. Linked. List § Implements a list as an array §

Array. List vs. Linked. List § Implements a list as an array § Implements a list as a doubly-linked list + Provides random access to the elements - No random access to the elements — needs to traverse the list to get to the i-th element - Inserting and removing elements requires shifting of subsequent elements - Needs to be resized when runs out of space + Inserting and removing elements is done by rearranging the links — no shifting + Nodes are allocated and released as necessary 20 -21

Array. List Linked. List get(i) and set(i, obj) O(1) O(n) add(i, obj) and remove(i)

Array. List Linked. List get(i) and set(i, obj) O(1) O(n) add(i, obj) and remove(i) O(n) add(0, obj) O(n) O(1) add(obj) O(1) contains(obj) O(n) 20 -22

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

for (int i = 0; i < list. size(); { Object x = list. get (i); . . . } Works well for an Array. List O(n); inefficient for a Linked. List O(n 2) i++) for (Object x : list) {. . . } Work well for both an Array. List and a Linked. List O(n) 20 -23

Code see Check. Pali. java § Check. Pali. java uses a list to check

Code see Check. Pali. java § Check. Pali. java uses a list to check if the letter of an input string are a paalindrome Contest Algorithms: 3. Collections 24

4. Stack<E> boolean is. Empty ( ) E push (E obj) E pop (

4. Stack<E> boolean is. Empty ( ) E push (E obj) E pop ( ) E peek ( ) see Use. Stack. Queue. java Returns the top element without removing it from the stack 20 -25

Code see Convert. Integer. java § Convert. Integer. java uses a stack to convert

Code see Convert. Integer. java § Convert. Integer. java uses a stack to convert a decimal into any base between 2 and 16: Contest Algorithms: 3. Collections 26

5. Queue<E> «interface» Queue and List, Deque boolean offer (E e) Linked. List Insert

5. Queue<E> «interface» Queue and List, Deque boolean offer (E e) Linked. List Insert e at rear of queue; return true if worked E remove () Remove and return front entry; exception if none E poll () Remove and return front entry; null if none see Use. Stack. Queue. java E peek () Return front entry without removing; null if none E element () Return front entry without removing; exception if none 27

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

§ Part of the Collection hierarchy, so. . . § Offers many other methods, including: § add § size § is. Empty § iterator 28

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

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 Example § Array: [91, 6, 85, 15, 92, 35, 30, 22, 39]

Radix Sort Example § Array: [91, 6, 85, 15, 92, 35, 30, 22, 39] use the units digit in each element (100 digit) partially sorted After Pass 0: [30, 91, 92, 22, 85, 15, 35, 6, 39] use the tens digit in each element (101 digit) After Pass 1: [6, 15, 22, 30, 35, 39, 85, 91, 92] fully sorted

 • The i-th pass distributes the array elements into one of the 10

• 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 10 i. § 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 is 142, then d == 3 passes

Code see Use. Radix. Sort. java private static final int NUM_VALS = 50; public

Code see Use. Radix. Sort. java 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. next. Int(100000); // apply the radix sort and output the sorted array radix. Sort(arr, 5); display. Array(arr); } // end of main() Contest Algorithms: 3. Collections 32

public static void radix. Sort(int[] arr, int d) { // an arraylist of 10

public static void radix. Sort(int[] arr, int d) { // an arraylist of 10 empty queues Array. List<Linked. List<Integer>> qs = new Array. List<>(); for (int i=0; i < 10; i++) qs. add( new Linked. List<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 radix. Sort() Contest Algorithms: 3. Collections 33

private static void distribute(int[] arr, Array. List<Linked. List<Integer>> qs, int power) { // insert

private static void distribute(int[] arr, Array. List<Linked. List<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(Array. List<Linked. List<Integer>> qs, int[] arr) // gather elements from the queues and copy back to the array { int i=0; for (Linked. List<Integer> q : qs) while (!q. is. Empty()) arr[i++] = q. remove(); } Contest Algorithms: 3. Collections 34

Execution

Execution

Radix Sort Efficiency § The runtime efficiency of radix. Sort() is O(radix*n) where the

Radix Sort Efficiency § The runtime efficiency of radix. Sort() is O(radix*n) where the list has n elements and the biggest element has radix digits § a fast linear sorting algorithm § But radix. Sort() 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)

see Use. Stack. Queue. java Deque ("deck") «interface» Queue «interface» § Deque is a

see Use. Stack. Queue. java Deque ("deck") «interface» Queue «interface» § Deque is a queue where you can insert and remove elements from both ends § short for Double Ended Queue Deque Array. Deque § Array. Deque stores its elements in a dynamic 'circular' array § always faster than Linked. List for random access § less node creation (on average) than Linked. List, so a bit faster for insertion Contest Algorithms: 3. Collections 37

 «interface» Priority Queues § Items are processed in order of priority, NOT in

«interface» Priority Queues § Items are processed in order of priority, NOT in order of addition. Queue Priority. Queue § The same methods as in Queue: § is. Empty, 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 20 -38

Code see Pair. java public class Pair< X, Y > { private X x;

Code see Pair. java 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 get. X() { return x; } public Y get. Y() { return y; } public void set. X(X el) { x = el; } public void set. Y(Y el) { y = el; } public String to. String() { return "(" + x + ", " + y + ")"; } // end of Pair class Contest Algorithms: 3. Collections } 39

see Use. Priority. Queue. java Priority. Queue< Pair<Integer, String>> pq = new Priority. Queue<

see Use. Priority. Queue. java Priority. Queue< Pair<Integer, String>> pq = new Priority. Queue< Pair<Integer, String>>(7, // initial capacity new Comparator< Pair<Integer, String>>() { public int compare( Pair<Integer, String> i, Pair<Integer, String> j) { return j. get. X() - i. get. X(); } // order based on the x in Pair, largest first. }); // enter these 7 money-name pq. offer( new Pair<Integer, pq. offer( new Pair<Integer, : Contest Algorithms: 3. Collections pairs String>(100, "john") ); // inserting is O(log n) String>(10, "billy") ); String>(20, "andy") ); String>(100, "steven") ); String>(70, "felix") ); String>(2000, "grace") ); String>(70, "martin") ); 40

/* priority queue will arrange items based on the first x in Pair, largest

/* 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 money Pair<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. get. Y() + " has $" + result. get. X()); // prints grace has $2000 (2000, grace) (100, steven) (100, john) (10, billy) (70, felix) (20, andy) (70, martin) result = pq. poll(); System. out. println(result. get. Y() + " has $" + result. get. X()); // prints steven has $100 result = pq. poll(); System. out. println(result. get. Y() + " has $" + result. get. X()); // prints john has $100 Contest Algorithms: 3. Collections 41

6. Sets § A set is a collection without duplicate values § What is

6. Sets § A set is a collection without duplicate values § What is a “duplicate” depends on the implementation § Designed for finding a value quickly «interface» Collection «interface» Set Tree. Set 20 -42 Hash. Set

 «interface» Set Tree. Set<E> Tree. Set Hash. Set § Works with Comparable objects

«interface» Set Tree. Set<E> Tree. Set Hash. 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 order § Methods: headset, tail. Set, subset() use this ordering see Use. Map. Set. java 20 -43

Code see Tree. Set. With. Comparator. java public class Tree. Set. With. Comparator {

Code see Tree. Set. With. Comparator. java public class Tree. Set. With. Comparator { public static void main(String a[]) { Tree. Set<String> ts = new Tree. Set<String>(new String. Comp()); ts. add("RED"); ts. add("ORANGE"); ts. add("BLUE"); ts. add("GREEN"); > java Tree. Set. With. Comparator [BLUE, GREEN, ORANGE, RED] System. out. println(ts); BLUE for(String s : ts) GREEN System. out. println(" " + s); ORANGE } RED } class String. Comp implements Comparator<String> { public int compare(String s 1, String s 2) { return s 1. compare. To(s 2); } } Contest Algorithms: 3. Collections 44

 «interface» Set Hash. Set<E> Tree. Set Hash. Set § Works with objects with

«interface» Set Hash. Set<E> Tree. Set Hash. Set § Works with objects with hash. Code() and equals() § Implements a set as a hash table § contains(), add(), and remove() run in O(1) time § faster than Tree. Set § for-each returns elements in no particular order 20 -45

7. Maps «interface» Map § A map stores data in key‑value pairs. § A

7. Maps «interface» Map § A map stores data in key‑value pairs. § A key acts like an index to locate the corresponding Tree. Map value in the map § a map is also called an associative array Hash. Map

 «interface» Map<K, V> Methods Map Tree. Map Hash. Map boolean is. Empty (

«interface» Map<K, V> Methods Map Tree. Map Hash. Map boolean is. Empty ( ) int size ( ) V get (K key) V put (K key, V value) V remove (K key) boolean contains. Key (K key) Set<K> key. Set ( ) Map. Entry<K, V> entry. Set() collection views; replace iteration 20 -47

Map Collection Views § A map does not have an iterator/for-each for accessing its

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 key. Set collection view § a set of key entries § the entry. Set collection view § a set of key-value entries § A view is backed by the original Map, so any changes done to the keyset / entry. Set will affect the Map as well.

Code see Use. Map. Set. java System. out. println( user. Scores. key. Set()); //

Code see Use. Map. Set. java System. out. println( user. Scores. key. Set()); // prints keys in ascending order if using Tree. Set // iterate through the map using entry set; // accessed in ascending key order if using Tree. Set for(Map. Entry<String, Integer> entry : user. Scores. entry. Set()) { String key = entry. get. Key(); Integer value = entry. get. Value(); System. out. println(" " + key + " => " + value); } Contest Algorithms: 3. Collections 49

 «interface» Map Tree. Map<K, V> Tree. Map Hash. Set § Takes a Comparator

«interface» Map Tree. Map<K, V> Tree. Map Hash. Set § Takes a Comparator as a parameter § also works with keys thatimplement Comparable § contains. Key(), get(), and put() run in O(log n) time § for-each on entry. Set returns elements in ascending order by key § submap() uses this ordering 20 -50

Tree. Map. submap() § Sorted. Map sub. Map(int from. Key, int to. Key) §

Tree. Map. submap() § Sorted. Map sub. Map(int from. Key, int to. Key) § returns a portion of the Tree. Map whose keys range from. Key (inclusive) to to. Key (exclusive) § The Sorted. Map returned by this method is backed by the original Tree. Map § any changes made to Sorted. Map will change the original Tree. Map as well Contest Algorithms: 3. Collections 51

Code see Use. Map. Set. java // display data between ["f". . "m") //

Code see Use. Map. Set. java // display data between ["f". . "m") // ('felix' is included, martin' is excluded) Sorted. Map<String, Integer> res = user. Scores. sub. Map("f", "m"); System. out. println(res. key. Set()); System. out. println(res. values()); Contest Algorithms: 3. Collections // prints [felix, grace, john] // prints [82, 75, 78] 52

 «interface» Map Hash. Map<K, V> Tree. Map Hash. Map § Works with key

«interface» Map Hash. Map<K, V> Tree. Map Hash. Map § Works with key objects with hash. Code() and equals() § Implements the key set as a hash table § contains. Key(), get(), and put() run in O(1) time § faster than Tree. Map § for-each on entry. Set returns elements in no particular order 20 -53

8. Bit. Set § An alternative to boolean[], with many useful methods: Bit. Set(int

8. Bit. Set § An alternative to boolean[], with many useful methods: Bit. Set(int nbits) constructor, all nbits are false initially void set(int) set a bit at index pos; may dynamically resize set boolean get(int) get bit status at index pos void clear(int) clear a bit (set to false) at index pos void or(Bit. Set) compute logical-or of two bitsets void and(Bit. Set) void xor(Bit. Set) String to. String(): nice list of comma-separated on-positions Boolean equals(Object): test if two bitsets are the same

Code Bit. Set bs 1 = new Bit. Set(20); bs 1. set(1); bs 1.

Code Bit. Set bs 1 = new Bit. Set(20); bs 1. set(1); bs 1. set(4); Bit. Set bs 2 = new Bit. Set(20); bs 2. set(4); bs 2. set(5); System. out. println("bits 1 = " + bs 1); System. out. println("bits 2 = " + bs 2); if(bs 1. equals(bs 2)) System. out. println ("bits 1 == bits 2n"); else System. out. println ("bits 1 ! = bits 2n"); see Use. Bit. Set. java > java Use. Bit. Set Bits 1 = {1, 4} Bits 2 = {4, 5} bits 1 ! = bits 2 ANDing bits 1 and bits 2 bits 1 = {4} // logically AND the first two Bit. Sets bs 1. and(bs 2); System. out. println("ANDing bits 1 and bits 2"); System. out. println("bits 1 = " + bs 1. to. String()); Contest Algorithms: 3. Collections 55

Bit. Set Implementation § Bit. Set uses a single bit to a boolean value.

Bit. Set Implementation § Bit. Set uses a single bit to a boolean value. § Internally the bits are managed inside a long[] § But Bit. Set only supports int keys, so is limited to 232 -1 bits § Bit. Set 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 Contest Algorithms: 3. Collections 56

§ A graph showing the space used by boolean[] and Bit. Set for 100,

§ A graph showing the space used by boolean[] and Bit. Set for 100, 000 elements: Contest Algorithms: 3. Collections 57

The Sieve of Eratosthenes § The process is a systematic way of selecting values

The Sieve of Eratosthenes § 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: Contest Algorithms: 3. Collections 58

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

§ 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 59

§ Continue until the end of the list is reached: § The uncrossed numbers

§ Continue until the end of the list is reached: § The uncrossed numbers are the primes from 2 to 29. Contest Algorithms: 3. Collections 60

Bitset Version of Sieve see Sieve. Bit. Set. java § Represent the list as

Bitset Version of Sieve see Sieve. Bit. Set. java § 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 Bit. Set can represent a very large number sequence, and is quite fast too. Contest Algorithms: 3. Collections 61

Bit. Set sieve = new Bit. Set(1024); int size = sieve. size(); // set

Bit. Set sieve = new Bit. Set(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 final. Bit = (int) Math. sqrt(size); // can stop at √n, not n for (int i = 2; i < final. Bit; i++) { if (sieve. get(i)) { for (int j = 2 * i; j < size; j += i) sieve. clear(j); // to false } } : Contest Algorithms: 3. Collections 62

// display prime numbers from 2 to 1023 int counter = 0; System. out.

// 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("%4 d", i); System. out. print(++counter % 7 == 0 ? "n" : " "); } } Contest Algorithms: 3. Collections 63

Contest Algorithms: 3. Collections 64

Contest Algorithms: 3. Collections 64