Arrays and Structures. 2 Data type A data type is a collection of objects and a set of operations that act on those objects.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Linear Lists – Array Representation
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
Presented by : Preeti Banswal. What is data structure? A data structure is a way of organizing data that considers not only the items stored, but also.
CS-240 Data Structures in C Arrays Dick Steflik. Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of.
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.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Chapter 8 Arrays and Strings
Chapter 2 Arrays and Structures Instructors: C. Y. Tang and J. S. Roger Jang All the material are integrated from the textbook "Fundamentals of Data Structures.
CS Data Structures Chapter 2 Arrays and Structures.
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 21 ARRAYS AND STRUCTURES. CHAPTER 22 Arrays Array: a set of index and value data structure For each index, there is a value associated with that.
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 8 Arrays and Strings
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
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],
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.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
 Program data code Data StructureAlgorithm  Data vs. Variables int i = 10; int i[5]={3,5,7,9,10};
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.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
CHAPTER 21 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of.
CCSA 221 Programming in C CHAPTER 7 WORKING WITH ARRAYS 1.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
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. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
◦ A one-dimensional array in C is declared implicitly by appending brackets to the name of a variable int list[5], *plist[5]; ◦ arrays start at index.
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.
CHAPTER 21 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of.
Introduction toData structures and Algorithms
Data Structure Sang Yong Han
Computer Programming BCT 1113
More Linking Up with Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 210 Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CHAPTER 2 ARRAYS AND STRUCTURES
Data Structures 4th Week
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
 Zero - set zero of a polynomial
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
CS111 Computer Programming
Data Structures (CS212D) Week # 2: Arrays.
Data Structure and Algorithms
Arrays Week 2.
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.
Data Structures and Algorithms Unit II
Presentation transcript:

Arrays and Structures

2 Data type A data type is a collection of objects and a set of operations that act on those objects.

Arrays and Structures 3 Abstract data type An abstract data type (ADT) is a data type organized in such a way that the specification of the objects and the specification of the operations on the objects is separated from the representation of the objects and the implementation of the operations.

Arrays and Structures 4 Why ADT? An ADT definition of the object can help us to fast grasp the essential elements of the object.

The array as an ADT

Arrays and Structures 6 Abstract data type of the array An array is a set of pairs,, such that each index that is defined has a value associated with it. An array is usually implemented as a consecutive set of memory locations. Advantageous to random access functions Array Create(j,list); /* return an array of j dimensions where list is a j-turple whose jth element is the size of the ith dimension */ Item Retrieve(index i); Array Store(index i,float x);

Arrays and Structures 7 C array Example: #define MAX_SIZE 100 float array[MAX_SIZE]; index set: 0... MAX_SIZE-1 C does not check an array index to ensure that it is in the range for which the array is defined.

Arrays and Structures 8 Dynamically allocated arrays in C Dynamically allocate an one-dimensional integer array of size n int *p = malloc(sizeof(int)*n); p=realloc(p,s) resizes memory allocated by malloc or calloc changes the size of the memory block pointed at by p to s keeps the contents of the first min{s, oldSize} bytes of the block unchanged. Dynamically allocate a two-dimensional integer array of size mxn int k,**x; x = malloc(sizeof(int*)*m); for(k = 0; k < m; ++k) x[k] = malloc(sizeof(int)*n);

Arrays and Structures 9 Structures A structure is a collection of data items, where each item has its own type and name.

Arrays and Structures 10 Define a structure Create our own structure data type. typedef struct human_being { char name[10]; int age; float salary; }; or typedef struct { char name[10]; int age; float salary; } human_being; Declare varaibles person1 and person2 of type human_being: human_being person1, person2;

Arrays and Structures 11 Define a self-referential structure typedef struct list { int data; list *link; };

