Review for CS141 Midterm Test Feb., 2008 TOPICS COVERED on the midterm 1. Fundamentals of algorithms and analysis Concepts of algorithms, pseudo-code, time complexity/efficiency, worst-case, average-case, asymptotic notation, big-O (for upper bound), big-Omega (for lower bound), Theta (for tight bound), manipulation of the notations, connection to limit. Math tools: log/exp/poly functions, summation and their asymptotic expressions, max/min operator, arithmetic/geometric progression, recurrence relation and techniques for solving recurrences (backward substitution, the Master Theorem, linear homogeneous recurrences). How to use these concepts and tools in analyses. Main data structrues learned in CS14 and efficient use of them in algorithms. 2. Brute force and exhaustive search algorithms Selection sort, bubble sort, sequential search, the naive string matching algorithm, closest pair, convex-hull, TSP, Knapsack, and assignment. The worst-case analyses of these algorithms. 3. Divide-and-conquer algorithms Mergesort, quicksort, binary tree traversals, fast multiplication of integers and matrices, closest pair and convex-hull revisited. The analyses of these algorithms and why they are more efficient than the brute force solutions. 4. Decrease-and-conquer algorithms DFS and BFS in graphs and directed graphs, topological sorting, Euclid's algorithm, selection, interpolation search. How would you solve the problem otherwise (without using the reduction/induction technique)? TYPES OF TEST QUESTIONS * Short statements. What are the differences between an algorithm and a program? What factors determine the speed of a program? When is a divide-and-conquer algorithm very efficient and when is it now? Is binary search really a divide-and-conquer algorithm? What kind of strings would make the brute force string matching algorithm to run in its worst-case (i.e. quadratic) time? * Given a math function/summation, find its asymptotic bound in theta 3n^2 + 2nlog n + 3^n = ? n --- \ / i^2 + i log (i+1) = ? --- i=1 * Given a recurrence relation, solve it by finding its closed form f(n) = 2, if n <= 1 f(n) = f(n-2) + n, n > 1 * Analyze the time complexity for a given algorithm (written in pseudo-code) (a) x = 0; y = 1; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) x++; y = x * x; (b) x = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n * n; j++ ) x++; (c) x = 0; for( i = 0; i < 2 * n; i++ ) for( j = 0; j < 3 * i; j++ ) x++; (d) x = 0; for( i = 1; i < n; i++ ) for( j = 0; j < i * log i; j++ ) x++; (e) x = 0; i = 1; while( i < n ) { i = 2 * i; x++; } (f) algorithm Test(A[1..n]); for i := 1 to ... for j := 1 to ... while .... call Test(A[i..j]); (g) Prove that in the Euclid's algorithm, the parameter n is reduced by at least a factor of two after two iterations. * Run an existing/known algorithm on a given instance Apply the Strassen's algorithm on two input matrices Run the topological sorting algorithm on some DAG * Design an efficient (new) algorithm in pseudo code for a given (simple) problem, perhaps with a target efficiency/running time. Some examples: 1) Write a brute force algorithm for finding a Hamiltonian circuit. 2) Write an algorithm to sort 4 keys in 5 comparisons. (hint: divide-and-conquer) 3) Given two sequences A and B, write an algorithm to decide if A is a subsequence of B in O(|B|) time. E.g. 32123 is a subsequence of 123123123123, but 321321 is not. (hint: perform a sequential search of the letters of A in B) 4) Given two sets A and B of n and m integers, respectively, determine if A is contained in B. Assume that n < m. Can you do this in O(m*log m) time? (hint: Heapsort/Mergesort A and B and compare the sorted lists sequentially.) Moreover, can you do this in O(m*log n) time? (hint: Sort A and binary search for each element of B in A. Mark off each element of A that is also ontained in B.) 5) Levitin, p. 106, Question 9. 6) Levitin, p. 171, Questions 6 and 7. 7) Levitin, p. 187, Question 3 (3-way algorithm to find a fake coin). 8) Questions in homeworks 1 and 2. How? Use design techniques learned, your basic training in math and programming (sometimes common sense), work on a few small instances to develop intuition before you start pseudo-coding.