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

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

Arrays 2008, Fall Pusan National University Ki-Joune Li.
Chapter Matrices Matrix Arithmetic
Applied Informatics Štefan BEREŽNÝ
Array pair C++ array requires the index set to be a set of consecutive integers starting at 0 C++ does not check an array index to ensure that it belongs.
Data Abstraction and Encapsulation
Chapter 4.
Chapter 2. C++ Class A class name Data members Member functions Levels of program access –Public: section of a class can be accessed by anyone –Private:
Matrix table of values
10.1 Gaussian Elimination Method
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
CS Data Structures Chapter 2 Arrays and Structures.
MATRICES. Matrices A matrix is a rectangular array of objects (usually numbers) arranged in m horizontal rows and n vertical columns. A matrix with m.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
 Row and Reduced Row Echelon  Elementary Matrices.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
Chapter 2 Arrays and Structures  The array as an abstract data type  Structures and Unions  The polynomial Abstract Data Type  The Sparse Matrix Abstract.
Chapter 2 1. Arrays Array: a set of index and value Data structure For each index, there is a value associated with that index. Eg. int list[5]: list[0],
Advance Data Structure Review of Chapter 2 張啟中. Review of Chapter 2 Arrays 1.3 Data Abstraction and Encapsulation 2.2 The Array As An abstract Data Type.
Data Structure (Part II) Chapter 2 – Arrays. Matrix A matrix with 5 rows and 3 columns can be represented by n = 3 m = 5 We say this is a 5×3 matrix.
Chapter 4 – Matrix CSNB 143 Discrete Mathematical Structures.
DISCRETE COMPUTATIONAL STRUCTURES CS Fall 2005.
Data Structure Sang Yong Han Chung-Ang University Spring
Data Structures & Algorithm Analysis Arrays. Array: a set of pairs (index and value) data structure For each index, there is a value associated with that.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
POINTERS.
Data Structures Chapter 2: Arrays 2-1. Four components in a C++ Class –class name –data members: the data that makes up the class –member functions: the.
Greatest Common Divisors & Least Common Multiples  Definition 4 Let a and b be integers, not both zero. The largest integer d such that d|a and d|b is.
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chap 2 Array ( 陣列 ). ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
LINKED LISTS.
MTH108 Business Math I Lecture 20.
Data Structure Sang Yong Han
More Linking Up with Linked Lists
Chapter 3 Linear List (Sequential List)
CSCE 3110 Data Structures & Algorithm Analysis
Abstract Data Types Polynomials CSCI 240
Chap 2 Array (陣列).
Data Structures 4th Week
Dynamic Array Multidimensional Array Matric Operation with Array
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
 Zero - set zero of a polynomial
CH2. ARRAYS.
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
CS100: Discrete structures
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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 };

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 , B(x)=x 4 +10x 3 +3x 2 +1

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

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

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

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.

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

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

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)

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

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

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

Dept. of Computer Eng. & IT, Amirkabir University of Technology Sparse Matrices(Cont’) 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

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();... }

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 col value smArray[0]0 [1]0 [2]1 [3]2 [4]2 [5]3 [6]3 [7]5 row col value (a) (b) Figure 2.3 : Sparse matrix and its transpose stored as triples

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);

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)

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)

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

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

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

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

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)

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)

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

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; }

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;

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; }

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