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.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
Chapter 4 Lists Pointers Singly Linked Lists
Chapter Matrices Matrix Arithmetic
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
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
Arrays and Structures. 2 Data type A data type is a collection of objects and a set of operations that act on those objects.
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 Basic Linear Algebra
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
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:
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
CS Data Structures Chapter 4 Lists.
CHAPTER 1 BASIC CONCEPT All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures.
CS Data Structures Chapter 2 Arrays and Structures.
Systems of Linear Equation and Matrices
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
Presentation by: H. Sarper
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
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 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.
Data Structures – Week #1 Introduction. February 17, 2012Borahan Tümer, Ph.D.2 Goals We will learn methods of how to (explicit goal) organize or structure.
Polynomial Addition using Linked lists
Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
 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.
מערכים (arrays) 02 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 1502 דצמבר דצמבר דצמבר 15 1 Department of Computer Science-BGU.
Arrays and Pointers1 Arrays of Arrays: We can have arrays of any type, even arrays whose elements are themselves arrays. With two bracket pairs, we obtain.
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
POINTERS.
CHAPTER 21 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of.
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.
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.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
1 Objective To provide background material in support of topics in Digital Image Processing that are based on matrices and/or vectors. Review Matrices.
◦ 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.
Programming Application in Civil Engineering
CHAPTER 21 ARRAYS AND STRUCTURES All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of.
Data Structure Sang Yong Han
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CHAPTER 2 ARRAYS AND STRUCTURES
Data Structures 4th Week
Data Structures 7th Week
Abstract Data Types Sparse Matrices CSCI 240
Data Structures Unit-2 Engineered for Tomorrow CSE, MVJCE.
 Zero - set zero of a polynomial
Instructor: Mr.Vahidipour
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Programming Techniques
Data Structure and Algorithms
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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 in C" and some supplement from the slides of Prof. Hsin-Hsi Chen (NTU).

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory.

Abstract Data Type Array Structure Array is objects : a set of pairs where for each value of index there is a value from the set item. functions: Array Create (j, list) : an array of j dimensions, where list is a j-tuple whose ith element is the size of the ith dimension. Item Retrieve (A, i) : If (i ε index) return the item indexed by i in array A, else return error. Array Store (A, i, x) : If (i ε index) insert and return the new array, else return error. end Array

Implementation in C int list[5], *plist[5]; VariableMemory Address list[0]base address = α list[1]α + sizeof(int) list[2]α + 2*sizeof(int) list[3]α + 3*sizeof(int) list[4]α + 4*sizeof(int) The names of the five array elements Assume the memory address α = 1414, list[0] = 6, list[2] = 8, plist[3] = list = 1414 printf( “ %d ”, list); // 1414, the variable “ list ” is the pointer to an int printf( “ %d ”, &list[0]) // 1414, return the address using “ & ” printf( “ %d ”, list[0]); // 6, the value stored in the address “ &list[0] ” printf( “ %d ”, (list + 2)); // *sizeof(int), “ (list +2) ” is a pointer printf( “ %d ”, *(list + 2)); // 8 printf( “ %d ”, plist[3]); // 1414, the value of a pointer is an address printf( “ %d ”, *plist[3]); // 6

Example: One-dimensional array accessed by address call print1(&one[0], 5) void print1(int *ptr, int rows) { /* print out a one-dimensional array using a pointer */ int i; printf(“Address Contents\n”); for (i=0; i < rows; i++) printf(“%8u%5d\n”, ptr+i, *(ptr+i)); printf(“\n”); } int one[] = {0, 1, 2, 3, 4}; Goal: print out address and value

Example: Array program #define MAX_SIZE 100 float sum(float [], int); float input[MAX_SIZE], answer; int i; void main (void) { for (i = 0; i < MAX_SIZE; i++) input[i] = i; answer = sum(input, MAX_SIZE); printf("The sum is: %f\n", answer); } float sum(float list[], int n) { int i; float tempsum = 0; for (i = 0; i < n; i++) tempsum += list[i]; return tempsum; } Result : The sum is:

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

