set & bit-vector discrete mathematics and its applications baojian hua [email protected]

42
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua [email protected]

Post on 22-Dec-2015

219 views

Category:

Documents


6 download

TRANSCRIPT

Set & Bit-Vector

Discrete Mathematics andIts Applications

Baojian [email protected]

Set

Set Interfacesignature

type set

type X

set newSet ();

int setSize (set s);

void setInsert (set s, X x);

set setUnion (set s1, set s2);

end

Interface in C#ifndef SET_H#define SET_H typedef struct set *set; // type settypedef void *poly; // type X

set newSet ();int setSize (set s);void setInsert (set s, poly x);set setUnion (set s1, set s2);…

#endif

Implementation in C#include “set.h” struct set{ // your favorite concrete representation};

set newSet (){ // real code goes here}

Sample Impl’ Using Linked List#include “linkedList.h”#include “set.h” struct set{ linkedList list;};

Sample Impl’ Using Linked List// functions set newSet (){ set s = malloc (sizeof (*s)); s->list = newLinkedList (); return s;}

lists

Sample Impl’ Using Linked List// functions int setSize (set s){ linkList l = s->list; int size = linkedListSize (l); return size;}

lists

Sample Impl’ Using Linked List// functions void setInsert (set s, poly x){ if (setExists (s, x)) return;

linkedListInsert (s->list, x); return;}

Sample Impl’ Using Linked List// functions int setExist (set s, poly x){ return linkedListExists (s->list, x);}

Equality TestingHow to do equality testing on “polymorphic” data?1. Extra “equals” function pointer as argument.int linkedListExist (linkedList l, poly x, tyEq equals);

2. Extra “equals” function pointers in data.int linkedListExist (linkedList l, poly x){ for (…p…) (p->data)->equals (p->data, x);}// As we can see next in Java.

Client Codeint main (){ set s1 = newSet (); set s2 = newSet (); for (…) setInsert (s1, …);

for (…) setInsert (s1, …); set s3 = setUnion (s1, s2);}

Summary So Far

set

set

set

set

set

Set in Java

Interface in Javapublic interface SetInter // the type “set”

{

int size ();

// “Object” is the most polymorphic type

void insert (Object x);

SetInter union (SetInter s);

}

Or Using Generic// Type “set”, with type argument “X”

public interface SetInter<X>

{

int size ();

void insert (X x);

SetInter<X> union (SetInter<X> s);

}

// We’ll use this strategy in following slides

Implementation in Javapublic class Set<X> implements SetInter<X>{ // any concrete internal representation

Set () // constructor { // code goes here }

int size () { // code goes here } …}

Sample Impl’ Using Linked Listimport ….linkedList;

public class Set<X> implements SetInter<X>{ private linkedList<X> list;

Set () { this.list = new LinkedList<X> (); }

}

Sample Impl’ Using Linked Listimport ….linkedList;

public class Set<X> implements SetInter<X>{ private linkedList<X> list;

int size () { return this.list.size (); }

}

Sample Impl’ Using Linked Listimport ….linkedList;

public class Set<X> implements SetInter<X>{ private linkedList<X> list;

void insert (X x) { if (exists (x)) // equality testing? return; this.list.insert (x); return; }}

Client Codeimport ….Set;

public class Main{ public static void main (String[] args) { SetInter<String> s1 = new Set<String> (); SetInter<String> s2 = new Set<String> ();

s1.size (); s1.union (s2); }}

Bit-Vector

Bit-Vector Interfaceinterface type bitArray bitArray newBitArray (int size); void assignOne (bitArray ba, int index); bitArray and (bitArray ba1, bitArray ba2); …end

Interface in C#ifndef BITARRAY_H#define BITARRAY_H

typedef struct bitArray *bitArray;

bitArray newBitArray (int size);void assignOne (bitArray ba, int index);bitArray and (bitArray ba1, bitArray ba2);…

#endif

Implementation in C#include “bitArray.h”

struct bitArray{ int *array; int size;};

OperationsbitArray newBitArray (int i){ bitArray ba = malloc (sizeof (*ba));

ba->array = malloc (sizeof (*(ba->array)) * i);

for (int k=0; k<i; k++) (ba->array)[i] = 0;

ba->size = i;

return ba;}

OperationsbitArray and (bitArray ba1, bitArray ba2){ if (ba1->size != ba2->size) error (…);

bitArray ba = newBitArray (); for (…) assignOne (ba, …);

return ba;}

Bit-Array in Java

In Java// I omit the interface for simplicitypublic class BitArray{ private int[] array;

BitArray (int size) { this.array = new int[size]; }}

Other Methodspublic class BitArray{ private int[] array;

BitArray and (BitArray ba2) { if (this.size () != ba2.size ()) throw new Error (…);

BitArray ba = new BitArray (this.size());

… return ba;

}}

Re-implement Set using Bit-Vector

Big Picture

Universe

set

setset

set

Client Codeint main ()

{

set universe = newSet ();

// insert elements into universe

set s1 = newSet(); set s2 = newSet ();

// insert elements into s1 and s2

setUnion (universe, s1, s2);

}

What does the Universe Look Like?Universe is a set of (element, index) tuple. For

instance:

Universe = {(“a”, 0), (“b”, 3), (“c”, 1”),

(“d”, 2)}

Question: How to build such kind of universe from

the input strings?

Answer: associate every set element e a unique

(and continuous) integer i (i will be used as an

index in the bit-vector.

Details leave to you.

Big Picture

1. Build the bit-array from the universe

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

Big Picture

1. Build the bit-array from the universebaSet1 = [0, 0, 0, 0]baSet2 = [0, 0, 0, 0]

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

Big Picture

1. Build the bit-array from the universebaSet1 = [0, 0, 0, 0]baSet2 = [0, 0, 0, 0]2. Build bit-array from setbaSet1 = [1, 0, 0, 0]baSet2 = [0, 0, 1, 0]

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

Big Picture

1. Build the bit-array from theuniversebaSet1 = [0, 0, 0, 0]baSet2 = [0, 0, 0, 0]2. Build bit-array from setbaSet1 = [1, 0, 0, 0]baSet2 = [0, 0, 1, 0]

{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}

{“a”} {“d”}

3. Bit-vector operations

baSet3 = or (baSet1, baSet2)

baSet3 = [1, 0, 1, 0]

4. Turn baSet3 to ordinary set

How? Leave it to you.

How to Store the Universe?// Method 1: stored in separate spaceint main (){ set universe = newSet (); // insert elements into universe

set s1 = newSet(); set s2 = newSet (); // insert elements into s1 and s2

setUnion (universe, s1, s2); // ugly}

How to Store the Universe?// Method 2: shared

Universe

set

setset

set

How to Make Things Shared?

In C: extern variables In C++ or Java: static fields What’s the pros and cons?

Client Codeint main (){ set universe = newUniverse (); // insert elements into universe

set s1 = newSet(); set s2 = newSet (); // insert elements into s1 and s2

setUnion (s1, s2); // hummm, no universe AT ALL!}