Chap 2 Array ( 陣列 ). ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the.

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

Arrays 2008, Fall Pusan National University Ki-Joune Li.
Class operators. class polynomial class polynomialpolynomial class polynomial { protected: int n; double *a; public: polynomial(){}; …..; // constructor.
Arrays and Matrices CSE, POSTECH. 2 2 Introduction Data is often available in tabular form Tabular data is often represented in arrays Matrix is an example.
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
Matrices CSE, POSTECH.
第一次小考解答 張啟中. 1. Compute the Address of Array 由下圖可知,對於陣列元素 A[i][j][k] 而言, 其位址為 α+(i-1)*200 + (j-1)*20 + (k-1) 所以 A[3][7][2] = α = α+521 A[1][1][1]A[1][1][2]A[1][1][3]
Implementing Data Structures
大綱 陣列 結構和聯結 多項式抽象資料型態 稀疏矩陣抽象資料型態 上三角和下三角表示法.
1.1 線性方程式系統簡介 1.2 高斯消去法與高斯-喬登消去法 1.3 線性方程式系統的應用(-Skip-)
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
Multidimensional Array Outline Two-Dimensional Arrays Lengths of Two-Dimensional Arrays Multidimension Arrays [Sample code]
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:
Introduction to pointers in C/C++. Pointers 特殊變數 存放變數在記憶體中的位址 MinGW C++ 中佔用 4 bytes 間接定址取執法 位址 指標變數 變數內容 變數.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.
Digital Signal Processing with Examples in M ATLAB ® Chap 1 Introduction Ming-Hong Shih, Aug 25, 2003.
資料結構實習-一 參數傳遞.
第六章 陣列.
A[0] a[1] pa ???? *pa ppa *ppa Address:4 byte Double:8 byte.
資料型態名稱用途儲存空間儲存值範圍 short 短整數儲存較小的 整數 2 bytes-32,768~32,767 Int 整數儲存整數 16 位元 : 2 bytes 32 位元 : 4 bytes 16 位元 : 32,768~32, 位元 : -2,147,483,648 ~ 2,147,483,647.
本章重點 2-1 有序串列(Ordered List) 2-2 介紹陣列(array) 2-3 矩陣(matrix)的應用
2005/7 Linear system-1 The Linear Equation System and Eliminations.
Introduction to Java Programming Lecture 11 Array II Multidimensional Arrays.
Matrix table of values
資料結構實習-六.
第 8 章 記憶體指標. 8.0 變數、數值、位址 8.1 指標與變數 對 C++ 語言而言,指標( pointers )是存放 變數或陣列的位址,因此也可以藉由指標 間接取得變數或陣列中的值。 對 C++ 語言而言,指標( pointers )是存放 變數或陣列的位址,因此也可以藉由指標 間接取得變數或陣列中的值。
CS Data Structures Chapter 2 Arrays and Structures.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
Introduction to Data Structure
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 3 Visual Basic.Net Visual Basic, like most programming languages, uses variables for storing values. Variables have a name (the word you use to.
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.
MATLAB. What is MATLAB? MATLAB is a simple programming language with its own extensive library of mathematical and graphical subroutines It integrates.
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
WEEK 6 Class Activities Lecturer’s slides.
Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.
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.
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
1. 2  Introduction  Array Operations  Number of Elements in an array One-dimensional array Two dimensional array Multi-dimensional arrays Representation.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
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.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Data Structure Sang Yong Han
The Java Collections Framework
Chapter 3 Linear List (Sequential List)
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Chap 2 Array (陣列).
Data Structures 4th Week
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
CH2. ARRAYS.
Data Structure Assistant Prof. Chih-Chia Yao( C. –C. Yao.
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
Linked List (Part I) Data structure.
CS111 Computer Programming
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

Chap 2 Array ( 陣列 )

ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the set of operations that may be applied to the objects of class –levels of program access : public, protected and private.

Definition of Rectangle #ifndef RECTANGLE_H #define RECTANGLE_H Class Rectangle{ Public: Rectangle(); ~Rectangle(); int GetHeight(); int GetWidth(); Private: int xLow, yLow, height, width; }; #endif

Array A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] Mapping: A[i][j]=A[0][0]+i*4+j A[2][1] char A[3][4];// row-major logical structurephysical structure

Array The Operations on 1-dim Array –Store 將新值寫入陣列中某個位置 A[3] = 45 O(1) A

Polynomial Representations Representation 1 private: int degree;// degree ≤ MaxDegree float coef [MaxDegree + 1]; Representation 2 private: int degree; float *coef; Polynomial::Polynomial(int d) { degree = d; coef = new float [degree+1]; } Representation 3 class Polynomial; // forward delcaration class term { friend Polynomial; private: float coef;// coefficient int exp;// exponent }; private: static term termArray[MaxTerms]; static int free; int Start, Finish; term Polynomial:: termArray[MaxTerms]; Int Polynomial::free = 0;// location of next free location in temArray

Figure 2.1 Array Representation of two Polynomials Represent the following two polynomials: A(x) = 2x B(x) = x x 3 + 3x A.StartA.FinishB.StartB.Finishfree coef exp

Polynomial Addition 只存非零值 (non-zero) :一元多項式 –Add the following two polynomials: A(x) = 2x B(x) = x4 + 10x3 + 3x2 + 1 A_coef A_exp B_coef B_exp C_coef C_exp

Polynomial Addition Polynomial Polynomial:: Add(Polynomial B) // return the sum of A(x) ( in *this) and B(x) { 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++; case ‘>’: NewTerm(termArray[a].coef, termArray[a].exp); a++; } // end of switch and while // add in remaining terms of A(x) for (; a<= Finish; a++) NewTerm(termArray[a].coef, termArray[a].exp); // add in remaining terms of B(x) for (; b<= B.Finish; b++) NewTerm(termArray[b].coef, termArray[b].exp); C.Finish = free – 1; return C; } // end of Add O(m+n)

Program 2.9 Adding a new Term 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(); } termArray[free].coef = c; termArray[free].exp = e; free++; } // end of NewTerm

