Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on:...
-
date post
19-Dec-2015 -
Category
Documents
-
view
213 -
download
0
Transcript of Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on:...
Scott Grissom, copyright 2004 Chapter 5 Slide 1
Analysis of Algorithms (Ch 5)
Chapter 5 focuses on:algorithm analysis
searching algorithms
sorting algorithms
recursion
• Comparing Algorithms (5.1)Given two algorithms that do the same thing, which is
better?
Is either good enough?
What about
special cases
hardware differences
software differences
small data sets
Scott Grissom, copyright 2004 Chapter 5 Slide 2
Runtime Time
Working code is not enough
Running time is an important issue
How to improve
How to compare
Space is not as important
Scott Grissom, copyright 2004 Chapter 5 Slide 3
Efficiency (5.2)
Running Timebased on algorithm AND input data
Each statement takes constant timeDetermine total operations
int total = 3;
total = total + 3 * 6 - 4;
What about this?int total = 0;
for (int I=0; I<=10; I++)
total = total+ I * I * I;
Scott Grissom, copyright 2004 Chapter 5 Slide 4
Data Set
Size of input data N?Predict time
t = f(n)
Using a stopwatch (benchmarking) is not sufficient.
What about this?int Total = 0
for (int i=0; i <=N; i++)
Total = Total + i * i * i;
Scott Grissom, copyright 2004 Chapter 5 Slide 5
Big O Notation
Predicting exact counts is a nuisanceInstead, use Big Oh or Order
t = O [f(n)]
t <= M*f(n) for all n > no
We can round offt = 6n + 14
t = O(n)
6n4 + 3n3 + 12n + 6453
2n2 + 6
6 NlogN + 2n2 + 13
12,345
The Most Common n3 n logN n2 NlogN 1 2n
place in correct order
Scott Grissom, copyright 2004 Chapter 5 Slide 6
Linear Search
• Start at the beginning and compare with every item until it is found (or not)
• Codeposition = -1;
for (int I=0; I<array.length; I++)
if (target == array[I])
position = I;
Considerbest case
average case
worst case
Benchmarkingsee Demo/LinearSearch.java
Consider EnhancementsFigure 5.4 on page 217
Scott Grissom, copyright 2004 Chapter 5 Slide 7
Binary Search
Items must be sortedGuess middle itemIf target is less than item then look in first halfIf target is greater than item then look in second halfElse, you found it
Codeposition = -1;while (start <= end)
middle = (start + end) / 2if target < array[middle]
end = middle - 1if target > array[middle]
start = middle + 1if target == array[middle]
position = middleBenchmarking
see Demo/BinarySearch.java
Consider EnhancementsFigure 5.5 on page 219
Scott Grissom, copyright 2004 Chapter 5 Slide 8
Asymptotic Analysis 5.3
Nested Loopswork inside outfor (i=0; i< N; i++)
for (j=0; j< N; j++)
S;
What about this?for (int i=1; i<1000; i++)
S;
And this one?for (int i=1; i<=N; i++)
method (i);
Scott Grissom, copyright 2004 Chapter 5 Slide 9
Sorting
• Place items in correct order (ascending)
• Be aware of these termsswap
comparison
pass
• Many algorithms have been developedSelection Sort
Bubble Sort
Merge Sort
Quick Sort
Scott Grissom, copyright 2004 Chapter 5 Slide 10
Selection Sort
• For each passfind smallest remaining item
place in correct location
• Repeat N-1 times
• Benchmarksee Demos/SelectionSort.java
Scott Grissom, copyright 2004 Chapter 5 Slide 11
Bubble Sort
• For each passswap neighbors out of order
• Repeat N-1 times
Consider EnhancementsFigure 5.6 on page 220
Scott Grissom, copyright 2004 Chapter 5 Slide 12
Sorting Analysis
Goal of sorting is to remove inversions
How many inversions are there worst case?
Consider algorithms that compare and swap neighbors. How many inversions are removed with a single swap?
Therefore, what is the expected runtime for ALL sorting algorithms that compare and swap neighbors?
Scott Grissom, copyright 2004 Chapter 5 Slide 13
Other Complexity Measures (5.4)
Big Omegabest case
t >= f(n)
Big Thetabest and worst case
t == f(n)
ConsiderLinear Search
Binary Search
Bubble Sort in book
Scott Grissom, copyright 2004 Chapter 5 Slide 14
Recursion -- 5.5
• Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems
• Book coverage is limited to the analysis of recursive algorithms
• A recursive definition is one which uses the word or concept being defined in the definition itself
• All recursive definitions have to have a non-recursive partcalled the base case
• If they didn't, there would be no way to terminate the recursive pathcalled infinite recursion
Scott Grissom, copyright 2004 Chapter 5 Slide 15
Recursive Definitions
• N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive
• This definition can be expressed recursively as:
• 1! = 1• N! = N * (N-1)!• Eventually, the base case of 1! is
reached
Scott Grissom, copyright 2004 Chapter 5 Slide 16
Recursive Programming
• A method in Java can invoke itselfa recursive method
• The code of a recursive method must be structured to handle both the base case and the recursive case
• Each call to the method sets up a new execution environment, with new parameters and local variables
Scott Grissom, copyright 2004 Chapter 5 Slide 17
Summation
• Consider the problem of computing the sum of all the numbers between 1 and any positive integer N
• This problem can be recursively defined as:
• Summation Solutionint Sum (int N){
if (N < = 1)
return 1
else
return N + sum(N - 1)
}
• Draw the recursive call stack• Too much recursion can cause a Stack
Overflow error• See Demos/Recursion.java
Scott Grissom, copyright 2004 Chapter 5 Slide 18
Practice Recursion
mystery (N){if (N <= 1)
print something
else
mystery (N - 1);
Do it again with (N - 2)Do it again with (N / 2)
big difference!
Scott Grissom, copyright 2004 Chapter 5 Slide 19
Recursive Binary Search
• The binary search algorithm can be implemented recursively
• It has the same general strategy as the iterative version
• Each recursive call cuts the search space in half
• Study code in Figure 5.10• Use the substitution method to solve• T(n) = T(n/2) + 3
Scott Grissom, copyright 2004 Chapter 5 Slide 20
Group Projects
• Write code to solve the following problems
• How many methods are called for each solution with respect to N?
• Print the values 1 to N1 2 3 4 5 6
• Print the values N to 16 5 4 3 2 1
• Fibonacci sequence1 1 2 3 5 8 13 21
fibonacci (10)
prints the first ten numbers in the sequence
• Factorialvalue = factorial (8);
• Powerresult = power (base, exponent)
Scott Grissom, copyright 2004 Chapter 5 Slide 21
Elegant Solutions?
• Note that just because we can use recursion to solve a problem, doesn't mean we should
• For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is easier to understand
• However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version
Scott Grissom, copyright 2004 Chapter 5 Slide 22
Towers of Hanoi
• A classic problem!• Three spindles and N disks of different
sizes• Move all disks from spindle 1 to 3• The world will end when the monks
successfully move 64 disks• How long will it take?
Scott Grissom, copyright 2004 Chapter 5 Slide 23
Merge Sort
• The approach:Divide the list into two equal parts
Recursively sort both parts
Merge the two parts into a single list again
• The merge process takes O(N)• The runtime is O(n log n)• T(n) = 2T(n/2) + n• Use the substitution method to solve
Scott Grissom, copyright 2004 Chapter 5 Slide 24
Quick Sort
• The approach of Quick Sort:Select a pivot
Divide the list into two lists based on the pivot
all items less than the pivot
all items greater than or equal to the pivot
Recursively sort both lists
Piece together the first list, pivot, and second list into the original list
• It is considered the fastest known sorting algorithm
• The runtime is O (n log n)
Scott Grissom, copyright 2004 Chapter 5 Slide 25
Summary
Given a computer that performs one billion operations per second on input of one million
log n would take almost no timen would take .001 secondsn log n would take _______n2 would take ________n3 would take ________2n would take ________2n - exponential growth is very slow
if computer performs 1 bip
n = 40 takes 18 minutes
n = 50 13 days
n = 60 310 years