Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Principles in Software Engineering Here are some key ideas you will learn about... 1. divide-and-conquer 2. recursion 3. greedy algorithms,

Similar presentations


Presentation on theme: "Engineering Principles in Software Engineering Here are some key ideas you will learn about... 1. divide-and-conquer 2. recursion 3. greedy algorithms,"— Presentation transcript:

1 Engineering Principles in Software Engineering Here are some key ideas you will learn about... 1. divide-and-conquer 2. recursion 3. greedy algorithms, tradeoffs 4. caching, dynamic programming 5. abstraction and reuse

2 1. Divide-and-conquer a major strategy for solving problems: break them into smaller sub-problems 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 sublists sort each separately merge them back together

3 2. Recursion a form of divide-and-conquer write functions that call themselves example: factorial n! = 1x2x3...n def fact(n): if n>1: return n*fact(n-1) return 1 # base case: fact(1)=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 <=

4 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”)

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

6 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

7 def calcProb(A,n): s = 0 for i in range(1,n): s += log(A[i]) return s def calcProb2(A,n): s = 0 C = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1] for i in range(1,n): if C[A[i]]==-1: C[A[i] = log(A[i]) s += C[A[i]] return s > calcProb([3,6,3,5,2,6,3,3,2,5]) 5.46479 calls log(.) 10 times calls log(.) 4 times alog(a)indexcache 30.477123 60.778156 30.4771250.69897 5 20.30103 2 60.77815 30.47712 3 20.30103 50.69897 5.46479

8 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 sizeR/W access time CPU registers101 cycles L1 cache (on chip)1kb-1Mb10 cycles main memory (RAM) 10Gb100 cycles disk drives100Gb-10Tb10,000 cycles 80486 1kb cache

9 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 ABCD A0 B180 C20220 D3247250 AC D B 18 20 32 25 22 AC D BE 18 19 20 32 25 26 22

10 extend table for larger results –add row/column for E –E connects to the network at B and C –compute dist of X  E based on X  B and X  C ABCDE A0 B180 C20220 D3247250 E371926510 AC D BE 18 19 20 32 25 26 22 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

11 5. Abstraction and Reuse Abstraction is the key to becoming a good programmer –don’t reinvent the wheel –more importantly, reuse things that have been debugged This is the basis of Object-Oriented Programming Many large software projects are built by plugging components together –write a small amount of code the makes things work together, like a making a browser out of: a) an HTML parser, b) a display engine, c) network URL query/retrieval functions, and d) plug-ins

12 Kinds of Abstraction –making a function out of things you do repeatedly parameterizing it so it can be applied to a wider range of inputs –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 other Complex or a scalar), conjugated, viewed as a+bi or re i  –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)? –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) define input: scanned TIFF images? output: ASCII strings? interface: 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)?


Download ppt "Engineering Principles in Software Engineering Here are some key ideas you will learn about... 1. divide-and-conquer 2. recursion 3. greedy algorithms,"

Similar presentations


Ads by Google