Engineering Principles in Software Engineering

23
Engineering Principles in Software Engineering five important concepts in CS that you will learn that can enhance software development

description

Engineering Principles in Software Engineering. five important concepts in CS that you will learn that can enhance software development. 1. Divide-and-conquer. a general strategy for solving problems an aspect of Computational Thinking - PowerPoint PPT Presentation

Transcript of Engineering Principles in Software Engineering

Page 1: Engineering Principles in Software Engineering

Engineering Principles in Software Engineering

five important concepts in CS that you will learn that can enhance

software development

Page 2: Engineering Principles in Software Engineering

1. Divide-and-conquer• a general strategy for solving problems

– an aspect of Computational Thinking– break problems into smaller sub-problems,

which might be easier to solve (if decoupled)– example: chess moves

• if I could just capture the opponents queen, then I could....

– or break large datasets into smaller pieces

Page 3: Engineering Principles in Software Engineering

• example: mergesort– given a list of numbers in random order– split the list into 2 halves– sort each half independently– merge the two sub-lists (interleave)

1 18 22 17 6 13 9 10 7 15 14 4

1 18 22 17 6 13 | 9 10 7 15 14 4

1 6 13 17 18 22 | 4 7 9 10 14 15

1 4 6 7 9 10 13 14 15 17 18 22

← here is a list to sort

← divide into 2 sub-lists

← sort each separately

← merge them back together

Page 4: Engineering Principles in Software Engineering

2. Recursion• a form of divide-and-conquer

• write functions that call themselves

• example: factorialn! = 1x2x3...n = n(n-1)!def fact(n):

if n<=1: return 1 // base case

return n*fact(n-1)

• example: mergesort– when you divide list into 2 halves, how do you

sort each half – by calling mergesort, of course!

call trace: fact(3) => fact(2) => fact(1) 1 <= 2*1=2<= 3*2=6 <=

Page 5: Engineering Principles in Software Engineering

3. Greedy algorithms

• most implementations involve making tradeoffs– we know NP-complete problems are hard and

probably cannot be solved in polynomial time– use a heuristic/shortcut – might get a pretty good

solution (but not optimal) in faster time• greedy methods do not guarantee an optimal

solution– however, in many cases, a near-optimal solution can

be good enough– it is important to know when a heuristic will NOT

produce an optimal solution, and to know how sub-optimal it is (i.e. an “error bound”)

Page 6: Engineering Principles in Software Engineering

• Examples of greedy algorithms– navigation, packet routing, shortest

path in graph, robot motion planning• choose the “closest” neighbor in the direction of the

destination

– document comparison (e.g. diff) • start by aligning the longest matching substrings

– knapsack packing • choose item with highest value/weight ratio first

– scheduling• schedule the longest job first, (or the one with most constraints)

..out to be more efficient to find the length of the longest subsequence. Then in the case where the......

.....increase the efficiency using the length of the longest subsequence. But if the first characters differ..

Page 7: Engineering Principles in Software Engineering

4. Caching• One way to improve the efficiency of many

programs is to use caching – saving intermediate results in memory that will get used multiple times– why calculate the same thing multiple times?– might require designing a special data structure (e.g. a

hash table) to store/retrieve these efficiently– amortization: the cost of calculating something gets

divided over all the times it is used

Page 8: Engineering Principles in Software Engineering

• calculating Fibonacci numbers– F(n) = F(n-1)+F(n-2)– base cases: F(1) = F(2) = 1– this sequence of numbers arises in several

patterns in nature, as well as the stock market– 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...

– (show how calculating this slows down, and can be dramatically speeded up by caching results of previous function calls)

Page 9: Engineering Principles in Software Engineering

• Caching also applies to hardware design– memory hierarchy– for constants or global variables that get used

frequently, put them in a register or L1 cache– analog to a “staging area”– variables used infrequently can stay in RAM– very large datasets can be swapped out to disk

typical size R/W access time

CPU registers 10 1 cycles

L1 cache (on chip) 1kb-1Mb 10 cycles

main memory (RAM)

10Gb 100 cycles

disk drives 100Gb-10Tb 10,000 cycles

80486

1kb cache

Page 10: Engineering Principles in Software Engineering

• An important example of caching is Dynamic Programming– Suppose our goal is to compute the

min. travel distance between A and E– build-up a table of smaller results for a

subgraph

A B C D

A 0

B 18 0

C 20 22 0

D 32 47 25 0

A C

D

B18

20

32 25

22

A C

D

B E18

19

20

32 25

2622

Page 11: Engineering Principles in Software Engineering

• extend table for larger results– add row/column for E– E connects to the network at B and C– compute dist of XE based on XB and XC

A B C D E

A 0

B 18 0

C 20 22 0

D 32 47 25 0

E 37 19 26 51 0

A C

D

B E18

19

20

32 25

2622

d(D,E)=min[d(D,B)+19,d(D,C)+26] =min(47+19,25+26) =min(66,51)=51

d(A,E)=min[d(A,B)+19,d(A,C)+26] =min(18+19,20+26) =min(37,47)=37

Page 12: Engineering Principles in Software Engineering

5. Abstraction and Reuse• Abstraction is the key to becoming a good

programmer– don’t reinvent the wheel– more importantly, reuse things that have

