Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
C and Data Structures Baojian Hua
Advertisements

1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
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
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Abstract Syntax Tree Discrete Mathematics and Its Applications Baojian Hua
Minimum Spanning Trees (MSTs) Prim's Algorithm For each vertex not in the tree, keep track of the lowest cost edge that would connect it to the tree This.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Extensible Array C and Data Structures Baojian Hua
Trees, Binary Search Trees, Balanced Trees, Graphs Svetlin Nakov Telerik Corporation
Elaboration or: Semantic Analysis Compiler Baojian Hua
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
A balanced life is a prefect life.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Abstract Data Type C and Data Structures Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Elaboration or: Semantic Analysis Compiler Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Relation Discrete Mathematics and Its Applications Baojian Hua
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
Trees and Red-Black Trees Gordon College Prof. Brinton.
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
Tree 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
Graph Traversal Discrete Mathematics and Its Applications 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 Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Graph Traversal C and Data Structures Baojian Hua
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Search Related Algorithms. Graph Code Adjacency List Representation:
Trees By Charl du Plessis. Contents Basic Terminology Basic Terminology Binary Search Trees Binary Search Trees Interval Trees Interval Trees Binary Indexed.
Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
CS Data Structures II Review & Final Exam. 2 Topics Review Final Exam.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Hash C and Data Structure Baojian Hua
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
abstract data types built on other ADTs
Interfaces.
HW05: Graphs and Shortest Paths
Graph Implementations
Discrete Mathematics and
Discrete Mathematics and
Presentation transcript:

Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua

Information Association In processing data structures, one often need to associate information with some kind of items Examples from graphs: Vertex: data, value, degree (in, out), … Edge: weight, … BFS: visited, distance, …, DFS: visited, discover, finish, … MST: tree edges, … Dijkstra: tree edges, … …

Several Methods Monomorphic data in item Polymorphic data in item Auxiliary table (dictionary) Dynamically extensible space in item Next, I ’ ll take DFS as a running example

Graph Representation: Adjacency List #include “linkedList.h” #include “graph.h” struct graph { linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; linkedList edges; }; struct edge { vertex from; vertex to; } >10->20->3

DFS Algorithm dfs (vertex start, tyVisit visit) { visit (start); markVisited (start); for (each adjacent vertex u of “start”) if (not visited u) // but how do we know? dfs (u, visit); }

DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); dfs (startV, visit); for (each vertex u in graph g) if (not visited u) // but how do we know? dfs (u, visit); }

Method #1: Monomorphic Data in Vertex #include “linkedList.h” #include “graph.h” struct graph { linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; int visited; // a flag linkedList edges; }; struct edge { vertex from; vertex to; } >10->20->3

Adjacency List-based: Creating New Vertex vertex newVertex (poly data) { vertex v = malloc (sizeof (*v)); v->data = data; v->visited = 0; v->edges = newLinkedList (); return v; } data v ### /\ visited=0 data edges

DFS Algorithm dfs (vertex start, tyVisit visit) { visit (start); start->visited = 1; for (each adjacent vertex u of “start”) if (0==u->visited) // Easy! :-) dfs (u, visit); }

DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); dfs (startV, visit); for (each vertex u in graph g) if (0==u->visited) dfs (u, visit); }

Sample Graph DFS a0a0 d0d0 b0b0 f0f0 e0e0 c0c0 dfs (g, “ a ”, strOutput);

Sample Graph DFS a1a1 d0d0 b0b0 f0f0 e0e0 c0c0 dfs (g, “ a ”, strOutput); print a;

Sample Graph DFS a1a1 d0d0 b1b1 f0f0 e0e0 c0c0 dfs (g, “ a ”, strOutput); print a; // a choice print b;

Sample Graph DFS a1a1 d0d0 b1b1 f0f0 e1e1 c0c0 dfs (g, “ a ”, strOutput); print a; // a choice print b; print e;

Sample Graph DFS a1a1 d1d1 b1b1 f0f0 e1e1 c0c0 dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;

Sample Graph DFS a1a1 d1d1 b1b1 f0f0 e1e1 c1c1 dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c;

