2017, Fall Pusan National University Ki-Joune Li Arrays 2017, Fall Pusan National University Ki-Joune Li
Most Basic Data Structure Primitive Elements : Integer, Float, String, etc.. Container of elements Ordered or not Duplicated or Not How to define the same object In fact, any container of elements is stored in ordered way. Only the interface makes it different ak, ak+1
Operations Operation defines the interface (or nature) to the users Should be implemented, whatever the internal implementation Operations Unordered Maintenance : create a new container, Insert, Delete, Update (?) Search : search by atttributes Information or statistics : number of elements, max, min, etc. Ordered Operations for Unordered Container + Scan : get the k-th, the last, or the next elements Sorting Ordered container can be used as an unordered container
Arrays Array Example : Polynomial Contains elements Ordered (or unordered) Set (No Order) Example : Polynomial Representation 1: float Coef[MaxDegree+1]; Representation 2: int degree; float *Coef; Coef=new float[degree+1];
Representation of Array Array of (Coefficient, Exponent): ((am,m),(am-1,m), … (a0,0)) Sparse Array : Example. 3.0x101-2.4x2+1.0x+9.6 : ((3.0,101),(-2.4,2),(1.0,1),(9.6,0)) MaxTerms Class Polynomial { private: static Term termArray[MaxTerms]; static int free; int Start, Finish; }; Class Term { friend Polynomial; private: float coef; float exp; }; free 3.0 101 -2.4 2 1.0 1 9.6 2.0 15 1.4 2 0.4 a.Start a.Finish b.Start b.Finish MaxTerms
Example : Adding two polynomials A = 3.0x101 - 2.4x2 + 1.0x + 9.6 3.0 101 -2.4 2 1.0 1 9.6 B = 4.0x15 + 1.4x2 + 0.4 4.0 15 1.4 2 0.4 C = 3.0 101 4.0 15 -1.0 2 1.0 1 10.0 Termination Condition - Aptr > A.finish or Bptr > B.finish If Terminated by Aptr > A.finish Append the rest of B to the tail of B Time Complexity : O(LenA + LenB )
Sparse Matrix Sparse Matrix Matrix with many zero elements Two Representations 1 2 3 4 5 6 7 Row Col Value 2 1 5 4 6 7 3 Class MatrixElement { friend SparseMatrix; private: int row,col; int value; }; vs. But Row Major ! Class SparseMatrix { private: int nRows,nCols,nElements; MatrixElement smArray[MaxElements]; };
Transposing a Matrix: An Algorithm 6 2 1 5 4 value col row A’ row col value 1 4 2 5 6 Sort by (row, col) Exchange row col 1 5 6 2 4 value col row AT Time Complexity : O(nElements + nElements log nElements) = O(nElements log nElements)
Transposing a Matrix: another Algorithm A.nCols= 6, A.nRows=7 A.nElements=5 AT row col value 1 4 2 5 6 row col value 4 1 1 4 2 2 6 5 1 Algorithm MatrixTranspose(SparseMatrix A) SparseMatrix B; swap(A.nRows,A.nCols); countB=0; if(A.nElements>0) { // for non-empty matrix for(c=0;c<A.nCols;c++) // for each element c of A for(i=0;i<A.nElements;i++) { // find elements in column c if(A.smArrays[i].col==c) { B.smArray[countB].row=c; B.smArray[countB].col=A.smArray[i].row; B.smArray[countB].value=A.smArrary[countB].value; ++countB; } } return B; end Algorithm Time Complexity : O(nCols·nElements) = O(nCols2·nRows) > O(nCols·nRows)
Transposing a Matrix: Another Improved Algorithm 7 6 5 4 3 2 1 index value col row AT index row col value 4.5 1 1.5 2 2.5 3 3.5 4 12.5 5 82.5 6 22.5 7 62.5 4.5 3 3.5 1 1.5 1 2.5 2 3 12.5 2 5 22.5 3 82.5 5 4 22.5 1 5 2 4 3 A Col Size 7 5 6 4 2 3 1 AT Row Start Time Complexity : O(nElements + ncol + nElements)