Structures (Records) struct { char name[10]; // a name that is a character array int age; // an integer value representing the age of the person float salary; // a float value representing the salary of the individual } person; strcpy(person.name, “ james ” ); person.age=10; person.salary=35000; An alternate way of grouping data that permits the data to vary in type. Example:

Create 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; human_being person1, person2;

Example: Embed a structure within a structure typedef struct { int month; int day; int year; } date; typedef struct human_being { char name[10]; int age; float salary; date dob; }; person1.dob.month = 2; person1.dob.day = 11; person1.dob.year = 1944;

Unions Similar to struct, but only one field is active. Example: Add fields for male and female. typedef struct sex_type { enum tag_field {female, male} sex; union { int children; boolean beard; } u; }; typedef struct human_being { char name[10]; int age; float salary; date dob; sex_type sex_info; } human_being person1, person2; person1.sex_info.sex = male; person1.sex_info.u.beard = FALSE; person2.sex_info.sex = female; person2.sex_info.u.children = 4;

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; abc Construct a list with three nodes item1.link=&item2; item2.link=&item3; malloc: obtain a node

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

Implementation on other data types Arrays are not only data structures in their own right, we can also use them to implement other abstract data types. Some examples of the ordered (linear) list : - Days of the week: (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday). - Values in a deck of cards: (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) - Floors of a building: (basement, lobby, mezzanine, first, second) Operations on lists : - Finding the length, n, of the list. - Reading the items from left to right (or right to left). - Retrieving the ith element from a list, 0 ≦ i < n. - Replacing the ith item of a list, 0 ≦ i < n. - Inserting a new item in the ith position of a list, 0 ≦ i < n. - Deleting an item from the ith position of a list, 0 ≦ i < n.

Abstract data type Polynomial Structure Polynomial is objects: ; a set of ordered pairs of where a i in Coefficients and e i in Exponents, e i are integers >= 0 functions: for all poly, poly1, poly2  Polynomial, coef  Coefficients, expon  Exponents Polynomial Zero( ) ::= return the polynomial, p(x) = 0 Boolean IsZero(poly) ::= if (poly) return FALSE else return TRUE Coefficient Coef(poly, expon) ::= if (expon  poly) return its coefficient else return Zero Exponent Lead_Exp(poly) ::= return the largest exponent in poly Polynomial Attach(poly,coef, expon) ::= if (expon  poly) return error else return the polynomial poly with the term inserted Polynomial Remove(poly, expon) ::= if (expon  poly) return the polynomial poly with the term whose exponent is expon deleted else return error Polynomial SingleMult(poly, coef, expon) ::= return the polynomial poly coef x expon Polynomial Add(poly1, poly2) ::= return the polynomial poly1 +poly2 Polynomial Mult(poly1, poly2) ::= return the polynomial poly1 poly2 End Polynomial degree : the largest exponent of a polynomial

Two types of Implementation for the polynomial ADT Type I : #define MAX_DEGREE 1001 typedef struct { int degree; float coef[MAX_DEGREE]; } polynomial; polynomial a; a.degree = n a. coef[i] = a n-i, 0 ≦ i ≦ n. Type II : MAX_TERMS 1001 /* size of terms array */ typedef struct { float coef; int expon; } polynomial; polynomial terms[MAX_TERMS]; int avail = 0; /* recording the available space*/ starta finish a startbfinishbavail coef exp advantage: easy implementation disadvantage: waste space when sparse storage requirements: start, finish, 2*(finish-start+1) nonparse:twice as much as Type I when all the items are nonzero P A =2x P B =x 4 +10x 3 +3x 2 +1 Poly A coef1…002 exp (i n d ex) 0… Poly B coef103101…0 exp (i n d ex) 01234… 100 0

Polynomials adding of Type I /* d =a + b, where a, b, and d are polynomials */ d = Zero( ) while (! IsZero(a) && ! IsZero(b)) do { switch COMPARE (Lead_Exp(a), Lead_Exp(b)) { case -1: d = Attach(d, Coef (b, Lead_Exp(b)), Lead_Exp(b)); b = Remove(b, Lead_Exp(b)); break; case 0: sum = Coef (a, Lead_Exp (a)) + Coef ( b, 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)); a = Remove(a, Lead_Exp(a)); } } insert any remaining terms of a or b into d // case -1: Lead_exp(a) < Lead_exp(b) // case 0: Lead_exp(a) = Lead_exp(b) // case 1: Lead_exp(a) < Lead_exp(b)

