Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relation Discrete Mathematics and Its Applications Baojian Hua

Similar presentations


Presentation on theme: "Relation Discrete Mathematics and Its Applications Baojian Hua"— Presentation transcript:

1 Relation Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn

2 Relation ADT A relation is a triple (A, B, R) A and B are two sets R \supset A*B Typical operations: relation creation search a vertex (or an edge) traverse all vertexes …

3 Example G = (A, B, R) A=B= {1, 2, 3, 4, 5, 6} R = {(1, 2), (2, 5), (3, 5), (3, 6), (4, 1), (4, 2), (5, 4), (6, 6)} 1 4 2 65 3

4 Representation Two popular strategies: array-based (adjacency matrix) Keep an extensible two-dimensional array M internally M[i][j] holds the relation info ’ of, if there exists one a generalization of the 0-1 matrix linear list-based (adjacency list) for every vertex vi, maintain a linear list list list stores vi ’ s outing edges Not the focus of this slide, we ’ d discuss this in later slides

5 Adjacency Matrix 0 0 1 2 5123 1 4 2 65 3 # # ## ## # # 3 4 5 4 Note the hash function: hash (n) = n-1

6 Adjacency List 1 2 3 4 1->2 3->53->6 5 4->1 6 2->5 4->2 5->4 6->6 Notice the pointers!

7 Graph in C

8 “ relation ” ADT in C: Interface // in file “relation.h” #ifndef RELATION_H #define RELATION_H typedef struct relationStruct *relation; relation newRelation (); void insertElem (relation r, poly data); void insertPair (relation r, poly from, poly to); // more to come later … #endif

9 Relation Implementation #1: Adjacency Matrix // adjacency matrix-based implementation #include “matrix.h” #include “hash.h” #include “relation.h” struct relationStruct { matrix matrix; int rows; int columns; // remember the index hash hashTable; }; ### 0 1 2 3 0 1 23

10 Matrix Interface // file “matrix.h” #ifndef MATRIX_H #define MATRIX_H typedef struct matrixStruct *matrix; matrix newMatrix (); void matrixInsert (matrix m, int i, int j); int matrixExtend (matrix m); #endif // Implementation could make use of a two- // dimensional extensible array, leave to you.

11 Adjacency Matrix-based: Relation Creation relation newRelation () { relation r = malloc (sizeof (*r)); r->matrix = newMatrix (); // an empty matrix r->rows = 0; r->columns = 0; g->hash = newHash (); return g; }

12 Adjacency Matrix-based: Inserting Element void insertElem (relation r, poly data) { int i = matrixExtend (r->matrix); hashInsert (r->hash, data, i); return; } ### 0 1 2 3 0 1 230 1 2 3 0123 ### 4 4

13 Relation Implementation #1: Inserting Pairs void insertPair (relation r, poly from, poly to) { int f = hashLookup (g->hash, from); int t = hashLookup (g->hash, to); matrixInsert (g->matrix, f, t); return; } ### 0 1 2 3 0 1 230 1 2 3 0123 #### 4 4

14 Client Code relation r = newRelation (); insertElem (r, 1); insertElem (r, 2); … insertElem (r, 6); insertPair (r, 1, 2); insertPair (r, 2, 5); … insertPair (r, 6, 6); 1 4 2 65 3


Download ppt "Relation Discrete Mathematics and Its Applications Baojian Hua"

Similar presentations


Ads by Google