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.

Slides:



Advertisements
Similar presentations
CSNB143 – Discrete Structure
Advertisements

Chapter Matrices Matrix Arithmetic
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
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
Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
Matrices CSE, POSTECH.
Maths for Computer Graphics
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
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:
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Ch 7.2: Review of Matrices For theoretical and computation reasons, we review results of matrix theory in this section and the next. A matrix A is an m.
Matrix table of values
Matrices MSU CSE 260.
Intro to Matrices Don’t be scared….
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.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Matrices Write and Augmented Matrix of a system of Linear Equations Write the system from the augmented matrix Solve Systems of Linear Equations using.
ECON 1150 Matrix Operations Special Matrices
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
C. – C. Yao Data Structure. C. – C. Yao Chap 1 Basic Concepts.
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],
Matrices CHAPTER 8.1 ~ 8.8. Ch _2 Contents  8.1 Matrix Algebra 8.1 Matrix Algebra  8.2 Systems of Linear Algebra Equations 8.2 Systems of Linear.
Lecture 7 Matrices CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Matrices. Definitions  A matrix is an m x n array of scalars, arranged conceptually as m rows and n columns.  m is referred to as the row dimension.
Chapter 4 – Matrix CSNB 143 Discrete Mathematical Structures.
Company LOGO Chapter 1: Matrix, Determinant, System of linear equations Module 1: Matrix Natural Science Department Example 1: The following rectangular.
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.
Data Structure Introduction.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
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.
CSCI 171 Presentation 9 Matrix Theory. Matrix – Rectangular array –i th row, j th column, i,j element –Square matrix, diagonal –Diagonal matrix –Equality.
Chapter 2 … part1 Matrices Linear Algebra S 1. Ch2_2 2.1 Addition, Scalar Multiplication, and Multiplication of Matrices Definition A matrix is a rectangular.
CSNB143 – Discrete Structure Topic 3 – Matrices. Learning Outcomes Students should understand all matrices operations. Students should be able to differentiate.
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.
1.3 Matrices and Matrix Operations. A matrix is a rectangular array of numbers. The numbers in the arry are called the Entries in the matrix. The size.
Matrices Digital Lesson. Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 A matrix is a rectangular array of real numbers. Each entry.
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
Linear Algebra Chapter 2 Matrices.
Chap 2 Array ( 陣列 ). ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the.
Section 2.4. Section Summary  Sequences. o Examples: Geometric Progression, Arithmetic Progression  Recurrence Relations o Example: Fibonacci Sequence.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
CS 285- Discrete Mathematics Lecture 11. Section 3.8 Matrices Introduction Matrix Arithmetic Transposes and Power of Matrices Zero – One Matrices Boolean.
2.1 Matrix Operations 2. Matrix Algebra. j -th column i -th row Diagonal entries Diagonal matrix : a square matrix whose nondiagonal entries are zero.
Matrices Introduction.
MTH108 Business Math I Lecture 20.
Data Structure Sang Yong Han
The Euclidean Algorithm
MATRICES.
Discrete Structures – CNS2300
CHAPTER 2 MATRICES Determinant Inverse.
1.5 Matricies.
CSCE 3110 Data Structures & Algorithm Analysis
Chap 2 Array (陣列).
Data Structures 4th Week
Matrix Multiplication
CS 213: Data Structures and Algorithms
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
2. Matrix Algebra 2.1 Matrix Operations.
Matrices Introduction.
Determinant of a Matrix
3.6 Multiply Matrices.
Arrays and Matrices Prof. Abdul Hameed.
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

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. A 5×3 matrix has 15 entries. entry

Matrix Suppose a m×n matrix A. If m = n, we call the matrix square. We use two-tuple index to locate a matrix entry. –Example: A(3, 2): entry locating at row 3, column 2. The matrix is square. It has 4 rows and 4 columns. A(3,2) =0 Col 0Col 1Col 2Col 3 Row 0 Row 1 Row 2 Row 3 In C++, it is natural to store a matrix in a 2D array, say a[m][n], and use a[i][j] to access the entry at (i, j).

Matrix Operations Transposition –Suppose A is a m×n matrix. –A T is called the transpose of A where A T is n×m and A T (i, j) = A(j, i) for i = 0, …., m-1, and j = 0, …, n-1. –Example: A(0, 2) = A T (2, 0) = 5

