The exercises given here do not need to be handed in for grading. For the ICA, see this link.
Analyze bubble sort, as defined in Wikipedia; this is not a great algorithm, as Obama has said.
Show that multiplying two \(nxn\) matrices can be accomplished in \(O(n^3)\) steps.
Consider Horner’s
method for evaluating polynomials. Derive the stated complexities in the
article.
Code a bottom up version of merge sort
Suppose you have a linked list of say \(10\) nodes, how would you add/insert data into a new node at position \(5\) say?. To be concrete you can use this linked list. Your method should only assume you have access to header; that is, it should start from the header.
What would the complexity be for a merge sort where you did a \((1/3, 2/3)\) split?
Use the Master theorem to analyze Karatsuba’s multiplication algorithm.
In simple uniform hashing, with \(n\) keys and an array of length \(m\), what is the expected number of collisions? Hint: consider the indicator random variable \(X_{ij} = 1\) if the keys \(k_i\) and \(k_j\) collide, then bust through it by the power of linearity of expectation.
Code the BFS algorithm to generated the predecessor subgraph.
Is a path in a minimum weight spanning tree, also a minimum weight path?
What happens if I try to use Prim’s algorithm in the case where the edges are directed?
Let \(e_1, \ldots, e_n\) are the edges of a minimum weight path between two vertices \(s\) and \(t\), in a directed and nonegatively weighted graph. Suppose I relax the edges, starting from \(e_n\) and going back to \(e_1\). Is it possible that \(d.t \not = \delta(s,t)\).
Use memoization in a top-down coding of the Fibonacci sequence
How would you implement a ternary heap?
Let \(X = (X_0, X_1,\ldots, X_n, \ldots)\) be a Markov chain on a finite state space \(S= \{1,2, \ldots, N\}\) with a transition matrix \(P\) so that \(\mathbb{P}(X_{n+1} = j \ | \ X_n = i) = P_{ij}\) Suppose we have the initial starting distribution \(\pi\) for \(X_0\) expressed as a row vector.
Given a transition matrix \(P\) and a starting distribution \(\pi\) for a Markov chain, write code to simulate a Markov chain.
Supplementary material on Markov chains
Consider a hidden Markov model on a finite state space \(S\), with initial distribution \(\pi(i) = \mathbb{P}(X_1=i)\), transition probabilities \(p_{ij}\), and emission probabilities \(e_{xy}\). Suppose we observe \(y[1,t]=(y_1,\ldots,y_t)\). Let \[M(t; j) = \max_{x[1,t-1] \in S^{n-1}} \mathbb{P}(X_t=j, X[1,t-1]=x[1,n-1], Y=y[1,n])\]
Using dynamic programming, write down a simple expression \(M(t; j)\), where the complexity for its computation is not exponential. Hint: \(M(1; j) = \pi_j e_{jy_1}.\)
Modify your idea to output the Viterbi path; that is, a path, \(x[1,t-1], x_t=j\) that witnesses \(M(t; j).\)
Consider the change making problem, where you have \(n\) types, with integer values \(0< v_1 < \cdots < v_n\). You are to make a given integer amount \(t\), so that \(t = \sum_{i=1} ^n c_iv_i\), where \(c_i\) are non-negative integers and we want to minimize the number of coins we use: \(\sum_{i=1} ^n c_i\).
Give an example to show that greedy does not always work for the TSP.
Let \(T\) be an optimal tree representing a prefix code for the source alphabet \(S\), which includes an element \(z\), with probability \(p_z\). Suppose that \(x,y \not \in S\) and that for some numbers \(0 \leq p_x,p_y\), we have \(p_x + p_y = p_z\). Consider the modified source alphabet \(S'\) with \(z\) removed and \(x\) and \(y\) added. Consider the tree \(T'\) defined from \(T\), where we give \(z\) the two children \(x\) and \(y\). Here, we have split \(z\). Now \(T'\) gives a code for \(S'\).
Show that you can generate the Huffman code in \(O(n \log n)\) time, by using a priority queue.
Describe in detail, how one deletes a node from a linked list.
What is:
Consider a finite set of integers \(S\) of size \(n\) Let \(z\) be a given integer.
Consider Dijkstra’s algorithm applied to a graph \(G = (V, E, w)\). Let \(S\) be the initial set that is set to be empty and eventually should be all of \(V\). Show that when we decide to add a node \(v\) to the set \(S\), at this instance, we must have that \(v\) is adjacent to \(S\).
Manually carry out Dijkstra’s algorithm in concrete example.
Consider the case of simple uniform hashing, with a hash function \(h\) mapping into the \(m\) slots of a hash table \(T\). On average, how many keys do I need, before every slot of the hash table is filled. Hint: Let \(W_i\) be the number of keys that are needed, after \(i-1\) slots have been filled; notice that \(W_1=1\).
Consider a hash function \(h\) with \(m\) slot and a hash table \(T[0, \ldots, m-1]\) that resolves collisions by chaining. Suppose that we have random keys \(K_1, \ldots, K_n\), and we do not have uniform hashing, but instead have \(h(K_1), \ldots, h(K_n)\) are iid random variables such that \(\mathbb{P}(h(K_1)=i) = p_i\). For each fixed key \(k\), find the average-case time of resolving an unsuccessful search.
Use the Master theorem to get the complexity of \(T\) where, \(T(n) = 7T(n/2) + \Theta(n^2)\).
Let \(A\) be an ordered array of integers of size \(n\). Write pseudocode for determining for an integer \(k\) in the array, that does better than \(O(n)\).
What is merge sort? Use the Master theorem to get the complexity of merge sort.
Give a simple example on four vertices to show that you really might need every relaxation performed by the Bellman-Ford algorithm.
Consider Prim’s algorithm.
Let \(T\) be a tree representing a prefix code for a source alphabet with distribution \(p\), containing the elements \(x\) and \(y\). Suppose that \(x\) and \(y\) are siblings and we create the merged tree \(T'\), with \(p_z:= p_x + p_y\), for new source alphabet with \(x\) and \(y\) removed and \(z = (x,y)\) added. Show that \[L(T') = L(T) - p_z.\]
Show by example that the Shannon code can be strictly worse than the Huffman code.
Version: 27 November 2026