Data Abstraction and Encapsulation

Post on 12-Jan-2016

84 views 0 download

description

Data Abstraction and Encapsulation. Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world. Data Abstraction and Encapsulation. - PowerPoint PPT Presentation

Transcript of Data Abstraction and Encapsulation

Data Abstraction and Encapsulation

• Definition: Data Encapsulation or Information Hiding is the concealing of the implementation details of a data object from the outside world.

Data Abstraction and Encapsulation

• Definition: Data Abstraction is the separation between the specification of a data object and its implementation.

• Definition: A data type is a collection of objects and a set of operations that act on those objects.

Advantages of Data Abstraction and Data Encapsulation

• Simplification of software development

• Testing and Debugging• Reusability

Data Abstraction and Encapsulation

• Definition: An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations.

Sparse Matrices

472748

9812

1164109

2826

4327

0002800

0000091

000000

006000

0003110

150220015

ADT 2.3 Abstract data type SparseMatrix

class SparseMatrix// objects: A set of triples, <row, column, value>,

where row and column are integers and form a unique combinations; value is also an integer.

public:SparseMatrix(int MaxRow, int MaxCol);SparseMatrix Transpose();SparseMatrix Add(SparseMatrix b);SparseMatrix Multiply(SparseMatrix b);

};

Sparse Matrix Representation

• Use triple <row, column, value>• Store triples row by row • For all triples within a row, their

column indices are in ascending order.

• Must know the number of rows and columns and the number of nonzero elements

Sparse Matrix Representation (Cont.)

class SparseMatrix; // forward declaration

class MatrixTerm {

friend class SparseMatrix

private:

int row, col, value;

};

class SparseMatrix:

private:

int Rows, Cols, Terms;

MatrixTerm smArray[MaxTerms];

Transposing A Matrix

• Intuitive way:for (each row i)

take element (i, j, value) and store it in (j, i, value) of the transpose

• More efficient way:for (all elements in column j)

place element (i, j, value) in position (j, i, value)

Program 2.10 Transposing a Matrix

SparseMatrix SparseMatrix::Transpose()// return the transpose of a (*this){

SparseMatrix b;b.Rows = Cols; // rows in b = columns in ab.Cols = Rows; // columns in b = rows in ab.Terms = Terms; // terms in b = terms in aif (Terms > 0) // nonzero matrix{

int CurrentB = 0;for (int c = 0; c < Cols; c++) // transpose by columns for (int i = 0; i < Terms; i++) // find elements in column c

if (smArray[i].col == c) { b.smArray[CurrentB].row = c; b.smArray[CurrentB].col = smArray[i].row; b.smArray[CurrentB].value = smArray[i].value; CurrentB++;}

} // end of if (Terms > 0)} // end of transpose

O(terms*columns)

Fast Matrix Transpose

• The O(terms*columns) time => O(rows*columns2) when terms is the order of rows*columns

• A better transpose function in Program 2.11. It first computes how many terms in each columns of matrix a before transposing to matrix b. Then it determines where is the starting point of each row for matrix b. Finally it moves each term from a to b.

Program 2.11 Fast Matrix Transposing

SparseMatrix SparseMatrix::Transpose()// The transpose of a(*this) is placed in b and is found in Q(terms + columns) time.{

int *RowSize = new int[Cols]; int *RowStart = new int[Cols]; SparseMatrix b;b.Rows = Cols; b.Cols = Rows; b.Terms = Terms; if (Terms > 0) // nonzero matrix{ // compute RowSize[i] = number of terms in row i of b for (int i = 0; i < Cols; i++) RowSize[i] = 0; // Initialize

for ( i= 0; i < Terms; i++) RowSize[smArray[i].col]++;

// RowStart[i] = starting position of row i in b RowStart[0] = 0; for (i = 1; i < Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1];

O(columns)

O(terms)

O(columns-1)

Program 2.11 Fast Matrix Transposing (Cont.)

for (i = 0; i < Terms; i++) // move from a to b

{

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]++;

} // end of for

} // end of if

delete [] RowSize;

delete [] RowStart;

return b;

} // end of FastTranspose

O(terms)

O(row * column)

Representation of Arrays

• Multidimensional arrays are usually implemented by one dimensional array via row major order.

• Example: One dimensional array

A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] A[10] A[11]

α α+1 α+2 α+3 α+4 α+5 α+6 α+7 α+8 α+9 α+10 α+11

Two Dimensional Array Row Major Order

X X X X

X X X X

X X X X

Col 0 Col 1 Col 2 Col u2 - 1

