Professional Documents
Culture Documents
Unit 1
Unit 1
data object
daysOfWeek = {S,M,T,W,Th,F,Sa}
Data Object
369
3 is more significant than 6
3 is immediately to the left of 6
9 is immediately to the right of 6
Data Structure
relationships
e0 is the zero’th (or front) element
en-1 is the last element
ei immediately precedes ei+1
Linear List Examples/Instances
Students in MyClass =
(Jack, Jill, Abe, Henry, Mary, …, Judy)
Exams in MyClass =
(exam1, exam2, exam3)
L = (a,b,c,d,e)
length = 5
Linear List Operations—
Retrieve(theIndex)
retrieve element with given index
L = (a,b,c,d,e)
Retrieve(0) = a
Retrieve(2) = c
Retrieve(4) = e
Retrieve(-1) = error
Retrieve(9) = error
Linear List Operations—
IndexOf(theElement)
L = (a,b,d,b,a)
IndexOf(d) = 2
IndexOf(a) = 0
IndexOf(z) = -1
Linear List Operations—
Delete(theIndex)
L = (a,b,c,d,e,f,g)
Delete(2) returns c
and L becomes (a,b,d,e,f,g)
L = (a,b,c,d,e,f,g)
L = (a,b,c,d,e,f,g)
L = (a,b,c,d,e,f,g)
class LinearListOfIntegers
{
bool IsEmpty() const;
int length() const;
int Retrieve(int index) const;
int IndexOf(int theElement) const;
int Delete(int index);
void Insert(int index, int theElement);
}
Data Structures In Text
a b c d
start
• location(x[i]) = start + i
Space Overhead
Memory
a b c d
start
i j k l
Space Overhead
x[]
a b c d
e f g h
i j k l
space overhead = overhead for 4 1D arrays
= 4 * 4 bytes
= 16 bytes
= (number of rows + 1) x 4 bytes
Array Representation In C++
x[]
a b c d
e f g h
i j k l
This representation is called the array-of-arrays
representation.
Requires contiguous memory of size 3, 4, 4, and 4 for the
4 1D arrays.
1 memory block of size number of rows and number of
rows blocks of size number of columns
Row-Major Mapping
• Example 3 x 4 array:
abcd
efgh
i jkl
• Convert into 1D array y by collecting elements by rows.
• Within a row elements are collected from left to right.
• Rows are collected from top to bottom.
• We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l}
row 0 row 1 row 2 … row i
Locating Element x[i][j]
0 c 2c 3c ic
row 0 row 1 row 2 … row i
4 5 6
7 8 9 l0
0 1 3 6
r r2 r3 … row i
1
• Order is: row 1, row 2, row 3, …
• Row i is preceded by rows 1, 2, …, i-1
• Size of row i is i.
• Number of elements that precede row i is
1 + 2 + 3 + … + i-1 = i(i-1)/2
• So element (i,j) is at position i(i-1)/2 + j -1 of
the 1D array.
Sparse Matrices
00304
00570 Row 2
4 x 5 matrix
00000
4 rows
02600
5 columns
Column 4
20 elements
Sparse Matrices
Sparse matrix #nonzero elements/#elements
is small.
Examples:
• Diagonal
Only elements along diagonal may be nonzero
n x n matrix ratio is n/n2 = 1/n
• Tridiagonal
• Only elements on 3 central diagonals may be nonzero
• Ratio is (3n-2)/n2 = 3/n – 2/n2
Sparse Matrices
• Lower triangular (?)
• Only elements on or below diagonal may be nonzero
• Ratio is n(n+1)(2n2) ~ 0.5
Web analysis.
authority page … page that has many links to it
hub page … links to many authority pages
Web Page Matrix
n = 2 billion (and growing by 1 million a day)
n x n array of ints => 16 * 1018 bytes (16 * 109
GB)
each page links to 10 (say) other pages on
average
on average there are 10 nonzero entries per row
space needed for nonzero elements is
approximately 20 billion x 4 bytes = 80 billion
bytes (80 GB)
Representation Of Unstructured
Sparse Matrices
Single linear list in row-major order.
scan the nonzero elements of the sparse matrix in row-
major order (i.e., scan the rows left to right
beginning with row 1 and picking up the nonzero
elements)
each nonzero element is represented by a triple
(row, column, value)
the list of triples is stored in a 1D array
Single Linear List Example
00304 list =
00570 row 1 1 2 2 4 4
00000 column 3 5 3 4 2 3
02600 value 3 4 5 7 2 6
One Linear List Per Row
0000
00304
0002
00570
3506
00000
0700
02600
4000
Matrix Transpose 0000
00304
0002
00570
3506
00000
0700
02600
4000
row 1 1 2 2 4 4 2 3 3 3 4 5
column 3 5 3 4 2 3 4 1 2 4 2 1
value 3 4 5 7 2 6 2 3 5 6 7 4
Matrix
0000
Transpose
Step 1: #nonzero in each row of
00304
0002 transpose.
00570 = #nonzero in each column o
3506
00000 original matrix
0700
02600 = [0, 1, 3, 1, 1]
4000
Step2: Start of each row of transpos
row 1 1 2 2 4 4 = sum of size of preceding
rows of
column 3 5 3 4 2 3
transpose
value 3 4 5 7 2 6
= [0, 0, 1, 4, 5]
Matrix Transpose
Step 1: #nonzero in each row of
transpose. Complexity
= #nonzero in each column of m x n original matrix
original matrix t nonzero elements
= [0, 1, 3, 1, 1] Step 1: O(n+t)
Step2: Start of each row of transpose Step 2: O(n)
= sum of size of preceding Step 3: O(t)
rows of
Overall O(n+t)
transpose
= [0, 0, 1, 4, 5]
Step 3: Move elements, left to right,
Runtime Performance
Matrix Transpose
500 x 500 matrix with 1994 nonzero elements
Run time measured on a 300MHz Pentium II PC
2D array 210 ms
SparseMatrix 6 ms
Performance
Matrix Addition.
500 x 500 matrices with 1994 and 999 nonzero
elements
2D array 880 ms
SparseMatrix 18 ms