already been tested and debugged

• This is the basis of Object-Oriented Programming

Page 13: Engineering Principles in Software Engineering

• Many large software projects are built by plugging components together

• write a small amount of (“glue”) code the makes things work together

• example: creating a web browser out of: a) an HTML text parser

b) a display engine (graphics, windows)

c) URL query/retrieval network functions

d) plug-ins

Page 14: Engineering Principles in Software Engineering

• Making a function out of things you do repeatedly– parameterizing it so it can be applied to a

wider range of inputs

Examples of Abstraction

Page 15: Engineering Principles in Software Engineering

Here is code for printing a histogram of basketball scores (which typically range between 50 and 100 points):

def histo(Scores): i = 50 while i<100: c = 0 for s in Scores: if si and s<i+5: c += 1 print i,’*’*c i += 5

Here is output for scores of Aggies inbasketball games so far this year:

histo([82,91,68,75,79,88,67,52,74,73,52,41,63,69,57,75,72,51,55,52,36,72])

50 ****55 **60 *65 ***70 ****75 ***80 *85 *90 *95

Page 16: Engineering Principles in Software Engineering

Suppose we want to generalize thiscode for printing a histogram of football scores too, which span adifferent range.

Add parameters of lower and upperbound of histogram A and B, and stepsize S.

def histo(Scores,A,B,S): i = A while i<B: c = 0 for s in Scores: if si and s<i+S: c += 1 print i,’*’*c i += S

Here is output for scores of Aggies infootball games last year:

histo([52,65,42,42,45,41,41,56,57,51,10,21,52],A=0,B=70,S=10)

010 *20 *3040 *****50 *****60 *

vs Rice W 52-31

vs Sam Houston W 65-28

vs #1 Alabama L 49-42

vs SMU W 42-13

@ Arkansas W 45-33

@ Ole Miss W 41-38

vs #24 Auburn L 45-41

vs Vanderbilt W 56-24

vs UTEP W 57-7

vs Mississippi St W 51-41

@ #22 LSU L 34-10

@ #5 Missouri L 28-21

vs #24 Duke* W 52-48

Page 17: Engineering Principles in Software Engineering

• Object-oriented classes– encapsulation – define internal representation

of data– interface – define methods, services– good design – make the external operations

independent of the internal representation (helps decouple code)

– example: a Complex number is a ‘thing’ that can be added/subtracted, multiplied (by another Complex or a scalar), conjugated, viewed as (a+bi) or (rei

Page 18: Engineering Principles in Software Engineering

• Here is an example of class definition of Complex Numbers in C++

class Complex{ double re,im; // interval variables

public: // constructor (initialization) Complex(double x,double y) { re = x; im = y; }

void conjugate() { im *= -1; }

double magnitude() { double z=re*re+im*im; return sqrt(z); }

void print() { cout << "(" << re << "+" << im << "i)"; }};

re=1.0 im=2.0

A Complex objectrepresenting1+2i has twomember variablesfor holding thereal and imaginarycomponents.

Page 19: Engineering Principles in Software Engineering

#include <iostream>#include <iomanip>#include <math.h>using namespace std;

class Complex {...from previous slide... };

int main(){ Complex p=Complex(1,2); cout << “|p|=“ << p.magnitude() << "\n"; Complex p2=p+p; // custom addition}

Output:> g++ complex.cpp –o complex -lm> complexp = (1.0+2.0i)|p| = 2.23607

Note how we get themagnitude of a Complexobject by invoking a method on it, p.magnitude(). The calculation is done internally to the object.

Page 20: Engineering Principles in Software Engineering

• Templates in C++– if you can sort a list of integers, why not

generalize it to sort lists with any data type that can be pairwise-compared (total order)?

void insertionSort(int a[], int n) { for (int i = 1; i <= n; i++) { int temp = a[i]; for (int j=i; j>0; j--) if(temp < a[j-1]) a[j] = a[j-1]; else break; a[j] = temp; } }

135264987

123564987

123564987

123456987

Page 21: Engineering Principles in Software Engineering

• Templates in C++– can use same algorithm to sort any type T, as

long as element can be compare with ‘<‘ operator

– works on float, characters, strings...

template <class T> void insertionSort(T a[], int n) { for(int i = 1; i <= n; i++) { T temp = a[i]; for (int j=i; j>0; j--) if(temp < a[j-1]) a[j] = a[j-1]; else break; a[j] = temp; } } defined for string, characters, floats...

Page 22: Engineering Principles in Software Engineering

• API design – Application-Programmer Interface– a coherent, complete, logical system of functions and

data formats– example: OCR (optical character recognition)– you don’t want to have to implement feature-based

character recognition that is font- and scale-independent yourself (probably)

– interface defines input (e.g. scanned TIFF images) and output (e.g. ASCII strings)String* OCRscan(TiffImage* input_image)

– are you going to indicate coordinates where word was found on the page?

– is the user able to load different character sets (alphabets)?

Page 23: Engineering Principles in Software Engineering

Engineering Principles in Software Engineering

A summary of the key ideas we talked about...

1. divide-and-conquer

2. recursion

3. greedy algorithms, tradeoffs

4. caching, dynamic programming

5. abstraction and reuse