Download presentation
Presentation is loading. Please wait.
1
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn
2
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, … …
3
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
4
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; } 0 1 2 3 0->10->20->3
5
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); }
6
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); }
7
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; } 0 1 2 3 0->10->20->3
8
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
9
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); }
10
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); }
11
Sample Graph DFS a0a0 d0d0 b0b0 f0f0 e0e0 c0c0 dfs (g, “ a ”, strOutput);
12
Sample Graph DFS a1a1 d0d0 b0b0 f0f0 e0e0 c0c0 dfs (g, “ a ”, strOutput); print a;
13
Sample Graph DFS a1a1 d0d0 b1b1 f0f0 e0e0 c0c0 dfs (g, “ a ”, strOutput); print a; // a choice print b;
14
Sample Graph DFS a1a1 d0d0 b1b1 f0f0 e1e1 c0c0 dfs (g, “ a ”, strOutput); print a; // a choice print b; print e;
15
Sample Graph DFS a1a1 d1d1 b1b1 f0f0 e1e1 c0c0 dfs (g, “ a ”, strOutput); print a; // a choice print b; print e; print d;
16
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;
17
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;
18
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;} 0 1 2 3 0->10->20->3
19
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
20
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); }
21
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); }
22
Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); 0 000 0 0
23
Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; 0 000 0 0 1 A garbage! A style of functional programming!
24
Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); print a; // a choice print b; 0 000 0 0 1 1 The rest is left as an exercise!
25
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; } 0 1 2 3 0->10->20->3
26
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.
27
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); }
28
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); }
29
Sample Graph DFS a d b fe c dfs (g, “ a ”, strOutput); Key (vertex) Value (int) tb tb = newTable();
30
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));
31
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
32
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));
33
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;} 0 1 2 3 0->10->20->3
34
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 ### /\
35
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
36
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
37
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
38
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
39
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).
40
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
41
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.
42
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;} 0 1 2 3 0->10->20->3
43
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); }
44
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); }
45
A Case Study: Order Statistics on Red-Black Tree
46
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
47
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
48
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?
49
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
50
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
51
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); …; }
52
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){…} … }
53
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!
54
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); … }
55
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.