May 23, 2023 @ 22:27 A. Writing Math and Writing algorithms in txt form ****************************************************** A.1 Writing Math **************** (a) Delineation: Start and Finish. A Math expression can be surrounded by a dollar sign. Thus 2*3 can be written $2*3$. A 2* 3*4 might be confusing but a $2* 3*4 $ clearly shows where it starts and where it ends. (b) Highlight math functions with a backslash if you want. A lgn or lg(n) or lg{n} can be made clearer with $\lgn$ or $\lg(n)$ or $\lg{n}$ Note the 6 ways to say the same. (c) Use or not use parentheses, curly braces, or nothing around arguments. A lgn has nothing, a lg(n) has parentheses and lg{n} has curly braces around the argument n. A better way is to write $\lgn$ or $\lg{n}$ or $\lg(n)$. (d) Complicated expressions. What is $\lg{n} * lg(n) * lgn $? Is it $lgn**3$ or $ (lgn)**3)$ of $\lg^3{n}$ ? The former might be $\lg{n**3}$ i.e. log of n-cubed, which is $3 * \lg{n}$. The middle $ (lgn)**3)$ is $\lg^3{n}$ i.e. a lazy $(\lg{n})^3$ or $(\lg{n})**3$. (e) Powers: Use two stars. I prefer to use 2**n to 2^n to indicate 2 raised to the n-th power. (But i contradict myself in (d).) Sometimes the hat symbol is difficult to read or can be missed, or you think it is there, but you are looking at a speck of dust! (f) Subscripts: Use an underscore (also known as underline) X=< x1, x2, ... , xn > will be written $X=$ Thus a_b or $a_b$ is a subscript b. If the subscript is complicated, surround it with braces, a_{2+3} or $a_{2+3}$ for a subscript 2 plus 3, or parentheses a_(2+3) or $a_(2+3)$ for a subscript 2 plus 3, (g) Superscript: Use the hat (caret) symbol. Use the hat (caret) symbol instead of an underscore (underline) symbol. Thus $2^3$ also a 2 raised to the third power (a superscript) that evaluates to 8. (h) Fractions / but numerator and denominator are parenthesized. So always write (x)/(y) and not x/y. If you use {x}/{y} it is also ok. (i) Asymptotic Notation . Easiest writing is $theta(n)$ or $Theta(n)$ $Omega(n)$ $omega(n)$ $O(n)$ $o(n)$ or more fomally with backslashes $\theta(n)$ or $\Theta(n)$ $\Omega(n)$ $\omega(n)$ $\O(n)$ $\o(n)$ or even more formally using also braces $\theta{(n)}$ or $\Theta{(n)}$ $\Omega{(n)}$ $\omega{(n)}$ $\O{ (n)}$ $\o{ (n)}$ You can even go one step further (but i won't) $\bigOmega(n)$ $\litleomega(n)$ but also an occasional $\littleOmega(n)$. $\bigOh (n)$ or $\bigoh(n)$ $\littleoh (n)$ or $\littleOh(n)$ but also an occasional $\littleO(n)$ are more reinforcing, but optional. A.2 Writing Algorithms ********************** Provide / Give / Show an algorithm means (i) present the algorithm using English and or pseudocode, or both as you see it fit, (ii) briefly and concisely show that it works as claimed, and (iii) analyze its running time by providing an asymptotically tight bound. Running time is worst-case derived running time. Giving a piece of code, or rumblings with out further description of it or proof of claims is neither 'Give an algorithm' nor 'show' nor 'provide'. A.2.1 Writing Algorithms: more info ********************** The algorithm universe is the set of algorithms covered in class. Efficient means an algorithm whose performance (space or time or both) cannot be improved upon asymptotically. Time efficient means asymptotic (worst-case derived) time efficiency cannot be improved upon. Space efficient means primarily asymptotic space efficiency cannot be improved upon. Moreover, for space efficiency a solution that uses an array, is more space efficient than a solution using a singly-linked list, and both are more space efficient than one using a doubly-linked list, and all are more space efficient than a binary search tree. Moreover a solution that uses the same array for input and output is more space efficient than one that does not. Comparison efficient means the number of comparisons cannot be improved upon and thus constants are important for higher-order terms: thus $\Theta (n)$ is worse than $3n$ , and $3n$ is worse than $2n+o(n)$. Algorithm introduces in class can be used as black-boxes without further description or justification. Thus if we defined in class QuickSort(A,n) and in an exam problem you plan to sort using QuickSort an array X of m keys you just write QuickSort(X,m). If you repeat the code of QuickSort or Partition, not only won't get you any points, but it will be interpreted for ignorance. A HashMap has not been introduced in class and it is a Java construct. If you use it you get an automatic 0. If you plan to use it properly you need to describe it fully, you need to describe fully and prove correctness of each operation needed, and you need to analyze fully the running time of those operations. And of course be reminded that running time is worst-case derived, not best case nor expected running time. Use T(.) for running time, S(.) for space resources, and C(.) for comparison counts with parameters as needed. ------------------------------------- A.2.2 An example Example. Give a time and space efficient algorithm for sorting n keys whose key values are known. Each key value is either a 0 or an 1s Step (i). We count the zeroes and the ones. We generate the output by listing the counted zeroes and then the counted ones. Step (i) better Input: A[0..n-1] where A[i] is 0 or 1, 0<= i < n. Output: sorted A (zeroes of input A preceded the ones) ExampleSort(A,n) 0. count =0; 1. for (i=0;i< n ; i++) 2. if A[i] == 0 count++; 3. for (i=0; i< count; i++) 4. A[i] = 0; 5. for (i=count; i< n; i++) 6. A[i] = 1; 7. return(A) Step (ii) We use a counter count in ExampleSort to Sort the 0s and 1s of input array A of n keys (or zeroes and ones). In line 0 we initialize count to 0. In lines 1-2 we go through A and count the 0s encountered. The number of ones is $n-count$. In lines 3-4 we reset $A$ so that the first count elements of it are set to 0 and the remaining, through lines 5-6, are set to 1. ExampleSort concludes by returning A in line 7. Step (iii). Let $T(n)$ be the running time of ExampleSort for an array A on n keys where each key value is a 0 or an 1. Running time of Line 0 is $Theta(1)$ obviously. Running time of Lines 1-2 is $Theta(n)$ since A is scanned once. Likewise Running time of Lines 3-4 is $O(n)$ since a subset/subsequence of A is set to contain 0 values. Running time of Lines 5-6 is $O(n)$ since a subset/subsequence of A is set to contain 1 values. Running time of Line 7 is $Theta(1)$ obviously. Adding all contributions we conclude that $T(n) = 2x Theta(1)+Theta(n)+ 2x O(n) = Theta(n)$. Thus $T(n)= Theta(n)$. The algorithm is time efficient since $T(n)=Theta(n)$. In order to sort the n keys we need to read their values in $Theta(n)$ time. The algorithm is space efficient. The same array is being used for input and output, that is array A. Other variables used are i, count, a total of 2. Thus constant extra space $Theta(1)$ is being used for those variables. -------------------------------------