Sample Graph DFS a1a1 d1d1 b1b1 f1f1 e1e1 c1c1 dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d; // a choice print c; print f;

Method #2: Polymorphic Data in Vertex #include “linkedList.h” #include “graph.h” #include “poly.h” struct graph { linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; poly visited; // a hook linkedList edges; }; struct edge { vertex from; vertex to;} >10->20->3

Adjacency List-based: Creating New Vertex vertex newVertex (poly data) { vertex v = malloc (sizeof (*v)); v->data = data; v->visited = newNat (0); v->edges = newLinkedList (); return v; } data v ### /\ visited data edges 0

DFS Algorithm dfs (vertex start, tyVisit visit) { visit (start); start->visited = newNat (1); for (each adjacent vertex u of “start”) if (natIsZero ((nat)(u->visited))) dfs (u, visit); }

DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); dfs (startV, visit); for (each vertex u in graph g) if (natIsZero ((nat)(u->visited))) dfs (u, visit); }

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput);

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; A garbage! A style of functional programming!

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; // a choice print b; The rest is left as an exercise!

Method #3: Auxiliary Table (Memoization) #include “linkedList.h” #include “graph.h” struct graph { linkedList vertices; }; // Data structure definitions unchanged! :-) typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; linkedList edges; }; struct edge { vertex from; vertex to; } >10->20->3

Interface for the “ table ” ADT // We need a table to memoize the data items that // satisfy some conditions. #ifndef TABLE_H #define TABLE_H typedef struct tableStruct *table; table newTable (); void tableInsert (table t, poly key, poly value); void tableLookup (table t, poly key); #endif // Implementation is any dictionary-like data // structure, such as linkedList, bst, hash, etc.

DFS Algorithm dfs (vertex start, tyVisit visit, table tb) { visit (start); tableInsert (tb, start, 1); for (each adjacent vertex u of “start”) if (0==tableLookup (tb, u)) dfs (u, visit, tb); }

DFS Algorithm void dfsMain (graph g, poly start, tyVisit visit) { vertex startV = searchVertex (g, start); table tb = newTable (); dfs (startV, visit, tb); for (each vertex u in graph g) if (!tableLookup (tb, u)) dfs (u, visit, tb); }

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); Key (vertex) Value (int) tb tb = newTable();

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; tb Key (vertex)a Value (nat)1 tableEnter(tb, “a”, newNat(1));

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; tb Key (vertex)a Value (nat)1 tableLookup (tb, “b”); // ==NULL

Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; // a choice print b; tb key (vertex)ab value (nat)11 The rest left to you! tableEnter (tb, “b”, newNat(1));

Method #4: Dynamically Extensible Space (DES) in Vertex #include “linkedList.h” #include “graph.h” #include “plist.h” struct graph { linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; plist list; // a list of hooks linkedList edges; }; struct edge { vertex from; vertex to;} >10->20->3

What ’ s a “ plist ” ? A “ plist ” stands for “ property list ” Property: some kind of value we care A generalization of polymorphic data fields data v ### /\ plist data edges ### /\

What ’ s a “ plist ” ? A “ plist ” stands for “ property list ” it ’ s just a list of hooks A hook holds some property Dynamically extensible data v ### /\ plist data edges ### … v1v2v3

Sample Vertex After DFS visited==1, discover==3, finish=6; // Suppose the function adding property p to // vertex v is: attach (vertex v, poly p); // which is roughly equivalent to: linkedListInsertHead (v->plist, p); data v ### plist “a” edges ### /\ 136

Sample Vertex After DFS visited==1, discover==3, finish=6; linkedListInsertHead (v->plist, p); // the calls: attach (“a”, newNat (6)); attach (“a”, newNat (3)); attach (“a”, newNat (1)); data v ### plist “a” edges ### /\ 136

What ’ s a “ plist ” ? data v ### /\ plist data edges ### /\ Suppose we have two calls: attach (v, newNat (1)); // discover attach (v, newNat (6)); // finish data v ### /\ plist data edges ### /\ 61