Disadvantages of Representing Polynomials by Arrays What should we do when free is going to exceed MaxTerms ? –Either quit or reused the space of unused polynomials. But costly. If use a single array of terms for each polynomial, it may alleviate the above issue but it penalizes the performance of the program due to the need of knowing the size of a polynomial beforehand.

Sparse Matrices

Sparse Matrix Representation Use triple Store triples row by row For all triples within a row, their column indices are in ascending order. Must know the number of rows and columns and the number of nonzero elements

Sparse Matrix Representation (Cont.) class SparseMatrix; // forward declaration class MatrixTerm { friend class SparseMatrix private: int row, col, value; }; In class SparseMatrix: private: int Rows, Cols, Terms; MatrixTerm smArray[MaxTerms];

Transposing A Matrix Intuitive way: for (each row i) take element (i, j, value) and store it in (j, i, value) of the transpose More efficient way: for (all elements in column j) place element (i, j, value) in position (j, i, value)

Transposing A Matrix The Operations on 2-dim Array –Transpose for (i = 0; i < rows; i++ ) for (j = 0; j < columns; j++ ) B[j][i] = A[i][j];

Program 2.10 Transposing a Matrix 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) } // end of transpose O(terms*columns)

Fast Matrix Transpose The O(terms*columns) time => O(rows*columns 2 ) when terms is the order of rows*columns A better transpose function in Program It first computes how many terms in each columns of matrix a before transposing to matrix b. Then it determines where is the starting point of each row for matrix b. Finally it moves each term from a to b.

Program 2.11 Fast Matrix Transposing SparseMatrix SparseMatrix::Transpose() // The transpose of a(*this) is placed in b and is found in Q(terms + columns) time. { int *Rows = new int[Cols]; int *RowStart = new int[Rows]; 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; for (i = 0; i < Cols; i++) RowStart[i] = RowStart[i-1] + RowSize[i-1]; O(columns) O(terms) O(columns-1)

Program 2.11 Fast Matrix Transposing (Cont.) for (i = 1; 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 O(terms) O(row * column)

Matrix Multiplication Definition: Given A and B, where A is mxn and B is nxp, the product matrix Result has dimension mxp. Its [i][j] element is for 0 ≤ i < m and 0 ≤ j < p.

Representation of Arrays Multidimensional arrays are usually implemented by one dimensional array via either row major order or column major order. Example: One dimensional array A[0]A[1]A[2]A[3]A[4]A[5]A[6]A[7]A[8]A[9]A[10]A[11] αα+1α+2α+3α+4α+5α+6α+7α+8α+9α+10α+12

Two Dimensional Array Row Major Order XXXX XXXX XXXX Col 0Col 1Col 2Col u Row 0 Row 1 Row u u 2 elements u 2 elements Row 0Row 1 Row u Row i i * u 2 element

Memory mapping char A[3][4];// row-major logical structurephysical structure A[0][0] A[0][1] A[0][2] A[0][3] A[1][0] A[1][1] A[1][2] A[1][3] Mapping: A[i][j]=A[0][0]+i*4+j A[2][1]

The address of elements in 2-dim array – 二維陣列宣告為 A[r][c] ,每個陣列元素佔 len bytes ,且其元第 1 個元素 A[0][0] 的記憶 體位址為  ,並以列為主 (row major) 來儲存 此陣列,則 A[0][3] 位址為  +3*len A[3][0] 位址為  +(3*r+0)*len... A[m][n] 位址為  +(m* r + n)*len –A[0][0] 位址為  ,目標元素 A[m][n] 位址的 算法 Loc(A[m][n]) = Loc(A[0][0]) + [(m  0)*r+(n  0)]* 元素大小

String Usually string is represented as a character array. General string operations include comparison, string concatenation, copy, insertion, string matching, printing, etc. HelloWorld\0

String Matching The Knuth- Morris-Pratt Algorithm Definition: If p = p 0 p 1 …p n-1 is a pattern, then its failure function, f, is defined as If a partial match is found such that s i-j … s i-1 = p 0 p 1 …p j-1 and s i ≠ p j then matching may be resumed by comparing s i and p f(j–1)+1 if j ≠ 0. If j = 0, then we may continue by comparing s i+1 and p 0.

Fast Matching Example Suppose exists a string s and a pattern pat = ‘abcabcacab’, let’s try to match pattern pat in string s. j pat a b c a b c a c a b f s = ‘- a b c a ? ?... ?’ pat = ‘a b c a b c a c a b’ ‘a b c a b c a c a b’ j = 4, p f(j-1)+1 = p 1 New start matching point