Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.

Similar presentations


Presentation on theme: "Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays."— Presentation transcript:

1 Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf (ab_sarraf@aut.ac.ir) Arrays (1)

2 Dept. of Computer Eng. & IT, Amirkabir University of Technology Objectives Arrays in C++ Implementation PolyNomial Sparse Matrix Representation String

3 Dept. of Computer Eng. & IT, Amirkabir University of Technology Arrays in C++ Array: a set of index and value Data structure For each index, there is a value associated with that index. Representation Implemented by using consecutive memory. Example: int list[5]: list[0], …, list[4] each contains an integer

4 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type Polynomial requires ordered lists the largest exponent is called degree Example: A(X)=3X 2 +2X+4, B(X)=X 4 +10X 3 +3X 2 +1 sum and product of polynomials A(x) = a i x i and B(x) = b i x i

5 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Class Polynomial { // objects : p(x) = a 0 x e 0 + … + a n x e 0 ; a set of ordered pairs of, // where a i Coefficient and e i Exponent // We assume that Exponent consists of integers ≥ 0 public: Polynomial(); // return the polynomial p(x)=0 int operator!(); // If *this is the zero polynomial, return 1; else return 0; Coefficient Coef(Exponent e); // return the coefficient of e in *this Exponent LeadExp(); // return the largest exponent in *this

6 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Polynomial Add(Polynomial poly); // return the sum of the polynomials *this and poly Polynomial Mult(Polynomial poly); // return the product of the polynomials *this and poly float Eval(float f); // Evaluate the polynomial *this at f and return the result. }; // end of polynomial

7 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Polynomial Representation Principle unique exponents are arranged in decreasing order

8 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Representation 1 (Array: static memory allocation) define the private data members of Polynomial private : int degree ; // degree≤MaxDegree float coef[MaxDegree+1] ; leads to a very simple algorithms for many of the operations on polynomials wastes computer memory for example, if a.degree << MaxDegree

9 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Representation 2 (Array: dynamic memory allocation) define coef with size a.degree+1 declare private data members private : int degree ; float *coef ; add a constructor to Polynomial Polynomial::Polynomial(int d) { degree=d ; coef=new float[degree+1] ; } wastes space for sparse polynomials for example, x 1000 +1

10 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Representation 3 previously, exponents are represented by array indices now, (non-zero) exponents are stored all Polynomials will be represented in a single array called termArray termArray is shared by all Polynomial objects it is declared as static each element in termArray is of type term class term { friend Polynomial ; private : float coef ; // coefficient int exp ; // exponent };

11 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) declare private data members of Polynomial class Polynomial { private : static term termArray[MaxTerms]; static int free; int Start, Finish; public: Polynomial ADD(Polynomial poly);... } required definitions of the static class members outside the class definition term Polynomial::termArray[MaxTerms]; int Polynomial::free=0; // next free location in termArray A(x)=2x 1000 +1, B(x)=x 4 +10x 3 +3x 2 +1

12 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) 2111031 100004320 0123456 coef exp A.StartA.FinishB.StartB.FinishFree Figure 2.1 : Array representation of two polynomials

13 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) A(x) has n nonzero terms A.Finish=A.Start+n-1 comparison with Representation 2 Representation 3 is superior when many zero terms are present as in A(x) when all terms are nonzero, as in B(x), Representation 3 uses about twice as much space as Representation 2

14 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) 2.3.2 Polynomial Addition Representation 3 is used C(x)=A(x)+B(x)

