CEG 221 Lesson 4: Algorithm Development I Mr. David Lippa.

Slides:



Advertisements
Similar presentations
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Advertisements

Program Design. Objectives Students should understand the basic steps in the programming process. Students should understand the need for good design.
Transformations We want to be able to make changes to the image larger/smaller rotate move This can be efficiently achieved through mathematical operations.
Lecture 1: Overview of Computers & Programming
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Kernighan/Ritchie: Kelley/Pohl:
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Chapter 4 Systems of Linear Equations; Matrices Section 6 Matrix Equations and Systems of Linear Equations.
Matrices: Inverse Matrix
Theoretical Program Checking Greg Bronevetsky. Background The field of Program Checking is about 13 years old. Pioneered by Manuel Blum, Hal Wasserman,
HCI 530 : Seminar (HCI) Damian Schofield. HCI 530: Seminar (HCI) Transforms –Two Dimensional –Three Dimensional The Graphics Pipeline.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
CEG 221 Lesson 5: Algorithm Development II Mr. David Lippa.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
CEG 221 Lesson 1: Review I Mr. David Lippa. Overview: Review of Required Concepts Basic data types –Normal basic types, size_t, time_t Basic programming.
Matlab Matlab is a powerful mathematical tool and this tutorial is intended to be an introduction to some of the functions that you might find useful.
Program Design and Development
CSCE 590E Spring 2007 Basic Math By Jijun Tang. Applied Trigonometry Trigonometric functions  Defined using right triangle  x y h.
Tirgul 9 Hash Tables (continued) Reminder Examples.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Chapter 3 Planning Your Solution
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.
Calculators and Trigonometric Functions of an Acute Angle Trigonometry MATH 103 S. Rook.
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.
Sort an array - the selection sort algorithm. Selection Sort Selection sort a classic computer algorithm that is used to sort an array The selection sort.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Exercise problems for students taking the Programming Parallel Computers course. Janusz Kowalik Piotr Arlukowicz Tadeusz Puzniakowski Informatics Institute.
Mathematical Fundamentals
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Recap of linear algebra: vectors, matrices, transformations, … Background knowledge for 3DM Marc van Kreveld.
Multi-Dimensional Arrays
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
4.6 Matrix Equations and Systems of Linear Equations In this section, you will study matrix equations and how to use them to solve systems of linear equations.
To accompany Quantitative Analysis for Management, 9e by Render/Stair/Hanna 5-1 1© 2006 by Prentice Hall, Inc. Upper Saddle River, NJ Module 5 Mathematical.
Lecture 4. RAM Model, Space and Time Complexity
Do it now activity Last term we learnt about how data is represented in a computer and about how to identify different volumes of data. How many bits in.
Software Life Cycle What Requirements Gathering, Problem definition
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Introduction to Data Structures and Algorithms CS 110: Data Structures and Algorithms First Semester,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
ECE 454 Computer Systems Programming Memory performance (Part II: Optimizing for caches) Ding Yuan ECE Dept., University of Toronto
Unit 39 Matrices Presentation 1Matrix Additional and Subtraction Presentation 2Scalar Multiplication Presentation 3Matrix Multiplication 1 Presentation.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
4.5 Inverse of a Square Matrix
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Learning Objectives for Section 4.5 Inverse of a Square Matrix
DATA & COMPUTER SECURITY (CSNB414) MODULE 3 MODERN SYMMETRIC ENCRYPTION.
3.4 Solution by Matrices. What is a Matrix? matrix A matrix is a rectangular array of numbers.
MATRICES Danny Nguyen Marissa Lally Clauberte Louis HOW TO'S: ADD, SUBTRACT, AND MULTIPLY MATRICES.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Computing Systems and Programming Programming.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Chapter 4 Systems of Linear Equations; Matrices
Advanced Algorithms Analysis and Design
13.4 Product of Two Matrices
10.5 Inverses of Matrices and Matrix Equations
Multiplying Matrices.
Multiplying Matrices.
Basic Concepts of Algorithm
Multiplying Matrices.
Multiplying Matrices.
Multiplying Matrices.
Presentation transcript:

CEG 221 Lesson 4: Algorithm Development I Mr. David Lippa

Overview Algorithm Development I –What is an algorithm? –Why are algorithms important? –Examples of Algorithms –Introduction to Algorithm Development –Example of Flushing Out an Algorithm Questions

What is an Algorithm? An algorithm is a high-level set of clear, step-by- step actions taken in order to solve a problem, frequently expressed in English or pseudo code. Pseudo code – a way to express program- specific explanations while still in English and at a very high level Examples of Algorithms: –Computing the remaining angles and side in an SAS Triangle –Computing an integral using rectangle approximation method (RAM)

Why are algorithms important? Algorithms provide a means of expressing the problem to be solved, the solution to that problem, and a general step-by-step way to reach the solution Algorithms are expressed in pseudo-code, and can then be easily written in a programming language and verified for correctness.

Example: Triangulation with SAS Let’s assume we have a triangle and we wish to compute the missing values: B a = 60 42° b = 100 A C Start with the mathematical computation if we were to do it manually: –c = A 2 + B 2 – 2 ab cos C –sin A = (a sin C) / c A = asin(a (sin C) / c ) –B = 180 – A – C (iff A and C are in degrees) We’re also assuming that angles are in degrees

