Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.

Similar presentations


Presentation on theme: "Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff."— Presentation transcript:

1 Linked List (Part II)

2

3 Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff it is reflexive, symmetric, and transitive over S. ○ Example: the “equal to” (=) relationship is an equivalence relation, since 1. x = x. 2. x = y implies y = x. 3. x = y and y = z implies x = z.

4 Equivalence Class Problem  To partition the set S into equivalence classes such that two members x and y of S are in the same equivalence class iff x ≡ y.  Example: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, and 11 ≡ 0. Then, we get the following equivalence classes: {0, 2, 4, 7, 11}; {1, 3, 5}; {6, 8, 9, 10}

5 Idea  Phase 1 The equivalence pairs (i, j) are read in and stored.  Phase 2 Begin at 0 and find all pairs of (0, j). ○ If 0 and j are in the same class, include k if any (j, k) exists. Because, i ≡ j and j ≡ k implies i ≡ k (transitivity). Continue in this way until the entire equivalence class containing 0 has been found and output. Start an object that is not output for finding new equivalence class.

6 Program 4.26 What kind of data structure can be used to hold these pairs? void Equivalence () { initialize; while more pairs { input the next pair (i, j); process this pair; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; }

7 Consideration  Consider implementation using array (for easy random access) m: the number of input pairs n: the number of objects  Declare a Boolean array, pairs[n][n]. pairs[i][j] =true iff (i, j) is an input pair.

8 Implementation Using Array  Example: (0, 2) = true and (2, k) = true for all k implies (0, k) = true.  Disadvantages: Could be wasteful of space if n is small. At least O(n 2 ) of time is required to perform initialization. 000000 000000 1 10100 000000 000000 000000 012345 0 1 2 3 4 5 1 11 1 (0, 2) = true and (2, 1) = trueImplies (0, 1) = true (2, 3) = trueImplies (0, 3) = true 0

9 Implementation Using Linked List  Use one linked list to represent each row of the array pairs. 01234567891011 first first is a 1D array with each element first[i] is a pointer to the first node for row i.

10 Program 4.27 void Equivalence () { read n;//read in the number of objects initialize first[0:n-1] to 0 and out[0:n-1] to false; while more pairs { input the next pair (i, j); process this pair; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; }

11 Example – Phase 1  Consider the following equivalence relations: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, and 11 ≡ 0. 01234567891011 first 4031 10 698 4 70 8 6951311202 4

12 Example – Phase 2  A Boolean array out[n] is used for determining whether object i is yet to be printed. The array is initialized to false. For each i such that out[i] = false, the elements in the list first[i] are output. For satisfying transitivity, a linked stack is created. out 00 01 00 23 00 45 00 67 00 89 00 1011

13 01234567891011 first 4031 10 698 4 70 8 6951311202 4 out 00 01 00 23 00 45 00 67 00 89 00 1011 1 4 0, 11, 4 11 7, 7 1 null 2 1, 2 Linked stack: The first equivalence class

14 Program 4.28 – Part I class ENode { friend void Equivalence(); public: ENode(int d=0, ENode *next=0) //constructor {data = d; link = next;} private: int data; ENode *link; };

15 Program 4.28 – Part II (Phase 1) void Equivalence() { ifstream inFile("equiv.in", ios::in); if (!inFile) throw "Cannot open input file."; int n; inFile >> n; ENode **first = new ENode*[n]; bool *out = new bool [n]; for (int i=0; i<n, i++) first[i] = 0; for (int i=0; i<n, i++) out[i] = false; //Phase 1 int x, y; inFile >> x >> y; while (inFile.good()) { first[x] = new ENode(y, first[x]); first[y] = new ENode(x, first[y]); inFile >> x >> y; }

16 Program 4.28 – Part III (Phase 2) for (int i=0; i<n; i++) { if (!out[i]) { cout << endl; << "A new class: " << i; out[i] = true; ENode *x = first[i]; ENode *top = 0; //initialize stack while (1) { while (x) { int j = x->data; if (!out[j]) { cout << ", " << j; out[j] = true; ENode *y = x->link; x->link = top; top = x; x = y; } else x = x->link; } if (!top) break; x = first[top->data]; top = top->link; //pop } Check if the stack is empty Check every node of out[i] if out[i] is true.

17 Array of Pointers  Declare a pointer: ENode *ptr = new ENode(1, 0);  Declare a pointer of array with fixed length: ENode *ptr[3]; for (int i=0; i<3; i++) ptr[i] = new ENode(i, 0);  Declare a pointer of array with arbitrary length: ENode **ptr; ptr = new ENode * [3]; for (int i=0; i<3; i++) ptr[i] = new ENode(i, 0); 012

18 Program 4.28 – Part IV for (int i=0; i<n; i++) { while (first[i]) { ENode *delnode = first[i]; first[i] = delnode->link; delete delnode; } delete [] first; delete [] out; }

19 Analysis of Equivalence()  Define m: the number of input pairs. n: the number of objects.  Space complexity: At most 2m nodes are inserted into first. The array out of length n is used. Space complexity: O(m+n).

20 Analysis of Equivalence()  Time complexity: Phase 1: ○ The initialization of first and out takes O(n) time. ○ The processing of each input pair is O(1) and there are m pairs. ○ Totally, the complexity for this phase is O(m+n). Phase 2: ○ The for-loop executes n times. Each unprinted node is put onto the linked stack at most once and there are 2m nodes to examine. ○ The time for this phase is O(m+n).

21

22 Introduction  In Chapter 2, we use array to implement a sparse matrix. The sequential representation permits easy access of matrix terms by row. However, accessing all the terms in a specific column is difficult.  To provide easy access both by row and by column, we devise a linked representation for a sparse matrix.

23 Introduction  Node structure for sparse matrices. Header nodes Element nodes ○ The field head is used to distinguish whether the node is a header node (true) or an element node (false). rowcolvaluehead next downright head

24 Header Nodes  Number of header nodes = 1 + max {number of rows, number of columns}.  The header node for row i is also the header node for column i. The down field: used to link into a column list. The right field: used to link into a row list. The next field: used to link the next header nodes.  The list of header nodes has its header node, H, where the fields row and col are used to store the matrix dimension, and value is used to stored the number of nonzero ters.

25 Node Structure next downright rowcolvalue downright Header nodeElement node downright rowcolvalue Special use for the first node of the list of header nodes. Link to the next nonzero term in the same column. Link to the next nonzero term in the same row.

26 Example  Consider the following 5x4 sparse matrix:  How many header nodes?  How many element nodes?

27 546 HH0H1H2H3H4 H0 H1 H2 H3 H4 00210430813333142

28 Representation of MatrixNode class MatrixNode { friend class Matrix; public: MatrixNode(bool h=false, int r=-1, int c=-1, int v=0, MatrixNode *rp=0, MatrixNode *dp=0, MatrixNode *nt=0); private: MatrixNode *down, *right, *next; int row, col, value; bool head; };

29 Representation of Matrix class Matrix { public: Matrix(int r=0, int c=0); private: MatrixNode *headnode; MatrixNode **head; };

30 Constructor of Matrix Matrix::Matrix(int r, int c) { headnode = 0; head = 0; if (r <= 0 || c <= 0) return; int p = max(r, c); head = new MatrixNode*[p]; for (int i=0; i<p; i++) head[i] = new MatrixNode(true); for (int i=0; i<p-1; i++) head[i]->next = head[i+1]; headnode = new MatrixNode(true, r, c, 0, 0, 0, head[0]); }

31 C++ Program of Insertion void Matrix::Insertion(int row, int col, int value) { MatrixNode *prev1 = head[row]; MatrixNode *temp1 = head[row]->right; while (temp1 != NULL) { if (col col) break; prev1= temp1; temp1 = temp1->right; } MatrixNode * prev2 = head[col]; MatrixNode * temp2 = head[col]->down; while (temp2 != NULL) { if (row row) break; prev2= temp2; temp2 = temp2->down; } MatrixNode *newNode = new MatrixNode(false, row, col, value, temp1, temp2); prev1->right = newNode; prev2->down = newNode; }

32 546 HH0H1H2H3H4 H0 H1 H2 H3 H4 00210430813333142 prev1 temp1 temp2 prev2 129 Insert a node at (1, 2)

33 Analysis of Insertion()  Suppose there are n nonzero entries.  Space complexity: O(1)  Time complexity: O(n) Compared to the implementation using array, inserting an arbitrary entry into the matrix has no need for data shift anymore. Easy to access a specific row or column.

34

35 Introduction  The difficulties of using a singly linked list The search of the list is limited to single direction. ○ The only way to the preceding node is to start at the beginning. ○ The same problem arises when one wishes to delete an arbitrary node from the list.  Doubly linked list A node in a doubly linked list has at least two fields ○ left (left link): link to the preceding node ○ right (right link): link to the following node

36 Representation class DbListNode { friend class DbList; public: DbListNode(int d=0, DbListNode *llink=0, DbListNode *rlink=0) { data = d; left = llink; right = rlink; }; private: int data; DbListNode *left, *right; }; class DbList { private: DbListNode *first, *last; }; leftrightleftrightleftright0 left0 first last dummy

37 Construction and Destruction DbList::DbList() { first = new DbListNode(); last = new DbListNode(); first->right = last; first->left = NULL; last->left = first; last->right = NULL; } DbList::~DbList() { while (first != NULL) { DbListNode *temp = first->right; delete first; first = temp; }

38 Inserting a Node into an Ordered List  Suppose the list is sorted in non-decreasing order. void DbList::Insertion(int d) { DbListNode *temp = first->right; while (temp != last) { if (temp->data >= d) break; temp = temp->right; } DbListNode *newNode = new DbListNode(d, temp->left, temp); temp->left->right = newNode; temp->left = newNode; } leftright 1 leftright 2 leftright 3 temp temp->left newNode

39 Deleting a Node from an Ordered List bool DbList::Deletion(int d) { DbListNode *temp = first->right; while (temp != last) { if (temp->data == d) { temp->left->right = temp->right; temp->right->left = temp->left; delete temp; return true; } temp = temp->right; } return false; } leftright 1 leftright 2 leftright 3 temp->righttemp->lefttemp


Download ppt "Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff."

Similar presentations


Ads by Google