CSC 103 Lecture Notes Week 1
Introduction to the Course
Review of Math and Java Background
Introduction to Algorithm Analysis
XAXB = XA+B
XA / XB = XA-B
(XA)B = XAB
XN + XN = 2XN != X2N
2N + 2N = 2N+1
XA = B iff logX B = A
log 2N = N
logA B = logC B / logC A , for A > 1, B,C > 0
log AB = log A + log B , for A,B > 0
log(AB) = B log A , for A,B > 0
log X < X, for all X > 0
N
sum 2i = 2N+1 - 1
i=1
N
sum Ai = (AN+1 - 1) / (A - 1)
i=1
N
sum i = N (N+1) / 2
i=1
A mod N = the remainder after division of A by N.
if A = B mod N then A + C = B + C mod N
if A = B mod N then AD = BD mod N
/** * Return the sum of the elements in the given numbers array, starting at * the given index position. If the position is past the end of the array, * return 0. */ public int sumArray(int[] numbers, int position) { /* * Base case: Return 0 if the position is past the end of the array. */ if (position >= numbers.length) return 0; /* * Recursive step: Add the element at the current position to the * recursive sum of the elements at position+1 to the end. */ else return numbers[position] + sumArray(numbers, position+1); }
/** * Return the largest element in the given array of integers. Return * Integer.MIN_VALUE if the array is null or empty. */ public int findLargest(int[] numbers) { int largestValue; // Largest value so far int i; // Array index /* * Return Integer.MIN_VALUE if the array is null or empty. */ if ((numbers == null) || (numbers.length == 0)) { return Integer.MIN_VALUE; } /* * Initialize the largest value to the zeroth array element, then loop * through the remaining elements, reassigning the largest value * whenever one of the elements is greater than the largest so far. */ for (largestValue = numbers[0], i = 1; i < numbers.length; i++) { if (largestValue < numbers[i]) { largestValue = numbers[i]; } } return largestValue; }
/** * Return the largest element in the given array of comparable objects. * Return null if the array is null or empty. */ public Object findLargest(Comparable[] objects) { Comparable largestValue; // Largest value so far int i; // Array index /* * Return null if the array is null or empty. */ if ((objects == null) || (objects.length == 0)) { return null; } /* * Initialize the largest value to the zeroth array element, then loop * through the remaining elements, reassigning the largest value * whenever one of the elements is greater than the largest so far. */ for (largestValue = objects[0], i = 1; i < objects.length; i++) { if (largestValue.compareTo(objects[i]) < 0) { largestValue = objects[i]; } } return largestValue; }
import java.io.*; import java.util.*; /**** * * Class BasicRecursionTest illustrates the use of a very basic recursive * function to sum the elements in an array. The elements are read from stdin * and stored in the array. The resulting sum is printed to stdout. * <p> * The summing computation is performed by the sumArray method in the <a href= * BasicRecursion.html> BasicRecursion </a> class. * * @author Gene Fisher (gfisher@calpoly.edu) * @version 2apr01 * */ public class BasicRecursionTest { /** * Call the recursiveSum method and print the result to stdout. */ public static void main(String args[]) throws IOException { BasicRecursion basic; // The class with the method BufferedReader reader; // Input stream reader StringTokenizer toker; // Input stream tokenizer final int NUM_ELEMENTS = 10; // Number of inputs allowed int[] numbers // Input array = new int[NUM_ELEMENTS]; int i; // Working array index /* * Instantiate a BasicRecursion class object. */ basic = new BasicRecursion(); /* * Prompt for the input. */ System.out.print("0nput up to " + Integer.toString(NUM_ELEMENTS) + " numbers: "); /* * Initialize the input reader and tokenizer. */ reader = new BufferedReader(new InputStreamReader(System.in)); toker = new StringTokenizer(reader.readLine()); /* * Read each input from the terminal. */ for (i = 0; toker.hasMoreElements() && i < 10; ) { /* * Store the next input in the array, ignoring any non-numeric * input. */ try { numbers[i] = Integer.parseInt(toker.nextToken()); } catch (NumberFormatException e) { continue; } /* * Increment the input index if the read was a legal number. */ i++; } /* * Output the array sum to stdout. */ System.out.print("The sum is: "); System.out.println(basic.sumArray(numbers, 0)); System.out.println(); } }
End of Text Chapter 1
On to Chapter 2 on Algorithm Analysis
The plot graphs on page 34 of the book provide helpful visualizations of how fast these functions grow in relation to one another.
Function Name Goodness O(c) or O(1) Constant Best O(log N) Logarithmic Very good O(log2 N) or O(log log N) Log-squared Very good O(N) Linear Generally good O(N log N) Decent, particularly for sorting O(N2) Quadratic OK, but we'd prefer something better O(N3) Cubic Ditto O(Nk) Polynomial Not so good, but we can live with it if we have to O(2N), O(XN), O(N!) Exponential Bad news
is O(N), assuming that the body does not break out of the loop.for (i = 0; i < N; i++) { ... }
is O(N2); (think about why this is the case).for (i = 0; i < N; i++) { for (j = 0; j < N; i++) { ... } }
for (i = 0; i < N; i++) { ... } for (i = 0; i < N; i++) { ... } for (i = 0; i < N; i++) { for (j = 0; j < N; i++) { ... } }is O(N2) overall, since O(N) + O(N) + O(N2) = O(N2)
/** * Return the position in the given numbers array of the first occurrence of * the given numeric value. Return -1 if the element is not in the array * or if the array is null. */ public int searchArray(int[] numbers, int value) { int i; // Array index /* * Return -1 if the array is null. */ if (numbers == null) { return -1; } /* * Loop through the array, comparing the input value with each * element. Return the index of the first element that equals the * value, if any. */ for (i = 0; i < numbers.length; i++) { if (numbers[i] == value) { return i; } } return -1; }
which equals(1 + 2 + 3 + ... + N) / N
N
( sum i ) / N
i=1
N
sum i = (N (N+1)) / 2
i=1
which reduces to 1 = 1.
1
sum i = (1 (1+1)) / 2
i=1
and prove
N
sum i = (N (N+1)) / 2
i=1
Working from the summation side of the equation, we have
N+1
sum i = ((N+1) (N+1+1)) / 2
i=1
= (N (N+1)) / 2 + N + 1 , by the inductive hypothesis
N
sum i + N+1 , by the definition of summation
i=1