Polynomials adding of Type II void padd (int starta, int finisha, int startb, int finishb,int * startd, int *finishd) { /* add A(x) and B(x) to obtain D(x) */ float coefficient; *startd = avail; while (starta <= finisha && startb <= finishb) switch (COMPARE(terms[starta].expon, terms[startb].expon)) { case -1: /* a expon < b expon */ attach(terms[avail], terms[startb].coef, terms[startb].expon); startb++; avail++; break; case 0: /* equal exponents */ coefficient = terms[starta].coef + terms[startb].coef; if (coefficient) attach (terms[avail], coefficient, terms[starta].expon); starta++; startb++; avail++; break; case 1: /* a expon > b expon */ attach(terms[avail], terms[starta].coef, terms[starta].expon); starta++; avail++; } /* add in remaining terms of A(x) */ for( ; starta <= finisha; starta++) { attach(terms[avail], terms[starta].coef, terms[starta].expon); avail++; } /* add in remaining terms of B(x) */ for( ; startb <= finishb; startb++) { attach(terms[avail], terms[startb].coef, terms[startb].expon); avail++; } *finishd =avail -1; }

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

The sparse matrix abstract data type Matrix a Nonzero rate : 15/15 Dense ! col_0col_1col_2 row_ row_ row_ row_31289 row_ col_ 0 col_ 1 col_ 2 col_ 3 col_ 4 col_ 5 row_ row_ row_ row_ row_ row_ Matrix b Nonzero rate : 8/36 Sparse !

Abstract data type Sparse_Matrix Structure Sparse_Matrix is objects: a set of triples,, where row and column are integers and form a unique combination, and value comes from the set item. functions: for all a, b  Sparse_Matrix, x  item, i, j, max_col, max_row  index Sparse_Marix Create(max_row, max_col) ::= return a Sparse_matrix that can hold up to max_items = max _row  max_col and whose maximum row size is max_row and whose maximum column size is max_col. Sparse_Matrix Transpose(a) ::= return the matrix produced by interchanging the row and column value of every triple. Sparse_Matrix Add(a, b) ::= if the dimensions of a and b are the same return the matrix produced by adding corresponding items, namely those with identical row and column values. else return error Sparse_Matrix 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] =  (a[i][k] b[k][j]) where d (i, j) is the (i,j)th element else return error.

Sparse_Matrix Create and transpose #define MAX_TERMS 101 /* maximum number of terms +1 */ typedef struct { int col; int row; int value; } term; term a[MAX_TERMS] for each row i (or column j) take element and store it in element of the transpose. rowcol valu e a[0]668 [1]0015 [2]0322 [3]05-15 [4]1111 [5]123 [6]23-6 [7]4091 [8]5228 rowcol valu e b[0]668 [1]0015 [2]0491 [3]1111 [4]213 [5]2528 [6]3022 [7]32-6 [8]50-15 Sparse matrix and its transpose stored as triples #(rows) #(columns) #(nonzero entries) any element within a matrix using the triple Difficulty: what position to put ?

Transpose of a sparse matrix in O(columns*elements) void transpose (term a[], term b[]) /* b is set to the transpose of a */ { int n, i, j, currentb; n = a[0].value; /* total number of elements */ b[0].row = a[0].col; /* rows in b = columns in a */ b[0].col = a[0].row; /*columns in b = rows in a */ b[0].value = n; if (n > 0) { /*non zero matrix */ currentb = 1; for (i = 0; i < a[0].col; i++) /* transpose by columns in a */ for( j = 1; j <= n; j++) /* find elements from the current column */ if (a[j].col == i) { /* element is in current column, add it to b */ b[currentb].row = a[j].col; b[currentb].col = a[j].row; b[currentb].value = a[j].value; currentb++ } } } Scan the array “columns” times. The array has “elements” elements. ==> O(columns*elements)

Analysis and improvement Discussion: compared with 2-D array representation O(columns*elements) vs. O(columns*rows) #(elements)  columns * rows, when the matrix is not sparse. O(columns*elements)  O(columns*columns*rows) Problem: Scan the array “ columns ” times. Improvement: Determine the number of elements in each column of the original matrix. => Determine the starting positions of each row in the transpose matrix.

Transpose of a sparse matrix in O(columns + elements) void fast_transpose(term a[ ], term b[ ]) { /* the transpose of a is placed in b */ int row_terms[MAX_COL], 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){ /*nonzero matrix*/ 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].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; } } } O(num_cols) O(num_terms) O(num_cols-1) O(num_terms) O(columns + elements)

