Arrays 2008, Fall Pusan National University Ki-Joune Li
STEMPNU 2 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 a k, a k+1
STEMPNU 3 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
STEMPNU 4 Array 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]; Arrays
STEMPNU 5 Representation of Array Representation 3: Array of (Coefficient, Exponent): ((a m,m),(a m-1,m), … (a 0,0)) Sparse Array : Example. 3.0x x x+9.6 : ((3.0,101),(-2.4,2),(1.0,1),(9.6,0)) MaxTerms How to define the end ? MaxTerms Class Polynomial { private: static Term termArray[MaxTerms]; static int free; int Start, Finish; }; Class Term { friend Polynomial; private: float coef; float exp; }; a.Starta.Finish b.Startb.Finish free
STEMPNU 6 Example : Adding two polynomials A = 3.0x x x B = 4.0x x C = 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(Len A + Len B )
STEMPNU 7 Sparse Matrix Matrix with many zero elements Two Representations RowColValue vs. Class MatrixTerm { friend SparseMatrix; private: int row,col; int value; }; Class SparseMatrix { private: int nRows,nCols,nElements; MatrixElements smArray[MaxElements]; }; But Row Major !
STEMPNU 8 Transposing a Matrix row colvalue row colvalue 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(nCols 2 ·nRows) > O(nCols·nRows) A A ATAT ATAT A.nCols= 6, A.nRows=7 A.nElements=5
STEMPNU 9 Transposing a Matrix: An Improved Algorithm row colvalue A A col row ATAT ATAT value col row A A Exchange row col Sort by (row, col) Time Complexity : O(n Elements + n Elements log n Elements ) = O(n Elements log n Elements )
STEMPNU 10 Transposing a Matrix: Another Improved Algorithm indexrowcolvalue A A indexvaluecolrow ATAT ATAT A T Row Size A T Row Start Time Complexity : O(n col + n col + n Elements )