algorithm efficiency there are often many approaches (algorithms) to solve a problem. how do we...

28
Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program design are two (sometimes conflicting) goals: 1. To design an algorithm that is easy to understand, code, and debug. 2. To design an algorithm that makes efficient use of the computer’s resources. Goal 1 is the concern of Software Engineering. Goal 2 is the concern of data structures and algorithm analysis. When goal 2 is important, how do we measure an algorithm’s cost?

Upload: oswald-black

Post on 05-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Algorithm Efficiency• There are often many approaches (algorithms) to

solve a problem. How do we choose between them?• At the heart of a computer program design are two

(sometimes conflicting) goals:1. To design an algorithm that is easy to understand, code,

and debug.2. To design an algorithm that makes efficient use of the

computer’s resources.• Goal 1 is the concern of Software Engineering.• Goal 2 is the concern of data structures and algorithm

analysis.• When goal 2 is important, how do we measure an

algorithm’s cost?

Page 2: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

How to Measure Efficiency?

• Empirical comparison (run the programs).– Only valid for that machine.– Only valid for that compiler.– Only valid for that coding of the algorithm.

• Asymptotic Algorithm Analysis– Must identify critical resources

• time - where we will concentrate• space

– Identify factors affecting that resource– For most algorithms, running time depends on “size” of the

input.– Running time is expressed as T(n) for some function T on input

size n.

Page 3: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Examples of Growth Rate

Example 1:int largest (int* array, int n) { // does not work for all cases!!!!!

int currlarge = 0;for (int p=0; p<n; p++)

if (array[p]>currlarge)currlarge=array[p];

return currlarge;}

Example 2:sum = 0;for (p=1; p<=n; p++)

for (j=1; j<=n; j++)sum++;

Page 4: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Growth Rate Graphs

10 20 30 40 50

250

500

750

1000

1250

1500

1750

10n

20n

5n log n

2n^22^n

Page 5: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Expanded View

2 4 6 8 10 12 14

50

100

150

200

250

300

350

400

10n

20n

5n log n

2n^22^n

Page 6: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Typical Growth Rates

• c constant• log N logarithmic• log2 N log-squared• N linear• N log N• N2 quadratic• N3 cubic• 2N exponential

Page 7: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Best, Worst and Average Cases• Not all inputs of a given size take the same time.• Sequential search for K in an array of n integers:– Begin at the first element in array and look at each

element in turn until K is found.• Best Case:• Worst Case:• Average Case:• While average time seems to be the fairest

measure, it may be difficult to determine.• When is the worst case time important?– Time critical events (real time processing).

Page 8: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Asymptotic Analysis: Big-oh• Definition: T(n) is in the set O(f(n)) if there exist

two positive constants c and n0 such that |T(n)|<= c|f(n)| for all n>n0.

• Usage: the algorithm is in O(n2) in [best, average, worst] case.

• Meaning: for all data sets big enough (i.e., n>n0), the algorithm always executes in less than c|f(n)| steps in [best, average, worst] case.

• Upper Bound:– Example: if T(n)=3n2 then T(n) is in O(n2).

• Tightest upper bound:– T(n)=3n2 is in O(n3), we prefer O(n2).

Page 9: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Big-oh Example

• Example 1. Finding the value X in an array.– T(n)=csn/2.

– For all values of n>1, |csn/2|<=cs|n|. Therefore, by the definition, T(n) is in O(n) for n0=1 and c=cs.

• Example 2. T(n)=c1n2+c2n in the average case– | c1n2+c2n |<=| c1n2+c2n2 |<=(c1+c2)|n2| for all n>1.

– Therefore, T(n) is in O(n2).

• Example 3: T(n)=c. This is in O(1).

Page 10: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Big-Omega• Definition :T(n) is in the set Ω(g(n)) if there exist two

positive constants c and n0 such that |T(n)| >= c|g(n)| for all n>n0.

• Meaning: For all data sets big enough (i.e., n>n0 ), the algorithm always executes in more than c|g(n)| steps.

• It is a LOWER bound.• Example: T(n)=c1n2+c2n – | c1n2+c2n |>=| c1n2 | for all n>1.– |T(n)|>= c|n2| for c=c1 and n0=1.

• Therefore, T(n) is in Ω(n2) by the definition• We want the greatest lower bound.

Page 11: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Theta Notation• When big-Oh and Ω meet, we indicate this by using Θ

(big-Theta) notation.• Definition: an algorithm is said to be Θ(h(n)) if it is in

O(h(n)) and it is in Ω (h(n)).• Simplifying rules:– if f(n) is in O(g(n)) and g(n) is in O(h(n)) then f(n) is in

O(h(n)).– if f(n) is in O(kg(n)) for any constant k>0, then f(n) is in

O(g(n)).– if f1(n) is in O(g1(n)) and f2(n) is in O(g2(n)), then (f1+f2)(n) is in

O(max(g1(n),g2(n))).– if f1(n) is in O(g1(n)) and f2(n) is in O(g2(n)), then f1(n)f2(n) is in

O(g1(n)g2(n)).

Page 12: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Big O rules

• If T(n) is a polynomial of degree k then T(n)= Θ(nk).

• logkn = O(n) for any constant k. Logarithms grow very slowly.

Page 13: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

General Algorithm Analysis Rules• The running time of a for loop is at most the

running time of the statements inside the for loop times the number of iterations.

• Analyze nested loops inside out. Then apply the previous rule.

• Consecutive statements just add (so apply the max rule).

• The running time of a if/else statement is never more than the running time of the test plus the larger of the times of the true and false case.

Page 14: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Running Time of a Program

• Example 1: a=b;– this assignment statement takes constant time, so it is

