0% found this document useful (0 votes)
44 views

Module 2 Part1 ArrayMatrics

1. Arrays can represent one-dimensional and two-dimensional data structures in memory through contiguous allocation. 2D arrays map rows and columns to indexes but have limitations for matrix operations. 2. Sparse matrices store only non-zero elements to save space, with structured patterns like diagonal and unstructured patterns. Unstructured sparse matrices use linear lists of row/column/value triples. 3. Common sparse matrix representations include single/multiple linear lists of elements and orthogonal row/column lists for fast access along both dimensions.

Uploaded by

MohitMangglam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Module 2 Part1 ArrayMatrics

1. Arrays can represent one-dimensional and two-dimensional data structures in memory through contiguous allocation. 2D arrays map rows and columns to indexes but have limitations for matrix operations. 2. Sparse matrices store only non-zero elements to save space, with structured patterns like diagonal and unstructured patterns. Unstructured sparse matrices use linear lists of row/column/value triples. 3. Common sparse matrix representations include single/multiple linear lists of elements and orthogonal row/column lists for fast access along both dimensions.

Uploaded by

MohitMangglam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Module - 2

Arrays And Matrics


1D Array Representation In C++
Memory

a b c d

start

• 1-dimensional array x = [a, b, c, d]


• map into contiguous memory locations

• location(x[i]) = start + i
Space Overhead
Memory

a b c d

start

space overhead = 4 bytes for start

(excludes space needed for the elements of x)


2D Arrays

The elements of a 2-dimensional array a


declared as:
int [][]a = new int[3][4];
may be shown as a table
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
Rows Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3] row 0


a[1][0] a[1][1] a[1][2] a[1][3] row 1
a[2][0] a[2][1] a[2][2] a[2][3] row 2
Columns Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

column 0 column 1 column 2 column 3


2D Array Representation In C++
2-dimensional array x
a, b, c, d
e, f, g, h
i, j, k, l
view 2D array as a 1D array of rows
x = [row0, row1, row 2]
row 0 = [a,b, c, d]
row 1 = [e, f, g, h]
row 2 = [i, j, k, l]
and store as 4 1D arrays
2D Array Representation In C++
x[]

a b c d

e f g h

i j k l
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

• assume x has r rows and c columns


• each row has c elements
• i rows to the left of row i
• so ic elements to the left of x[i][0]
• so x[i][j] is mapped to position
ic + j of the 1D array
Column-Major Mapping
abcd
efgh
i jkl
• Convert into 1D array y by collecting elements
by columns.
• Within a column elements are collected from
top to bottom.
• Columns are collected from left to right.
• We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
Matrix
Table of values. Has rows and columns, but
numbering begins at 1 rather than 0.
a b c d row 1
e f g h row 2
i jkl row 3
• Use notation x(i,j) rather than x[i][j].
• May use a 2D array to represent a matrix.
Shortcomings Of Using A 2D
Array For A Matrix

• Indexes are off by 1.


• C++ arrays do not support matrix operations
such as add, transpose, multiply, and so on.
– Suppose that x and y are 2D arrays. Can’t do x + y, x
–y, x * y, etc. in Java.
• Develop a class Matrix for object-oriented
support of all matrix operations.
Diagonal Matrix

An n x n matrix in which all nonzero


terms are on the diagonal.
Diagonal Matrix
1000
0200
0030
0004

• x(i,j) is on diagonal iff i = j


• number of diagonal elements in an
n x n matrix is n
• non diagonal elements are zero
• store diagonal only vs n2 whole
Lower Triangular Matrix
An n x n matrix in which all nonzero terms are either
on or below the diagonal.
100 0
230 0
456 0
7 8 9 10
• x(i,j) is part of lower triangle iff i >= j.
• number of elements in lower triangle is 1 + 2 +
… + n = n(n+1)/2.
• store only the lower triangle
Array Of Arrays Representation
x[]

2 3

4 5 6

7 8 9 l0

Use an irregular 2-D array … length of rows is not


required to be the same.
Creating And Using An Irregular Array
// declare a two-dimensional array variable
// and allocate the desired number of rows
int ** irregularArray = new int* [numberOfRows];

// now allocate space for the elements in each row


for (int i = 0; i < numberOfRows; i++)
irregularArray[i] = new int [length[i]];

// use the array like any regular array


irregularArray[2][3] = 5;
irregularArray[4][6] = irregularArray[2][3] + 2;
irregularArray[1][1] += 3;
Map Lower Triangular Array Into A 1D Array

Use row-major order, but omit terms that are


not part of the lower triangle.

For the matrix


100 0
230 0
456 0
7 8 9 10
we get
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Index Of Element [i][j]

0 1 3 6
r 1 r2 r3 … row i

• 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

sparse … many elements are zero


dense … few elements are zero
Example Of Sparse Matrices
diagonal
tridiagonal
lower triangular (?)

These are structured sparse matrices.


May be mapped into a 1D array so that a
mapping function can be used to locate an
element.
Unstructured Sparse Matrices
Airline flight matrix.
 airports are numbered 1 through n
 flight(i,j) = list of nonstop flights from airport i
to airport j
 n = 1000 (say)
 n x n array of list references => 4 million bytes
 total number of flights = 20,000 (say)
 need at most 20,000 list references => at most
80,000 bytes
Unstructured Sparse Matrices
Web page matrix.
web pages are numbered 1 through n
web(i,j) = number of links from page i to page j

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
each nonzero element is represented by a triple
(row, column, value)
the list of triples may be an array list or a linked list
(chain)
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
Array Linear List Representation

row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6

element 0 1 2 3 4 5
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
Chain Representation

Node structure.

row col
value next
Single Chain

row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6

1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 null

firstNode
One Linear List Per Row

00304 row1 = [(3, 3), (5,4)]


00570 row2 = [(3,5), (4,7)]
00000 row3 = []
02600 row4 = [(2,2), (3,6)]
Array Of Row Chains

Node structure.

next
col value
Array Of Row Chains

null
3 3 5 4

00304 null
00570 3 5 4 7

00000
null
02600
null
2 2 3 6

row[]
Orthogonal List Representation

Both row and column lists.

Node structure.
row col value
down next
Row Lists

1 3 3 1 5 4
n
00304
00570 2 3 5 2 4 7
n
00000
02600 null

4 2 2 4 3 6
n
Column Lists

1 3 3 1 5 4
n
00304
00570 2 3 5 2 4 7

00000
02600

4 2 2 4 3 6
n n
Orthogonal Lists

1 3 3 1 5 4
n n
00304
00570 2 3 5 2 4 7
n
00000
02600 null

4 2 2 4 3 6
n n n

row[]

You might also like