Representation of Arrays N = n

Introduction to Data Structures
CHAPTER 2
ARRAYS
2.1 The Array as an Abstract Data Type
2.2 Structures and Unions
2.3 The Polynomial Abstract Data Type
2.4 The Sparse Matrix Abstract Data Type
2.5 Representation of Multidimensional Arrays
2.6 The String Abstract Data Type
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-1
Contents of DS
Chapter 1 Basic Concepts
Chapter 2 Arrays
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Stacks and Queues
Linked Lists
Trees
Graph
Chapter 7 Sorting
Chapter 8 Hashing
Chapter 9 Heap Structures
Chapter 10 Search Structures
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-2
2.1 The Array as an Abstract Data Type
 Array: a set of index and value <index, value>,
A[1]
A[2]
A[5]
23
28
555
-10
56
1
2
3
4
5
 Array as a Abstract Data Type (Class)
{<index, value>}+{op1:retrieve, op2:Store}
 Representation
Usually, implemented by using consecutive memory.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-3
Abstract Data Type: Array
ADT 2.1: Abstract Data Type Array
(p.50)
structure Array is
objects: A set of pairs <index, value>; index: one or more dimension
functions:
for all A  Array, i  index, x  item, j, size  integer
Array Create(j, list) ::= return an array of j dimensions where list is a
j-tuple whose ith element is the size of the ith
dimension.
Item Retrieve(A, i) ::= if (i  index) return the item associated with index
value i in array A
else return error
Array Store(A, i, x) ::= if (i in index) return an array that is identical to A
except the new pair <i, x>
else return error
end Array
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-4
Arrays in C/C++
int list[5];
// five integers list[0], list[1], list[2], list[3], list[4]
memory
list[0]
base address =  =1100
list[1]
 + sizeof(int) = 1102
list[2]
 + 2*sizeof(int) = 1104
list[3]
 + 3*sizeof(int) = 1106
list[4]
 + 4*sizeof(int) = 1108
1110
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-5
Arrays in C or C++: Print out Address and Value
Example:
ptr
int list[] = {20, 15, 12, 9, 7};
Goal: print out address and value
list
void print1(int *ptr, int rows)
{
// print out a one-dimensional array using a pointer
int i;
printf(“Address Contents\n”);
for (i=0; i < n; i++)
printf(“%8u%5d\n”, ptr+i, *(ptr+i));
printf(“\n”);
The i’th element
}
20 15 12
9
7
Address
Contents
1100
20
1102
15
1104
12
1106
9
1108
7
call print(list, 5) or print(&list[0], 5)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-6
2.3 The Polynomial Abstract Data Type
 Ordered List or Linear List:
 (item0, item2, item3, …, itemn-1)
 ( ) empty list
 Examples:
 Values in a card deck
(Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)
 Years of USA fought in World War II
(1941, 1942, 1943, 1944, 1945)
 …
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-7
Ordered List
 Ordered (Linear) List: (item0, item2, item3, …, itemn-1)
 Operations on Ordered List
(1) Find the length, n, of the list.
(2) Read the items from left to right (or right to left).
(3) Retrieve the i’th element, 0 ≤ i < n.
(4) Replace the item in the i’th position, 0 ≤ i < n.
(5) Insert a new element at the position i, 0 ≤ i ≤ n, causing elements
numbered i, i+1, …, n to become numbered i+1, i+2, …, n+1
(6) Delete the element at position i, 0 ≤ i < n, causing elements
numbered i+1, …, n-1 to become numbered i, i+1, …, n-2 (moving?)
 Array (sequential mapping)?
(1)~(4) good, (5)~(6) bad, moving is necessary
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-8
Ordered List Representations
 In the study of D.S. we are interested in:
“The ways of representing ordered lists.
So that these operators can be carried out efficiently.”
Method 1. Sequential mapping.
i.e. Represent an order list by an array.
e.g.
1 2 3
12 13
2
3
4
......
K
A
Insert ?
Delete ?
Method 2. Non-sequential mapping.
i.e., by linked list (Ch. 4)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-9
The Polynomial Abstract Data Type
 Polynomial
 Two polynomial examples are (p. 60):
• A(x) = 3x20 + 2x5 + 4
• B(x) = x4 + 10x3 + 3x2 + 1
 Assume that we have two polynomials,
