Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH. 2 2 Any real-life matrix you know?

Slides:



Advertisements
Similar presentations
Linear Transformations and Matrices Jim Van Verth Lars M. Bishop
Advertisements

Sparse Matrices sparse … many elements are zero dense … few elements are zero.
Applied Informatics Štefan BEREŽNÝ
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.
Matrices CSE, POSTECH.
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.
1.7 Diagonal, Triangular, and Symmetric Matrices.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Tirgul 9 Amortized analysis Graph representation.
Chapter 2 Matrices Definition of a matrix.
Chapter 1 Systems of Linear Equations
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Matrix table of values
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
Module 6 Matrices & Applications Chapter 26 Matrices and Applications I.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Chapter 11 Section 11.0 Review of Matrices. Matrices A matrix (despite the glamour of the movie) is a collection of numbers arranged in a rectangle or.
Chapter 1 Section 1.2 Echelon Form and Gauss-Jordan Elimination.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1.7 Diagonal, Triangular, and Symmetric Matrices 1.
Solving Systems of Equations by matrices
Multi-Dimensional Arrays
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Lesson 13-1: Matrices & Systems Objective: Students will: State the dimensions of a matrix Solve systems using matrices.
Overview Definitions Basic matrix operations (+, -, x) Determinants and inverses.
SNU IDB Lab. Ch8. Arrays and Matrices © copyright 2006 SNU IDB Lab.
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Data Structure Sang Yong Han Chung-Ang University Spring
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
Assembly - Arrays תרגול 7 מערכים.
Sparse Matrices Matrix  table of values. Sparse Matrices Matrix  table of values Row 2 Column 4 4 x 5 matrix.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
1. 2  Introduction  Array Operations  Number of Elements in an array One-dimensional array Two dimensional array Multi-dimensional arrays Representation.
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.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
Chapter 1 Section 1.6 Algebraic Properties of Matrix Operations.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Introduction Types of Matrices Operations
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.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
Lecture 1 Linear algebra Vectors, matrices. Linear algebra Encyclopedia Britannica:“a branch of mathematics that is concerned with mathematical structures.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
MATRICES A rectangular arrangement of elements is called matrix. Types of matrices: Null matrix: A matrix whose all elements are zero is called a null.
MTH108 Business Math I Lecture 20.
Data Structure Sang Yong Han
1.5 Matricies.
Arrays.
L9Matrix and linear equation
Chapter 8: Lesson 8.1 Matrices & Systems of Equations
Reminder: Array Representation In C++
بردارها و ماتريس ها ساختمان داده ها
Reminder: Array Representation In C++
Matrix Algebra - Overview
Unit 3: Matrices
Determinant of a Matrix
Multi-Dimensional Arrays
Arrays.
Arrays.
Reminder: Array Representation In C++
Arrays and Matrices Prof. Abdul Hameed.
Presentation transcript:

Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH

2 2 Any real-life matrix you know?

3 3 All excel tables in the world Google Squared

4 4 Twitter? Or entire Web corpus? MC 몽 하하 길 길 MC 몽 길 하하 MC 몽 Imagine matrix in Twitter or Web (billions): lots of 0’s!!!

5 5 API supported List statuses = twitter.getUserTimeline(); System.out.println(" -- " + args[0].toUpperCase() + " timeline --"); //prints each status of the public user timeline for (Status status : statuses) { System.out.println(" - " + status.getText()); } Future Assignment? Maybe

6 6 Sudoku? Future Assignment? Maybe

7 1D Array Representation In Java, C, and C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start + i

8 Space Overhead space overhead = 4 bytes for start + 4 bytes for x.length = 8 bytes (excludes space needed for the elements of x) Memory abcd start

9 2D Arrays The elements of a 2-dimensional array a declared as: int [][]a = new int[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]

10 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

11 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

12 2D Array Representation In Java, C, and 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 4 1D arrays 2-dimensional array x a, b, c, d e, f, g, h i, j, k, l

13 2D Array Representation In Java, C, and C++ x.length = 3 x[0].length = x[1].length = x[2].length = 4 abcd efgh ijkl x[]

14 Space Overhead space overhead = overhead for 4 1D arrays = 4 * 8 bytes = 32 bytes = (number of rows + 1) x 8 bytes abcd efgh ijkl x[]

15 2D  1D? In fact, any nD  1D?

16 Row-Major Mapping Example 3 x 4 array: a b c d e f g h i j k l Convert into 1D array y by collecting elements by rows. Within a row elements are collected from left to right. Rows are collected from top to bottom. We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l} row 0 row 1row 2…row i