Θ(1)• Example 2:

sum=0;for (I=1; I<=n; I++)

sum+=n;• Example 3:

sum=0;for (j=1; j<=n; j++)

for (I=1; I<=j; I++)sum++;

for (k=1; k<=n; k++)a[k]=k-1;

Page 15: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

More Examples• Example 4:

sum1=0;for (I=1; I<=n; I++)

for (j=1; j<=n; j++)sum1++;

sum2=0;for (I=1; I<=n; I++)

for (j=1; j<=I; j++)sum2++:

• Example 5:sum1=0;for (k=1; k<n; k*=2)

for (j=1; j<=n; j++)sum1++:

sum2=0;for (k=1; k<=n; k*=2)

for (j=1; j<=k; j++)sum2++;

Page 16: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Binary Searchint binary (int value, int* array, int size){

int left=-1;int right=size;while (left+1!= right){

int mid=(left+right)/2;if (value < array[mid]) right=mid;else if (value>array[mid]) left=mid;else return mid;

}return -1;

}

Page 17: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Binary Search ExamplePosition Key

0 111 132 213 264 295 366 407 418 459 5110 5411 5612 6513 7214 7715 83

Now let’s search for the value 45.

(0+15)/2 -> 7

(8+15)/2 -> 11

(8+10)/2 -> 9

(8+8)/2 -> 8

Page 18: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Unsuccessful Search

Position Key0 111 132 213 264 295 366 407 418 459 5110 5411 5612 6513 7214 7715 83

How many elements are examined in the worse case?

Now let’s search for the value 24

(0+15)/2 ->7(0+6)/2 ->3

(0+2)/2 ->1

(2+2)/2 ->2

(3+2)/2 ->?? Left+1==Right so stop

Page 19: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Case Study – Maximum Subsequence

• Given a sequence of integers a1, a2,…, an, find the subsequence that gives you the largest sum.

• Since there is no size limit on the subsequence, then if the sequence is all positive or all negative then the solution is trivial.

Page 20: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Simple Solution

• Look at all possible combinations of start and stop positions of the subsequence.

for (i=0 i<n; i++)for (j=i; j<n; j++){

thissum=0;for (k=i; k<=j;k++)

thissum=thissum+a[k];if (thissum>maxsum) maxsum=thissum; }

Page 21: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Analysis of Simple Solution

• Inner loop is executed j-i+1 times.• The middle loop changes j from i to n-1.– Looking at the j-I+1 when j goes from I to n-1, we

have 1+2+…+(n-i).– So this is done (n-i+1)(n-i)/2 times.– (n2-2ni+i2+n-i)/2

Page 22: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

More Analysis

• The outer loop changes i from 0 to n-1.– n2 summed n times is n3

– The sum of 2ni when i changes from 0 to n-1 is 2n(n-1)(n)/2 = n3-n2

– The sum of i2 when i changes from 0 to n-1 is (n-1)(n)(2n-1)/6 = (2n3-3n2-n)/6

– The sum of n when i changes is n2.– The sum of i when i changes from 0 to n-1 is (n-1)(n)/2 =

(n2-n)/2.– Total is (n3+ n3-n2+ (2n3-3n2-n)/6+ n2+ (n2-n)/2)/6– This is O(n3).

Page 23: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

An improved Algorithm

• Start at position i and find the sum of all subsequences that start at position i. Then repeat for all starting positions.

for (i=0 i<n; i++){ thissum=0;

for (j=i; j<n; j++){

thissum=thissum+a[j];if (thissum>maxsum) maxsum=thissum;}}

Page 24: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Analysis of Improved Algorithm

• The inner loop goes from i to n-1.• When i is 0, this is n times• When i is 1 it is n-1 times• Until i is n-1 then 1 time• Summing this up backwards, we get 1+2+…+n

= n(n+1)/2=(n2+n)/2= O(n2)

Page 25: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Final great algorithm

for (j=0; j<n; j++){

thissum=thissum+a[j];if (thissum>maxsum)maxsum=thissum;else

if(thissum<0) thissum=0;}O(n)

Page 26: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Analyzing Problems• Upper bound: The upper bound of best known algorithm

to solve the problem.• Lower bound: The lower bound for every possible

algorithm to solve that problem, even unknown algorithms.

• Example: Sorting• Cost of I/O: Ω(n)• Bubble or insertion sort O(n2)• A better sort (Quicksort Mergesort, Heapsort) O(n log n)• We prove in chapter 8 that sorting is Ω(n log n)

Page 27: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Multiple Parameters

• Compute the rank ordering for all C pixel values in a picture of P pixels.

• Monitors have a fixed number of colors (256, 16M, 64M).

• Need to count the number of each color and determine the most used and least used colors.

for (i=0; i<C; i++)count[i]=0;

for (i=0; i<P; i++)count[value[i]]++;

sort(count);• If we use P as the measure, then time is O(P logP)• Which is bigger, C or P? 600x400=2400; 1024x1024=1M• More accurate is O(P + C log C)

Page 28: Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of a computer program

Space Bounds

• Space bounds can also be analyzed with asymptotic complexity analysis.– Time: Algorithm– Space: Data Structure

• Space/Time Tradeoff Principle:– One can often achieve a reduction in time if one is willing

to sacrifice space, or vice versa. • Encoding or packing information– Boolean flags- takes one bit, but a byte is the

smallest storage, so pack 8 booleans into 1 byte. Takes more time, less space.

• Table Lookup– Factorials - Compute once, use many times

• Disk based Space/Time Tradeoff Principle:– The smaller you can make your disk storage requirements,

the faster your program will run. Disk is slow.