A(x) = aixi and B(x) = bixi
then:
 A(x) + B(x) = (ai + bi)xi
 A(x) · B(x) = (aixi · (bjxj))
 Similarly, we can define subtraction and division on
polynomials, as well as many other operations.
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-10
The Polynomial Abstract Data Type
structure Polynomial is
p. 61
en
e1
objects: p( x )  a1 x  ...  an x ; a set of ordered pairs of <ei, ai>
where ai in Coefficients and ei in Exponents, ei are integers >= 0
functions:
for all ploy, poly1, poly2  Polynomial, coef  Coefficients, expon  Exponents
Polynomial Zero()
::= return the polynomial p(x) = 0
Boolean IsZero(poly)
::= if (poly) return FALSE else return TRUE
Coefficient Coef(poly, expon) ::= if (expon  poly) return its coefficient
else return zero
Exponent Lead_Exp(poly)
::= return the largest exponent in poly
Polynomial Attach(poly, coef, expon) ::= if (expon  poly) return error
else return the polynomial with the
term <coef, expon> inserted
Polynomial SingleMult(poly, coef, expon) ::= return the polynomial
poly * coef * xexpon
Polynomial Add(poly1, poly2) ::= return the polynomial poly1 + poly2
Polynomial Mult(poly1, poly2) ::= return the polynomial poly1 * poly2
end Polynomial
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-11
Polynomial Representation: Method 1
Method 1: Fixed size
#define MAX_DEGREE 101
typedef struct {
int degree;
float coef[MAX_DEGREE];
} polynomial;
A.degree = n
1
n
A( x )  a0  a1 x  ...  an x
A.coef[i] = an-i
coef
an an-1 an-2 an-3 an-4 an-5 . . . a0
0 1
2
3
4 5
• e.g. B(x) = x4+10x3+3x2+1
coef
0
0 1
3
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
n
MaxDegree
Waste memory
1 10 3
2
...
1
4 5
...
...
MaxDegree
= 100
Chapter 2 Arrays
2-12
Polynomial Representation: Method 2
Method 2: Dynamic with coef and exp
MAX_TERMS 100
typedef struct {
float coef;
int expon;
} polynomial;
p( x )  a1 x e1  ...  an x en
terms
coef = ai
expon = ei
polynomial terms[MAX_TERMS];
int avail = 0;
avail
coef
expon
0
1
2
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
…
99
Chapter 2 Arrays
2-13
Polynomial Representation: Method 2 (cont.)
e.g. A(x) = 2x1000 + 1
B(x) = x4 + 10x3 + 3x2 + 1
starta finisha startb
coef
2
expon 1000
0
1
0
1
1
4
2
A
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
10
3
3
3
2
4
B
finishb
avail
1
0
5
6
Free
Chapter 2 Arrays
2-14
Polynomial Representation: Method 2 (cont.)
• Function to add a new term
void attach (float c, int e)
p. 65
{
// add a new term to the polynomial
if (avail >= MAX_TERMS) {
fprintf(stderr, “Too many terms in the polynomial\n ”);
exit(1);
}
terms[avail].coef = c;
terms[avail++].expon = e;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-15
Polynomial Addition
p. 64
void padd(int starta, int finisha, int startb, int finisha, int *startd, int *finishd)
{ /* add A(x) and B(x) to obtain D(x) */
float coeff; *startd = avail;
while (starta <= finisha && startb <= finishb)
switch (COMPARE(terms[starta].expon, terms[startb].expon)) {
case -1: /* a expon < b expon */
attach(terms[startb].coef, terms[startb].expon);
startb++; break;
case 0: /* equal exponents */
coeff = terms[starta].coef + terms[startb].coef;
if (coeff) attach(coeff, terms[starta].expon);
starta++; startb++; break;
case 1: /* a expon > b expon */
attach(terms[starta].coef, terms[starta].expon);
starta++;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-16
Polynomial Addition (cont.)
/* add in remaining terms of A(x) */
for (; starta <= finisha; starta++)
attach(terms[starta].coef, terms[starta].expon);
/* add in remaining terms of B(x) */
for (; startb <= finishb; startb++)
attach(terms[startb].coef, terms[startb].expon);
*finishd = avail – 1;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-17
Polynomial Addition: Example
A(x) = 2x4 + 1
B(x) = x4 + 10x3 + 3x2 + 1
D(x)= A(x) + B(x)
starta finisha
coef
exp
2
4
0
1
0
1
a
avail
1
4
2
10
3
3
startb b
3
2
4
1
0
5
2
4
0
1
0
1
a
7
8
9
10 11
8
9
10 11
finishb
starta finisha
coef
exp
6
startd avail
1
4
2
10
3
3
startb
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
3
2
4
b
1
0
5
3
4
6
7
finishb
Chapter 2 Arrays
2-18
Polynomial Addition (cont.)
starta finisha
coef
exp
2
4
0
1
0
1
a
1
4
2
startd
10
3
3
3
2
4
b
startb
1
0
5
2
4
0
a
1
0
1
1
4
2
startb
10
3
7
3
2
8
9
startd
10
3
3
3
2
4
b
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
10 11
finishb
starta finisha
coef
exp
3
4
6
avail
1
0
5
3
4
6
avail
10
3
7
3
2
8
2
0
9
10 11
finishb
Chapter 2 Arrays
2-19
Polynomial Addition: Complexity Analysis

Program 2.5: Adding two polynomials
p.64


Analysis
Discussion
Let n and m be the number of nonzero in
whil
e
loop
m+n loop
for
loop
m次
for
loop
the polynomial A and B.
O(m+n)
…when A’s exp and B’s exp are all different,
while loop n+m-1
worst case.
n次
1. Array is a useful representational form for ordered list.
2. bad D.S. e.g.
m
n
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
unsorted  O(m*n)
Chapter 2 Arrays
2-20
Polynomial Representation: Comparison
 Representation 1:
 Advantage: easy implementation
 Disadvantage: waste space when sparse
 Representation 2:
 Storage requirements: start, finish, 2*(finish-start+1)
 Non-sparse: twice as much as Representation 1,
when all the items are nonzero
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-21
2.4 Sparse Matrices
 What is a Sparse Matrix?
col0 col1 col2 col3 col4 col5
col0 col1 col2
row0
15
9
12
row0
15
0
0
22
0
-15
row1
4
11
3
row1
0
11
3
0
0
0
row2
7
6
3
row2
0
0
0
-6
0
0
row3
-1
10
1
row3
0
0
0
0
0
0
row4
91
21
5
row4
91
0
0
0
0
0
row5
0
0
28
0
0
0
15/15
8/36  sparse matrix
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-22
ADT Sparse Matrices
structure Sparse_Matrix is
objects: a set of triples, <row, column, value>
functions:
for all a, b  Sparse_Matrix, x  item, i, j, max_col, max_row  index
Sparse_Matrix Create(max_row, max_col) ::= return a sparse matrix with up to
max_items = max_row * max_col
Sparse_Matrix Transpose(a) ::= return the matrix by interchanging row and
column value: a[i][j] = a[j][i]
Sparse_Matrix Add(a, b) ::= if (dimension of a = dimension of b)
return the matrix d produced by adding
corresponding items in a and b: d[i][j] = a[i][j] +
b[i][j]
else return error
Sparse_Matrix Multiply(a, b) ::= if (num_col a = num_row b)
return the matrix d produced by multiplying
a by b according to formula: d[i][j] = Σ(a[i][k] *
b[k][j])
else return error
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-23
Sparse Matrices: Concepts

Matrix

Operations: Transpose, Product, Add,
Interested in studying
• representation
• op
1. Transpose
2. Product
3. Add
:
“ways to represent matrices so that the operations can
be carried out efficiently”.

╳
Representation
0
Method 1 two-dim array A(0:m-1, 0:n-1) m ×n  ..
problem: sparse matrix. e.g., Fig. 2.3(b), p.69  0
0 .. .. .. 0 

.. 1 0 .. .. 
.. .. 0 .. 0 

 0 0 .. 1 .. .. 
 2 0 .. .. 0 2 


 0 1 0 .. .. 0 


Method 2 3-tuples form
(i, j, value)
ˇ
e.g., p.69  (0,0,15), (0,3,22), (0,5,-15),…
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-24
Sparse Matrices Representation: Example
0
0
1
2
3
4
5
1
2
3
4
5
15 0 0 22 0  15
 0 11 3
0 0
0


0
 0 0 0 6 0


0
0
0
0
0
0


91 0 0
0 0
0


0 0
0
 0 0 28
a
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
row column value
a[ 0]
6
6
8
[1]
0
0
15
[2]
0
3
22
[3]
0
5
 15
[4]
1
1
11
[5]
1
2
3
[ 6]
2
3
6
[7 ]
4
0
91
[8]
5
2
28
Chapter 2 Arrays
2-25
Sparse Matrices Transpose: Example

Example of Matrix Transpose:
col0 col1 col2
col0 col1 col2 col3 col4
row0
15
9
12
row0
15
4
7
-1
91
row1
4
11
3
row1
9
11
6
10
21
row2
7
6
3
row2
12
3
3
1
5
row3
-1
10
1
row4
91
21
5
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-26
Sparse Matrices Transpose: Methods



Problem: Transpose a sparse matrix
Method 1  1° (i,j,v)  (j,i,v)
2° Sorting
Method 2  p.71. Program 2.7, transpose()
 Concept
for all elements in column j
place element <i, j, value> into <j, i, value>


Complexity: O(nt) t: # of nonzero
Discussion: if t = nm  time of Transpose = O(n2m)
Method 3  p.72. Program 2.8, fast_transpose()
 Concept
knowing # of elements in each column
 can determine starting position of each row in transpose matrix

Complexity: O(n + t)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-27
Sparse Matrices Transpose: Method 2
void transpose(term a[], term b[]) /* b is set to the transpose of a */
{
int n, i, j, currentb;
n = a[0].value;
b[0].row = a[0].col;
/* rows in b = columns in a */
b[0].col = a[0].row;
/* columns in b = rows in a */
b[0].value = n;
if (n > 0) { /* nonzero matrix */
currentb = 1;
for (i = 0; i < a[0].col; i++) /* transpose by columns */
for (j = 1; i <= n; j++)
/* find elements from the current column */
if (a[i].col == i) {
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++;
}
}
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
p. 71
Chapter 2 Arrays
2-28
Sparse Matrices Transpose: Method 3
• Example
starting_pos [0] [1] [2] [3] [4] [5]
1 3 4 6 8 8
row column value
a[ 0]
6
6
8
row column value
a[ 0]
6
6
8
[1]
0
0
15
[1]
0
0
15
[2]
0
3
22
[2]
0
4
91
[3]
0
5
 15
[3]
1
1
11
[4]
1
1
11
[4]
2
1
3
[5]
1
2
3
[5]
2
5
28
[ 6]
2
3
6
[ 6]
3
0
22
[7 ]
4
0
91
[7 ]
3
2
6
[8]
5
2
28
[8]
5
0
 15
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-29
Sparse Matrices Transpose: Method 3 (cont.)
void fast_transpose(term a[], term b[])
p. 72
{
int row_terms[MAX_COL], starting_pos[MAX_COL];
int I, j, num_cols = a[0].col, num_terms = a[0].value;
b[0].row = num_cols; b[0].col = a[0].row; b[0].value = num_terms;
if (num_terms > 0) { /* nonzero matrix */
for (i = 0; i < num_cols; i++) row_terms[i] = 0;
/* Initialize */
for (i = 0; i < num_terms; i++) row_terms[a[i].col]]++;
starting_pos[0] = 1;
for (i = 0; i < num_cols; i++)
starting_pos[i] = starting_pos[i-1] + row_terms[i-1];
for (i = 1; i < num_terms; i++) { // move from a to b
j = starting_pos[a[i].col]++;
b[j]. row = a[i].col; b[j].col = a[i].row; b.[j].value = a[i].value;
} // end of for
}
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-30
2.5 Representation of Arrays

N=2
declare A(2:6, 3:5)
col 3
row
row
row
row
row
col 4
col 5
2
3
4
5
6
# of elements = (6-2+1)*(5-3+1) = 5*3 = 15
row major:
row 2
row 3
row 6
....
5-3+1
Lexicographic order: A(2,3), A(2,4), A(2,5), A(3,3), A(3,4), …
Column major: A(2,3), A(3,3), …, A(6,3), A(2,4), … …., A(6,5)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-31
Representation of Arrays

N=2
declare A(p0:q0, p1:q1)
col p1
col p1+1 ….
row p0
row p0+1
:
declare A(2:4, 3:5)
col q1
:
# of elements = (q0-p0+1)*(q1-p1+1)
row q0
row major:
row p0
row p0+1
row q0
....
q0-p0+1
 N=n
# of elements = (q0-p0+1) * (q1-p1+1) * … * (qn-1-pn-1+1) =
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Π (qi-pi+1)
Chapter 2 Arrays
2-32
Representation of Arrays: N = 1


Problem: Representing N dimensional arrays in a one dimension
memory:
e.g. If A(4,2,1,3) is stored at location 100 then
A(4,2,1,4) is stored at location 101.
Formula ? If A(0,0,0,0) =   A(i,j,k,l) = ?
call function(A,…)

Call function(A…)
x:=A(5,4,3,8)
A(0,0,0,0)
N=1
declare A(0:u0-1)
0 1

A(i) at +i
……
i
……
# of elements = u0
+i
內部計算  constant time 解決 i.e. O(1)
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-33
Representation of Arrays: N = 2

N=2
declare A(0:u0-1, 0:u1 -1)
col 0
col 1
row 0
row 1
:
col u1-1
# of elements = u0‧ u1
:
row u0-1
row major:
row 0
row 1
....

u1
column major:
A(1,j)
row u0-1
∵ address of A(2,j) =  + 2 * u1 + j
∴ address of A(i, j) =  + i * u2 + j
A(i,j) = ? Ex 2.2
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-34
Representation of Arrays: N = 3

N=3
declare A(0:u0-1, 0:u1 –1, 0:u2 -1)
u1
u0
u2
i u1 u2 elements
u1 × u2

A(0, u1, u2)
u1 × u2
A(1, u1, u2)
...
________
________
________
____.
A(i, u1, u2)
A(i,j,k)
u2
...
u1
A(u0-1, u1, u2)
Location of A(i,j,k) =  + i u1 u2 + j u2 + k
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-35
Representation of Arrays: N = n

In general,
declare A(0:u0-1, 0:u1 –1, …, 0:un-1 -1)
Location of A(i0, i1, ..., in-1)
=  + i0 u1 u2 ... un-1
+ i1 u2 ... un-1
:
+ in-2 un-1
+ in-1
n -1
 α   i j a j where
j0
n -1
a j   uk
0  j  n -1
k  j1
a n-1  1
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-36
2.6 The String Abstract Data Type
 The string: component elements are characters.
 A string to have the form, S = s0, …, sn-1, where si are characters
taken from the character set of the programming language.
 If n = 0, then S is an empty or null string.
 Operations in ADT 2.4, p. 81
 String Pattern Matching Problem
 Simple Algorithm
 The KMP Algorithm
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-37
String Pattern matching
Pattern matching (Program 2.12)
void nfind(char *string, char *pat)
{ // match the last character of pattern first, and then match from the beginning
int i, j, start = 0;
int lasts = strlen(string) – 1;
int lastp = strlen(pat) – 1;
int endmatch = lastp;
for (i=0; endmatch <= lasts; endmatch++, start++)
{ /* only check end index first */
if (string[endmatch] == pat[lastp])
for (j=0, i=start; j < lastp && string[i] == pat[j]; i++, j++);
if (j == lastp)
return start;
/* successful */
}
return –1;
}
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-38
String Pattern matching: Example 1
Example [Exhaustive patter matching]:
pat = “aab”
string = “abaabbaabaa”
aab
abaabbaabaa
aab
abaabbaabaa
aab
abaabbaabaa
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-39
String Pattern matching: Example 2
Exhaustive pattern matching vs.
Intelligent pattern matching
aab
aab
abaabbaabaa
abaabbaabaa
aab
abaabbaabaa
aab
abaabbaabaa
aab
abaabbaabaa
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-40
String Pattern matching: Comparison
 Analysis of String Pattern Matching Algorithms
 Simple Algorithm O(m* n), p.86
 The KMP Algorithm O(m+ n), p.89
(Knuth-Morris-Pratt, p. 92)
“Fast pattern matching in string,” SIAM Journal on Computing,
6:2 1977, pp. 323-350
Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin
Chapter 2 Arrays
2-41