Dynamically Extensible Data Structures. Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn. Information Association. In processing data structures, one often need to associate information with some kind of items Examples from graphs:
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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
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); }
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); }
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 data ### plist /\ 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 v1 v2 v3 v data … ### plist edges ### /\
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); “a” 1 3 6 v data ### plist /\ edges ###
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)); “a” 1 3 6 v data ### plist /\ edges ###
6 1 data v data ### plist /\ edges ### /\ What’s a “plist”? data v Suppose we have two calls: attach (v, newNat (1)); // discover attach (v, newNat (6)); // finish data ### plist /\ edges ### /\
6 1 data v data ### plist /\ edges ### /\ How to Find a Property? data v 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 ### plist /\ edges ### /\ Associate every data item with a tag (think a key).
How to Find a Property? 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 data ### plist /\ edges ### /\ k2 6 k1 1 data v data Associate every data item with a tag (think of a key). (k1, 1), (k2, 6) All ks are unique. ### plist /\ edges ### /\
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.
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); }
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 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
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.