Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
C and Data Structures Baojian Hua
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Data Structure & Abstract Data Type
C Module System C and Data Structures Baojian Hua
Data Structure & Abstract Data Type C and Data Structures Baojian Hua
Abstract Syntax Tree Discrete Mathematics and Its Applications Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Abstract Data Type C and Data Structures Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
C and Data Structures Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
String C and Data Structures Baojian Hua
Pointers and Arrays C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Hash Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Hash C and Data Structure Baojian Hua
Polymorphism C and Data Structures Baojian Hua
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Abstract Syntax Trees Compiler Baojian Hua
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Hash C and Data Structure Baojian Hua
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
CSC172 Data Structures Linked Lists (C version)
LinkedList Class.
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Discrete Mathematics and
Recursive Objects Singly Linked Lists.
Review: C++ class represents an ADT
Presentation transcript:

Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua

Map

Map Interface signature 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

Interface in C #ifndef MAP_H #define MAP_H typedef struct map *map; // type map typedef void *poly; // type key, value map newMap (); void mapInsert (map m, poly k, poly v); poly mapLookup (map m, poly k); void mapDelete (map m, poly k); … #endif

Implementation in C #include “map.h” struct map { // your favorite concrete representation }; map newMap () { // real code goes here } …

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

Sample Impl ’ Using Linked List // functions map newMap () { map m = malloc (sizeof (*m)); m->list = newLinkedList (); return m; } list m

Sample Impl ’ Using Linked List // functions void mapInsert (map m, poly k, poly v) { linkList list = m->list; linkedListInsert (list, newTuple (k, v)); return; } list m

Sample Impl ’ Using Linked List // functions poly mapLookup (map m, poly k) { linkList list = s->list; while(…) { // scan the list and lookup key k // return v, if found } return NULL; } list m

Set

Set Interface signature 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

Interface in C #ifndef SET_H #define SET_H typedef struct set *set; // type set typedef void *poly; // type t 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; } list s

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

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 Testing How to do equality testing on “polymorphic” data? 1. “equals” function pointer as argument. int linkedListExist (linkedList list, poly x, tyEq equals); 2. “equals” function pointers in data. int linkedListExist (linkedList list, poly x) { for (…p…) (p->data)->equals (p->data, x); } // As we can see next in C++ or Java.

Client Code int main () { set s1 = newSet (); set s2 = newSet (); for (…) setInsert (s1, …); for (…) setInsert (s2, …); set s3 = setUnion (s1, s2); }

Summary So Far set

Set in Java

Interface in Java public interface SetInter // the type “set” { int size (); // “Object” is very polymorphic… void insert (Object x); SetInter union (SetInter s); … } // Follow this, all the stuffs are essentially // same with those in C

Or Using Generic // Type “set”, with type argument “X” public interface SetInter { int size (); void insert (X x); SetInter union (SetInter s); … } // We’ll discuss this strategy in following // slides

Implementation in Java public class Set implements SetInter { // any concrete internal representation Set () // constructor { // code goes here } int size () { // code goes here } … }

Sample Impl ’ Using Linked List import ….linkedList; public class Set implements SetInter { private linkedList list; Set () { this.list = new LinkedList (); }

Sample Impl ’ Using Linked List import ….linkedList; public class Set implements SetInter { private linkedList list; int size () { return this.list.size (); }

Sample Impl ’ Using Linked List import ….linkedList; public class Set implements SetInter { private linkedList list; void insert (X x) { if (exists (x)) // equality testing? return; this.list.insert (x); return; }

Client Code import ….Set; public class Main { public static void main (String[] args) { SetInter s1 = new Set (); SetInter s2 = new Set (); s1.size (); s1.union (s2); }

Bit-Vector

Bit-Vector Interface interface 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” // a not-so efficient one struct bitArray { int *array; int size; };

Operations bitArray 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; }

Operations bitArray 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 simplicity public class BitArray { private int[] array; BitArray (int size) { this.array = new int[size]; }

Other Methods public 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; }

Bit-Vector-based Set Representation

Big Picture Universe set

Client Code int main () { // cook a universe set set universe = newSet (); // cook sets s1 and s2 set s1 = newSet (); set s2 = newSet (); 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 set element? 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”}{“a”}{“d”}{“d”}

Big Picture 1. Build the bit-array from the universe baSet1 = [0, 0, 0, 0] baSet2 = [0, 0, 0, 0] {( “ a ”, 0), ( “ b ”, 3), ( “ c ”, 1 ” ), ( “ d ”, 2)} {“a”}{“a”}{“d”}{“d”}

Big Picture 1. Build the bit-array from the universe baSet1 = [0, 0, 0, 0] baSet2 = [0, 0, 0, 0] 2. Build bit-array from set baSet1 = [1, 0, 0, 0] baSet2 = [0, 0, 1, 0] {( “ a ”, 0), ( “ b ”, 3), ( “ c ”, 1 ” ), ( “ d ”, 2)} {“a”}{“a”}{“d”}{“d”}

Big Picture 1. Build the bit-array from the universe baSet1 = [0, 0, 0, 0] baSet2 = [0, 0, 0, 0] 2. Build bit-array from set baSet1 = [1, 0, 0, 0] baSet2 = [0, 0, 1, 0] {( “ a ”, 0), ( “ b ”, 3), ( “ c ”, 1 ” ), ( “ d ”, 2)} {“a”}{“a”}{“d”}{“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 memory int 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 }

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

How to Make Things Shared? In C: extern variables In C++ or Java: static fields What ’ s the pros and cons?

Client Code int 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! }