software quality what are the important qualities of a good algorithm? · correctness · robustness...

Post on 14-Dec-2015

221 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Software QualityWhat are the important qualities of a good algorithm?

· correctness· robustness

· ease of implementation· readability and maintainability· efficiency

What does this mean?

Measuring time & spaceAn 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...

83 2

1 5 4

7 6

Time and space may have different characteristics.

... However, the same approach is used to study both.

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.

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 tj denote time to execute the jth instruction

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ______

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ______ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ______

. . . . . . . . . . . . . . . . . . . . ______. . . . . . . . . . . . . . . . . . . . . . . . . . . . . ______

. . . . . . . . . . . . . . . . . . . __________________

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ________

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ____

. . . ____

Count

Ex. time = t1 + t2 + t3 + (n+1)*t4 + n*t5 + (n*(n-1)/2+1)*t6 + (n*(n-1)/2)*t7

+ (n*(n-1)/2)*t8 + n*t9 + t10

exec

utio

n tim

e (s

ec.)

1

2

3

4

5

6

Estimating time & spaceConsider an algorithm to determine the BCS rankings.

Such an algorithm depends largely upon the number of Division 1football 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)40 80 120 160 200 240 280 320 360 400 440 480

estimate(n)

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 > c1) [c2*estimate(j) ≥ count(j)], for some constants c1, c2

· 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

Example

· What is countOfConditions(n)? -- assume that n is arr.length

Defn: O(est(n)) means for all j (j > c1) ______________________ , for constants c1, c2

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 )

Simplify, Simplify, SimplifyThe best Big-O functions have two properties:

(1) they are simplified(2) they are a good approximation of function shape

Simplification axiomsO( 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( na ) dominates O( nb ) when a > b

O( n(log n) ) dominates O( n )

O( n2 ) dominates O( n(log n) )

O( an ) dominates O( nb ) for any constants a, b ≥ 2

O( an ) dominates O( bn ) when a > b

O( n! ) dominates O( an ) for any constant a

O( nm ) dominates O( n! )

constantlogarithmic

linear

polynomial

exponential

factorial

Simplify the following9 * n3

2 * n5 + 3 * n2

2 * n10 + 3n

8*n2 * (3*n3 + 2*n2 + 5*n)

3*m2 + 4*n3

25

Function GrowthDoes functional growth really matter?

1 0 1 1 2 3

2 1 4 8 4 9

4 2 16 64 16 81

8 3 64 512 256 43046721

16 4 256 4096 65536 (note 3)

32 5 1024 32768 4294967296

64 6 4096 262144 (note 1)

128 7 16384 2097152 (note 2)

256 8 65536 16777216

n log2n n2 n3 2n 3n

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.

Function Pix (linear scale)

Function Pix (logarithmic scale)

Function Pix (w/o factorial)

Code AnalysisMost single instructions ... O(1)

x = a*b / 2;

System.out.println( arr[3] );

Sequence of instructions ... O(S1) + O(S2) + ... + O(Sn) { 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++;}

More Code AnalysisSelection ... It’s a choice!

if (condition) S1;else S2;

in the worst case ... maximum of O(S1), O(S2)

in the best case ... minimum of O(S1), O(S2)

in the average case ... expectations regarding S1 and S2

WARNINGS 1) Average case is generally difficult/impossible.2) Best case is worst!

What is the Big-O?Assume that Sj is O(1).

for (int r=0; r!=w/2; r++) { S1; for (int c=0; r!=w; r++) { S2; } S3;}

int z = w;while (z > 0) { S4; for (int r=w; r>3; r--) { S5; } z = z / 2;}

. . . . . . . . . . . . . . . . . . O(1)

. . . . . . . . . . . . . . . . . . O(1)

. . . . . . . . . . . . . . . . . O(1) ____ ____ _______

. . . . . . . . . . . . . . . . O(1)

. . . . . . . . . . . . . . . . . . O(1)

. . . . . . . . . . . . . . . . . O(1)

. . . . . . . . . . . . . . O(1)

____ _____ ________

Overall: ______

/** 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

/** 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?

top related