Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Quality What are the important qualities of a good algorithm? · correctness · robustness · ease of implementation · readability and maintainability.

Similar presentations


Presentation on theme: "Software Quality What are the important qualities of a good algorithm? · correctness · robustness · ease of implementation · readability and maintainability."— Presentation transcript:

1 Software Quality What are the important qualities of a good algorithm? · correctness · robustness · ease of implementation · readability and maintainability · efficiency What does this mean?

2 Measuring time & space An algorithm’s run time and/or memory requirements depend upon ____. · consider an algorithm to alphabetize the La Crosse phone book. · consider a program to print new license plates for WI. · consider an algorithm to print class lists for UW-L courses. What is the relationship between time & space? · consider detasseling corn · consider the tile arranging puzzle... 832 154 76 Time and space may have different characteristics.... However, the same approach is used to study both.

3 Counting the Cost (execution time) private int[][] mileage = {{0, 722, 2244, 800, 896}, {722, 0, 1047, 2113, 831}, {2244, 1047, 0, 1425, 1576}, {800, 2113, 1425, 0, 2849, {896, 831, 1576, 2849, 0} }; private int[][] mileage = {{0, 722, 2244, 800, 896}, {722, 0, 1047, 2113, 831}, {2244, 1047, 0, 1425, 1576}, {800, 2113, 1425, 0, 2849, {896, 831, 1576, 2849, 0} }; Example: Averaging a distance table algorithm: Average the cells under the diagonal.

4 Counting the Cost (execution time) Count the number of times each instruction executes. public int averageDistance(int[][] mileage) { int fromCity, toCity; int totalDistance = 0; fromCity = 0; while (fromCity != mileage.length) { toCity = fromCity + 1; while (toCity != mileage[].length) { totalDistance = totalDistance + mileage[fromCity][toCity]; toCity++; } fromCity++; } return totalDistance / (mileage.length*(mileage.length-1)/2); } public int averageDistance(int[][] mileage) { int fromCity, toCity; int totalDistance = 0; fromCity = 0; while (fromCity != mileage.length) { toCity = fromCity + 1; while (toCity != mileage[].length) { totalDistance = totalDistance + mileage[fromCity][toCity]; toCity++; } fromCity++; } return totalDistance / (mileage.length*(mileage.length-1)/2); } Let n denote mileage.length t j denote time to execute the j th instruction............................... ______..................................... ______.................... ______............................. ______................... _________ _________.................................. ________.................................... ____... ____ Count Ex. time = t 1 + t 2 + t 3 + (n+1)*t 4 + n*t 5 + (n*(n-1)/2+1)*t 6 + (n*(n-1)/2)*t 7 + (n*(n-1)/2)*t 8 + n*t 9 + t 10

5 execution time (sec.) 1 2 3 4 5 6 Estimating time & space Consider an algorithm to determine the BCS rankings. Such an algorithm depends largely upon the number of Division 1 football teams. Our analysis of the algorithm’s execution time is based on (1) an estimate of this time. (2) an estimate based upon “larger” numbers of teams. · · · · · Number of BCS teams (n) 4080120160200240280320360400440480 estimate(n)

6 Big-O Approximation Rather than worry about exact execution time, we count... · count the number of instructions executed · count the number of variable assignments · count the number of method calls · etc. · Let count(n) = the exact count for data amount, n · We select estimate(n) = an approximation with the following property for all j (j > c 1 ) [c 2 *estimate(j) ≥ count(j)], for some constants c 1, c 2 · For any such estimate it it proper to write O(estimate(n)) = count(n) stated “the big-O for count(n) is estimate(n)” but an approximation is sufficient

7 Example · What is countOfConditions(n)? -- assume that n is arr.length Defn: O(est(n)) means for all j (j > c 1 ) ______________________, for constants c 1, c 2 int ndx = 0; while (ndx != arr.length-1) { if (arr[ndx] < arr[ndx+1]) { temp = arr[ndx]; arr[ndx] = arr[ndx+1]; arr[ndx+1] = temp; } ndx++; } int ndx = 0; while (ndx != arr.length-1) { if (arr[ndx] < arr[ndx+1]) { temp = arr[ndx]; arr[ndx] = arr[ndx+1]; arr[ndx+1] = temp; } ndx++; } · What is maxCountOfAssignments(n)? · This algorithm is O( n )

8 Simplify, Simplify, Simplify The best Big-O functions have two properties: (1) they are simplified (2) they are a good approximation of function shape Simplification axioms O( c * f(n) ) = O( f(n) ), if c is a constant O( f(n) + g(n) ) = O( f(n) ), if f(n) dominates g(n) O( f(n) * g(n) ) = O(f(n)) * O(g(n)) Common Dominating functions (for variables n and m) O(log n) dominates O(1) O( n ) dominates O(log n) O( n a ) dominates O( n b ) when a > b O( n(log n) ) dominates O( n ) O( n 2 ) dominates O( n(log n) ) O( a n ) dominates O( n b ) for any constants a, b ≥ 2 O( a n ) dominates O( b n ) when a > b O( n! ) dominates O( a n ) for any constant a O( n m ) dominates O( n! ) constantlogarithmic linear polynomial exponential factorial

9 Simplify the following 9 * n 3 2 * n 5 + 3 * n 2 2 * n 10 + 3 n 8*n 2 * (3*n 3 + 2*n 2 + 5*n) 3*m 2 + 4*n 3 25

10 Function Growth Does functional growth really matter? 101123 214849 4216641681 836451225643046721 164256409665536(note 3) 3251024327684294967296 6464096262144(note 1) 1287163842097152(note 2) 25686553616777216 nlog 2 nn2n2 n3n3 2n2n 3n3n note 1 - roughly ta day of computation for a 1 petaflop supercomputer note 2 - about 50 billion times the age of the universe note 3 - a really BIG number.

11 Function Pix (linear scale)

12 Function Pix (logarithmic scale)

13 Function Pix (w/o factorial)

14 Code Analysis Most single instructions... O(1) x = a*b / 2; System.out.println( arr[3] ); Sequence of instructions... O(S 1 ) + O(S 2 ) +... + O(S n ) { temp = arr[j]; arr[j] = arr[k]; arr[k] = temp; } Loop... O(loopBody) * O(numberOfRepetitions) int j=0; while (j!=arr.length-1) { arr[j] = arr[j+1]; for (int k=0; k!=arr.length/2; k++) { temp = arr[k]; arr[k] = arr[arr.length-1-k]; arr[arr.length-1-k] = temp; } j++; }

15 More Code Analysis Selection... It’s a choice! if (condition) S 1 ; else S 2 ; in the worst case... maximum of O(S 1 ), O(S 2 ) in the best case... minimum of O(S 1 ), O(S 2 ) in the average case... expectations regarding S 1 and S 2 WARNINGS 1) Average case is generally difficult/impossible. 2) Best case is worst!

