0% found this document useful (0 votes)
50 views58 pages

L03 Arrays

Arrays basic techniques and operations.

Uploaded by

prashant
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)
50 views58 pages

L03 Arrays

Arrays basic techniques and operations.

Uploaded by

prashant
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/ 58

CS 2351 Data Structures

Arrays

Prof. Chung-Ta King


Department of Computer Science
National Tsing Hua University

National Tsing Hua


National University
Tsing ® copyright OIA
Hua University
Arrays
 You think that you know arrays
– You know how to define an array in C
int a[100];
int *a = malloc(100 * sizeof(int));
– You know the indices are from 0 to 99
– Given an index, i, you know how to read a value from or
write a value into the corresponding entry a[i]
a[i] = ...; ... = a[i];
*(a + i) = ...; ... = *(a + i);
 But, think again …

National Tsing Hua University ® copyright OIA 2


National Tsing Hua University
Arrays
 For an array
– Why the indices must start from 0?
– Why the indices must be consecutive?
– Why the array has to store the same type of data?
 In a very general sense, an array is a set of pairs
<index, value>
– e.g. student id: {(James, #1), (Claire, #2), …, (Tony, #n)}
 Though general arrays look “general”, they can often
be implemented efficiently using C-type arrays!
– We shall study in this chapter how C-type arrays may be
extended to support more general arrays and operations

National Tsing Hua University ® copyright OIA 3


National Tsing Hua University
C-Type Arrays vs. General Arrays
 A conceptual C-type array:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]
D E A E H
 A conceptual general array:
A possible implementation
using C-type arrays
6 A 2 E [0] [1] [2] [3] [4] [5] [6]
11 E Index 6 2 13 11 1
13 H
1 D Value A E H E D

National Tsing Hua


National University
Tsing ® copyright OIA
Hua University
ADT of General Arrays of Floats
ADT GeneralArray is
objects: A set of <index, value>. Each index in IndexSet has a value
of float. IndexSet is a finite ordered set of one or more dimensions,
e.g. {(0, 0), (0, 1), (1, 0), (1, 1), (1, 2), (2, 1), (2, 2)} for 2-D.
functions:
int SizeOf(); // Return the number of entries in the array
float Retrieve(index i);
/* if (i is in IndexSet) return the float associated with i; else signal
an error */
void Store(index i, float x);
/* if (i is in IndexSet) replace the old pair with <i, x>; else signal an
error */
end GeneralArray

National Tsing Hua University ® copyright OIA 5


National Tsing Hua University
Notes on the ADT of General Arrays
 We only define the interface, not implementation
– We have not specified how the set of <index, value> pairs
are organized and structured  often depends on appl.
– C-type arrays are a special case of general arrays
– C-type arrays can be used to implement general arrays
 General arrays are more flexible, may use memory
more efficiently (depending on implementation), and
allow index set checked for validity
 We will see different applications of general arrays in
this chapter, which can be implemented efficiently
using C-type arrays

National Tsing Hua University ® copyright OIA 6


National Tsing Hua University
How to implement an ADT in C++?

National Tsing Hua University ® copyright OIA 7


National Tsing Hua University
Outline
 C++ class
 From general arrays to ordered list
 Polynomial as an example
– Space optimization in data structure
 Sparse matrices as another example
– Time optimization in associated operations

National Tsing Hua University ® copyright OIA 8


National Tsing Hua University
An Example C++ Class
#ifndef RECTANGLE_H
#define RECTANGLE_H
// In the header file Rectangle.h
class Rectangle {
public:
Rectangle(); // constructor
~Rectangle(); // destructor
int GetHeight();
int GetWidth();
private:
int xLow, yLow, height, width;
};
#endif

National Tsing Hua University ® copyright OIA 9


National Tsing Hua University
C++ Class Definition
 A C++ class consists of 4 components
– Class name: Rectangle
– Data members: xLow, yLow, height, width
– Member functions: GetHeight(), GetWidth()
– Levels of program access: public, protected, private
 Program access of data members/member functions
– Public: can be accessed from any where in the program
– Private: can be accessed from within its class or by a
friend class or function
– Protected: can be accessed from within its class or from its
subclasses or by a friend

National Tsing Hua University ® copyright OIA 10


National Tsing Hua University
C++ Constructors and Destructors
 Constructor: a member function to initialize data
members of an object, e.g. Rectangle()
– Has same name as class, must be public, no return value
– If defined, automatically executed when object is created
– Can define default initial values
 Destructor: a member function to delete data
members immediately before object is deleted or
goes out of scope , e.g. ~Rectangle()

National Tsing Hua University ® copyright OIA 11


National Tsing Hua University
Data Abstraction & Encapsulation in C++
 Data encapsulation is enforced in C++ by declaring all
data members of a class to be private or protected
– External access to data members only by member
functions
 Data abstraction of classes:
– Specification: must be in public portion, consist of names
of public member functions, type of their arguments and
return values (function prototype)
 usually placed in a header file
– Implementation: usually placed in a source file of the same
name

National Tsing Hua University ® copyright OIA 12


National Tsing Hua University
Implementation of a C++ Class

// In the source file Rectangle.cpp


#include “Rectangle.h”
Rectangle::Rectangle(int x=0, int y=0,
int h=0, int w=0):
xLow(x), yLow(y), height(h), width(w)
{}
int Rectangle::GetHeight() { Default values
return height;
}
int Rectangle::GetWidth() {
return width;
}

National Tsing Hua University ® copyright OIA 13


National Tsing Hua University
Declaring and Invoking a C++ Class

#include <iostream>
#include “Rectangle.h”
main() {
Rectangle r,s; //object of class Rectangle
Rectangle *t = &s; // object pointer
...
if (r.GetHeight()*r.GetWidth() >
t->GetHeight()*t->GetWidth()) cout<<“r”;
else cout<<“s”;
cout<<“ has the greater area” << endl;
};

National Tsing Hua University ® copyright OIA 14


National Tsing Hua University
Operator Overloading
 How to check if two Rectangle objects are equal?
– You may write a function, e.g. equal(r,s), to compare,
which takes these two objects as arguments, compares
the four data members, and returns a true or false
– Isn’t it wonderful if you could just say
if (r == s) { ... }
So, the operator == not only compares variables of basic
data types, e.g. int, float, but also user defined types
 operator overloading
– User defined data types can be treated same as basic data
types

National Tsing Hua University ® copyright OIA 15


National Tsing Hua University
Overloading == as a Member Function
bool Rectangle::operator==(cont Rectangle& s)
{
if (this == &s) return true;
if ((xLow == s.xLow) && (yLow = s.yLow)
&& (height == s.height)
&& (width == s.width)) return true;
else return false;
}
 The pointer this inside a member function points
to the class object that invoked it  *this points
to class itself

National Tsing Hua University ® copyright OIA 16


National Tsing Hua University
Overloading << as Non-member Function
ostream& operator<<(ostream& os,Rectangle& r)
{ os << “Position is: “ << r.xLow << “ “;
os << r.yLow << endl;
os << “Height is: “ << r.Height << endl;
os << “Width is: “ << r.Width << endl;
return os;
};
 This function accesses private data members of class
Rectangle and must be made a friend of it (see
next page) Return cout; thus have cout<<endl
 With the overloaded operator, we can do
cout << r << endl;

National Tsing Hua University ® copyright OIA 17


National Tsing Hua University
Class Rectangle
class Rectangle {
friend ostream& operator<<(ostream& os,
Rectangle& r);
public:
Rectangle(int x = 0, int y = 0,
int h = 0, int w = 0)
: xLow(x), yLow(y), height(h), width(w){ }
bool operator==(const Rectangle& s){
...
}
private:
int xLow, yLow, height, width;
};

National Tsing Hua University ® copyright OIA 18


National Tsing Hua University
ADT of General Arrays in C++

class GeneralArray {
private:
/* A set of <index, value>, where IndexSet is a
finite ordered set of one or more dimensions */
public:
GeneralArray(int j; RangeList list,
float initValue = defatultValue);
/* Constructor creates a j-D array of floats.
Range of k-D is given by kth element of list.
For each i in IndexSet, insert <i,initValue> */
float Retrieve(index i);
void Store(index i, float x);
}; // end of GeneralArray

National Tsing Hua University ® copyright OIA 19


National Tsing Hua University
Outline
 C++ class
 From general arrays to ordered list
 Polynomial as an example
– Space optimization in data structure
 Sparse matrices as another example
– Time optimization in associated operations

National Tsing Hua University ® copyright OIA 20


National Tsing Hua University
From General Arrays to Ordered Lists
 A general array only specifies that there is a set of
<index, value> pairs  no special order imposed
 An ordered (linear) list is a special case of general
arrays, in which the items are ordered linearly
– Days of Week: (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
– Months: (Jan, Feb, Mar, …, Nov, Dec)
– Poker: (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack,
Queen, King, Ace) 2 B
6 F
11 K
13 M
1 A 2 B 6 F 11 K 13 M
1 A

National Tsing Hua University ® copyright OIA 21


National Tsing Hua University
Operations on Ordered Lists
 Find the length, n, of the list
 Read the items from left to right (or right to left)
 Retrieve the ith item, 0 ≤ i < n
 Store a new value into ith position, 0 ≤ i < n
 Insert/delete the item at position i, 0 ≤ i < n

 It is not necessary to include all operations


 Depending on applications, different representations
support different subsets of operations efficiently

National Tsing Hua University ® copyright OIA 22


National Tsing Hua University
Outline
 C++ class
 From general arrays to ordered list
 Polynomial as an example
– Space optimization in data structure
– Representation, addition, time complexity analysis
 Sparse matrices as another example
– Time optimization in associated operations

National Tsing Hua University ® copyright OIA 23


National Tsing Hua University
Polynomials
 p(x) = a0xe0 + a1xe1 +, …, anxen =∑ aixei
– Each aixei is a term with coefficient ai and exponent ei
– Degree of p(x) is largest exponent of the non-zero term
– Ex.: p(x)=x5+4x3+2x2+1
has 4 terms with coefficients 1, 4 ,2, 1, and a degree of 5
 Intuitive representation in a C-type array
– Store (ai, ei) by assigning ai to A[n-i], where n is the degree

x5 4x3 2x2 x0

1 0 4 2 0 1 exponent  index
coefficient  value
A[0] A[1] A[2] A[3] A[4] A[5]

National Tsing Hua University ® copyright OIA 24


National Tsing Hua University
Operations for Polynomials
Let a(x) = ∑ aixi and b(x) = ∑ bixi
 Polynomial addition
– a(x) + b(x) = ∑ (ai+bi)xi
– Ex.: a(x)=x5+4x3+2x2+1 (degree = 5)
b(x)=3x6+4x3+x (degree = 6)
a(x) + b(x) = 3x6+x5+8x3+2x2+x+1 (degree = 6)
 Polynomial multiplication
– a(x)  b(x) = ∑ (aixi  ∑ (bjxj))
 How about insertion/deletion?

National Tsing Hua University ® copyright OIA 25


National Tsing Hua University
How to represent to be
ADT of Polynomials most efficient in space?
class Polynomial {
// a set of ordered pairs of <ai, ei>, where ai is
// nonzero float, ei is non-negative integer
public: We will ignore destructor hereafter. It is
Polynomial(void); // Constructor p(x) to=treat
programmer’s responsibility 0 her
~Polynomial(void); // memory well 
Destructor
Polynomial Add(Polynomial poly);
// Return the sum of *this and poly
Polynomial Mult(Polynomial poly);
// Return multiplication of *this and poly
float Eval(float x);
// Evaluate *this at x and return result
};

National Tsing Hua University ® copyright OIA 26


National Tsing Hua University
1st Representation of Data Members
 Use C-type arrays with fixed space
private:
Usage:
// degree ≤ MaxDegree
Polynomial a;
int degree;
a.degree = n;
// coefficient array
a.coef[i] = an-i
float coef[MaxDegree+1];

x5 4x3 2x2 x0

1 0 4 2 0 1
A[0] A[1] A[2] A[3] A[4] A[5]
– Must know MaxDegree, may allocate too much space,
waste memory in a sparse polynomial, e.g., x1000+1

National Tsing Hua University ® copyright OIA 27


National Tsing Hua University
2nd Representation of Data Members
 Use C-type arrays with dynamically allocated space:
// constructor
private: Polynomial::Polynomial(int d)
int degree; { degree = d;
float *coef; coef = new float[degree+1];
}

– No need to know MaxDegree in advance, allocate exact


space as needed
– Disadvantage: waste memory in a sparse polynomial

National Tsing Hua University ® copyright OIA 28


National Tsing Hua University
3rd Representation of Data Members
 Store only nonzero terms:
class Polynomial; private:
// forward decl. // array of nonzero terms
class Term { Term* termArray;
friend Polynomial; // termArray size
float coef; int capacity;
int exp; // # nonzero terms
}; int terms;
– Coefficients are stored in order of decreasing exponents
– Better if polynomial is sparse, but if polynomial is full, it
requires double the space of 2nd representation
 considerations for space optimization

National Tsing Hua University ® copyright OIA 29


National Tsing Hua University
Polynomial Addition (1/3)
Polynomial Polynomial::Add(Polynomial b) {
Polynomial c;
int aPos = 0, bPos = 0;
while((aPos < terms) && (bPos < b.terms))
if(termArray[aPos].exp == b.termArray[bPos].exp){
float t = termArray[aPos].coef
+ b.termArray[bPos].coef;
if (t) c.NewTerm(t, termArray[aPos].exp);
aPos++; bPos++; Append to the end
} else of Polynomial c
if(termArray[aPos].exp < b.termArray[bPos].exp){
c.NewTerm(b.termArray[bPos].coef,
b.termArray[bPos].exp);
bPos++;
}

National Tsing Hua University ® copyright OIA 30


National Tsing Hua University
Polynomial Addition (2/3)

else{
c.NewTerm(termArray[aPos].coef,termArray[aPos].exp);
aPos++;
}
// add in remaining terms of *this
for(; aPos < terms; aPos++)
c.NewTerm(termArray[aPos].coef,
termArray[aPos].exp);
// add in remaining terms of b
for(; bPos < b.terms; bPos++)
c.NewTerm(b.termArray[bPos].coef,
b.termArray[bPos].exp);
return c;
}

National Tsing Hua University ® copyright OIA 31


National Tsing Hua University
Polynomial Addition (3/3)

void Polynomial::NewTerm(const float c, const int e)


{ //Add a new term to the end of termArray
if (terms == capacity)
{ // double capacity of termArray
capacity *= 2;
term *temp = new term[capacity];
copy(termArray, termArray + terms, temp);
delete[] termArray;
termArray = temp;
}
termArray[terms].coef = c;
termArray[terms].exp = e;
}

National Tsing Hua University ® copyright OIA 32


National Tsing Hua University
A Running Example

a(x) = x5 + 9x4 + 7x3 + 2x

aPos aPos aPos aPos aPos

b(x) = x6 + 3x5 + 6x + 3

bPos bPos bPos bPos

c(x) = x6 +(1+3)x5 + 9x4 + 7x3 +(2+6)x + 3


=x6 + 4x5 + 9x4 + 7x3 + 8x + 3

National Tsing Hua University ® copyright OIA 33


National Tsing Hua University
Time Complexity Analysis
 Inside the while loop, every statement has O(1) time
 How many times the “while loop” is executed in the
worst case?
– Let a(x) have m terms and b(x) have n terms
– Each iteration accesses next element in a(x), b(x), or both
– Worst case: m + n – 1
e.g., a(x) = 7x5 + x3 + x; b(x) = x6 + 2x4 + 6x2 +3
– Access remaining terms in a(x): O(m), and b(x): O(n)
 Hence, total running time = O(m + n)

National Tsing Hua University ® copyright OIA 34


National Tsing Hua University
Outline
 C++ class
 From general arrays to ordered list
 Polynomial as an example
– Space optimization in data structure
 Sparse matrices as another example
– Time optimization in associated operations
– Representation, transpose, multiplication, time complexity
analysis

National Tsing Hua University ® copyright OIA 35


National Tsing Hua University
Sparse Matrix
 A matrix has many zero elements
15 0 0 22 0 -15
0 11 3 0 0 0
a[6][6] = 0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0

 2D array representation is inefficient


– Waste both memory and running time to store and
compute those zero elements

National Tsing Hua University ® copyright OIA 36


National Tsing Hua University
Example of Sparse Matrices
 Web page matrix
– Web pages are numbered 1 through n
– web(i,j) = number of links from page i to page j
 Space analysis
– n = 2 billion = 2  109 pages
– If use n x n array of ints  4  1018  4 bytes
– Each page links to 10 (say) other pages on average, i.e. 10
nonzero entries per row
– If use general array  2  109  10  8 bytes

National Tsing Hua


National University
Tsing ® copyright OIA
Hua University
Example of Sparse Matrices
 Social network
– People are numbered 1 through n
– friend(i,j) = 1, if i and j are friends; 0, otherwise
– What does it mean by (friend matrix)2?
– n = 100M (say), each person has 100 friends in average
– If use n x n array  1016  4 bytes
– If use general array  108  100  8 bytes

National Tsing Hua


National University
Tsing ® copyright OIA
Hua University
Sparse Matrix Representation
 Use an array, smArray[], of triple <row, col, value> to
store nonzero elements (2D index space)
 Triples are stored in row-major order  ordered list
row col value
smArray[0] 0 0 15
15 0 0 22 0 -15 smArray[1] 0 3 22
0 11 3 0 0 0
smArray[2] 0 5 -15
a[6][6] = 0 0 0 -6 0 0
0 0 0 0 0 0 smArray[3] 1 1 11
91 0 0 0 0 0 smArray[4] 1 2 3
0 0 28 0 0 0 smArray[5] 2 3 -6
smArray[6] 4 0 91
How about insertion/deletion? smArray[7] 5 2 28

National Tsing Hua University ® copyright OIA 39


National Tsing Hua University
ADT of Sparse Matrix
class SparseMatrix{
public:
SparseMatrix(int r, int c, int t);
// t is the capacity of nonzero terms
SparseMatrix Transpose(void);
SparseMatrix Add(SparseMatrix b);
SparseMatrix Multiply(SparseMatrix b);
private:
class MatrixTerm {
int rows, cols;
friend SparseMatrix;
int terms, capacity;
int row, col, value;
MatrixTerm *smArray;
};
};

National Tsing Hua University ® copyright OIA 40


National Tsing Hua University
Approximate Memory Requirements
 500 x 500 matrix with 1994 nonzero elements, 4
bytes per element

2D array 500 x 500 x 4 = 1million bytes


Class SparseMatrix 3 x 1994 x 4 + 4 x 4
= 23,944 bytes

National Tsing Hua


National University
Tsing ® copyright OIA
Hua University
Matrix Transpose

15 0 0 22 0 -15
0 11 3 0 0 0
0 0 0 -6 0 0
A=
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
15 0 0 0 91 0
0 11 0 0 0 0
0 3 0 0 0 28
AT =
22 0 -6 0 0 0
0 0 0 0 0 0
-15 0 0 0 0 0

National Tsing Hua University ® copyright OIA 42


National Tsing Hua University
Transpose of Matrix
 Intuitive idea: check columns sequentially and collect
terms with same column together
A row col value AT row col value
smArray[0] 0 0 15 smArray[0] 0 0 15
smArray[1] 0 3 22 smArray[1] 0 4 91
smArray[2] 0 5 -15 smArray[2] 1 1 11
smArray[3] 1 1 11 smArray[3] 2 1 3
smArray[4] 1 2 3 smArray[4] 2 5 28
smArray[5] 2 3 -6 smArray[5] 3 0 22
smArray[6] 4 0 91 smArray[6] 3 2 -6
smArray[7] 5 2 28 smArray[7] 5 0 -15

National Tsing Hua University ® copyright OIA 43


National Tsing Hua University
1st Transpose Algorithm
SparseMatrix SparseMatrix::Transpose() {
SparseMatrix b(cols, rows, terms);
if (terms > 0) { // has nonzero terms
int currentB = 0;
for(int c=0; c<cols; c++) // O(cols)
for(int i=0; i<terms; i++) // O(terms)
if(smArray[i].col == c){
b.smArray[currentB].row = c;
b.smArray[currentB].col = smArray[i].row;
b.smArray[currentB++].value=smArray[i].value;
}
}
return b; Time complexity: O(cols  terms)
} ~O(cols  cols  rows)

National Tsing Hua University ® copyright OIA 44


National Tsing Hua University
2nd Transpose Algorithm: Fast Transpose
 Cause of inefficiency for 1st transpose algorithm:
– Do not know locations of different columns
– This information can be calculated beforehand
 Use additional space to calculate and store
– rowSize[i]: # of nonzero terms in the ith row of AT
– rowStart[i]: location of nonzero term of the ith row of AT in
smArray
– For i>0, rowStart[i]=rowStart[i-1]+rowSize[i-1]
 Then copy elements from A to AT one by one
 Time complexity: O(terms + cols)!

National Tsing Hua University ® copyright OIA 45


National Tsing Hua University
Fast Transpose

Count the # of nonzero terms in each row of AT


A row col value AT rowSize rowStart AT row col
smArray[0] 0 0 15 [0] 2 smArray[0]
smArray[1] 0 3 22 [1] 1 smArray[1]
smArray[2] 0 5 -15 [2] 2 smArray[2]
smArray[3] 1 1 11 [3] 2 smArray[3]
smArray[4] 1 2 3 [4] 0 smArray[4]
smArray[5] 2 3 -6 [5] 1 smArray[5]
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]

National Tsing Hua University ® copyright OIA 46


National Tsing Hua University
Fast Transpose

Calculate location of 1st nonzero term of ith row of AT in smArray


A row col value AT rowSize rowStart AT row col
smArray[0] 0 0 15 [0] 2 0 smArray[0]
smArray[1] 0 3 22 [1] 1 2 smArray[1]
smArray[2] 0 5 -15 [2] 2 3 smArray[2]
smArray[3] 1 1 11 [3] 2 5 smArray[3]
smArray[4] 1 2 3 [4] 0 7 smArray[4]
smArray[5] 2 3 -6 [5] 1 7 smArray[5]
smArray[6] 4 0 91 smArray[6]
smArray[7] 5 2 28 smArray[7]

National Tsing Hua University ® copyright OIA 47


National Tsing Hua University
Fast Transpose

Copy elements from A to AT one by one


row col value AT rowSize rowStart AT row col value
[0] 0 0 15 [0] 2 0 smArray[0] 0 0 15
[1] 0 3 22 [1] 1 2 smArray[1]
[2] 0 5 -15 [2] 2 3 smArray[2]
[3] 1 1 11 [3] 2 5 smArray[3]
[4] 1 2 3 [4] 0 7 smArray[4]
[5] 2 3 -6 [5] 1 7 smArray[5]
[6] 4 0 91 smArray[6]
[7] 5 2 28 smArray[7]

National Tsing Hua University ® copyright OIA 48


National Tsing Hua University
Fast Transpose

Copy elements from A to AT one by one


row col value AT rowSize rowStart AT row col value
[0] 0 0 15 [0] 2 1 smArray[0] 0 0 15
[1] 0 3 22 [1] 1 2 smArray[1]
[2] 0 5 -15 [2] 2 3 smArray[2]
[3] 1 1 11 [3] 2 5 smArray[3]
[4] 1 2 3 [4] 0 7 smArray[4]
[5] 2 3 -6 [5] 1 7 smArray[5] 3 0 22
[6] 4 0 91 smArray[6]
[7] 5 2 28 smArray[7]

National Tsing Hua University ® copyright OIA 49


National Tsing Hua University
Fast Transpose

Copy elements from A to AT one by one


row col value AT rowSize rowStart AT row col value
[0] 0 0 15 [0] 2 1 smArray[0] 0 0 15
[1] 0 3 22 [1] 1 3 smArray[1] 0 4 91
[2] 0 5 -15 [2] 2 4 smArray[2] 1 1 11
[3] 1 1 11 [3] 2 7 smArray[3] 2 1 3
[4] 1 2 3 [4] 0 7 smArray[4]
[5] 2 3 -6 [5] 1 8 smArray[5] 3 0 22
[6] 4 0 91 smArray[6] 3 2 -6
[7] 5 2 28 smArray[7] 5 0 -15

National Tsing Hua University ® copyright OIA 50


National Tsing Hua University
Fast Transpose

Copy elements from A to AT one by one


row col value AT rowSize rowStart AT row col value
[0] 0 0 15 [0] 2 2 smArray[0] 0 0 15
[1] 0 3 22 [1] 1 3 smArray[1] 0 4 91
[2] 0 5 -15 [2] 2 5 smArray[2] 1 1 11
[3] 1 1 11 [3] 2 7 smArray[3] 2 1 3
[4] 1 2 3 [4] 0 7 smArray[4] 2 5 28
[5] 2 3 -6 [5] 1 8 smArray[5] 3 0 22
[6] 4 0 91 smArray[6] 3 2 -6
[7] 5 2 28 smArray[7] 5 0 -15

National Tsing Hua University ® copyright OIA 51


National Tsing Hua University
Fast Transpose (1/2)

SparseMatrix SparseMatrix::FastTranspose( )
{ SparseMatrix b(cols, rows, terms);
if (terms > 0) {
int *rowSize = new int[cols];
int *rowStart = new int[cols];
// compute rowSize[i]=# of terms in row i of b
fill(rowSize, rowSize+cols, 0);
for(int i=0; i<terms; i++)
rowSize[smArray[i].col]++;
// rowStart[i] = starting pos. of row i in b
rowStart[0] = 0;
for(int i=1; i<cols; i++)
rowStart[i]=rowStart[i-1]+rowSize[i-1];

National Tsing Hua University ® copyright OIA 52


National Tsing Hua University
Fast Transpose (2/2)
// copy terms from *this to b
for(int i=0; i<terms; i++){
int j = rowStart[smArray[i].col];
b.smArray[j].row = smArray[i].col;
b.smArray[j].col = smArray[i].row;
b.smArray[j].value = smArray[i].value;
rowStart[smArray[i].col]++;
// Increase the start pos by 1
}
delete [] rowSize;
delete [] rowStart;
}
return b;
}

National Tsing Hua University ® copyright OIA 53


National Tsing Hua University
Running Time Comparison

1st Transpose Algorithm 2nd Transpose Algorithm

O(cols  terms) O(cols + terms)

 For a dense matrix (terms = rows  cols)


– 2nd algorithm is faster: O(rows  cols)
– 1st algorithm is slower: O(rows  cols2)
 For a sparse matrix (terms << rows  cols)
– 2nd algorithm is much faster
 Considerations for time optimization

National Tsing Hua University ® copyright OIA 54


National Tsing Hua University
Sparse Matrix Multiplication
 Compute the transpose of b
c: m x p a: m x n b: n x p
0 5 2 0 0 7 3
x 0
= 4
3
6
5

X = 03 + 50 + 24 + 03 + 06 + 75 = 43


c(i,j) =  a(i,k)  b(k,j)

National Tsing Hua University ® copyright OIA 55


National Tsing Hua University
Sparse Matrix Multiplication
 Use approach similar to Polynomial Addition
to compute the X
c: m x p a: m x n bT: p x n
0 5 2 0 0 7 3 0 4 3 6 5
x
=
p p p q q q q q

Please refer to the


textbook for code x =(2)(4)+ (7)(5)= 43

National Tsing Hua University ® copyright OIA 56


National Tsing Hua University
Time Complexity

SparseMatrix SparseMatrix::Multiply(SparseMatrix b) {
SparseMatrix bT = b.FastTranspose(); //O(b.terms+b.cols)
for ith row in smArray // O(rows)
for jth row in bT.smArray // O(b.cols)
Perform “Polynomal Addition” // O(Terms[i]+b.Terms[j])
}

 Complexity:
– O(rows  b.cols  (Term[i] + b.Terms[j]))
– rows  Term[i] = a.terms
b.cols  b.Terms[j] = b.terms
– O(rows  b.terms + b.cols  a.terms)

National Tsing Hua University ® copyright OIA 57


National Tsing Hua University
Summary
 General arrays as ADT with easy C-type array ext.
 C++ class
 Polynomial as an example of ordered list (linear
index space)
– 3 versions of presentations for space optimization
 Sparse matrix as another example of ordered list (2
dimensional index space)
– 2 transpose algorithms for time optimization
 What if we want to support insertion/deletion
efficiently?

National Tsing Hua


National University
Tsing ® copyright OIA
Hua University

You might also like