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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Data Abstraction and Encapsulation
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
Introduction to Java Programming Lecture 13 Classes I OO Programming.
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
“Rule” By OX. By Check CREATE TABLE 員工薪資 ( 編號 int IDENTITY PRIMARY KEY, 薪資 smallmoney, CHECK ( 薪資 > 0 AND 薪資
大綱 陣列 結構和聯結 多項式抽象資料型態 稀疏矩陣抽象資料型態 上三角和下三角表示法.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
Chapter 2 聯立線性方程式與矩陣 緒言 線性方程式組 (systems of linear equations) 出現 在多數線性模式 (linear model) 中。根據以往解 題的經驗,讀者們也許已發現方程式的解僅與 該方程式的係數有關,求解的過程也僅與係數 的運算有關,只要係數間的相關位置不改變,
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
第一章 演算法:效率、分析與量級 1.1演算法 1.2發展有效率演算法的重要性 1.3演算法的分析 1.4量級(Order)
資料結構實習-四.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
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:
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
Introduction to pointers in C/C++. Pointers 特殊變數 存放變數在記憶體中的位址 MinGW C++ 中佔用 4 bytes 間接定址取執法 位址 指標變數 變數內容 變數.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
第九章 結構. 結構的宣告 結構 它是由許多不同 ( 或相同 ) 資料型態的變數所組成的 集合,通常利用結構標籤稱呼此集合 struct student { char *name; int score; }; struct 為保留字,表示結構的宣告開始 結構項目需定義於大括號「 { } 」內 結尾需加上分號.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Arrays.
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
7 陣列與搜尋 7.1 陣列 一般資料變數 宣告一維陣列 起始一維陣列 7-4
Chapter 20 塑模動態觀點:狀態圖 Statechart Diagram. 學習目標  說明狀態圖的目的  定義狀態圖的基本記號  展示狀態圖的建構  定義活動、內部事件及遞延事件的狀態 圖記號.
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
Digital Signal Processing with Examples in M ATLAB ® Chap 1 Introduction Ming-Hong Shih, Aug 25, 2003.
3.1 矩陣的行列式 3.2 使用基本運算求行列式 3.3 行列式的性質 3.4 特徵值介紹 3.5 行列式的應用
資料結構實習-一 參數傳遞.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
資料結構實習-二.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
第 17 章 樣板 17-1 樣板的基礎 17-1 樣板的基礎 17-2 樣板函數 17-2 樣板函數 17-3 樣板類別 17-3 樣板類別 17-4 非型態參數的樣板類別 17-4 非型態參數的樣板類別 17-5 樣板類別的繼承 17-5 樣板類別的繼承 17-6 特化樣板與靜態成員 17-6 特化樣板與靜態成員.
Chapter 10 m-way 搜尋樹與B-Tree
JAVA 程式設計與資料結構 第十六章 Hash Tables. Introduction Hash Tables 結構為一個 Array ,稱之為 Bucket array 。 如果想要新增一個物件,要根據這個物件的特性 將其加入 Hash Table 內。 Bucket Array 用 A 來代替,其.
本章重點 2-1 有序串列(Ordered List) 2-2 介紹陣列(array) 2-3 矩陣(matrix)的應用
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
2005/7 Linear system-1 The Linear Equation System and Eliminations.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
Visual C++重點複習.
資料結構實習-六.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 參 資料蒐集的方法.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
Microsoft Excel.
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Data Structure (Part I) Chapter 2 – Arrays Data Abstraction and Encapsulation in C++ Section 1.3 –Data Encapsulation Also called information hiding.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
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],
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.
3 - 1 資料型態 Data Types Primitive Data Types Accumulated Data Types - Array - Structures - Union Abstract Data Types Examples.
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.
Chap 2 Array ( 陣列 ). ADT In C++, class consists four components: –class name –data members : the data that makes up the class –member functions : the.
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.
Data Structure Sang Yong Han
CSCE 3110 Data Structures & Algorithm Analysis
Chap 2 Array (陣列).
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
Speaker: Liu Yu-Jiun Date: 2009/4/15
Presentation transcript:

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  2.5 The Representation of Arrays Example  2.3 The Polynomial Abstract Data Type  2.4 The Sparse Matrix Abstract Data Type  2.6 The String Abstract Data Type

定義: Data Type A data type is a collection of objects and a set of operations that act on those objects. 定義: Abstract Data Type An abstract data type(ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations.

資料型態 Data Types Primitive Data Types Accumulated Data Types - Array - Structures - Union Abstract Data Types Examples

Example: Data Type of C++ 表一: C++ 的基本資料型態

Accumulated Data Type Array Struct Union

如何理解 Abstract Data Types ADTs 是一個物件的型態的抽象定義具有一般 化的特性,其中也包含這個物件相關的操作。 Data Structures 課程,旨在瞭解每一個 ADT , 要用什麼樣的結構來表達與儲存,而這樣的表 達與儲存方式,又對於該物件的操作帶來什麼 樣的優缺點。亦即關注在二個重點:  物件儲存與表達方式(資料結構)  物件操作方式(演算法)

The Array as an Abstract Data Type Array  A collection of data of the same type  An array is usually implemented as a consecutive set of memory locations int list[5], *plist[5] ADT definition of an Array  More general structure than "a consecutive set of memory locations.“  An array is a set of pairs,, in mathematical, call correspondence or mapping

class GeneralArray { // objects: A set of pairs where for each value of index in // IndexSet there is a value of type float. IndexSet is a finite ordered set of one // or more dimensions, for example, {0, …, n - 1} for one dimension, {(0, 0), // (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)} for two dimensions, etc. public: GeneralArray(int j, RangeList list, float initValue = defaultValue) ; // The constructor GeneralArray creates a j dimensional array of floats; the // range of the kth dimension is given by the kth element of list. For each // index i in the index set, insert into the array. float Retrieve(index i) ; // if (i is in the index set of the array) return the float associated with i in the // array; else signal an error. void Store(index i, float x) ; // if (i is in the index set of the array) delete any pair of the form // present in the array and insert the new pair ; else signal an error. } ; // end of GeneralArray

Array (陣列) D 陣列是用來存放同樣型態的資料 D 陣列的大小必須在程式中預先設定 D 在程式執行中,陣列的大小無法改變 D 陣列中的資料是透過索引( index )來存取 const int ArraySize = 100; int iArray[ArraySize];

一維陣列和記憶體間的對應 int iArray[100]; m m + 2 m + 4 m + 6 m 假定 sizeof(int) = 2 Memory

多維陣列和記憶體間的對應 int mesh[7][11]; m m + 22 m + 44 m + 66 m + 88 m m 假定 sizeof(int) = 2 Memory

多維陣列的宣告 type array_name[arraySize 1 ] [arraySize n ]; 【範例】 int mesh[7][11]; float cube[6][8][3];

m m + 48 m + 96 m m m m 假定 sizeof(int) = 2

struct 和記憶體間的對應 struct studentType { char Name[20];// 姓名 char Address[30];// 地址 char PhoneNumber[10];// 電話 int Age;// 年齡 char Department[4];// 系別 int Year; // 年級 char Class;// 班級 }; studentType Student1, Student2; m m + 20 m + 50 m + 66 m + 60 m + 62 m + 68 假定 sizeof(int) = 2

Self-Referential Structures One or more of its components is a pointer to itself. typedef struct list { char data; list *link; } list item1, item2, item3; item1.data=‘a’; item2.data=‘b’; item3.data=‘c’; item1.link=item2.link=item3.link=NULL; Construct a list with three nodes item1.link=&item2; item2.link=&item3; malloc: obtain a node abc

union 的宣告 union { field1 declaration ; field2 declaration ; } variable_name ; typedef union { field1 declaration ; field2 declaration ; } type_name ; 【範例】 union { char charValue; int intValue; float floatValue; } dataCell; typedef union { char charValue; int intValue; float floatValue; } dataCellType; 宣告變數宣告型態

union 和記憶體間的對應 union { char charValue;/* 1 byte */ int intValue;/* 2 byte */ float floatValue;/* 4 byte */ } dataCell; m m + 4 typedef struct { char opcode; union { int intValue; char strValue[256]; } data; } instruction; m m m+1 m+5

Example Ordered List Polynomial ADT Sparse Matrix ADT String ADT

Ordered List Examples (MONDAY, TUEDSAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAYY, SUNDAY) (2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, ACE) (1941, 1942, 1943, 1944, 1945) (a1, a2, a3, …, an-1, an) () ordered (linear) list: (item1, item2, item3, …, itemn)

Operations on Ordered List Find the length, n, of the list. Read the items from left to right (or right to left). Retrieve the i’th element. Store a new value into the i’th position. Insert a new element at the position i, causing elements numbered i, i+1, …, n to become numbered i+1, i+2, …, n+1 Delete the element at position i, causing elements numbered i+1, …, n to become numbered i, i+1, …, n-1 array (sequential mapping) ? (1)~(4) O (5)~(6) X

Polynomial (多項式) Addition:Multiplication:

class Polynomial { // objects: p(x) = a 0 xe0 + … + a n xen ; a set of ordered pairs of, where // a i ∈ Coefficient and e i ∈ Exponent // We assume that Exponent consists of integer ≥ 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 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

Polynomial : Representation 1 Representation by Degrees private int degree; // degree  MaxDegree float coef [MaxDegree + 1]; Example: Let A(x)= Σ a i x i, then a.degree= n, a.coef[i]=a n-i,,0<= i<= n

Polynomial : Representation 1 012n-1n n degree coef

Polynomial : Representation 2 Representation by Degrees, but dynamic allocation space private: degree int degree; //degree <= MaxDegree coef float *coef; polynomial::polynomial(int d) { degree= d, coef=new float[degree+1]; }

Polynomial : Representation 3 Representation by Terms. Class polynomial; Class term { Friend polynomial; private: int exp coef float coef; }; In polynomial class In Implement file of polynomial class private: static term termArray[MaxTerms]; // MaxTerms is a constant. // termArray[MaxTerms] shared by all polynomial objects. static int free; int start, finish; term Polynomial::termArray[MaxTerms]; int polynomial::free = 0;

A(X)=2X B(X)=X 4 +10X 3 +3X 2 +1 coef exp A.start A.finish B.start B. finish free Polynomial: Representation 3-1 In general, A.Finish = A.Start + n –1. For zero polynomial, A.Finish = A.Start – 1

Polynomial : Representation k-1 k numTerm coef ? 012k-1 expon ?

The Representations of Polynomials Compare Representation 1Representation 2Representation 3 Advantages Simply algorithms for most operations Save space than R1 Save space when polynomial is sparse than R1, R2 Disadvantages Waste spaces if a.degree << MaxDegree Waste spaces when polynomial is sparse Waste twice space when all terms are nonzero than R2 , and algorithms is complex than R1, R2

操作分析 多項式相加  As Representation 3 , we take O(m+n) at time complexity , if A(x) has m terms, B(x) has n terms. See Book pp 多項式相乘

col1 col2 col3 col4 col5 col6 row0 row1 row2 row3 row4 row5 (a) (b) *Figure 2.3:Two matrices 8/36 6*65*3 15/15 Sparse Matrix sparse matrix data structure?

若以二維陣列的表示法: #define MAX_ROW 100 #define MAX_COL 100 typedef int matrix[MAX_ROW][MAX_COL]; matrix m1, m2; 來儲存稀疏矩陣,那麼,矩陣中含有許多的 0 。有沒有必要 儲存這些 0 呢?有沒有比較節省記憶體空間的另類表示法?

Abstract Data Type Sparse Matrix class SparseMatrix { //objects: a set of triples,, where row and column are integers and // form a unique combination, and value comes from the set item. public: SparseMatrix(int MaxRow, int MaxCol); //create a SparseMatrix that can hold up to MaxItems= MaxRow*MaxCol and whose //maximum row size is MaxRowand whose maximum column size is MaxCol Transpose SparseMatrix Transpose(); // return the matrix produced by interchanging the row and column value of every triple. Add SparseMatrix Add(SparseMatrix b); //if the dimensions of a(*this) and b are the same, return the matrix produced by adding //corresponding items, namely those with identical row and column values. else return //error. Multiply SparseMatrix Multiply(a, b); //if number of columns in a equals number of rows in b return the matrix d produced by //multiplying a by b according to the formula: d[i][j]= Sum(a[i][k](b[k][j]), //where d(i, j) is the (i, j)th element, k=0 ~ ((columns of a) –1) else return error. };

Representation of Sparse Matrix class SparseMatrix; class MatrixTerm { friend class SparseMatrix private: int col, row, value; }; In class SparseMatrix private: int col, row,Terms; MatrixTerm smArray[MaxTerms]; rowcolumns // Note: triples are ordered by row and within rows by columns

rowcolvalue a[0]0015 a[1]0322 a[2]05-15 a[3]1111 a[4]123 a[5]23-6 a[6]4091 a[7] row 0 row 1 row 2 row 3 row 4 row term col

Sparse Matrix 運算分析 轉置 (transpose) 相加 相乘

Class String { //objects: a finite set of zero or more characters. public: String (char *init, int m); // Constructor that initializes *this to string init of length is m. int operator==(string t); // if the string represented by *this equal string t return 1(true) else return 0(false) int operator!(); // if *this is empty then return 1(TRUE); else return 0 (FALSE). Length int Length(); //return the number of characters in *this. Concat String Concat(String t); //return a string whose elements are those of *this followed by those of t. Substr String Substr(int i, int j); //return the string containing j characters of *this at positions i, i+1,..., i+j-1, //if these are valid positions of *this; else return empty string. int Find(String pat); //return an index i such that pat matches the substring of *this that begins at //position i. Return –1 if pat is either empty or not a substring of *this. }; Abstract Data Type String

Pattern Matching Given two strings, string and pat, where pat is a pattern to be searched for in string Two methods  a simple algorithm O(S*P)  O(n 2 )  optimal algorithm (by Knuth-Morris-Pratt) Linear complexity O(S+P)  O(n)

aabpattern ababba a aaa no match ababba a baa match O(n*m) A simple algorithm aabpattern

The Failure Function Definition  If p= p 0 p 1...p n-1 is a pattern, then its failure function, f is defined as: f(j) = largest i = 0 = -1, otherwise Example j pat a b c a b c a c a b f

Rule for Optimal Pattern Matching 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, continue by comparing s i+1 and p 0 string a b c a ? ?... ? pat a b c a b c a c a b f Continue here

Example of Optimal Pattern Matching j pat a b c a b c a c a b f str c b a b c a a b c a b c a b c a c a b