Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log.

Similar presentations


Presentation on theme: "COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log."— Presentation transcript:

1 COMP 103 Bitsets

2 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log n) for everything, if balanced  Can we do even better?  BitSets  Hash tables  Same cost, regardless of size! O(1) !!!

3 3 More operations on Sets  Operations on single elements:  contains  add, remove  Operations on whole sets:  size (cardinality) of a set  iterate through the set  intersection(values common to two sets)  union(values in either of two sets)  set difference(values in one set but not the other)  test for equality/subset  Depending on the application, we may need any or all these operations to be fast!

4 4 eg. Set Intersection  Unsorted arrays / linked lists:  Algorithm: ?  Cost: # comparisons =  Sorted arrays / linked lists:  Algorithm: ?  Cost: # comparisons = BXDTQWEVZCRF FYUJHIMXOKPT BXDTQWEVZCRF F YUJHIMXOKPT

5 5 Set Intersection  Binary Search Trees:  Algorithm:  Cost:  Ex: Work out algorithms and costs for the other “whole set” operations, using different Set implementations.

6 6 Bit Sets If the range of possible elements for a set is:  discrete  finite  not too big... then can use an array of booleans:  one cell for each possible element  true if that element is in the set  false if that element is not in the set abcdefghijklmnopqrstuvwxyz ✔✗✔✔✔✔✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗ abcdefghijklmnopqrstuvwxyz ✔✔✔✔✔✔✔✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗ abcdefghijklmnopqrstuvwxyz ✗✗✗✗✗✗✗✗✗✗✗✗✗✗✔✔✔✔✔✗✗✗✗✗✗✗ a,e,i,o,u b,d,f,h,k,l,t g,j,p,q,y

7 7 BitSet Implementation private boolean[] data; public BitSet(int maxItems) { data = new boolean[maxItems]; } public boolean contains(int value) { if (value = data.length) return false; return data[value]; } public void add(int value) { if (value >= 0 && value < data.length) data[value] = true; } public void remove(int value) { if (value >= 0 && value < data.length) data[value] = false; }  Exercise: Extend add and remove to return booleans. Or signal an error. Or use array list and expand as needed.

8 8 BitSets: Costs uy ✔✗  set.contains(‘f’)  set.add(‘y’)  set.remove(‘u’)  Intersection: for (i=0…N) ans.data[i] = set1.data[i] && set2.data[i]  Cost: O(N) (number of possible values!!) but NOT item comparisons!!  Other operations: union, difference, equal, subset, …? abcdefghijklmnopqrstvwxz ✔✗✔✔✔✔✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗✗ Cost: O(1)

9 9 BitSets are the best  Very Fast!  Can be improved:  boolean[ ] data uses just one bit in each memory location  could use every bit, by thinking of the whole array as an int/long!  can then operate on sets using bitwise operations: & and |.  But:  Values must be integers or characters (to index into an array)  Number of possible values must be not too large (especially for intersection, union, iteration)  Eg: Days, months, timetable hours 00000100000100001000000000000001 ….…. Java might use less space than this, e.g. one byte per boolean.

10 10 O(1) Sets with big values? ✔ What about:  Sets of objects (including strings, URLs, email addresses)?  Sets of floating point numbers (double)? Need a way to compute an array index for an object, eg: add(“A sentence that belongs in a set of sentences”) “Hashing”: the number is the “hash code” of object 0123456789581N ✔✗✔✔✗✗✗✗✗✗✗ ⋯⋯ ✗ Hash function 581


Download ppt "COMP 103 Bitsets. 2 Sets, and more Sets!  Unsorted Array  Sorted ArrayO(n) for at least one of  Linked Listcontains, add, remove  Binary Search TreeO(log."

Similar presentations


Ads by Google