set, map & bit-vector discrete mathematics and its applications baojian hua [email protected]
Post on 21-Dec-2015
230 views
TRANSCRIPT
![Page 2: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/2.jpg)
Set
![Page 3: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/3.jpg)
Set Interfacesignature
type set // set type
type t // element type
set newSet ();
int setSize (set s);
void setInsert (set s, t x);
set setUnion (set s1, set s2);
…
end
![Page 4: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/4.jpg)
Interface in C#ifndef SET_H#define SET_H typedef struct setStruct *set; // type settypedef void *poly; // type t
set newSet ();int setSize (set s);void setInsert (set s, poly x);set setUnion (set s1, set s2);…
#endif
![Page 5: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/5.jpg)
Implementation in C// file “set.c”#include “set.h” struct setStruct{ // your favorite concrete representation};
set newSet (){ // real code goes here}
…
![Page 6: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/6.jpg)
Sample Impl’ Using Linked List#include “linkedList.h”#include “set.h” struct setStruct{ linkedList list;};
![Page 7: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/7.jpg)
Sample Impl’ Using Linked List// functions set newSet (){ set s = (set)malloc (sizeof (*s)); s->list = newLinkedList (); return s;}
lists
![Page 8: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/8.jpg)
Sample Impl’ Using Linked Listint setSize (set s){ linkList l = s->list; return linkedListSize (l);}
lists
![Page 9: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/9.jpg)
Sample Impl’ Using Linked Listvoid setInsert (set s, poly x){ if (setExists (s, x)) return;
linkedListInsert (s->list, x); return;}
![Page 10: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/10.jpg)
Sample Impl’ Using Linked Listint setExists (set s, poly x){ return linkedListExists (s->list, x);}
![Page 11: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/11.jpg)
Equality Testing// How to perform equality testing on // “polymorphic” data? Many solutions:// #1: “equals” function pointer as argument.int linkedListExists (linkedList list, poly x, tyEq equals);
// #2: “equals” function pointers in data.int linkedListExist (linkedList list, poly x){ foreach (node p in list) (p->data)->equals (p->data, x);}// As we can see next in C++ or Java.
![Page 12: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/12.jpg)
Client Codeint main (){ set s1 = newSet (); set s2 = newSet (); for (…) setInsert (s1, …);
for (…) setInsert (s2, …); set s3 = setUnion (s1, s2);
setOutput (s3);}
![Page 13: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/13.jpg)
Summary So Far
set
set
set
set
set
![Page 14: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/14.jpg)
Set in Java
![Page 15: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/15.jpg)
Interface in Javapublic interface SetInter // the type “set”
{
int size ();
// “Object” is very polymorphic…
void insert (Object x);
void union (SetInter s);
…
}
// Follow this, all the stuffs are essentially
// same with those in C
![Page 16: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/16.jpg)
Or Using Generic// Type “set”, with type argument “X”
public interface SetInter<X>
{
int size ();
void insert (X x);
void union (SetInter<X> s);
…
}
// We’ll discuss this strategy in following
// slides
![Page 17: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/17.jpg)
Implementation in Javapublic class Set<X> implements SetInter<X>{ // any concrete internal representation
public Set () { // code goes here }
public int size () { // code goes here } …}
![Page 18: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/18.jpg)
Sample Impl’ Using Linked Listimport ….linkedList;
public class Set<X> implements SetInter<X>{ private linkedList<X> list;
public Set () { this.list = new LinkedList<X> (); }
}
![Page 19: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/19.jpg)
Sample Impl’ Using Linked Listimport ….linkedList;
public class Set<X> implements SetInter<X>{ private linkedList<X> list;
public int size () { return this.list.size (); }
}
![Page 20: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/20.jpg)
Sample Impl’ Using Linked Listimport ….linkedList;
public class Set<X> implements SetInter<X>{ private linkedList<X> list;
public void insert (X x) {
if (exists (x)) // equality testing? return; this.list.insert (x); return;
}}
![Page 21: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/21.jpg)
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); }}
![Page 22: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/22.jpg)
Bit-Vector
![Page 23: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/23.jpg)
Bit-Vector Interfaceinterface type bitArray bitArray newBitArray (int size); void assignOne (bitArray ba, int index); bitArray and (bitArray ba1, bitArray ba2); …end
![Page 24: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/24.jpg)
Interface in C#ifndef BIT_ARRAY_H#define BIT_ARRAY_H
typedef struct bitArrayStruct *bitArray;
bitArray newBitArray (int size);void assignOne (bitArray ba, int index);bitArray and (bitArray ba1, bitArray ba2);…
#endif
![Page 25: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/25.jpg)
Implementation in C#include “bitArray.h”
// a not-so efficient onestruct bitArrayStruct{ int *array; int size;};
// What’s this set?
0 0 1 0 1 1 0
0 size-1
array
size
a
![Page 26: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/26.jpg)
OperationsbitArray newBitArray (int s){ bitArray ba = malloc (sizeof (*ba));
ba->array = malloc (sizeof (*(ba->array)) * s);
for (int k=0; k<s; k++) (ba->array)[k] = 0;
ba->size = s;
return ba;}
? ? ? ? ? ? ?
0 s-1
array
size
ba
![Page 27: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/27.jpg)
OperationsbitArray newBitArray (int s){ bitArray ba = malloc (sizeof (*ba));
ba->array = malloc (sizeof (*(ba->array)) * s);
for (int k=0; k<s; k++) (ba->array)[k] = 0;
ba->size = s;
return ba;}
0 0 0 0 0 0 0
0 s-1
array
size
ba
![Page 28: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/28.jpg)
OperationsbitArray and (bitArray ba1, bitArray ba2){ if (ba1->size != ba2->size) error (…);
bitArray ba = newBitArray (); for (…) …;
return ba;}
![Page 29: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/29.jpg)
Bit-Array in Java
![Page 30: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/30.jpg)
In Java// I omit the interface for simplicitypublic class BitArray{ private int[] array;
public BitArray (int size) { this.array = new int[size]; }}
![Page 31: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/31.jpg)
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;
}}
![Page 32: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/32.jpg)
Map
![Page 33: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/33.jpg)
Map Interfacesignature
type map
type key
type value
map newMap ();
void mapInsert (map m, key k, value v);
value mapLookup (map m, key k);
void mapDelete (map m, key k);
…
end
![Page 34: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/34.jpg)
Interface in C#ifndef MAP_H#define MAP_H typedef struct mapStruct *map; // type maptypedef void *key; // type keytypedef void *value // type value
map newMap ();void mapInsert (map m, key k, value v);poly mapLookup (map m, key k);void mapDelete (map m, key k);…
#endif
![Page 35: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/35.jpg)
Implementation in C#include “map.h” struct mapStruct{ // your favorite concrete representation};
map newMap (){ // real code goes here}
// other functions
![Page 36: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/36.jpg)
Sample Impl’ Using Linked List#include “linkedList.h”#include “map.h” struct mapStruct{ linkedList list;};
![Page 37: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/37.jpg)
Sample Impl’ Using Linked Listmap newMap (){ map m = (map)malloc (sizeof (*m)); m->list = newLinkedList (); return m;}
listm
![Page 38: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/38.jpg)
Sample Impl’ Using Linked Listvoid mapInsert (map m, key k, value v){ linkedList list = m->list; linkedListInsert (list, newTuple (k, v)); return;}
list
m
data
next
data
next
data
next
…
k1 v1 k2 v2 k3 v3
![Page 39: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/39.jpg)
Sample Impl’ Using Linked Listvalue mapLookup (map m, key k){ linkedList list = m->list; while(…) { // walk through the list and lookup key k // return corresponding v, if found }
return NULL;}
listm
![Page 40: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/40.jpg)
Bit-Vector-based Set Representation
![Page 41: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/41.jpg)
Big Picture
Universe
set
setset
set
![Page 42: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/42.jpg)
Client Codeint main (){ // cook a universe set set universe = newSet ();
// cook sets s1 and s2 set s1 = newSet (); set s2 = newSet (); setUnion (universe, s1, s2);}
![Page 43: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/43.jpg)
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 set elements?// Answer: associate every set element e a unique// (and continuous) integer i (what’s the use?).// Details leave to you.
![Page 44: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/44.jpg)
Big Picture
1. Build the bit-array from the universe
{(“a”, 0), (“b”, 3), (“c”, 1”), (“d”, 2)}
{“a”} {“d”}
![Page 45: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/45.jpg)
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”}
![Page 46: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/46.jpg)
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”}
![Page 47: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/47.jpg)
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.
![Page 48: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/48.jpg)
How to Store the Universe?// Method 1: stored in a separate setint main (){ // cook a universe set set universe = newSet ();
// cook two sets s1 and s2 set s1 = newSet (); set s2 = newSet ();
setUnion (universe, s1, s2); // ugly}
![Page 49: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/49.jpg)
How to Store the Universe?// Method 2: shared
Universe
set
setset
set
![Page 50: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/50.jpg)
How to Make Things Shared?
Same ideas, but different mechanisms in different languages: C: external variables C++, Java or C#: static fields
What’s the pros and cons?
![Page 51: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/51.jpg)
External Variablesset universe;
void newUniverse ()
{
universe = …;
}
![Page 52: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/52.jpg)
Static Fieldsclass Set<X>
{
static set universe;
…;
public Set ()
{…}
public setUnion (Set<X> s)
{…}
}
![Page 53: Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn](https://reader035.vdocument.in/reader035/viewer/2022062221/56649d645503460f94a46f62/html5/thumbnails/53.jpg)
Client Codeint main (){ // cook a universe set set universe = newUniverse ();
// cook two sets s1 and s2 set s1 = newSet (); set s2 = newSet ();
setUnion (s1, s2); // hummm, no universe AT ALL!}