15 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Polynomial Polynomial::Add(Polynomial B) { Polynomial C; int a = Start; int b = B.Start; C.Start = free; float c; while ((a<=Finish) && (b<=B.Finish) switch (compare(termArray[a].exp, termArray[b].exp)){ case '=': c = termArray[a].coef + termArray[b].coef; if (c) NewTerm(c, termArray[a].exp); a++; b++; break; case '<': NewTerm(termArray[b].coef, termArray[b].exp); b++; break; case '>': NewTerm(termArray[a].coef, termArray[a].exp); a++; }// end of switch and while for (; a<=Finish; a++) // add in remaining terms of A(x) NewTerm(termArray[a].coef, termArray[a].exp); for (; b<=B.Finish; b++) // add in remaining terms of B(x) Newterm(termArray[b].coef, termArray[b].exp); C.Finish = free - 1; return C; } // end of Add Analysis: O(n+m) where n, m is the number of non-zeros in A, B.

16 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) void Polynomial::NewTerm(float c, int e) // Add a new term to C(x). { if (free>=MaxTerms) { cerr << "Too many terms in polynomials" << endl; exit(1); } termArray[free].coef = c; termArray[free].exp = e; free++; }// end of NewTerm Program 2.9 : Adding a new term

17 Dept. of Computer Eng. & IT, Amirkabir University of Technology Polynomial Abstract Data Type(Cont’) Analysis of Add m and n are number of nonzero-terms in A and B, respectively the asymptotic computing time is O(n+m) Disadvantages of representing polynomials by arrays space must be reused for unused polynomials linked lists in chapter 4 provide a solution

18 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrix A general matrix consists of m rows and n columns of numbers An m×n matrix It is natural to store a matrix in a two-dimensional array, say A[m][n] A matrix is called sparse if it consists of many zero entries Implementing a spare matrix by a two-dimensional array waste a lot of memory Space complexity is O(m×n)

19 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrix

20 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) Class SparseMatrix { // objects : A set of triples,, where row // and column are integers and form a unique combination; value // is also an integer. public: SparseMatrix(int MaxRow, int MaxCol); // the constructor function creates a SparseMatrix // that can hold up to MaxItems=MaxRow x MaxCol and whose // maximum row size is MaxRow and whose maximum // column size is MaxCol SparseMatrix Transpose(); // returns the SparseMatrix obtained by interchanging the // row and column value of every triple in *this

21 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) SparseMatrix Add(SparseMatrix b); // if the dimensions of a(*this) and b are the same, then // the matrix produced by adding corresponding items, // namely those with identical row and column values is // returned else error. SparseMatrix Multiply(SparseMatrix b); // if number of columns in a (*this) equals number of // rows in b then the matrix d produced by multiplying a // by b according to the formula // d[i][j]= (a[i][k]·b[k][j]), // where d[i][j] is the (i, j)th element, is returned. // k ranges from 0 to the number of columns in a-1 // else error. }; ADT 2.3 : Abstract data type SparseMatrix

22 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) 2.4.2 Sparse Matrix Representation Representation use the triple to represent an element store the triples by rows for each row, the column indices are in ascending order store the number of rows, columns, and nonzero elements

23 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) C++ code class SparseMatrix; // forward declaration class MatrixTerm { friend class SparseMatrix private: int row, col, value; }; class SparseMatrix { private: int Rows, Cols, Terms; MatrixTerm smArray[MaxTerms]; public: SparseMatrix Transpose();... }

24 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) Representation of the matrix of Figure 2.2(b) using smArray smArray[0]0 [1]0 [2]0 [3]1 [4]1 [5]2 [6]4 [7]5 row 0 3 5 1 2 3 0 2 col 15 22 -15 11 3 -6 91 28 value smArray[0]0 [1]0 [2]1 [3]2 [4]2 [5]3 [6]3 [7]5 row 0 4 1 1 5 0 2 0 col 15 91 11 3 28 22 -6 -15 value (a) (b) Figure 2.3 : Sparse matrix and its transpose stored as triples

25 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) Transposing a Matrix n element at [i][j] will be at [j][i] for (each row i) take element (i, j, value) and store it in (j, i, value) of the transpose; difficulty: where to put (0, 0, 15)  (0, 0, 15) (0, 3, 22)  (3, 0, 22) (0, 5, -15)  (5, 0, -15) (1, 1, 11)  (1, 1, 11) need to insert many new triples, elements are moved down very often Find the elements in the order for (all elements in column j) place element (i, j, value) in position (j, i, value);

26 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) SparseMatrix SparseMatrix::Transpose() // return the transpose of a (*this) { SparseMatrix b; b.Rows = Cols; // rows in b = columns in a b.Cols = Rows; // columns in b = rows in a b.Terms = Terms; // terms in b = terms in a if (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) return b; } // end of transpose Time Complexity: O(terms*columns)

