1 / 62

Arrays and Structures

Arrays and Structures. 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 organized in such a way that the specification of the objects and

Download Presentation

Arrays and Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. Arrays and Structures

  5. The array as an ADT

  6. An array is a set of pairs, <index, value>, 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); Abstract data type of the array 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 (a0, a1, a2, ..., an-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]; } Arrays and Structures

  13. Polynomials We try to use arrays to represent polynomials

  14. Polynomial • Examples • A(x)=3x2+2x+4 • B(x)=x4+10x3+3x2+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)=a0xe0+a1xe1+... +anxen; a set of ordered pairs of // <ei, ai> where ai Coefficient and ei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. coef: 0 1 ... MaxDegree-1 an an-1 a0 Polynomial representation • Representation 1 typedef struct { int degree; /* degree < MaxDegree */ float coef[MaxDegree]; } polynomial; polynomial a;a.degree = n; a.coef[i] = an-i; • Disadvantage: Representation 1 is wasteful in memory. 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; } } Arrays and Structures

  18. coef: 0 1 2 Index=5 Cd=2-Index=-1 Index=1 Cd=2-Index=1 Index=2 Cd=2-Index=0 Index=0 Cd=2-Index=2 4 3 2 A(x) coef: 0 1 2 3 4 3 1 10 B(x) 0 1 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=0 Cd=4-Index=4 Index=5 Cd=4-Index=-1 coef: 0 1 2 3 4 C(x) • A(x)=3x2+2x+4 • B(x)=x4+10x3+3x2+1 • C(x)=A(x)+B(x) 5 1 10 6 2

  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 calledsparse. For instance, A(x)=x10000+1. Arrays and Structures

  20. Polynomial representation (cont’d) • Disadvantage: • When all coefficients are all nonzero, Representation 3 uses twice as mush space as Representation 2. • Representation 3 typedef struct { float coef; int exp; } polynomial; polynomial terms[MaxTerms]; int avail = 0; int Start, Finish; A(x)=100x4+x2+1B(x)=x1000+2x100+x2+5 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++; } 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? Arrays and Structures

  23. Sparse matrices

  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,<row,column,value>, 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. row col value Number of nonzero terms a[0] 6 6 7 a[1] 0 3 22 Number of rows a[2] 0 5 -15 Number of columns a[3] 1 1 11 a[4] 1 2 3 a[5] 2 3 -6 a[6] 4 0 91 a[7] 5 2 28 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. Arrays and Structures

  27. transpose transpose The transposed matrix should hold the ordering rule of the entry.

  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) Arrays and Structures

  31. transpose

  32. row row row row row row row row col col col col col col col col value value value value value value value value row row row row row row row row col col col col col col col col value value value value value value value value a[0] a[0] a[0] a[0] a[0] a[0] a[0] a[0] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 b[0] b[0] b[0] b[0] b[0] b[0] b[0] b[0] 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 a[1] a[1] a[1] a[1] a[1] a[1] a[1] a[1] 0 0 0 0 0 0 0 0 3 3 3 3 3 3 3 3 22 22 22 22 22 22 22 22 b[1] b[1] b[1] b[1] b[1] b[1] b[1] b[1] 0 0 4 4 91 91 a[2] a[2] a[2] a[2] a[2] a[2] a[2] a[2] 0 0 0 0 0 0 0 0 5 5 5 5 5 5 5 5 -15 -15 -15 -15 -15 -15 -15 -15 b[2] b[2] b[2] b[2] b[2] b[2] b[2] b[2] 1 1 1 1 1 1 1 1 1 1 11 11 11 11 11 a[3] a[3] a[3] a[3] a[3] a[3] a[3] a[3] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 11 11 11 11 11 11 11 11 b[3] b[3] b[3] b[3] b[3] b[3] b[3] b[3] 2 2 2 2 1 1 1 1 3 3 3 3 a[4] a[4] a[4] a[4] a[4] a[4] a[4] a[4] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 b[4] b[4] b[4] b[4] b[4] b[4] b[4] b[4] 2 5 28 a[5] a[5] a[5] a[5] a[5] a[5] a[5] a[5] 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 -6 -6 -6 -6 -6 -6 -6 -6 b[5] b[5] b[5] b[5] b[5] b[5] b[5] b[5] 3 3 3 3 3 3 3 0 0 0 0 0 0 0 22 22 22 22 22 22 22 a[6] a[6] a[6] a[6] a[6] a[6] a[6] a[6] 4 4 4 4 4 4 4 4 0 0 0 0 0 0 0 0 91 91 91 91 91 91 91 91 b[6] b[6] b[6] b[6] b[6] b[6] b[6] b[6] 3 3 3 2 2 2 -6 -6 -6 a[7] a[7] a[7] a[7] a[7] a[7] a[7] a[7] 5 5 5 5 5 5 5 5 2 2 2 2 2 2 2 2 28 28 28 28 28 28 28 28 b[7] b[7] b[7] b[7] b[7] b[7] b[7] b[7] 5 5 5 5 5 5 0 0 0 0 0 0 -15 -15 -15 -15 -15 -15 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 row_terms row_terms row_terms row_terms row_terms row_terms row_terms row_terms 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 starting_pos starting_pos starting_pos starting_pos starting_pos starting_pos starting_pos starting_pos 2 1 1 1 1 2 1 1 2 3 3 2 3 3 3 2 3 3 3 4 4 5 4 3 7 6 7 6 6 7 6 5 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 7

  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 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; 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; 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); } } } Arrays and Structures

  37. transpose

  38. A A transpose * A*A=

  39. Classical multiplication algorithm 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) Analysis fast_transpose(b,new_b) 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) 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. Arrays and Structures

  41. Representation of arrays

  42. Representation of arrays • Multidimensional arrays are usually implemented by storing the elements in a one-dimensional array. • If an array is declared A[p1...q1][p2...q2]...[pn...qn] where pi...qi 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 asA[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. Sequential representation of array declared as A[u1][u2] For example, char A[u1][u2]; Location of A[0][0]=  u2 elements Location of A[i][j] = +i*u2+j Arrays and Structures

  45. General formula • The addressing formula for any element A[i1][i2]...[in] in an n-dimensional array declared as A[u1][u2]...[un] is Arrays and Structures

  46. The string ADT

  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. s a b c e f g ... a b g pat s a b c e f g ... a b g pat s a b c e f g ... a b g pat A simple algorithm 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

More Related