Data Structures and Algorithms I Day 2, 9/1/11

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

Matrix Multiplication To Multiply matrix A by matrix B: Multiply corresponding entries and then add the resulting products (1)(-1)+ (2)(3) Multiply each.
Maths for Computer Graphics
Multidimensional arrays Many problems require information be organized as a two- dimensional or multidimensional list Examples –Matrices –Graphical animation.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Introduction to Computer Science Recursive Array Programming Recursive Sorting Algorithms Unit 16.
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Table of Contents Matrices - Multiplication Assume that matrix A is of order m  n and matrix B is of order p  q. To determine whether or not A can be.
100’s of free ppt’s from library
Determinants 2 x 2 and 3 x 3 Matrices. Matrices A matrix is an array of numbers that are arranged in rows and columns. A matrix is “square” if it has.
4.2 Operations with Matrices Scalar multiplication.
ECON 1150 Matrix Operations Special Matrices
Matrix Algebra. Quick Review Quick Review Solutions.
Overview Definitions Basic matrix operations (+, -, x) Determinants and inverses.
13.1 Matrices and Their Sums
Relations, Functions, and Matrices Mathematical Structures for Computer Science Chapter 4 Copyright © 2006 W.H. Freeman & Co.MSCS Slides Relations, Functions.
CSCI 171 Presentation 9 Matrix Theory. Matrix – Rectangular array –i th row, j th column, i,j element –Square matrix, diagonal –Diagonal matrix –Equality.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
8.2 Operations With Matrices
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
MATRIX: A rectangular arrangement of numbers in rows and columns. The ORDER of a matrix is the number of the rows and columns. The ENTRIES are the numbers.
2.2 Multiplying Matrices Mr. Anderson Pre Calculus Falconer Central High School Falconer, NY.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
MATRIX A set of numbers arranged in rows and columns enclosed in round or square brackets is called a matrix. The order of a matrix gives the number of.
3.6 Multiplying Matrices Homework 3-17odd and odd.
Chapter 1 Section 1.6 Algebraic Properties of Matrix Operations.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMP 338 Data Structures and Algorithms I Day 1, 8/30/11.
A very brief introduction to Matrix (Section 2.7) Definitions Some properties Basic matrix operations Zero-One (Boolean) matrices.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
13.4 Product of Two Matrices
Sections 2.4 and 2.5 Matrix Operations
Matrices and Matrix Operations
12-1 Organizing Data Using Matrices
Matrix Operations Free powerpoints at
Matrix Operations.
Data Structures and Algorithms I Day 9, 9/22/11 Heap Sort
Data Structures and Algorithms I Day 1, 8/30/11
Discrete Structures – CNS2300
1.5 Matricies.
Matrix Operations.
Matrix Operations Free powerpoints at
Matrix Multiplication
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Data Structures and Algorithms I Sorting
Matrix Operations Free powerpoints at
Sequences and Summations
WarmUp 2-3 on your calculator or on paper..
7.3 Matrices.
Matrices Elements, Adding and Subtracting
Determinants 2 x 2 and 3 x 3 Matrices.
MATRICES MATRIX OPERATIONS.
Section 2.4 Matrices.
2.2 Introduction to Matrices
Objectives Multiply two matrices.
slides adapted from Marty Stepp
3.5 Perform Basic Matrix Operations
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Algorithms CSCI 235, Spring 2019 Lecture 16 Quick Sort Read Ch. 7
Determinants 2 x 2 and 3 x 3 Matrices.
3.6 Multiply Matrices.
Chapter 4 Matrices & Determinants
Determinants 2 x 2 and 3 x 3 Matrices.
Determinants 2 x 2 and 3 x 3 Matrices.
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II
Matrix Multiplication Sec. 4.2
Matrices - Operations MULTIPLICATION OF MATRICES
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Data Structures and Algorithms I Day 2, 9/1/11 CMP 338 Data Structures and Algorithms I Day 2, 9/1/11