16 What is the Big-O? Assume that S j is O(1). for (int r=0; r!=w/2; r++) { S 1 ; for (int c=0; r!=w; r++) { S 2 ; } S 3 ; } int z = w; while (z > 0) { S 4 ; for (int r=w; r>3; r--) { S 5 ; } z = z / 2; }.................. O(1)................. O(1) ____ _______................ O(1).................. O(1)................. O(1).............. O(1) _________________ Overall: ______

17 /** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void selectionSort(double[] arr) { int lastSorted = -1; int minNdx; double temp; while (lastSorted != arr.length-1) { minNdx = smallestNdxAmongUnsorted(arr, lastSorted); temp = arr[minNdx]; arr[minNdx] = arr[lastSorted+1]; arr[lastSorted+1] = temp; lastSorted++; } /** post: ls+1 <= result < arr.length * and forAll j:(ls<=j<arr.length) [arr[result] <= arr[j]] */ public int smallestNdxAmongUnsorted(double[] arr, int ls) { int smallNdx = ls + 1; int j = ls + 2; while (j < arr.length) { if (arr[j] < arr[smallNdx]) smallNdx = j; j++; } return smallNdx; } /** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void selectionSort(double[] arr) { int lastSorted = -1; int minNdx; double temp; while (lastSorted != arr.length-1) { minNdx = smallestNdxAmongUnsorted(arr, lastSorted); temp = arr[minNdx]; arr[minNdx] = arr[lastSorted+1]; arr[lastSorted+1] = temp; lastSorted++; } /** post: ls+1 <= result < arr.length * and forAll j:(ls<=j<arr.length) [arr[result] <= arr[j]] */ public int smallestNdxAmongUnsorted(double[] arr, int ls) { int smallNdx = ls + 1; int j = ls + 2; while (j < arr.length) { if (arr[j] < arr[smallNdx]) smallNdx = j; j++; } return smallNdx; } Selection Sort

18 /** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void bubbleSort(double[] arr) { int lastSwapNdx = arr.length; int newSwapNdx; double temp; while (lastSwapNdx > 0) { newSwapNdx = 0; for (int j=0; j!=lastSwapNdx-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; newSwapNdx = j+1; } lastSwapNdx = newSwapNdx; } /** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void bubbleSort(double[] arr) { int lastSwapNdx = arr.length; int newSwapNdx; double temp; while (lastSwapNdx > 0) { newSwapNdx = 0; for (int j=0; j!=lastSwapNdx-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; newSwapNdx = j+1; } lastSwapNdx = newSwapNdx; } Bubble Sort What is the best case? What is the worst case?


Download ppt "Software Quality What are the important qualities of a good algorithm? · correctness · robustness · ease of implementation · readability and maintainability."

Similar presentations


Ads by Google