Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CEG 221 Lesson 4: Algorithm Development I Mr. David Lippa."— Presentation transcript:

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

2 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

3 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)

4 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.

5 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

6 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

7 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π

8 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

9 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

10 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

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

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

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

14 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; }

15 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.

16 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; }

17 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.

18 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

19 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

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

21 QUESTIONS


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

Similar presentations


Ads by Google