Arrays and Structures 12 Ordered (linear) list The ordered list can be written in the form (a 0, a 1, a 2,..., a n-1 ). Empty list: () Operations (1) Find the length, n, of the list. (2) Read the list from left to right (or right to left). (3) Retrieve the ith element. (4) Store a new value into the ith position. (5) Insert a new element at the position i, causing elements numbered i, i+1,..., n-1 to become numbered i+1, i+2,..., n. (6) Delete the element at position i, causing elements numbered i+1,..., n-1 to become numbered i, i+1,..., n-2. The ordered list can be represented by an array. Only operations (5) and (6) require real effort. (5) void insert(int i, int e) { int j; for(j=n; j >i; j--) a[j]=a[j-1]; a[i]=e; } (6) void delete(int i) { int j; for(j=i; j < n-1; j++) a[j]=a[j+1]; }

Polynomials We try to use arrays to represent polynomials

Arrays and Structures 14 Polynomial Examples A(x)=3x 2 +2x+4 B(x)=x 4 +10x 3 +3x 2 +1 The largest exponent of a polynomial is called its degree. The coefficients that are zero are not displayed. The addition and multiplication of two polynomials A(x) and B(x) are defined as

Arrays and Structures 15 ADT Polynomial structure Polynomial //objects: p(x)=a 0 x e 0 +a 1 x e a n x e n ; a set of ordered pairs of // where a i  Coefficient and e i  Exponent functions: Polynomial Zero() Boolean IsZero(poly) Coefficeint Coef(poly, expon); Exponent Lead_Exp(poly); Polynomial Attach(poly,coef,expon) Polynomial Remove(poly,expon) Polynommial SingleMult(poly,coef,expon) Polynomial Add(Polynomial poly); Polynomial Mult(Polynomial Poly); // Evaluate the polynomial *this at f and return the result.

Arrays and Structures 16 Polynomial representation Representation 1 typedef struct { int degree; /* degree < MaxDegree */ float coef[MaxDegree]; } polynomial; polynomial a; a.degree = n; a.coef[i] = a n-i ; Disadvantage: Representation 1 is wasteful in memory. MaxDegr ee a0a0 a n-1 anan coef:

Arrays and Structures 17 Program 2.5: Initial version of padd /* d = a + b */ d = Zero(); while (!IsZero(a) && !IsZero(b)) { switch(COMARE(Lead_Exp(a),Lead_Exp(b)) { case -1: d = Attach(d,Coef(b,Lead_Exp(b)),Lead_Exp(b)); // Lead_Exp(a) < Lead_Exp(b) b = Remove(b,Lead_Exp(b)); break; case 0: sum = Coef(a,Lead_Exp(a))+Coef(b,Lead_Exp(a)); // Lead_Exp(a) == Lead_Exp(b) if (sum) { Attach(d,sum,Lead_Exp(a); } a = Remove(a,Lead_Exp(a)); b = Remove(b,Lead_Exp(b)); break; case 1: d = Attach(d,Coef(a,Lead_Exp(a)),Lead_Exp(a)); // Lead_Exp(a) > Lead_Exp(b) a = Remove(a,Lead_Exp(a)); break; }

A(x)=3x 2 +2x+4 B(x)=x 4 +10x 3 +3x 2 +1 C(x)=A(x)+B(x) coef: A(x) coef: 0 3 B(x) 4210 coef: 3 C(x) Index=0 Cd=2-Index=2 Index=0 Cd=4-Index=4 Index=1 Cd=4-Index=3 Index=2 Cd=4-Index=2 Index=1 Cd=4-Index=1 Index=4 Cd=4-Index=0 Index=1 Cd=2-Index=1 Index=2 Cd=2-Index=0 Index=5 Cd=4-Index=-1 Index=5 Cd=2-Index=-1

Arrays and Structures 19 Polynomial representation (cont ’ d) Representation 2 typedef struct { int degree; float *coef; } polynomial ; polynomial a; a.degree = n; a.coef = (float*)malloc(sizeof(float)*(a.degree+1)); Disadvantage: Representation 2 is wasteful in computer memory if there are many zero coefficients in the polynomial. Such polynomials are called sparse. For instance, A(x)=x

Arrays and Structures 20 Polynomial representation (cont ’ d) Representation 3 typedef struct { float coef; int exp; } polynomial; polynomial terms[MaxTerms]; int avail = 0; int Start, Finish; startafinishastartbfinishbavail coef exp A(x)=100x 4 +x 2 +1 B(x)=x x 100 +x 2 +5 Disadvantage: When all coefficients are all nonzero, Representation 3 uses twice as mush space as Representation 2. Unless we know in advance that each of our polynomials has very few zero terms, Representation 3 is preferable.

Arrays and Structures 21 Program 2.6: Revised version of padd void padd(int starta,int finisha,int startb,int finishb,int *startd,int *finishd) { float c; *startd = avail; while ((starta<=finisha)&&(startb<=finishb)) switch(COMPARE(terms[starta].expon,term[startb].expon)) { case -1:attach(terms[startb].coef,terms[b].expon); startb++; break; case 0:c=terms[starta].coef+terms[startb].coef; if (c) attach(c,terms[starta].expon); starta++; startb++; break; case 1:attach(terms[starta].coef,terms[starta].expon); starta++; break; } for(;starta<=finisha;starta++) attach(terms[starta].coef,terms[starta].expon); for(;startb<=finishb;startb++) attach(terms[startb].coef,terms[startb].expon); *finishd = avail-1; } void attach(float c,int e) { if (avail>=MAX_TERMS) { printf(“Too many terms\n”); exit(1); } terms[avail].coef = c; terms[avail].expon = e; avail++; } void attach(float c,int e) { if (avail>=MAX_TERMS) { printf(“Too many terms\n”); exit(1); } terms[avail].coef = c; terms[avail].expon = e; avail++; }

Arrays and Structures 22 Disadvantages of Representation 3 Disadvantage When some polynomials are no longer needed, some functions would be invoked to make the continuous free space at one end. Alternative approach Each polynomial has its own array of terms. This array can be created dynamically. Disadvantage: This approach requires us to run the addition algorithm twice: one to determine the number of items in the resulting polynomial and the other to actually perform the addition. Can you propose a better solution?

Sparse matrices

Arrays and Structures 24 Introduction A matrix that has many zero entries is called a sparse matrix.

Arrays and Structures 25 ADT SparseMatrix structure Sparse_Matrix { // objects: A set of triples,, where row and column are integers // and form a unique combination; value is also an integer; functions: Sparse_Matrix Create(max_row, max_col); Sparse_Matrix Transpose(a); Sparse_Matrix Add(a, b); Sparse_Matrix Multiply(a, b); };

Arrays and Structures 26 Sparse matrix representation typedef struct { int row, col, value; } term; term a[MAX_TERMS]; The triples are ordered by row and within rows by columns. 2825a[7] 9104a[6] -632a[5] 321a[4] 1111a[3] -1550a[2] 2230a[1] 766a[0] valuecolrow Number of rows Number of columns Number of nonzero terms

transpose rowcolvalue a[0]667 a[1]0322 a[2]05-15 a[3]1111 a[4]123 a[5]23-6 a[6]4091 a[7]5228 rowcolvalue a[0]667 a[1]0491 a[2]1111 a[3]213 a[4]2528 a[5]3022 a[6]32-6 a[7]50-15 transpose The transposed matrix should hold the ordering rule of the entry.

Arrays and Structures 28 Program 2.8: Transpose of a sparse matrix void transpose(term a[], term b[]) { int n, I, j, currentb; n = a[0].value; b[0].row = a[0].col; b[0].col = a[0].row; b[0].value= n; if (n>0) { currentb = 1; for(i = 0; i < b[0].row; i++) for(j = 1; j <= n; j++) if (a[j].col == i) { b[currentb].row = a[j].col; b[currentb].col = a[i].row; b[currentb].value = a[i].value; currentb++; } a[0].col

Arrays and Structures 29 Analysis of Transpose Time complexity: O(elements  columns) for(i = 0; i < a[0].col; i++) for(j = 1; j <= n; j++) if (a[j].col == i) { b[currentb].row = a[j].col; b[currentb].col = a[i].row; b[currentb].value = a[i].value; currentb++; }

Arrays and Structures 30 Program 2.9: fast_transpose void fast_transpose(term a[], term b[]) { int row_terms[MAX_COL]; int starting_pos[MAX_COL]; int i,j, num_cols = a[0].col, num_terms = a[0].value; b[0].row = num_cols; b[0].col = a[0].row; b[0].value = num_terms; if (num_terms > 0) { for(i = 0; i < num_cols;i++) row_terms[i]=0; for(i = 1; i<=num_terms; i++) row_terms[a[i].col]++; starting_pos[0] = 1; for(i=1;i<num_cols;i++) starting_pos[i] = starting_pos[i-1]+row_terms[i-1]; for(i=1; i<=num_terms;i++) { j=starting_pos[a[i].col]; b[j].col = a[i].row; b[j].row = a[i].col; b[j].value = a[i].value; start_pos[a[i].col]++; } Time Complexity: O(elements+columns)

row_terms starting_pos rowcolvalue a[0]667 a[1]0322 a[2]05-15 a[3]1111 a[4]123 a[5]23-6 a[6]4091 a[7]5228 rowcolvalue a[0]667 a[1]0491 a[2]1111 a[3]213 a[4]2528 a[5]3022 a[6]32-6 a[7]50-15 transpose

Arrays and Structures 33 Multiplication of sparse matrices The product of two matrices A and B, A is an mxn matrix and B is an nxp matrix, is an mxp matrix whose ijth element is defined as

Arrays and Structures 34 Multiplication of sparse matrices (cont ’ d) The product of two sparse matrices may no longer be sparse.

Arrays and Structures 35 Program 2.10: sparse matrix multiplication void mmult(term a[], term b[], term d[]) { int i,j, column, total_b = b[0].value; int totald = 0,row_a = a[0].row; int cols_a = a[0].col, total_a = a[0].value; int cols_b = b[0].col,row_begin = 1; int row = a[1].row,sum = 0; int new_b[MAX_TERMS][3]; if (cols_a != b[0].row) { fprintf(stderr,”Imcompatible matrices\n”); exit(1); } fast_transpose(b,new_b); /* set boundary condition */ a[total_a+1].row = rows_a; new_b[total_b+1].row = cols_b; new_b[total_b+1].col = 0; void mmult(term a[], term b[], term d[]) { int i,j, column, total_b = b[0].value; int totald = 0,row_a = a[0].row; int cols_a = a[0].col, total_a = a[0].value; int cols_b = b[0].col,row_begin = 1; int row = a[1].row,sum = 0; int new_b[MAX_TERMS][3]; if (cols_a != b[0].row) { fprintf(stderr,”Imcompatible matrices\n”); exit(1); } fast_transpose(b,new_b); /* set boundary condition */ a[total_a+1].row = rows_a; new_b[total_b+1].row = cols_b; new_b[total_b+1].col = 0; for(i=1; i < total_a;) { column = new_b[1].row; for(j = 1; j <= total_b+1;) { if(a[i].row != row) { storesum(d,&totald,row,column,&sum); i = row_begin; for(;new_b[j].row == column; j++); column = new_b[j].row; } else if (new_b[j].row != column) { storesum(d, &totald, row, column, &sum); i = row_begin; column = new_b[j].row; } else switch(COMPARE(a[i].col, new_b[j].col)) { case -1: i++; break; case 0: sum += (a[i++].value * new_b[j++].value); break; case 1: j++; break; } for(;a[i].row == row; i++); row_begin = i; row = a[i].row; } d[0].row = rows_a; d[0].col = cols_b; d[0].value = totald; for(i=1; i < total_a;) { column = new_b[1].row; for(j = 1; j <= total_b+1;) { if(a[i].row != row) { storesum(d,&totald,row,column,&sum); i = row_begin; for(;new_b[j].row == column; j++); column = new_b[j].row; } else if (new_b[j].row != column) { storesum(d, &totald, row, column, &sum); i = row_begin; column = new_b[j].row; } else switch(COMPARE(a[i].col, new_b[j].col)) { case -1: i++; break; case 0: sum += (a[i++].value * new_b[j++].value); break; case 1: j++; break; } for(;a[i].row == row; i++); row_begin = i; row = a[i].row; } d[0].row = rows_a; d[0].col = cols_b; d[0].value = totald;

Arrays and Structures 36 Program 2.11: Store a matrix term void store_sum(term d[],int *totald,int row,int column,int *sum) { if (*sum) { if (*totald < MAX_TERMS) { *totald++; d[*totald].row = row; d[*totald].col = column; d[*totald].value = *sum; *sum=0; } else { fprintf(stderr,” Number of terms in product exceeds %d\n”,MaxTerms); }

transpose

rowcolvalue a[0]667 a[1]0322 a[2]05-15 a[3]1111 a[4]123 a[5]23-6 a[6]4091 a[7]5228 rowcolvalue nb[0]667 nb[1]0491 nb[2]1111 nb[3]213 nb[4]2528 nb[5]3022 nb[6]32-6 nb[7]50-15 transpose A*A= rowcolvalue d[0]667 d[1]02-15*28 d[2]1111*11 d[3]1211*3 d[4]133*-6 d[5]4391*22 d[6]4591*-15 d[7]5328*-6 * AA

Arrays and Structures 39 Analysis Time complexity: O(max{cols_b+total_b,cols_b  total_a + rows_a  total_b}) = O(cols_b  total_a + rows_a  total_b) cols_b*total_a + rows_a*total_b  cols_b*rows_a*cols_a + rows_a*rows_b*cols_b =cols_b*rows_a*cols_a + rows_a*cols_a*cols_b =O(cols_b*rows_a*cols_a) for(int i = 0; i <a_row; i++) { for(int j = 0; j < b_col; j++) { sum = 0; for(int k = 0; k < a_col; k++) { sum+= a[i][k]*b[k][j]; } c[i][j] = sum; } }// Time complexity O(a_row*a_col*b_col) for(int i = 0; i <a_row; i++) { for(int j = 0; j < b_col; j++) { sum = 0; for(int k = 0; k < a_col; k++) { sum+= a[i][k]*b[k][j]; } c[i][j] = sum; } }// Time complexity O(a_row*a_col*b_col) Classical multiplication algorithm fast_transpose(b,new_b)

Arrays and Structures 40 Disadvantages of representing sparse matrices by arrays Disadvantage Although it is an efficient way to represent all our sparse matrices in one array, some functions would be invoked to make the continuous free space at one end when some matrices are no longer needed. Alternative approach Each sparse matrix has its own array. This array can be created dynamically. Disadvantage: similar to those with the polynomial representation.

Representation of arrays

Arrays and Structures 42 Representation of arrays Multidimensional arrays are usually implemented by storing the elements in a one- dimensional array. If an array is declared A[p 1...q 1 ][p 2...q 2 ]...[p n...q n ] where p i...q i is the range of index values in dimension i, then the number of elements is A[4..5][2..4][1..2][3..4] has 2*3*2*2=24 elements.

Arrays and Structures 43 Row major order Arrays are often represented in row major order. In row major order, the elements of A[4..5][2..4][1..2][3..4] will be stored as A[4][2][1][3],A[4][2][1][4],A[4][2][2][3],A[4][2][2][4],...,A[5] [4][2][4]. See Sec. 2.6 Exercise 3 for column major order.

Arrays and Structures 44 col0col 1col 2...col u 2 -1 row 0 row 1...A[i][j] row u 1 -1 Sequential representation of array declared as A[u 1 ][u 2 ] Location of A[i][j] =  +i*u 2 +j u 2 elements Location of A[0][0]=  For example, char A[ u 1 ][u 2 ];

Arrays and Structures 45 General formula The addressing formula for any element A[i 1 ][i 2 ]...[i n ] in an n-dimensional array declared as A[u 1 ][u 2 ]...[u n ] is

The string ADT

Arrays and Structures 47 ADT String structure String { // objects: A finite ordered set of zero or more characters. functions: String Null(m); Integer Compare(s,t); Boolean IsNull(s); Integer Length(s); String Concat(s,t); String Substr(s, i, j); };

Arrays and Structures 48 String pattern matching Problem. Determine whether string pat is in string s or not.

Arrays and Structures 49 A simple algorithm...gfecba gba s pat...gfecba gba s pat...gfecba gba s pat

Arrays and Structures 50 A simple algorithm (cont ’ d) int simple_find(char*string,char* pat) { int i = 0,len_s=strlen(string),len_p=strlen(pat); char*p = pat; char*s = string; if (*p&&*s) { while(i+len_p<=len_s) { if (*p==*s) { p++; s++; if (!*p) return i; } else { i++; s=string+i; p = pat; } return –1; } Time complexity: O(Length(string)  Length(pat))

Arrays and Structures 51 Program 2.13: Pattern matching by checking end indices first int nfind(char *string,char *pat) { int I,j,start=0; int lasts = strlen(string)-1; int lastp = strlen(pat)-1; int endmatch = lastp; for(i=0; endmatch <=lasts; endmatch++,start++) { if(string[endmatch]==pat[lastp]) { for(j=0,i=start; j < lastp && string[i]==pat[j];i++,j++); if(j==lastp) return start; } return -1; } Time complexity: O(strlen(string)  strlen(pat))

Arrays and Structures 52 The Knuth-Morris-Pratt algorithm sisi s i+1 s i+2 s-ab???....? patabcabcacab sisi s i+1 s i+2 s i+3 s i+4 s-abcac....? patabcabcacab It is unnecessary to move backward in s!! abcab… abcab… The simple algorithm only advances one character It is possible to advance three characters if pat is analyzed in advance.

Arrays and Structures 53 The Knuth-Morris-Pratt algorithm (cont ’ d) Definition. If p=p 0 p 1 p 2...p n-1 is a pattern, then its failure function f is defined as j patabcabcacab f

Arrays and Structures 54 The Knuth-Morris-Pratt algorithm (cont ’ d) = = = ….  ?

Arrays and Structures 55 The Knuth-Morris-Pratt algorithm (cont ’ d) 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 be comparing s i+1 and p 0. j patabcabcacab f abcabcad s j patabcabcacab f

Arrays and Structures 56 Program 2.14:The Knuth- Morris-Pratt algorithm int pmatch(char*string,char*pat) { int PosP = 0,PosS = 0; int LengthP=strlen(pat),LengthS=strlen(string); while((PosP<LengthP)&&(PosS<LengthS)) { if (pat[PosP]==string[PosS]) { PosP++; PosS++; } else { if (PosP==0) PosS++; else PosP=failure[PosP-1]+1; } if (PosP<LengthP) return –1; else return PosS-LengthP; } The time complexity of pmatch is O(LengthS). If the fail function is not known in advance, pattern matching can be carried out in timeO(strlen(string)+strlen(pat)). Execute at most LengthS times

ababc 01 ababc 01 ababc 01 eabcababc string pattern f ababc 01 ababc 01 ababc 01 PosP=0 PosS=0 PosP=0 PosS=1 PosP=1 PosS=2 PosP=2 PosS=3 PosP=0 PosS=3 PosP=0 PosS=4 PosP=1 PosS=5 PosP = f(PosP-1)+1 PosP=2 PosS=6 PosP=3 PosS=7 PosP=4 PosS=8

Arrays and Structures 58 The Knuth-Morris-Pratt algorithm (cont ’ d) The failure function can be equivalently rewritten as j patabcabcacab f Note f(j-1) jf(j-1)+1 =

Arrays and Structures 59 j pataaaaaabbbccc f01234 More examples of failure functions j patabcdabcdabcf f j patabcdabcdabca f f 2 (10)=f(6) f 3 (10)=f 2 (6)=f(2) f(10)  

j patabcdabcdabcf f j patabcdabcdabcf f j patabcdabcdabcf f j patabcdabcdabcf f f(f(10))+1 f(10)+1 f(f(f(10)))+1

Arrays and Structures 61 Program 2.15: Computing the failure function void fail(char *pat) { int i,j,LengthP = strlen(pat); failure[0] = -1; for(j=1;j<LengthP; j++) { i = failure[j-1]; while((pat[j]!=pat[i+1])&&(i>=0)) i=failure[i]; if (pat[j]==pat[i+1]) failure[j] = i+1; else failure[j]=-1; } Time complexity: O(strlen(pat))

Arrays and Structures 62 Summary Representing sparse polynomials and sparse matrices by arrays Storing pairs of Analytic formula for the indexes of nonzero elements e.g. the diagonal marix, tridiagonal matrix, upper/lower triangular matrix,…