Exercise 01
About
The PerformanceTest program in the empirical package, contains three methods that implement the algorithms(solutions) for computing the range of numbers in an array.
For this exercise, we will look at algorithm performance by observing which of the three algorithm runs the fastest. We will compare each algorithm using data sets that increase in size. This performance is determined by running the algorithm and measuring how long it takes to complete each data set in milliseconds (ms).
Range
The range is the difference between the highest and lowest numbers in the array.
Algorithm 1 : Range$1
Description: This algorithm uses nested loops to examine every pair of elements in the array, computing their difference and remembering the largest difference found.
1 | maxDiff = 0; |
Algorithm 2 : Range$2
Description: This algorithm reduces the amount of computations performed by disregarding one of the (i, j) and (j, i) pairs, that give identical comparisons. It still uses nested loops to examine every pair of elements in the array but now only once, computes their difference and remembering the largest difference found.
1 | maxDiff = 0; |
Algorithm 3 : Range$3
Description: This algorithm uses a loop to find the largest value and smallest value in the array, compute their difference and return this difference.
1 | max = element[0] |
Analysis/Questions
Instructions: Answer the questions on in a document/spreadsheet and submit to your github repository.
Download and examine the L2_Range_RunTimes EXCEL file, which contains the location to populate the run time data of the three (3) algorithms listed above. There are three (3) different spreadsheets, one for each algorithm. Note the fourth (4th) spreadsheet combines the data for all three algorithms on one graph, however, algorithm 3 uses the secondary scale at top and right of graph.
Run the PerformanceTest program and input the out of the show run time for the data sets listed for each of the range algorithms in L2_Range_RunTimes EXCEL file.
If you are experiencing an OutOfMemoryError : Java heap space for your PerformanceTest program. This is possibly due to your Java Virtual Machine (JVM) needing more memory to run this application. A quick fix for this might be to do the following: Go to Run > Run Configurations > select PerformanceTest from left window > select (x) = Arguments
Type in the box below in the VM arguments window: -Xmx3200m.
This sets a 3.2GB allocation, which should be enough, but if you need more you can just replace 3200 with a larger number like 3800 etc…
What do observe for each of the algorithms shown with their corresponding data set? Can you tell which algorithm was the most efficient?
For algorithms 1 and 2, did reducing the amount of computations by half improve the runtime significantly? Explain your reasoning, if you felt it had a small or large change.
For algorithms 2 and 3, did reducing the number of loops improve the runtime significantly? Explain your reasoning, if you felt it had a small or large change.
Exercise 02
About
The Fibonacci sequence/series is a mathematical model that is often used in numeric optimization. It is based on a sequence of numbers in which the first two numbers in the series are 0 and 1, and each subsequent number is the sum of the previous two numbers.1
2
3n 0 1 2 3 4 5 6 7 8 9...
value: 0 1 1 2 3 5 8 13 21 34...
Update the recursive method fib to compute the Fibonacci value of n, in FibonacciTest of
L2_AlgorithmsLab. This should be the Fibonacci sequence solution in its most basic form.
Modify the program FibonacciTest so that it finds the number of times the fib method is called. Use a static variable to keep track of the calls and increment it every time the method is called.
Create a performance test, similar to that shown for the ranges in Activity 1 for the method in Question 1 for fib(50). Plot 5-6 (five to six) data points in your L2_Range_RunTimes EXCEL file , what do you observe? Are the results as expected? How does this fit with the theoretical model of the series?
The code in Question 1 was inefficient, because it takes too many recursive calls. It ends up re- computing each Fibonacci number many times. This code runs very slow for even relatively small values of n. It can take minutes or hours to compute even the 40 th or 50 th Fibonacci number on certain computers. Write a new version of the Fibonacci method _fib that is still recursive but is more efficient than the one in Question 1. Do this by creating a helper method that accepts an additional parameter, the storage for the previous Fibonacci numbers, that you can carry through and modify during each recursive call.
Write a new version of the Fibonacci method fbn that uses iteration to generate the result for the nth value in the Fibonacci sequence.
Update the program FibonacciTest to run a similar empirical tests for the three Fibonacci sequence algorithms in this activity (Q1, Q4, Q5) and update your L2_Range_RunTimes EXCEL file to show the results.
Exercise 03
About
A fractal is a geometric figure that is recursively constructed i.e. it can be divided into parts, each of which is a reduced-size copy of the whole. In fact, when one observes a fractal shape, one observes that the shape contains smaller versions of itself, so that it looks similar at all magnifications.
In this activity, we will describe a recursive method for drawing various levels of a Sierpinski triangle. A Sierpinski triangle (named after a famous Polish mathematician), is a fractal that is composed of infinitely many sub-triangles. We cannot draw the actual fractal, because it is composed of infinitely many sub-triangles, but we can write a method that produces a number of levels that approximates the actual fractal.