17 Locating Element x[i][j] assume x has r rows and c columns each row has c elements i rows to the left of row i so ic elements to the left of x[i][0] so x[i][j] is mapped to position ic + j of the 1D array row 0 row 1row 2…row i 0c2c3cic

18 Column-Major Mapping a b c d e f g h i j k l Convert into 1D array y by collecting elements by columns. Within a column elements are collected from top to bottom. Columns are collected from left to right. We get y = {a, e, i, b, f, j, c, g, k, d, h, l}

19 Matrix (Linear algebra? Non-CS?) Table of values to organize data (e.g., M$ Excel) Has rows and columns, but numbering begins at 1 rather than 0. a b c d row 1 e f g h row 2 i j k l row 3 Use notation x(i,j) rather than x[i][j]. May use a 2D array to represent a matrix.

20 Shortcomings Of Using A 2D Array For A Matrix Indexes are off by 1. Java arrays do not support matrix operations such as add, transpose, multiply, and so on. Potentially large space waste

21 Exercise transpose(a) add(a,b) multiply(a,b)

22 Special Matrices

23 Diagonal Matrix An n x n matrix in which all nonzero terms are on the diagonal.

24 Diagonal Matrix x(i,j) is on diagonal iff i = j number of diagonal elements in an n x n matrix is n non diagonal elements are zero store diagonal only vs n 2 whole

25 Lower Triangular Matrix An n x n matrix in which all nonzero terms are either on or below the diagonal. x(i,j) is part of lower triangle iff i >= j. number of elements in lower triangle is … + n = n(n+1)/2. store only the lower triangle

26 Array Of Arrays Representation Use an irregular 2-D array … length of rows is not required to be the same x[] 789l0

27 Creating And Using An Irregular Array // declare a two-dimensional array variable // and allocate the desired number of rows int [][] irregularArray = new int [numberOfRows][]; // now allocate space for the elements in each row for (int i = 0; i < numberOfRows; i++) irregularArray[i] = new int [size[i]]; // use the array like any regular array irregularArray[2][3] = 5; irregularArray[4][6] = irregularArray[2][3] + 2; irregularArray[1][1] += 3;

28 Map Lower Triangular Array Into A 1D Array Use row-major order, but omit terms that are not part of the lower triangle. For the matrix we get 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

29 Index Of Element [i][j] Order is: row 1, row 2, row 3, … Row i is preceded by rows 1, 2, …, i-1 Size of row i is i. Number of elements that precede row i is … + i-1 = i(i-1)/2 So element (i,j) is at position i(i-1)/2 + j -1 of the 1D array. r 1r2r3 …row i 0136

30 Sparse Matrices

31 Example Of Sparse Matrices diagonal tridiagonal lower triangular (?) These are structured sparse matrices. May be mapped into a 1D array so that a mapping function can be used to locate an element.

32 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 20 billion x 4 bytes = 80 billion bytes (80 GB)

33 Representation Of Unstructured Sparse Matrices Single linear list in row-major order. scan the nonzero elements of the sparse matrix in row- major order each nonzero element is represented by a triple (row, column, value) the list of triples may be an array list or a linked list (chain)

34 Case I: Single Linear List Example list = row column value

35 Chain Representation Node structure. rowcol nextvalue

36 Single Chain row list = column value null firstNode 2

37 Case II: One Linear List Per Row row1 = [(3, 3), (5,4)] row2 = [(3,5), (4,7)] row3 = [] row4 = [(2,2), (3,6)]

38 Array Of Row Chains Node structure. next valuecol

39 Array Of Row Chains row[] 33 null

40 Orthogonal List Representation Both row and column lists. Node structure. rowcol nextdown value

41 Row Lists null n n n

42 Column Lists n n n

43 Orthogonal Lists null row[] nn n nn n

44 Approximate Memory Requirements 500 x 500 matrix with 1994 nonzero elements 2D array 500 x 500 x 4 = 1million bytes Single Array List 3 x 1994 x 4 = 23,928 bytes One Chain Per Row x 4 = 25,928

45 Runtime Performance Matrix Transpose 500 x 500 matrix with 1994 nonzero elements 2D array 210 ms Single Array List 6 ms One Chain Per Row 12 ms

46 Performance Matrix Addition. 500 x 500 matrices with 1994 and 999 nonzero elements 2D array 880 ms Single Array List 18 ms One Chain Per Row 29 ms

47 Coming Up Next READING: Chapter 8 NEXT: Stack (Chapter 9)