Sparse matrix multiplication void mmult (term a[ ], term b[ ], term d[ ] ) /* multiply two sparse matrices */ { int i, j, column, totalb = b[].value, totald = 0; int rows_a = a[0].row, cols_a = a[0].col, totala = a[0].value; int cols_b = b[0].col, int row_begin = 1, row = a[1].row, sum =0; int new_b[MAX_TERMS][3]; if (cols_a != b[0].row){ fprintf (stderr, “ Incompatible matrices\n ” ); exit (1); } fast_transpose(b, new_b); /* set boundary condition */ a[totala+1].row = rows_a; new_b[totalb+1].row = cols_b; new_b[totalb+1].col = 0; for (i = 1; i <= totala; ) { column = new_b[1].row; for (j = 1; j <= totalb+1;) { /* mutiply row of a by column of b */ 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: /* go to next term in a */ i++; break; case 0: /* add terms, go to next term in a and b */ sum += (a[i++].value * new_b[j++].value); break; case 1: /* advance to next term in b*/ j++ } } /* end of for j <= totalb+1 */ for (; a[i].row == row; i++) ; row_begin = i; row = a[i].row; } /* end of for i <=totala */ d[0].row = rows_a; d[0].col = cols_b; d[0].value = totald; }

storesum function void storesum(term d[ ], int *totald, int row, int column, int *sum) { /* if *sum != 0, then it along with its row and column position is stored as the *totald+1 entry in d */ if (*sum) if (*totald < MAX_TERMS) { d[++*totald].row = row; d[*totald].col = column; d[*totald].value = *sum; } else { fprintf(stderr, ” Numbers of terms in product exceed %d\n ”, MAX_TERMS); exit(1); } }

Analyzing the algorithm cols_b * termsrow 1 + totalb + cols_b * termsrow 2 + totalb + … + cols_b * termsrow rows_a + totalb = cols_b * (termsrow 1 + termsrow 2 + … + termsrow rows_a ) + rows_a * totalb = cols_b * totala + row_a * totalb O(cols_b * totala + rows_a * totalb)

Compared with classic multiplication algorithm for (i =0; i < rows_a; i++) for (j=0; j < cols_b; j++) { sum =0; for (k=0; k < cols_a; k++) sum += (a[i][k] *b[k][j]); d[i][j] =sum; } O(rows_a * cols_a * cols_b) mmult vs. classic O(cols_b * totala + rows_a * totalb) vs. O(rows_a * cols_a * cols_b)

Matrix-chain multiplication n matrices A 1, A 2, …, A n with size p 0  p 1, p 1  p 2, p 2  p 3, …, p n-1  p n To determine the multiplication order such that # of scalar multiplications is minimized. To compute A i  A i+1, we need p i-1 p i p i+1 scalar multiplications. e.g. n=4, A 1 : 3  5, A 2 : 5  4, A 3 : 4  2, A 4 : 2  5 ((A 1  A 2 )  A 3 )  A 4, # of scalar multiplications: 3 * 5 * * 4 * * 2 * 5 = 114 (A 1  (A 2  A 3 ))  A 4, # of scalar multiplications: 3 * 5 * * 4 * * 2 * 5 = 100 (A 1  A 2 )  (A 3  A 4 ), # of scalar multiplications: 3 * 5 * * 4 * * 2 * 5 = 160

Let m(i, j) denote the minimum cost for computing A i  A i+1  …  A j Computation sequence : Time complexity : O(n 3 ) Matrix-chain multiplication (cont.)

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

Representation of multidimensional arrays The internal representation of multidimensional arrays requires more complex addressing formulas. a[upper 0 ] [upper 1 ] … [upper n-1 ] => #(elements of a) = There are two ways to represent multidimensional arrays: row major order and column major order. (row major order stores multidimensional arrays by rows) Ex. A[upper 0 ][upper 1 ] : upper 0 rows (row 0, row 1, …,row upper0-1 ), each row containing upper 1 elements Assume that α is the address of A[0][0]  the address of A[i][0] : α+ i *upper 1  A[i][j] : α+ i *upper 1 +j row major : column major :

Representation of Multidimensional Arrays (cont.) n dimensional array addressing formula for A[i 0 ][i 1 ] … [i n-1 ] : Assume that the address of A[0][0] … [0] is α.  the address of A[i 0 ][0][0] … [0] : α + i 0 *upper 1 *upper 2 * … *upper n-1  A[i 0 ][i 1 ][0] … [0] : α + i 0 *upper 1 *upper 2 * … *upper n-1 + i 1 *upper 2 *upper 3 * … *upper n-1  A[i 0 ][i 1 ][i 2 ] … [i n-1 ] : α + i 0 *upper 1 *upper 2 * … *upper n-1 + i 1 *upper 2 *upper 3 * … *upper n-1 + i 2 *upper 3 *upper 4 * … *upper n-1 ﹕ + i n-2 *upper n-1 + i n-1

Outline The array as an abstract data type Structures and unions The polynomial abstract data type The sparse matrix abstract data type Representation of multidimensional arrays The string abstract data type