Programming Homework 1 Create a matrix library with the following API Class: Matrix Matrix(int r, int c) Creates a matrix with r rows and c columns Individual entries are random doubles multAdd(Matrix C, Matrix A, Matrix B) Throw an exception if matrices are not conformant Otherwise, set C += A*B Note: Real library would have more methods e.g. getters and setters for individual matrix elements

Matrices Implementation: 2-D array of double Multiply-add: for (int i=0; i<M; i++) for (int j=0; j<N; j++) for (int k=0; k<L, k++) c[i][j] += a[i][k]*b[k][j] Analysis: ~ MNL double multiply-adds ~ N3 for square matrices Data structures for sparse matrices

Partition, take 1 int partition(double a[], double p, int lo, int hi) int i = lo; int j = hi while (true) while (a[i++] <= p) while (p < a[--j]) if (i<j) swap(a, i, j) else return j

Partition, take 2 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) while (a[++i] <= p) while (p < a[--j]) if (i<j) swap(a, i, j) else return j

Partition, take 3 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) while (a[++i] <= p) while (p < a[--j]) if (i<j) swap(a, i, j) else return j

Partition, take 4 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi < a.length int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[++i] <= p) {a[lo..i-1]<=p<a[i]} while (p < a[--j]) {a[j]<=p<a[j+1..hi]} if (i<j) swap(a, i, j) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 5 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[++i] <= p) if (i == hi) break {a[lo..i-1]<=p<a[i]} while (p < a[--j]) if (j == lo) break {a[j]<=p<a[j+1..hi]} if (i<j) swap(a, i, j) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 6 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[++i] <= p) if (i == hi) break {a[lo..i-1]<=p<a[i] || i==hi && a[lo..hi]<p} while (p < a[--j]) if (j == lo) break {a[j]<=p<a[j+1..hi] || j==lo && p<a[lo..hi]} if (i<j) swap(a, i, j) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 7 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo-1; int j = hi+1 while (true) {a[lo..i] <= p <a[j..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i, j) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 8 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) {a[lo..i] <= p <a[j..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i, j) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 9 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) {a[lo..i-1] <= p <a[j+1..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i, j) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 10 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) {a[lo..i-1] <= p <a[j+1..hi]} while (a[i] <= p) if (i++ == hi) break {a[lo..i-1]<=p<a[i] || hi<i && a[lo..hi]<p} while (p < a[j]) if (lo == j++) break {a[j]<=p<a[j+1..hi] || j<lo && p<=a[lo..hi]} if (i<j) swap(a, i++, j--) else return j {a[lo..j] <= p < a[j+1..hi]}

Partition, take 10 int partition(double a[], double p, int lo, int hi) assert 0 <= lo <= hi+1 < a.length+1 int i = lo; int j = hi while (true) while (a[i] <= p) if (i++ == hi) break while (p < a[j]) if (lo == j++) break if (i<j) swap(a, i++, j--) else return j

Partition, take 1 int partition(double a[], double p, int lo, int hi) int i = lo; int j = hi while (true) while (a[i++] <= p) while (p < a[j++]) if (i<j) swap(a, i, j) else return j

Programming Homework 2 Implement the following API double pivot(double[] a, int lo, int hi) Assert that 0 <= lo <= hi+1 < ||a||+1 Return p where a[i]<=p<=a[j] for some lo<=j,i<=hi int partition(double[] a, double p, int lo, int hi) Return j, lo-1 <= j <= hi,such that a[lo..j] <= p < a[j+1..hi] double select(double[] a, int lo, int hi, int k) Assert that 0<= lo <= hi+1 <= ||a||+1 and 0 <= k <= hi-lo Return the smallest double d such that k elements of a[lo..hi] are less than or equal to d

Selection Algorithm Item select(double[] a, int k, int lo, int hi) if (k == 0 && lo == hi) return a[lo] Item p = pivot(a, lo, hi) // a[lo+(lo+hi)/2] int j =partition(a, p, lo, hi) if (k <= j) return select(a, k, lo, j) else return select(a, k-(j+1), j+1, hi)