3 Array
3 Array
data structure
For each index, there is a value associated with
that index.
representation
implemented by using consecutive memory.
element
Structure Array is
objects: A set of pairs <index, value> where for each value of index
there is a value from the set item. Index is a finite ordered set of
one or more dimensions, for example, {0, … , n-1} for one dimension,
{(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2)} for two dimensions,
etc.
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
Create(3,{2,2,1}) : A j-tuple whose ith element is the size of the
ith dimension. Items are undefined.
3D, 2x2x1 array
Item Retrieve(A, i) ::= if (i index) return the item associated with
index value i in array A
else return error
Notations:
list2: a pointer to list2[0]
(list2 + i): a pointer to list2[i] ( &list2[i] )
*(list2 + i): ( list2[i] )
Example: 1-dimension array addressing
int one[] = {0, 1, 2, 3, 4};
Goal: print out address and value
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 < rows; i++)
printf(“%4u%5d\n”, ptr+i, *(ptr+i));
printf(“\n”);
}
call print1(&one[0], 5)
Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4
*Figure 2.1: One- dimensional array addressing
Structures (records)
Anonymous structure Named structure
or
typedef struct {
char name[10];
int age;
float salary
} human_being;
item3.data=‘c’;
item1.link=item2.link=item3.link=NULL;
Polynomials
2.4 page64 - 71
Ordered List Examples
ordered (linear) list: (item1, item2, item3, …, item n)
(MONDAY, TUEDSAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAYY, SUNDAY)
(2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King,
Ace)
(1941, 1942, 1943, 1944, 1945)
(a1, a2, a3, …, an-1, an)
Operations on Ordered List
Find the length, n , of the list.
Read the items from left to right (or right to left).
Retrieve the ith element.
Store a new value into the ith position.
Insert a new element at the position i , causing elements
numbered i, i+1, …, n to become numbered
i+1, i+2, …, n+1
Delete the element at position i , causing elements
numbered i+1, …, n to become numbered i, i+1, …, n-1
array (sequential mapping)?
Polynomials A(X)=3X20+2X5+4, B(X)=X4+10X3+3X2+1
Structure Polynomial is
functions:
for all poly, poly1, poly2 Polynomial, coef Coefficients, expon
Exponents
End Polynomial
*ADT2.2:Abstract data type Polynomial
Polynomial Addition
A(X)=3X20+2X5+4
B(X)=X4+10X3+3X2+1
C(X) = A(X) + B(X),C(X) = ?
Polynomial Addition
/* d =a + b, where a, b, and d are polynomials */ Example:
d = Zero( ) A(X)=3X20+2X5+4
while (! IsZero(a) && ! IsZero(b)) do { B(X)=X4+10X3+3X2+1
switch COMPARE (Lead_Exp(a), Lead_Exp(b)) {
case -1: d =
Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b)); data structure 1:
b = Remove(b, Lead_Exp(b));
break;
case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b,
Lead_Exp(b));
if (sum) {
Attach (d, sum, Lead_Exp(a));
a = Remove(a , Lead_Exp(a));
b = Remove(b , Lead_Exp(b));
}
break; #define MAX_DEGREE 101 (100 + 1)
typedef struct {
int degree;
float coef[MAX_DEGREE];
} polynomial;
case 1: d =
Attach(d, Coef (a, Lead_Exp(a)), Lead_Exp(a));
a = Remove(a, Lead_Exp(a));
}
}
coef 2 1 1 10 3 1
exp 1000 0 4 3 2 0
0 1 2 3 4 5 6
specification representation A(X)=2X1000+1
poly <start, finish> B(X)=X4+10X3+3X2+1
A <0,1>
B <2,5>*Figure 2.3: Array representation of two polynomials
storage requirements: startA, finishA, 2*(finishA-startA+1)
nonparse: twice as much as (1)
when all the items are nonzero
Analysis: O(n+m)
where n (m) is the number of nonzeros in A(B).
*Program 2.6: Function to add two polynomial
void attach(float coefficient, int exponent)
{
/* 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 = coefficient;
terms[avail++].expon = exponent;
}
*Program 2.7:Function to add anew term
4 x 5 matrix
00304 4 rows
00570 Row 2
5 columns
00000 20 elements
02600 6 nonzero
Column 4 elements
Sparse Matrices
Sparse Matrices
Sparse Matrices
An n x n matrix may be stored as an n x n
array.
This takes O(n²) space.
The example structured sparse matrices may
be mapped into a 1D array so that a mapping
function can be used to locate an element
quickly; the space required by the 1D array is
less than that required by an n x n array.
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 pointers => 4 million bytes
total number of nonempty flight lists = 20,000 (say)
need at most 20,000 list pointers => 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
Sparse Matrix
col0 col1 col2 col3 col4 col5
row0 15 0 0 22 0 − 15
row1
0 11 3 0 0 0
row2 0 0 0 −6 0 0
row3 0 0 0 0 0 0
row4 91 0 0 0 0 0
row5 0 0 28 0 0 0
5*3 6*6
(a) 15/15 (b) 8/36
# of rows (columns)
# of nonzero terms
a[0] 6 6 8 b[0] 6 6 8
[1] 0 0 15 [1] 0 0 15
[2] 0 3 22 [2] 0 4 91
[3] 0 5 -15 transpose [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
(a) (b)
void transpose (term a[], term b[])
/* b is set to the transpose of a */
{
int n, i, j, currentb;
n = a[0].value; /* total number of elements */
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) { /*non zero matrix */
currentb = 1;
for (i = 0; i < a[0].col; i++)
/* transpose by columns in a */
for( j = 1; j <= n; j++)
/* find elements from the current column */
if (a[j].col == i) {
/* element is in current column, add it to b */
columns
elements
b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
currentb++
}
}
}
Solution: fastTranspose
Determine the number of elements in each column of
the original matrix.
==>
Determine the starting positions of each row in the
transpose matrix.
a[0] 6 6 8 After transpose
a[1] 0 0 15
a[2] 0 3 22
a[3] 0 5 -15
a[4] 1 1 11
a[5] 1 2 3
a[6] 2 3 -6
a[7] 4 0 91
a[8] 5 2 28
1 0 0 1 1 1 1 1 1
1 0 0 0 0 0 = 1 1 1
1 0 0 0 0 0 1 1 1
Col 0 Col 1 Col 2
Sparse Matrix Multiplication
Alternative 1. Scan all of B to find all elements in j.
Alternative 2. Compute the transpose of B.
(Put all column elements consecutively)
D=A*B
row
column 0 0 9 0 15 0 -1 0 5 3
15 0 -1 5 -1 0 4
0 3 5 3 0 1 5
b[0] 3 4 7 b[0] 4 3 7
a[0] 2 3 4 b[1] 0 1 5
b[1] 0 2 9
a[1] 0 0 15 b[2] 0 2 3
b[2] 1 0 5
a[2] 0 2 -1 b[3] 1 1 -1
b[3] 1 1 -1
a[3] 1 1 3 transpose b[4] 2 0 9
b[4] 1 3 4
a[4] 1 2 5 b[5] 2 2 1
b[5] 2 0 3
b[6] 2 2 1 b[6] 3 1 4
b[7] 2 3 5 b[7] 3 2 5
void mmult (term a[ ], term b[ ], term d[ ] )
/* multiply two sparse matrices */
{
int i, j, column, totalb = b[0].value, totald = 0;
int rows_a = a[0].row, cols_a = a[0].col,
totala = a[0].value; int cols_b = b[0].col,
int row_begin = 1, row = a[1].row, sum =0;
term new_b[MAX_TERMS];
if (cols_a != b[0].row){
fprintf (stderr, “Incompatible matrices\n”);
exit (1);
}
fast_transpose(b, new_b); cols_b + totalb
/* set boundary condition */
a[totala+1].row = rows_a;
new_b[totalb+1].row = cols_b;
new_b[totalb+1].col = 0;
Complexity: O(m*n)
Two Phases of KMP
Knuth, Morris, Pratt pattern matching algorithm
Failure array
String Matching The Knuth-
Morris-Pratt Algorithm
or 0
0
The prefix function, Π
Following pseudocode computes the prefix fucnction, Π:
Compute-Prefix-Function (p)
1 m length[p] //’p’ pattern to be matched
2 Π[1] 0
3 k0
4 for q 2 to m
5 do while k > 0 and p[k+1] != p[q]
6 do k Π[k]
7 If p[k+1] = p[q]
8 then k k +1
9 Π[q] k
10 return Π
Example: compute Π for the pattern ‘p’ below:
p a b a b a c a
Initially: m = length(p) = 7
Π[1] = 0 (or -1)
k=0
q 1 2 3 4 5 6 7
Step 1: q = 2, k=0 p a b a b a c a
Π[2] = 0
Π 0 0
q 1 2 3 4 5 6 7
Step 2: q = 3, k = 0, p a b a b a c a
Π[3] = 1
Π 0 0 1
q 1 2 3 4 5 6 7
Step 3: q = 4, k = 1 p a b a b a c a
Π[4] = 2 Π 0 0 1 2
Step 4: q = 5, k =2 q 1 2 3 4 5 6 7
Π[5] = 3 p a b a b a c a
Π 0 0 1 2 3
Step 5: q = 6, k = 0 q 1 2 3 4 5 6 7
Π[6] = 0 p a b a b a c a
Π 0 0 1 2 3 0
q 1 2 3 4 5 6 7
Step 6: q = 7, k = 0
p a b a b a c a
Π[7] = 1
Π 0 0 1 2 3 0 1
S b a c b a b a b a b a c a c a
p a b a b a c a
q 1 2 3 4 5 6 7
p a b a b a c a
Π 0 0 1 2 3 0 1
Failure function
Initially: n = size of S = 15;
m = size of p = 7 q 1 2 3 4 5 6 7
p a b a b a c a
Step 1: i = 1 Π 0 0 1 2 3 0 1
comparing p[1] with S[1]
S b a c b a b a b a b a c a a b
p a b a b a c a
P[1] does not match with S[1]. ‘p’ will be shifted one position to the right.
Step 2: i = 2
comparing p[1] with S[2]
S b a c b a b a b a b a c a a b
p a b a b a c a
P[1] matches S[2]. Since there is a match, p is not shifted.
Step 3: i = 3
Comparing p[2] with S[3] p[2] does not match with S[3]
S b a c b a b a b a b a c a a b
p a b a b a c a
Backtracking on p, comparing p[1] and S[3] (Π[1] + 1 = 0 + 1 = 1)
Step 4: i = 3
comparing p[1] with S[3] p[1] does not match with S[3]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 5: i = 4
comparing p[1] with S[4] p[1] does not match with S[4]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 6: i = 5
comparing p[1] with S[5] p[1] matches with S[5]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 7: i = 6
Comparing p[2] with S[6] p[2] matches with S[6]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 8: i = 7
Comparing p[3] with S[7] p[3] matches with S[7]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 9: i = 8
Comparing p[4] with S[8] p[4] matches with S[8]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 10: i = 9
Comparing p[5] with S[9] p[5] matches with S[9]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 11: i = 10
Comparing p[6] with S[10] p[6] doesn’t match with S[10]
S b a c b a b a b a b a c a a b
p a b a b a c a
Backtracking on p, comparing p[4] with S[10] because after mismatch q = Π[5] = 3
(Π[5] + 1 = 3 + 1 = 4
Step 12: i = 11
Comparing p[4] with S[10] p[4] matches with S[10]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 13: i = 12
Comparing p[5] with S[11] p[5] matches with S[11]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 14: i = 13
Comparing p[6] with S[12] p[6] matches with S[12]
S b a c b a b a b a b a c a a b
p a b a b a c a
Step 15: i = 13
Comparing p[7] with S[13] p[7] matches with S[13]
S b a c b a b a b a b a c a a b
p a b a b a c a