Linked List (Part II). Introduction Definition of equivalence relation: A relation ≡ over a set...

Preview:

Citation preview

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 it is reflexive, symmetric, and transitive over S.○ Example: the “equal to” (=) relationship is an

equivalence relation, since1. x = x.

2. x = y implies y = x.

3. x = y and y = z implies x = z.

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}

Idea Phase 1

The equivalence pairs (i, j) are read in and stored.

Phase 2Begin 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.

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; }

Consideration

Consider implementation using array (for easy random access)m: the number of input pairsn: the number of objects

Declare a Boolean array, pairs[n][n].pairs[i][j] =true iff (i, j) is an input pair.

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(n2) of time is required to perform

initialization.

0 0 0 0 0 0

0 0 0 0 0 0

1

1 0 1 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

0 1 2 3 4 5

0

1

2

3

4

5

1

1 1

1

(0, 2) = true and (2, 1) = true Implies (0, 1) = true

(2, 3) = true Implies (0, 3) = true

0

Implementation Using Linked List Use one linked list to represent each row of

the array pairs.

0 1 2 3 4 5 6 7 8 9 10 11first

first is a 1D array with each element first[i] is a pointer to the first node for row i.

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; }

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.

0 1 2 3 4 5 6 7 8 9 10 11first

4 03 1 10 69 847

0

8

10

6

9

5

1

311

20

2

11

4

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.

out0 0

0 1

0 0

2 3

0 0

4 5

0 0

6 7

0 0

8 9

0 0

10 11

0 1 2 3 4 5 6 7 8 9 10 11first

4 03 1 10 69 847

0

8

10

6

9

5

1

311

20

2

11

4

out0 0

0 1

0 0

2 3

0 0

4 5

0 0

6 7

0 0

8 9

0 0

10 11

11

1

4

0 , 11 , 4

11

7

, 7

1

null2

1

, 2

Linked stack:

The first equivalence class

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;};

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; }

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.

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);

0 1 2

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; }

Analysis of Equivalence()

Definem: 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).

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).

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.

Introduction

Node structure for sparse matrices.Header nodesElement nodes

○ The field head is used to distinguish whether the node is a header node (true) or an element node (false).

row col value head

next

down right

head

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.

Node Structure

next

down right

row col value

down right

Header node Element node

down right

row col value

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.

Example Consider the following 5x4 sparse matrix:

How many header nodes?

How many element nodes?

0600

1008

0000

3004

0002

5 4 6

H H0 H1 H2 H3 H4

H0

H1

H2

H3

H4

0 0 2

1 0 4

3 0 8

1 3 3

3 3 1

4 2

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;};

Representation of Matrix

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

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]);}

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

5 4 6

H H0 H1 H2 H3 H4

H0

H1

H2

H3

H4

0 0 2

1 0 4

3 0 8

1 3 3

3 3 1

4 2

prev1 temp1

temp2

prev2

1 2 9

Insert a node at (1, 2)

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.

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 listA 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

Representationclass 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;};

left right left right left right0 right left 0

first last

dummydummy

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; }}

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;}

left right

1left right

2

left right

3

temptemp->left

newNode

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;}

left right

1

left right

2

left right

3

temp->righttemp->left temp

Recommended