Row 0

Row 1

Row u1 - 1

u2

elements

u2

elements

Row 0 Row 1 Row u1 - 1Row i

i * u2 element

Generalizing Array Representation

The address indexing of Array A[i1][i2],…,[in] is

α+ i1 u2 u3 … un

+ i2 u3 u4 … un

+ i3 u4 u5 … un

:

:

+ in-1 un

+ in

=α+

1

11

1n

n

jkkj

n

jjj

a

njuawhereai

• 樣式比對– 假設有兩個字串 string和 pat ,其中 pat 是要在 string 中找尋的

樣式。

17

• int String::Find(String pat)• {// 如果在 *this 字串裡面找不到 pat ,那麼傳回 -1 ;否則回傳

pat在 *this 裡的起始位置。• for (int start = 0; start <= Length( )- pat.Length();

star ++)• { // 從 str [start] 開始檢查有沒有相同的字元• int j;• for (j = 0; j < pat.Length( ) && str [start+ j] = =

pat.str[j]; j++)• if (j = = pat.Length( )) return start; // 找到相同的字串• // 在 start 這個位置沒找到匹配• }• return - 1 ; // pat 為空字串或者不存在於在 s 中• }

• 範例 :假設 pat = “aab”且 string = “ababbaabaa” 。下圖儲存 nfind 如何比對 pat 和 string 中的字元。

• 圖:模擬 nfind

j lastp (a) 樣式

start endmatch lasts (b) 不同

aa aa bb

aa bb aa bb bb aa aa bb aa aa

18

start endmatch lasts (c) 不同

start endmatch lasts (d) 不同

          start endmatch lasts (e) 不同

start endmatch lasts (f) 不同

aa bb aa bb bb aa aa bb aa aa

aa bb aa bb bb aa aa bb aa aa

aa bb aa bb bb aa aa bb aa aa

aa bb aa bb bb aa aa bb aa aa

19

start endmatch lasts (g) 相同

• 樣式的失敗函數 (failure function) 定義:若 P=p0p1…pn-1 為一個樣式,則其失敗函數f定義如下:

對於上述的範例樣式 pat = “abcabcacab” ,我們可得:

aa bb aa bb bb aa aa bb aa aa

,若不存在時,若存在一個這樣的的最大使得

1

0......)( 2110 ijippppppp

if lijijiji

jj 00 11 22 33 44 55 66 77 88 99

patpat aa bb cc aa bb cc aa cc aa bb

ff -1-1 -1-1 -1-1 00 11 22 33 -1-1 00 11

20

• 根據失敗函數的定義,我們可以知道:如果找到部分的匹配使得

,則比對工作可以從比較  和 繼續。若j=0,則從比較   和  繼續。這樣的樣式比對規則較換成函數 pmatch 。

0,...... .1101 jpspppss jijiji 若且

is 1)1( jfp

1is0p

21

程式: Knuth,Morris, Pratt 的樣式比對演算法•int String::FastFind(String pat) •2 {// 決定 pat 是否為 s 的子字串。•3 int posP = 0, posS = 0;•4 int lengthP = pat.Length(), lengthS = Length();•5 while((posP < lengthP) && (posS < lengthS))•6 if (pat.str[posP] = = str[posS]) {// 匹配到相同的字元•7 posP++; posS++;•8 }•9 else•10 if (posP = = 0) •11 posS++;•12 else posP = pat . f [posP- 1] + 1;•13 if (posP < lengthP) return - 1; // 字串沒有完全比完•14 else return posS- lengthP;•15 }

• 有一個快速的方法可計算失敗函數。它以下列重新定義的失敗函數為依據:

• 這個定義產生了程式 2.16 中的函數,他用來計算樣式的失敗函數。

值滿足上式若沒有的最小整數值為使得其中

k

kpp

j

jfjf jk

jfm

1)1(m

0

1

1)1(

1

)(

))(()()()( 11 jffjfjfjf mm 且注意,

23

• 程式:計算失敗函數• 1 void String::FailureFunction()• 2 {// 為字串樣本 *this 計算失敗函數。• 3 int lengthP = Length( );• 4 f[0] = - 1;• 5 for (int j = 1; j < lengthP; j++) // 計算 f [j]• 6 {• 7 int i = f [j- 1];• 8 while ((*(str+ j) != *(str+ i+ 1)) && (i >= 0)) i =

f [i];• 9 if (*(str+ j) = = *(str+ i+ 1)) • 10 f [j] = i+ 1;• 11 else f [j] = - 1;• 12 }• 13 }

24