Structure String is objects : a finite set of zero or more characters. functions : for all s, t  String, i, j, m  non-negative integers String Null(m) ::= return a string whose maximum length is m characters, but initially set to NULL. We write NULL as “”. Integer Compare(s, t) ::= if s equals t return 0 else if s precedes t return -1 else return +1 Boolean IsNull(s) ::= if (Compare(s, NULL)) return FALSE else return TRUE Integer Length(s) ::= if (Compare(s, NULL)) return the number of characters in s else return 0 String Concat(s, t) ::= if (Compare(t, NULL)) return a string whose elements are those of s followed by those of t else return s String Subset(s, i, j) ::= if ((j>0) && (i+j-1)<Length(s)) return the string containing the characters of s at position i, i+1, …, i+j-1. else return NULL.

C string functions

String insertion function void strnins(char *s, char *t, int i) { /* insert string t into string s at position i */ char string[MAX_SIZE], *temp = string; if (i strlen(s)) { fprintf(stderr, ” Position is out of bounds \n ” ); exist(1); } if (!strlen(s)) strcpy(s, t); else if (strlen(t)) { strncpy(temp, s, i); strcat(temp, t); strcat(temp, (s+i)); strcpy(s, temp); } s→amobile \0\0 t→uto\0 temp→\0 initially temp→a\0 (a) after strncpy (temp, s, i) temp→auto\0 (b) after strcat (temp, t) temp→automobile\0 (c) after strcat (temp, (s+i)) Example: Never be used in practice, since it ’ s wasteful in use of time and space !

Pattern matching algorithm (checking end indices first) int nfind(char *string, char *pat) { /* match the lat character of pattern first, and then match from the beginning */ 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; /* successful */ } return -1; }

Simulation of nfind aab ↑↑ jlastp (a) pattern ababbaabaa ↑↑↑ startendmatchlasts (b) no match ababbaabaa ↑↑↑ startendmatchlasts (c) no match ababbaabaa ↑↑↑ startendmatchlasts (d) no match ababbaabaa ↑↑↑ startendmatchlasts (e) no match ababbaabaa ↑↑↑ startendmatchlasts (f) no match ababbaabaa ↑↑↑ star t endmatchlasts (g) match

Failure function used in KMP algorithm Definition: If P = p 0 p 1 … p n-1 is a pattern, then its failure function, f, is defined as: Example: pat = ‘ abcabcacab ’ A fast way to compute the failure function: Largest i < j such that p 0 p 1 … p i = p j-i p j-i-1 … p j if such an i ≧ 0 exists if j = 0 Where m is the least integer k for which if there is no k satisfying the above j patabcabcacab f

KMP algorithm (1/5) Case 1: the first symbol of P dose not appear again in P Case 2: the first symbol of P appear again in P

KMP algorithm (2/5) Case 3: not only the first symbol of P appears in P, prefixes of P appear in P twice

KMP algorithm (3/5) The KMP Algorithm The Prefix function

KMP algorithm (4/5)

KMP algorithm (5/5) #include #define max_string_size 100 #define max_pattern_size 100 int pmatch(); void fail(); int failure[max_pattern_size]; char string[max_string_size]; char pat[max_pattern_size]; int pmatch (char *string, char *pat) { /* Knuth, Morris, Pratt string matching algorithm */ int i = 0, j =0; int lens = strlen(string); int lenp = strlen(pat); while (i < lens && j < lenp) { if (string[i] == pat[j]) { i++; j++; } else if (j == 0) i++; else j = failure[j-1] + 1; } return ( (j == lenp) ? (i-lenp) : -1); } void fail (char *pat) { /* compute the pattern ’ s failure function */ int n = strlen(pat); failure[0] = -1; for (j = 1; j < n; 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; } O(strlen(stirng)) O(strlen(pattern)) O(strlen(stirng)+strlen(pat))

An Example for KMP algorithm (1/6) P 1 ≠T 1, mismatch Since j=1, i = i + 1

An Example for KMP algorithm (2/6) T 5 ≠P 4, mismatch P f(4-1)+1 =P 0+1 =P 1

An Example for KMP algorithm (3/6) P 1 ≠T 5, mismatch Since j=1, i = i + 1

An Example for KMP algorithm (4/6) Match exactly P f(12)+1 =P 4+1 =P 5

An Example for KMP algorithm (5/6) T 19 ≠P 6, mismatch P f(6-1)+1 =P 0+1 =P 1

An Example for KMP algorithm (6/6)...