CEG 221 Lesson 4: Algorithm Development I - PowerPoint PPT Presentation

About This Presentation
Title:

CEG 221 Lesson 4: Algorithm Development I

Description:

CEG 221 Lesson 4: Algorithm Development I Mr. David Lippa – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 22
Provided by: DavidL355
Category:

less

Transcript and Presenter's Notes

Title: CEG 221 Lesson 4: Algorithm Development I


1
CEG 221Lesson 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
  • Lets assume we have a triangle and we wish to
    compute the missing values
  • 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)
  • Were 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 algorithms 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 2p

8
Flushing Out Initial Pseudo Code Hiding the Work
  • 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. Thats why we can
    hide the work in a function.
  • Example Matrix Multiplication

9
Introduction to Algorithm Development
  • Well 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 MultiplicationInitial 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
  • Ci, j i j

11
Matrix MultiplicationDot 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 As row by the kth
    value in Bs column
  • Assume every value in C is initialized to 0
  • For each column k in A (or for each row k in
    B)
  • Ci, j Ai, k x Bk, j

12
Matrix MultiplicationRefined 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
  • Ci, j Ai, k x Bk, 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
  • Ci, j Ci numCols j

0 0,0
1 0,1
2 0,2
3 0,3
6 1,2
5 1,1
4 1,0
7 1,3
8 2,0
9 2,1
10 2,2
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 MultiplicationWrapping 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 lt numRowsA i)
  • for (j 0 j lt numColsB j)
  • for (k 0 k lt numColsA i)
  • pCat(i, j, numColsB)
  • Aat(i, k, numColsA) Bat(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 cant!
  • This brings us to defining requirements for each
    function. If those requirements arent met, we
    return an error value, in this case the NULL
    pointer.

18
Matrix Multiplication Requirements
  • Neither matrix can be NULL
  • If As dimensions are m x n and Bs
    dimensions are NOT n x p, bail out because we
    cannot compute A x B
  • If malloc() fails to allocate memory, bail out

19
Matrix Multiplication Final Code
  • double mult(double pA, int numRowsA, int
    numColsA,
  • double pB, int numRowsB, int numColsB)
  • // (m by n) x (p by q) -gt cant multiply -gt bail
    out
  • if (numColsA ! numRowsB) return NULL
  • double pC malloc(numRowsA numColsB
    sizeof(double))
  • // no memory, bad matrices -gt bail out
  • if (pA NULL pB NULL pC NULL)
    return NULL
  • memset(pC, 0, numRowsA numColsB
    sizeof(double))
  • for (i 0 i lt numRowsA i)
  • for (j 0 j lt numColsB j)
  • for (k 0 k lt numColsA i)
  • pCat(i, j, numColsB)
  • Aat(i, k, numColsA) Bat(k, j,
    numColsB)

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

21
QUESTIONS
Write a Comment
User Comments (0)
About PowerShow.com