How to Find a Property? data v ### /\ plist data edges ### /\ Suppose we have two functions: attach (v, newNat (1)); // discover attach (v, newNat (6)); // finish // How to find v ’ s finish time? findPlist (v->plist);??? data v ### /\ plist data edges ### /\ 61 Associate every data item with a tag (think a key).

How to Find a Property? data v ### /\ plist data edges ### /\ Modifications to attach functions: k1 = attach (v, newNat (1)); // discover k2 = attach (v, newNat (6)); // finish // How to find v ’ s finish time? findPlist (v, k1); // return 1 findPlist (v, k2); // return 6 data v ### /\ plist data edges ### /\ Associate every data item with a tag (think of a key). (k1, 1), (k2, 6) All ks are unique. k26k11

Property List Interface // In file “plist.h” #ifndef PLIST_H #define PLIST_H #include “key.h” typedef struct plistStruct *plist; // essentially a list of tuples: (key, data) void plistInsert (plist list, key k, poly data); poly plistLookup (plist list, key k); #endif // Read the offered code for implementation.

Representation Revisited #include “linkedList.h” #include “graph.h” #include “plist.h” struct graph { linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; plist list; // a list of hooks linkedList edges; }; struct edge { vertex from; vertex to;} >10->20->3

DFS with DES dfsMain (graph g, vertex start, tyVisit visit) { key visited = newKey (); dfs (start, visit, visited); for (each vertex u in graph g) if (!(find (u, visited)) dfs (u, visit, visited); }

DFS with DES dfs (vertex v, tyVisit visit, key visited) { visit (v); attach (v, visited, newNat (1)); for (each successor t of v) if (!(find (t, visited))) dfs (t, visit, visited); }

A Case Study: Order Statistics on Red-Black Tree

Red-Black Tree in C // In file “rbTree.h” #ifndef RED_BLACK_TREE #define RED_BLACK_TREE typedef struct rbTree *rbTree; rbTree newRbTree (); void rbTreeInsert (rbTree t, poly data); void rbTreeDelete (rbTree t, poly data); #endif

Red-Black Tree in C // In file “rbTree.c” #include “rbTree.h” struct rbTree { poly data; int color; // 0 for black, 1 for red rbTree left; rbTree right; }; // functions left to you

Rank We want to add a “ rank ” field (property) to the red-black tree: to maintain the tree vertex ’ s order statistics may after the red-black tree and its operations have been implemented after the fact How to implement this?

Red-Black Tree with Rank // In file “rbTree.h” #ifndef RED_BLACK_TREE #define RED_BLACK_TREE typedef struct rbTree *rbTree; rbTree newRbTree (); void rbTreeInsert (rbTree t, poly data); void rbTreeDelete (rbTree t, poly data); void rbTreeInsertRank(rbTree t, poly data, key k); void rbTreeDeleteRank(rbTree t, poly data, key k); int rbTreeRank (rbTree t, poly data, key k); #endif

Red-Black Tree in C // In file “rbTree.c” #include “plist.h” #include “rbTree.h” struct rbTree { poly data; enum {RED, BLACK} color; plist plist; rbTree left; rbTree right; }; // functions left to you

Client Code // In file “main.c” #include “key.h” #include “rbTree.h” int main () { rbTree t1 = newRbTree (); rbTreeInsert (t1, newNat (9)); …; key rank = newKey (); rbTree t2 = newRbTree (); rbTreeInsertRank (t2, newNat (88), rank); …; }

Red-Black Tree in Java class RbTree { X data; int color; RbTree left; RbTree right; // constructors and methods omitted void insert (X data){…} void delete (X data){…} … }

Red-Black Tree with Rank // Is Inheritance good for this purpose? class RbTreeRank extends RbTree { int size; // constructors and methods overwritten? void insert (X data){…} void delete (X data){…} … } // Not so great!

Red-Black Tree with Rank class RbTree { X data; int color; Plist plist; RbTree left; RbTree right; // constructors and methods are overrided void insert (X data){…} void delete (X data){…} void insert (X data, key k); void delete (X data, key k); … }

Comparison What ’ s the representations difference? What ’ s the efficiency difference? What ’ s the space efficiency difference? What are the overall wins and hurts of various methods?