ceg 221 lesson 4: algorithm development i mr. david lippa

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

Post on 21-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

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

CEG 221Lesson 4: Algorithm Development I

Mr. David Lippa

Page 2: 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

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

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)

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

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.

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

Example: Triangulation with SAS

• Let’s assume we have a triangle and we wish to compute the missing values:

B

a = 6042°

b = 100

A

C

• Start with the mathematical computation if we were to do it manually:– c = A2 + B2 – 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

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

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

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

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π

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

• 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

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

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

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

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

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

Matrix Multiplication:Dot Product

• Since dot products are not implemented in C, we have to do it ourselves– Dot product(X, Y) = X1 ∙ Y1 + X2 ∙ Y2 + … + Xn ∙ Yn

– 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]

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

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]

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

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 * 4 + 0 = 0– 1 * 4 + 0 = 4– 2 * 4 + 1 = 11

• From these values, we can derive that:

C[i, j] = C[i * numCols + j]

00,0

10,1

20,2

61,2

51,1

41,0

82,0

92,1

102,2

30,3

71,3

112,3

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

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;

}

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

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.

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

Matrix Multiplication: The Codedouble* mult(double *pA, int numRowsA, int numColsA,

double *pB, int numRowsB, int numColsB){

// allocate memory, set pC to 0double *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;

}

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

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.

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

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

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

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 outif (numColsA != numRowsB) return NULL;

double *pC = malloc(numRowsA * numColsB * sizeof(double));

// no memory, bad matrices -> bail outif (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

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

Next Time

• Algorithm Development II– Review of basic algorithm development– Advanced algorithm development

• Optimization of algorithm• Optimization of code

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

QUESTIONS

???