Title: Data structure
1Data structure
24.8 EQUIVALENCE CLASSES
3Introduction
- 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 - x x.
- x y implies y x.
- x y and y z implies x z.
4Equivalence 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
5Idea
- 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.
6Program 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?
7Consideration
- Consider implementation using array (for easy
random access) - m the number of input pairs
- n the number of objects
- Declare a Boolean array, pairsnn.
- pairsij true iff (i, j) is an input pair.
8Implementation 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
1
2
3
4
5
0
0
0
0
0
0
1
0
1
1
(0, 2) true and
0
0
0
0
0
0
1
(2, 1) true
Implies (0, 1) true
1
0
1
0
0
2
1
1
0
0
0
0
0
0
0
3
(2, 3) true
Implies (0, 3) true
0
0
0
0
0
0
4
0
0
0
0
0
0
5
9Implementation 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
11
first
first is a 1D array with each element firsti is
a pointer to the first node for row i.
10Program 4.27
void Equivalence () read n //read in the
number of objects initialize first0n-1 to
0 and out0n-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
11Example 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
11
first
12Example Phase 2
- A Boolean array outn is used for determining
whether object i is yet to be printed. - The array is initialized to false.
- For each i such that outi false, the elements
in the list firsti are output. - For satisfying transitivity, a linked stack is
created.
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
130
, 11
, 4
, 7
, 2
The first equivalence class
null
Linked stack
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
1
1
1
1
1
0
1
2
3
4
5
6
7
8
9
10
11
first
14Program 4.28 Part I
class ENode friend void Equivalence() public
ENode(int d0, ENode next0) //constructor d
ata d link next private int data
ENode link
15Program 4.28 Part II (Phase 1)
void Equivalence() ifstream
inFile("equiv.in", iosin) if
(!inFile) throw "Cannot open input file."
int n inFile gtgt n ENode first new
ENoden bool out new bool n for
(int i0 iltn, i) firsti 0 for
(int i0 iltn, i) outi false
//Phase 1 int x, y inFile gtgt x gtgt y
while (inFile.good()) firstx
new ENode(y, firstx) firsty new
ENode(x, firsty) inFile gtgt x gtgt y
16Program 4.28 Part III (Phase 2)
for (int i0 iltn i) if
(!outi) cout ltlt endl ltlt
"A new class " ltlt i outi true
ENode x firsti ENode
top 0 //initialize stack while
(1) while (x)
int j x-gtdata
if (!outj)
cout ltlt ", " ltlt j
outj true ENode y
x-gtlink x-gtlink top
top x x
y else x
x-gtlink if
(!top) break x
firsttop-gtdata top top-gtlink
//pop
17Array of Pointers
- Declare a pointer
- ENode ptr new ENode(1, 0)
- Declare a pointer of array with fixed length
- ENode ptr3
- for (int i0 ilt3 i)
- ptri new ENode(i, 0)
- Declare a pointer of array with arbitrary length
- ENode ptr
- ptr new ENode 3
- for (int i0 ilt3 i)
- ptri new ENode(i, 0)
0
1
2
18Program 4.28 Part IV
for (int i0 iltn i) while
(firsti) ENode delnode
firsti firsti delnode-gtlink
delete delnode
delete first delete out
19Analysis 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(mn).
20Analysis 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(mn).
- 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(mn).
214.9 SPARSE MATRIX
22Introduction
- 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.
23Introduction
- 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
head
next
down
right
24Header 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.
25Node Structure
Header node
Element node
next
row
col
value
Link to the next nonzero term in the same row.
down
right
down
right
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.
26Example
- Consider the following 5x4 sparse matrix
- How many header nodes?
- How many element nodes?
27H
H0
H1
H2
H3
H4
H0
H1
H2
H3
H4
28Representation of MatrixNode
class MatrixNode friend class Matrix public
MatrixNode(bool hfalse, int r-1, int c-1,
int v0, MatrixNode
rp0, MatrixNode dp0,
MatrixNode nt0) private MatrixNode
down, right, next int row, col, value
bool head
29Representation of Matrix
class Matrix public Matrix(int r0, int
c0) private MatrixNode headnode
MatrixNode head
30Constructor of Matrix
MatrixMatrix(int r, int c) headnode
0 head 0 if (r lt 0 c lt 0)
return int p max(r, c) head new
MatrixNodep for (int i0 iltp i)
headi new MatrixNode(true) for
(int i0 iltp-1 i) headi-gtnext
headi1 headnode new MatrixNode(true,
r, c, 0, 0, 0, head0)
31C Program of Insertion
void MatrixInsertion(int row, int col, int
value) MatrixNode prev1 headrow
MatrixNode temp1 headrow-gtright
while (temp1 ! NULL) if (col lt
temp1-gtcol) break prev1
temp1 temp1 temp1-gtright
MatrixNode prev2 headcol
MatrixNode temp2 headcol-gtdown while
(temp2 ! NULL) if (row lt
temp2-gtrow) break prev2
temp2 temp2 temp2-gtdown
MatrixNode newNode new MatrixNode(false,
row, col, value, temp1, temp2)
prev1-gtright newNode prev2-gtdown
newNode
32H
H0
H1
H2
H3
H4
prev2
H0
H1
prev1
temp1
H2
H3
temp2
H4
Insert a node at (1, 2)
33Analysis 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.
344.10 Doubly linked list
35Introduction
- 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
36Representation
class DbListNode friend class DbList public
DbListNode(int d0, DbListNode llink0,
DbListNode rlink0) data d left
llink right rlink private int
data DbListNode left, right class
DbList private DbListNode first,
last
first
last
dummy
dummy
37Construction and Destruction
DbListDbList() first new
DbListNode() last new DbListNode()
first-gtright last first-gtleft NULL
last-gtleft first last-gtright
NULL DbListDbList() while (first !
NULL) DbListNode temp
first-gtright delete first
first temp
38Inserting a Node into an Ordered List
- Suppose the list is sorted in non-decreasing
order.
void DbListInsertion(int d) DbListNode
temp first-gtright while (temp ! last)
if (temp-gtdata gt d)
break temp temp-gtright
DbListNode newNode new DbListNode(d,
temp-gtleft, temp) temp-gtleft-gtright
newNode temp-gtleft newNode
newNode
temp-gtleft
temp
39Deleting a Node from an Ordered List
bool DbListDeletion(int d) DbListNode
temp first-gtright while (temp ! last)
if (temp-gtdata d)
temp-gtleft-gtright temp-gtright
temp-gtright-gtleft temp-gtleft
delete temp return true
temp temp-gtright
return false
temp-gtright
temp-gtleft
temp