Docsity

Arrays - Data Structures and Algorithm - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Arrays - Data Structures and Algorithm - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

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 index.

representation (possible)

implemented by using consecutive memory.

Arrays in C

int list[5], *plist[5];

list[5]: five integers

list[0], list[1], list[2], list[3], list[4]

*plist[5]: five pointers to integers

plist[0], plist[1], plist[2], plist[3], plist[4]

implementation of 1-D array

list[0] base address = α

list[1] α + sizeof(int)

list[2] α + 2*sizeof(int)

list[3] α + 3*sizeof(int)

list[4] α + 4*size(int) Docsity.com

Arrays in C (cont’d)

Compare int *list1 and int list2[5] in C.

Same: list1 and list2 are pointers.

Difference: list2 reserves five locations.

Notations:

list2 - a pointer to list2[0]

(list2 + i) - a pointer to list2[i] (&list2[i])

*(list2 + i) - list2[i]

en n

p ( x ) = a xe^1^ +...+ a x

1

Polynomials A(X)=3X 20 +2X 5 +4, B(X)=X 4 +10X 3 +3X 2 +

Other Data Structures

Based on Arrays

  • Arrays:

•Basic data structure

•May store any type of elements

Polynomials: defined by a list of coefficients and

exponents

  • degree of polynomial = the largest exponent in a

polynomial

Polynomial ADT

Objects : a set of ordered pairs of <e (^) i ,ai> where ai in Coefficients and ei in Exponents, ei are integers >= 0 Methods: 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 <coef, expon> inserted

Polynomial Addition (1)

#define MAX_DEGREE 101 typedef struct { int degree; float coef[MAX_DEGREE]; } polynomial;

Addition(polynomial * a, polynomial * b, polynomial* c)

advantage: easy implementation

disadvantage: waste space when sparse

Running time?

  • Use one global array to store all

polynomials

Polynomial Addition (2)

2 1 1 10 3 1

1000 0 4 3 2 0

coef

exp

starta finisha startb finishb avail

A(X)=2X 1000 +

B(X)=X 4 +10X 3 +3X 2 +

col1 col2 col3 col4 col5 col row row row row row row

sparse matrix

data structure?

Sparse Matrices

Sparse Matrix ADT

Objects: a set of triples, <row, column, value>, where row and column are integers and form a unique combination, and value comes from the set item.

Methods :

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.

(1) Represented by a two-dimensional array.

Sparse matrix wastes space.

(2) Each element is characterized by <row, col, value>.

Sparse Matrix Representation

Sparse_matrix Create(max_row, max_col) ::=

#define MAX_TERMS 101 /* maximum number of terms +1*/ typedef struct { int col; int row; int value; } term; term A[MAX_TERMS]

The terms in A should be ordered

based on <row, col>

Sparse Matrix Operations

• Transpose of a sparse matrix.

• What is the transpose of a matrix?

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

transpose

Transpose of a Sparse Matrix (cont’d)

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 */ Docsity.com