Matrix Operations Addition: –Suppose A is a R A × C A matrix and B a R B × C B matrix. –To sum up A and B, R A must equal to R B ; C A must equal to C B. –If C = A + B, then C(i, j) = A(i, j) + B(i, j) for i=0, …, R A -1, j=0, …, C A -1. –Example: 3 + 0

Matrix Operations Multiplication –Suppose A is a R A ×C A matrix and B a R B ×C B matrix. –To compute A×B, C A must equal to R B. If C = A×B, C is a R A ×C B matrix, where C(i, j) = Σ(A(i, k) . A(k, j)). –Example: C(2, 1) Row 2 Col 1

Sparse Matrix A sparse matrix is a matrix that has many zero entries. This is a __×__ matrix. There are _____ entries. There are _____ nonzero entries. There are _____ zero entries. Consider we use a 2D array to represent a n×n sparse matrix. How many entries are required? _____ entries. The space complexity is O( ).

2.4 Sparse Matrix If n is large, say n = 5000, we need 25 million elements to store the matrix. 25 million units of time for operation such as addition and transposition. Using a representation that stores only the nonzero entries can reduce the space and time requirements considerably.

ADT SparseMatrix class SparseMatrix { public: //r rows, c columns, a capacity of t nonzero entries. SparseMatrix(int r, int c, int t); //interchange the row and column value of every tuple in *this SparseMatrix &Transpose(); //If the dimensions of *this and b are the same, sum up a*this and b. //Otherwise, throw exception. SparseMatrix &Add(SparseMatrix &b); //If the number of columns in *this equals to the number of rows in b, //compute and return the multiplication of *this and b. SparseMatrix &Multiply(SparseMatrix &b); };

2.4.2 Sparse Matrix Representation The information we need to know –The number of rows –The number of columns –The number of nonzero entries –All the nonzero entries are stored in an array. Therefore, we also have to know The capacity of the array Each element contains a triple to store. –The triples are ordered by rows and within rows by columns.

R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: Sparse Matrix Representation Example

2.4.2 Sparse Matrix Representation class SparseMatrix; class MatrixEntry { friend class SparseMatrix; private: int row, col, value; }; class SparseMatrix { private: int rows, cols, terms, capacity; MatrixEntry *smArray; };

R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: Transposing a Matrix Sparse matrix and its transpose smArray R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 3 C: 0 22 R: 5 C: R: 1 C: 1 11 R: 2 C: 1 3 R: 3 C: 2 -6 R: 0 C: 4 91 R: 2 C: 5 28 smArray Consider column 0. For all elements in column 0 Store (i, 0, value) of the original matrix as (0, i, value) of the transpose R: 0 C: 0 15 R: 4 C: 0 91

Algorithm of Program 2.10 SparseMatrix SparseMatrix::Transpose() { 1Construct a SparseMatrix, b(cols, rows, terms), as output result; 2currentB = 0; 3If (terms > 0) { 4 For (c = 0; c < cols; c++) 5 For (i=0; i < terms; i++) 6 If (smArray[i].col == c) { 7 b.smArray[currentB].row = smArray[i].col; 8 b.smArray[currentB].col = smArray[i].row; 9 b.smArray[currentB++].value = smArray[i].value; 10 } 11} 12Return b; }

R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: Algorithm of Program smArray R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 b.smArray currentB Line 1: Construct a SparseMatrix, b(cols, rows, terms), as output result;Line 2: currentB = 0;Line 3: If (terms > 0) Line 4: For (c = 0; c < cols; c++)c = 0 Line 5: For (i=0; i < terms; i++) Line 6: If (smArray[i].col == )c0 R: 0 C: R: 0 C: 0 R: 0 C: 0 15 R: 0 C: R: 0 C: 4 R: 0 C: 4 91 Line 7: b.smArray[currentB].row = smArray[i].col;Line 8: b.smArray[currentB].col = smArray[i].row;Line 9: b.smArray[currentB++].value = smArray[i].value; c = 1 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: 0 -15

Analysis of Transpose() Space complexity: –We need an extra SparseMatrix to buffer the entries. –O(terms) Time complexity: –Line 1 to 2: constant time. –Line 4 to 10: cols iterations. For each iteration, every term is checked through Line 5 to 10. Line 6 is executed exactly terms times. –Line 12: constant time. –Overall, time complexity is O(cols . terms).

Considerations In worst case, the matrix contains rows . cols entries. Therefore, the time complexity becomes O(cols . terms) = O(rows . cols 2 ) –worse than O(rows . cols) time using the simple form! Why do we need the for-loop at Line 5? –Because the transposed smArray has to be ordered by columns. –If using a little more space, we can transpose a matrix in O(terms + cols).

How to locate the entry in transposed smArray? R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 R: 0 C: 0 15 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: ? R: 0 C: 0 15 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 Col: rowStart: rowSize: 1

Algorithm of Program 2.11 SparseMatrix SparseMatrix::FastTranspose() { 1.Construct a SparseMatrix, b(cols, rows, terms), as output result; 2.If (terms > 0) { 3. Let rowSize be an integer array of size cols. 4. Let rowStart be an integer array of size cols. 5. Initialize each element in rowSize to For (i=0; i<terms; i++) 7. rowSize [smArray[i].col]++; 8. rowStart [0] = 0; 9. For (i = 1; i < cols; i++) 10. rowStart [i] = rowSize [i-1] + rowStart [i-1]; 11. For (i=0; i < terms; i++) { 12. j = rowStart [ smArray [i].col ]; 13. Copy smArray[ i ] to smArray[ j ]; 14. Interchange row and col of smArray[ j ]; 15. rowStart [ smArray [i].col ]++; 16. } 17.} 18.Return b; }

R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 R: 0 C: 0 15 R: 3 C: 0 22 R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 Col: rowSize: rowStart: i R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 2 -6 R: 5 C: rowStart: 8

Analysis of FastTranspose() Space complexity: –SparseMatrix b to buffer the entries: O(terms). –rowSize: cols elements; O(cols). –rowStart: cols elements; O(cols). –Overall, O(terms+cols). Time complexity: –Line 5: O(cols) to initialize rowSize. –Line 6: O(terms) to scan all terms in smArray. –Line 9: O(cols) to compute rowStart. –Line 11 to 16: the loop executes terms times. In each iteration, Line 12 to 15 runs in constant time. Therefore, time complexity is O(terms). –Overall, time complexity of FastTranspose() is O(terms+cols).

Analysis of FastTranspose() The time of O(terms+cols) becomes O(rows . cols) when terms equals to rows . cols. –The same complexity with the simple form. –However, the actual computation time of FastTranspose will be a bit larger. When terms is sufficiently small, FastTranspose will be faster.

2.4.4 Matrix Multiplication Definition: –Given two matrices, a mxn and b nxp. The product matrix d has dimension m x p. Its (i, j) element is

2.4.4 Matrix Multiplication The product of two sparse matrices may no longer be sparse. We would like to multiply two sparse matrices represented as ordered lists.

2.4.4 Matrix Multiplication We have to find all the elements in column j. Scan all the elements in smArray? Exhausted! We can compute the transpose of b. This puts all column elements in consecutive order. i j

StoreSum() and ChangeSize() ChangeSize1D(int newSize) –Program 2.13 –To change the size of smArray to newSize. StoreSum(int sum, int r, int c) –Program 2.12 –To store sum in the row of r and the column of c if sum is nonzero. –Invoke ChangeSize1D when smArray is full.

Program 2.14 R: 0 C: 0 15 R: 0 C: 4 91 R: 1 C: 1 11 R: 2 C: 1 3 R: 2 C: 5 28 R: 3 C: 0 22 R: 3 C: 2 -6 R: 5 C: R: 0 C: 0 R: 5 C: 2 R: 0 C: 5 R: 1 C: 1 R: 1 C: 2 R: 2 C: 3 R: 4 C: 0 R: 0 C: R: 0 C: 0 15 R: 0 C: 3 22 R: 0 C: R: 1 C: 1 11 R: 1 C: 2 3 R: 2 C: 3 -6 R: 4 C: 0 91 R: 5 C: 2 28 currColIndexcurrRowIndex R: 6 C: -1 dummy currRowA currColB d:d: currRowBegin Sum = Sum = 0