LMU ☀️ CMSI 2130
ALGORITHMS
Practice Final Answers
  1. Solve for $x$. No calculators. Solve exactly. Show your work. $$ 3^{\log_2(\log_5{x})} = \left(\frac{6}{\log_2{5}}\right)^{\log_2{3}} $$

    ANSWER:

    $$ \begin{eqnarray} 3^{\log_2(\log_5 x)}(\log_2 5)^{\log_2 3} & = & 6^{\log_2 3} \\ (\log_5 x)^{\log_2 3}(\log_2 5)^{\log_2 3} & = & 6^{\log_2 3} \\ (\log_5 x)(\log_2 5) & = & 6 \\ \frac{\log_2 x}{\log_2 5}(\log_2 5) & = & 6 \\ \log_2 x & = & 6 \\ x & = & 2^6 \\ x & = & 64 \\ \end{eqnarray} $$
  2. Give the complexity function $T$ for the following code fragment, both as (a) a recurrence relation and (b) in closed form using $\Theta$-notation.
    public static long f(List a) {
        if (a.size() > 1) {
            f(a.subList(0, a.size()/2));
            f(a.subList(3*a.size()/8, 7*a.size()/8));
            f(a.subList(a.size()/4, 3*a.size()/4));
            f(a.subList(a.size()/8, 5*a.size()/8));
            f(a.subList(a.size()/2, a.size()));
            for (int i = 0; i < a.size(); i++) {
                for (int j = 0; j < i; j++) {
                    System.out.print("*");
                }
            }
        }
    }
    

    ANSWER:

    Pretty much by inspection, you can see there are 5 recursive calls, each operating on a list half the size of the input, followed by the printing of $\sum_{i=1}^n{i}$ stars, so:

    $$ T(n) = 5T(\frac{n}{2}) + \frac{n(n+1)}{2} $$

    Using the Master Theorem: $a=5$, $b=2$, $d=2$, and since $d < \log_2{5}$, we have $T(n) \in \Theta(n^{\log_2{5}})$.

  3. Sometimes you will see the complexity function $\widetilde{O}$ — it is defined like this: $$ \widetilde{O}(f(n)) =_{def} O(f(n)(\log{n})^k)$$ for some positive $k$. When an algorithm is known to have a complexity function in $\widetilde{O}(n^3)$, for example, some people will just say instead that it is in $\widetilde{O}(n^{3+\varepsilon})$ — where, of course, $\varepsilon$ is understood to be some arbitrarily small positive constant. Why is this okay?

    ANSWER:

    Okay so $\widetilde{O}(f)$ is the set of all functions asymptotically bounded above by $\lambda n. cf(n)(\log n)^k$ for some constants $c$ and $k$. So let’s consider $\widetilde{O}(n^3)$. This is the set of all functions asymptotically bounded above by:

    $$ \lambda n. cf(n^3)(\log n)(\log n)(\log n)\cdots(\log n) $$

    No matter how many $\log n$ factors we have, that value will, beyond some value $N$, be less than: $$ \lambda n. cf(n^{3+\varepsilon}) $$ no matter how small you make $\varepsilon$, it still outruns all those log factors, in the limit, at least. How can we prove this? Um, well, that would be a good question to ask on Math StackExchange.

  4. Generate the ciphertext for the message "woohoo" using the bifid algorithm with key "THEQUICKBROWNFOX". Lay out the key row by row, left to right within each row, as is the convention. Show the entire derivation.

    ANSWER:

    Here’s the key:

    T H E Q U
    I C K B R
    O W N F X
    A D G L M
    P S V Y Z
    
    Now
      W   O   O   H   O   O
      2   2   2   0   2   2
      1   0   0   1   0   0
    
    yields
      22  20  22  10  01  00
       N   O   N   I   H   T
    

    The ciphertext is NONIHT.

  5. A binary min-heap starts off empty. Show its shape (actually draw the tree) after each of the steps in the following sequence. By min-heap we mean smaller values should appear on top.
    1. Insert 10
    2. Insert 4
    3. Insert 16
    4. Insert 3
    5. Insert 1
    6. Remove
    7. Insert 7
    8. Remove
    9. Remove
    10. Remove

    ANSWER:

    10   4    4       3       1       3       3       4      7     10
        /    / \     / \     / \     / \     / \     / \    / \    /
       10   10 16   4  16   3  16   4  16   4  16   7  16  10 16  16
                   /       / \     /       / \     /
                  10      10  4   10      10  7   10
    
  6. For the following graph:

    samplegraph.png

    ANSWERS INLINE:

    1. How many blocks are there? 1
    2. Give a Depth First Traversal. ABEDFC
    3. Give a Breadth First Traversal. ABECDF
    4. Give a Minimum Spanning Tree. ED-7|AC-4|BE-1|CF2|AE3
    5. Can Dijkstra's algorithm be used to find a shortest path from $F$ to $B$? No
    6. Give the shortest path from $F$ to $B$. F(6)D(-7)E(-1)B
    7. Is this graph simple? Why or why not? No, it has parallel edges
    8. Which edges are bridges? No bridges
    9. Which nodes are articulation points? No articulation points
    10. Is there a Hamilton cycle? If so, name one. If not, why not? ABEDFCA
    11. Identify a trail (any trail). A(10)B
    12. Is there an Euler tour? If so, name one. If not, why not? Yes. A(10)B(-1)E(-7)D(3)D(6)F(2)C(4)E(3)A(5)C(-4)A
    13. Give a smallest vertex cover. DCBA
    14. What is the degree of node $D$? 4
    15. How many isolated nodes are there? 0
    16. Is the graph complete? No, no link from A to D
    17. Extra credit: What is the chromatic number of this graph? 3
    18. Extra credit: What is the maximum flow of this graph?
  7. Show how to find, in polynomial time, the smallest independent set that is also a vertex cover.

    ANSWER:

    I don't know yet. I just sent Dr. Dorin a query to see if he knows.

  8. Determining whether a simple, undirected graph is 3-colorable is NP-complete, but determining whether it is 2-colorable is in P. Give a $\Theta(e)$ algorithm for determining 2-colorability.
  9. ANSWER:

    Do the standard depth-first traversal, marking nodes with colors as you go, always checking previously colored nodes. If you complete your traversal without running into any conflicts, report true. Report false on the first conflict.

  10. Is bogosort an example of a Las Vegas or Monte Carlo algorithm?

    ANSWER:

    Las Vegas, because it will eventually finish (assuming the random number generator is really random), though it might take a long, long, time.

  11. Fill out the bottom-up dynamic programming table for the 0-1 knapsack problem given a 15kg sack and the following items, processing them alphabetical order. (Note: the table is sideways to fit on the page, so you will be filling it out column by column.)

    ANSWERS INLINE:

    A
    weight=4
    value=13
    B
    weight=6
    value=10
    C
    weight=5
    value=8
    D
    weight=7
    value=5
    E
    weight=3
    value=14
    100000
    200000
    3000014
    41313131314
    51313131314
    61313131314
    71313131327
    81313131327
    91313212127
    101323232327
    111323232327
    121323232335
    131323232337
    141323232337
    151323313137