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, since
1. 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 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.
Program 4.26
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;
}
What kind of data structure can be used
to hold these pairs?
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.
Implementation Using Array
Example:
(0, 2) = true and (2, k) = true for all k implies
(0, k) = true.
0 1 2 3 4 5
0 0 1
0 1
0 1
0 0 0
(0, 2) = true and 1 0 0 0 0 0 0 (2, 1) = true Implies (0, 1) = true
2 0 1 0 1 0 0
3 0 0 0 0 0 0 (2, 3) = true Implies (0, 3) = true
4 0 0 0 0 0 0
5 0 0 0 0 0 0
Disadvantages:
Could be wasteful of space if n is small.
At least O(n2) of time is required to perform
initialization.
Implementation Using Linked List
Use one linked list to represent each row of
the array pairs.
first 0 1 2 3 4 5 6 7 8 9 10 11
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.
first 0 1 2 3 4 5 6 7 8 9 10 11
11
4 3 11 1
5 7
0 3 10
8 4 6
9 8 6 0
2
4 1 0 10 9 2
Example – Phase 2
A Boolean array out[n] is used for determining whether
object i is yet to be printed.
0 1 2 3 4 5 6 7 8 9 10 11
out
0 0 0 0 0 0 0 0 0 0 0 0
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.
0 , 11 , 4 , 7 , 2 The first equivalence class
Linked stack: 7
4 11
2 null
0 1 2 3 4 5 6 7 8 9 10 11
out
1
0 0 1
0 0 0
1 0 0 1
0 0 0 0 0
1
first 0 1 2 3 4 5 6 7 8 9 10 11
11
4 3 11 1
5 7
0 3 10
8 4 6
9 8 6 0
2
4 1 0 10 9 2
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> 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; idata;
if (!out[j])
{
cout link;
x->link = top;
top = x;
x = y;
}
else x = x->link; Check every node of out[i] if out[i] is true.
}
if (!top) break;
x = first[top->data];
top = top->link; //pop Check if the stack is empty
}
}
}
Array of Pointers
Declare a pointer:
ENode *ptr = new ENode(1, 0);
Declare a pointer of array with fixed length:
ENode *ptr[3]; 0 1 2
for (int i=0; ilink;
delete delnode;
}
}
delete [] first;
delete [] out;
}
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).
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 nodes
Element 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
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
Header node Element node
next row col value
Link to the next
down right down right nonzero term in
the same row.
row col value Link to the next
nonzero term in
the same column.
down right
Special use for the
first node of the list of
header nodes.
Example
Consider the following 5x4 sparse matrix:
2 0 0 0
4
0 0 3
0 0 0 0
8 0 0 1
0
0 6 0
How many header nodes?
How many element nodes?
H H0 H1 H2 H3 H4
5 4 6
0 0 2
H0
1 0 4 1 3 3
H1
H2
3 0 8 3 3 1
H3
4 2
H4
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 next = head[i+1];
headnode = new MatrixNode(true, r, c, 0, 0, 0, head[0]);
}
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;
}
H H0 H1 H2 H3 H4
5 4 6
prev2
0 0 2
H0
1 0 4 1 2 9 1 3 3
H1
prev1 temp1
H2
3 0 8 3 3 1
H3
temp2 4 2
H4
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 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
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;
};
first last
0 right left right left right left right left 0
dummy dummy
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;
}
newNode
2
temp->left temp
left right
1 3
left right left right
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;
}
temp->left temp temp->right
1 2 3
left right left right left right