27 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) Analysis of transpose the number of iterations of the for loop at line 12 is terms the number of iterations of the for loop at line 11 is columns total time is O(terms*columns) total space is O(space for a and b) Using two-dimensional arrays for(int j=0; j<columns; j++) for(int i=0; i<rows; i++) B[j][i]=A[i][j]; total time is O(rows*columns)

28 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) FastTranspose algorithm Determine the number of elements in each column of the original matrix. Determine the starting positions of each row in the transpose matrix. [0][1][2][3][4] RowSize =32101 RowStart =03566 [5] 1 7

29 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’)

30 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’)

31 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’)

32 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) SparseMatrix SparseMatrix::FastTranspose() // The transpose of a (*this) is placed in b and is found in // O(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; O(Columns) O(Terms)

33 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) for (i =1; i<Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1]; 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 Program 2.11 : Transposing a matrix faster O(Columns) O(Terms) Total: O(Columns+Terms)

34 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) By using sparse matrix Pick a row of A and find all elements in column j of B for j = 0, 1,.., B.col-1, where B.col is the number of columns in B By using the transpose of B We can avoid the scanning all of B to find all the elements in column j All column elements are in consecutive order in B A : m x n B : n x p Matrix Multiplication

35 Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) int SparseMatrix::StoreSum(int sum,int& LastInResult,int r,int c){ if (sum != 0) { if (LastInResult < MaxTerms -1) { LastInResult++; smArray[LastInResult].row = r; smArray[LastInResult].col = c; smArray[LastInResult].value = sum; return 0; } else { cerr << "Number of terms in product exceeds MaxTerms" << endl; return 1; } else return 0; }

36 Dept. of Computer Eng. & IT, Amirkabir University of Technology SparseMatrix SparseMatrix::Multiply(SparseMatrix b) if (Cols != b.Rows) { cout << "Incompatible matrices" << endl; return EmptyMatrix(); } if ((Terms == MaxTerms) || (b.Terms == MaxTerms)) { cout << "One additional space in a or b needed" << endl; return EmptyMatrix(); } SparseMatrix bXpose = b.FastTranspose(); SparseMatrix result; int currRowIndex = 0, LastInResult = -1, currRowBegin = 0, currRowA = smArray[0].row; smArray[Terms].row = Rows; bXpose.smArray[b.Terms].row = b.Cols; bXpose.smArray[b.Terms].col = -1; int sum = 0;

37 Dept. of Computer Eng. & IT, Amirkabir University of Technology while (currRowIndex<Terms) { int currColB = bXpose.smArray[0].row; int currColIndex = 0; while (currColIndex <= b.Terms){ if (smArray[currRowIndex].row != currRowA){ if (result.StoreSum(sum, LastInResult, currRowA, currColB)) return EmptyMatrix(); else sum = 0; // sum currRowIndex = currRowBegin; while (bXpose.smArray[currColIndex].row==currColB) currColIndex++; currColB = bXpose.smArray[currColIndex].row; }else if (bXpose.smArray[currColIndex].row != currColB){ if (result.StoreSum(sum, LastInResult, currRowA, currColB)) return EmptyMatrix(); else sum = 0; currRowIndex = currRowBegin; currColB = bXpose.smArray[currColIndex].row; }

38 Dept. of Computer Eng. & IT, Amirkabir University of Technology else switch (compare(smArray[currRowIndex].col, bXpose.smArray[currColIndex].col)){ case '<' : currRowIndex++; break; case '=' : sum += smArray[currRowIndex].value *bXpose.smArray[currColIndex].value; currRowIndex++; currColIndex++; break; case '>' : currColIndex++; } // switch }// while(currCollindex <=b.Terms) while (smArray[currRowIndex].row == currRowA) currRowIndex ++ ; currRowBegin = currRowIndex; currRowA = smArray[ccurrRowIndex].row; }//while(currRowIndex<Terns) result.Rows = Rows; result.Cols = bCols; return result; }//Multiply


Download ppt "Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays."

Similar presentations


Ads by Google