SAS: From Math to Pseudocode Now that we have all the math done, we can develop the algorithm’s pseudo code: –Get the sides and their enclosing angle from the user (in degrees) –Run the computation from the previous slide –Convert angle A to degrees prior to computing angle B –Display results to the user

Flushing Out Initial Pseudo Code Pseudo code breaks up large tasks into single operations that can each be broken up further or expressed by a C primitive Example: –“Get user input” means to prompt the user using “printf”; read the input with “scanf” –“Convert radians to degrees” is just a bit of math – multiply by 360 and divide by 2π

For a simple operation, like converting from degrees to radians, using functions to hide the work is less important. But how would you like to write out 10 or 1000 lines of code to perform a simple task on 1 data value or case? Probably not. That’s why we can hide the work in a function. Example: Matrix Multiplication Flushing Out Initial Pseudo Code: Hiding the Work

Introduction to Algorithm Development We’ll get to Matrix Multiplication in a minute. Developing this algorithm will help you practice seeing how to take a problem, find a solution, develop an algorithm, flush out the algorithm, and then finally implementing it. Keep in mind that “hiding the work” is important – this is crucial to modular design

Matrix Multiplication: Initial Design Break down the math: –[A] x [B] = [C]  for each element in [C], dot product the rows of [A] with the columns of [B] to get the first value in [C]. –[A] is an m x n matrix and [B] is an n x p matrix –[C] is an m x p matrix For each row i in [A] For each column j in [B] C[i, j] = i ∙ j

Matrix Multiplication: Dot Product Since dot products are not implemented in C, we have to do it ourselves –Dot product(X, Y) = X 1 ∙ Y 1 + X 2 ∙ Y 2 + … + X n ∙ Y n –There are n pairs that need to be multiplied together –Multiply the kth value in A’s row by the kth value in B’s column –Assume every value in C is initialized to 0 For each column k in [A] (or for each row k in [B]) C[i, j] += A[i, k] x B[k, j]

Matrix Multiplication: Refined Design Now we can pull it all together: Assumptions: –Every element of C is initialized to 0. For each row i in [A] For each column j in [B] For each column k in [A] C[i, j] += A[i, k] x B[k, j]

Matrix Multiplication: Accessing a Dynamically Allocated 2D Array Since C does not implement accessors [i, j] for 2D arrays allocated dynamically, we must implement it. Below is a 3 x 4 matrix with 2D and 1D coordinates overlayed. Examples: –(0, 0) maps onto 0 –(1, 0) maps onto 4 –(2, 1) maps onto 9 Calculation: –0 * = 0 –1 * = 4 –2 * = 11 From these values, we can derive that: C[i, j] = C[i * numCols + j] 0 0,0 1 0,1 2 0,2 6 1,2 5 1,1 4 1,0 8 2,0 9 2,1 10 2,2 3 0,3 7 1,3 11 2,3

Matrix Multiplication: Accessing a Dynamically Allocated 2D Array Code for the “at” function: int at(int i, int j, int numCols) { return i * numCols + j; }

Matrix Multiplication: Wrapping Up Pseudo Code This pseudo code can now be written into C code that takes: –2 Pointers to an array of doubles: [A], [B] –Numbers of rows & columns of each (m, n, p) And allocates memory with dynamic memory allocation to return [C], an m x p matrix that is the result of [A] x [B] Now, any time we need to multiply any matrices, we can use and reuse this module.

Matrix Multiplication: The Code double* mult(double *pA, int numRowsA, int numColsA, double *pB, int numRowsB, int numColsB) { // allocate memory, set pC to 0 double *pC = malloc(numRowsA * numColsB * sizeof(double)); memset(pC, 0, numRowsA * numColsB * sizeof(double)); for (i = 0; i < numRowsA; i++) for (j = 0; j < numColsB; j++) for (k = 0; k < numColsA; i++) { pC[at(i, j, numColsB)] = A[at(i, k, numColsA)] * B[at(k, j, numColsB)]; } return pC; }

Flushing Out the Code There are two more steps to algorithm development when you use functions: –Error handling – what if your inputs are bad? Pointers can be NULL What do you get if you multiply a 2 x 3 matrix by a 5 x 7? You can’t! –This brings us to defining requirements for each function. If those requirements aren’t met, we return an error value, in this case the NULL pointer.

Matrix Multiplication: Requirements Neither matrix can be NULL If [A]’s dimensions are m x n and [B]’s dimensions are NOT n x p, bail out because we cannot compute [A] x [B] If malloc() fails to allocate memory, bail out

double* mult(double *pA, int numRowsA, int numColsA, double *pB, int numRowsB, int numColsB) { // (m by n) x (p by q) -> can’t multiply -> bail out if (numColsA != numRowsB) return NULL; double *pC = malloc(numRowsA * numColsB * sizeof(double)); // no memory, bad matrices -> bail out if (pA == NULL || pB == NULL || pC == NULL) return NULL; memset(pC, 0, numRowsA * numColsB * sizeof(double)); for (i = 0; i < numRowsA; i++) for (j = 0; j < numColsB; j++) for (k = 0; k < numColsA; i++) { pC[at(i, j, numColsB)] = A[at(i, k, numColsA)] * B[at(k, j, numColsB)]; } return pC; } Matrix Multiplication: Final Code

Next Time Algorithm Development II –Review of basic algorithm development –Advanced algorithm development Optimization of algorithm Optimization of code

QUESTIONS