Data Structure Sang Yong Han Chung-Ang University Spring 20111.

Slides:



Advertisements
Similar presentations
Sparse Matrices sparse … many elements are zero dense … few elements are zero.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Arrays 2008, Fall Pusan National University Ki-Joune Li.
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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
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.
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
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 4.
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.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Matrix table of values
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.
Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH. 2 2 Any real-life matrix you know?
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 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.
SNU IDB Lab. Ch8. Arrays and Matrices © copyright 2006 SNU IDB Lab.
Polynomial Addition using Linked lists
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.
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.
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.
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
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.
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.
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More Linking.
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
◦ 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.
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
LINKED LISTS.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
Windows Programming Lecture 03. Pointers and Arrays.
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
More Linking Up with Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
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
Reminder: Array Representation In C++
Instructor: Mr.Vahidipour
بردارها و ماتريس ها ساختمان داده ها
Reminder: Array Representation In C++
CSCE 3110 Data Structures & Algorithm Analysis
CS111 Computer Programming
2017, Fall Pusan National University Ki-Joune Li
2018, Fall Pusan National University Ki-Joune Li
Reminder: Array Representation In C++
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

Data Structure Sang Yong Han Chung-Ang University Spring 20111

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.

Objects: A set of pairs where for each value of index there is a value from the set item. Index 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. Methods: for all A  Array, i  index, x  item, j, size  integer Array Create(j, list) ::= return an array of j dimensions where list is a j-tuple whose kth element is the size of the kth dimension. Items are undefined. Item Retrieve(A, i) ::= if (i  index) return the item associated with index value i in array A else return error Array Store(A, i, x) ::= if (i in index) return an array that is identical to array A except the new pair has been inserted else return error The Array ADT

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)

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]

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

2D Arrays The elements of a 2-dimensional array a declared as: int a[3][4]; may be shown as a table a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

Rows Of A 2D Array a[0][0] a[0][1] a[0][2] a[0][3] row 0 a[1][0] a[1][1] a[1][2] a[1][3] row 1 a[2][0] a[2][1] a[2][2] a[2][3] row 2

Columns Of A 2D Array a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] column 0column 1column 2column 3

2D Array Representation In C view 2D array as a 1D array of rows x = [row0, row1, row 2] row 0 = [a,b, c, d] row 1 = [e, f, g, h] row 2 = [i, j, k, l] and store as 1D arrays 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l

Array Representation In C This representation is called the array-of-arrays representation. Contiguous space required for this representation ? abcd efgh ijkl x[]

Polynomials A(X)=3X 20 +2X 5 +4, B(X)=X 4 +10X 3 +3X 2 +1 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 where a i in Coefficients and e i in Exponents, e i 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 inserted

Polyomial ADT (cont’d) 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

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) coef exp starta finisha startb finishb avail A(X)=2X B(X)=X 4 +10X 3 +3X 2 +1

Polynomial Addition (2) (cont’d) #define MAX_TERMS 100 typedef struct { int exp; float coef; } polynomial; polynomial terms[MAX_TERMS]; Addition(int starta, int enda, int startb, int endb, int startc, int endc) { … } advantage: less space disadvantage: longer code Time Complexity ? Running time?

col1 col2 col3 col4 col5 col6 row0 row1 row2 row3 row4 row5 8/36 6*65*3 15/15 sparse matrix data structure? Sparse Matrices

Sparse Matrix - Airline flight Airline flight matrix.  airports are numbered 1 through n  flight(i,j) = list of nonstop flights from airport i to airport j  n = 1000 (say)  n x n array of list pointers => 4 million bytes  total number of nonempty flight lists = 20,000 (say)

Sparse Matrix - Web page matrix Web page matrix. web pages are numbered 1 through n web(i,j) = number of links from page i to page j Web analysis. authority page … page that has many links to it hub page … links to many authority pages

Web Page Matrix  n = 2 billion (and growing by 1 million a day)  n x n array of ints => 16 * bytes (16 * 10 9 GB)  each page links to 10 (say) other pages on average  on average there are 10 nonzero entries per row  space needed for nonzero elements is approximately 2billion x 10 x 4 bytes = 80 billion bytes (80 GB)

Sparse Matrix ADT Objects: a set of triples,, 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.

Sparse Matrix ADT (cont’d) 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.

(1)Represented by a two-dimensional array. Sparse matrix wastes space. (2)Each element is characterized by. 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

Matrix Transpose

Transpose of a Sparse Matrix in 2D array representation for (j = 0; j < columns; j++) for( i = 0; i <rows; i++) b[j][i] = a[i][j]; Time and Space Complexity ?

Sparse Matrix Operations Transpose of a sparse matrix. row col value row col value a[0] b[0] [1] [1] [2] [2] [3] [3] [4] [4] [5] [5] [6] [6] [7] [7] [8] [8] transpose

(1) for each row i take element and store it in element of the transpose. difficulty: where to put ? (0, 0, 15) ====> (0, 0, 15) (0, 3, 22) ====> (3, 0, 22) (0, 5, -15) ====> (5, 0, -15) (1, 1, 11) ====> (1, 1, 11) Move elements down very often. (2) For all elements in column j, place element in element Transpose a Sparse Matrix

Transpose of a Sparse Matrix 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 */

Time Complexity Analysis of Matrix Transpose

Fast Matrix Transpose Step 1: #nonzero in each row of transpose. = #nonzero in each column of original matrix = [2, 1, 2, 2, 0, 1] Step2: Calculate Starting position of each row of transpose = [1, 3, 4, 6, 8, 8] Step 3: Move elements from original list to transpose list. Complexity m x n original matrix t nonzero elements Step 1: O(n+t) Step 2: O(n) S: O(t) Overall O(n+t)

Time Complexity Analysis of Fast Matrix Transpose m x n original matrix t nonzero elements Step 1: O(n+t) Step 2: O(n) Step 3: O(t) Overall O(n+t)

Runtime Performance - Transpose 500 x 500 matrix with 1994 nonzero elements Run time measured on a 300MHz Pentium II PC 2D array 210 ms SparseMatrix 6 ms

Runtime Performance - Addition Matrix Addition. 500 x 500 matrices with 1994 and 999 nonzero elements 2D array 880 ms SparseMatrix 18 ms