Solutions to CS 600 Test I


February 28, 2002



Question

1. Find the big-oh relationships for the following functions. Give your calculations. [25]

$n^{2}$, $2^{n \ln n}$, $n^{n}$, $(\ln n )^{2}$, $3 n^{2} + 5 n \ln n$.

Solution

First guess the order as follows:

$(\ln n )^{2}$ $\prec$ {$n^{2}$, $3 n^{2} + 5 n \ln n$} $\prec$ $n^{n}$ $\prec$ $2^{n \ln n}$,
where $\prec$ indicates the big-oh relation.

Then let us try to check our guess.

(1) $n^{2} = \Theta (3 n^{2} + 5 n \ln n)$
For $\lim_{n \leftarrow \infty} (3 n^{2} + 5 n \ln n)/n^{2}$ = $\lim_{n \leftarrow \infty}(3+ 5\ln n/n)$ = 3 + $\lim_{n \leftarrow \infty}(5\ln n/n)$ = 3 + $\lim_{n \leftarrow \infty}(5(1/n)/1)$ = 3 + $\lim_{n \leftarrow \infty}(5/n))$ = 3
Hence $n^{2} = \Theta (3 n^{2} + 5 n \ln n)$.

(2) $(\ln n )^{2}$ = O( $n^{2}$ )
For $\lim_{n \leftarrow \infty} (\ln n)^{2}/n^{2}$ = $\lim_{n \leftarrow \infty} (1/n)(2 \ln n) / 2n$ = $\lim_{n \leftarrow \infty} \ln n / n^{2}$ = $\lim_{n \leftarrow \infty} (1/n) / 2n $ = $\lim_{n \leftarrow \infty} 1 / 2n^{2} $ = $0$.
Hence $(\ln n )^{2}$ = O( $n^{2}$ ).

(3) $n^{2}$ = O( $n^{n}$ )
For $\lim_{n \leftarrow \infty} n^{n} / n^{2} $ = $\lim_{n \leftarrow \infty} n^{n-2}$ = $\infty$.
Hence $n^{2}$ = O( $n^{n}$ ).

(4) $2^{n \ln n} = O( n^{n}$)
For $2^{n \ln n} = 2^{\ln n^{n}} $
= $2^{\lg n^{n} / \lg e}$, where $e$ is the base of the natural log.
= $( 2^{\lg n^{n}} ) ^{1/\lg e}$ = $ ( n^{n} )^{1/\lg e}$ .
Since $\lg e > 1$, $ ( n^{n} )^{1/\lg e} < n^{n} $
Hence $2^{n \ln n} = O( n^{n}$) . Hence $2^{n \ln n} = O( n^{n}$)

Hence $(\ln n )^{2}$ $\prec$ {$n^{2}$, $3 n^{2} + 5 n \ln n$} $\prec$ $n^{n}$ $\prec$ $2^{n \ln n}$


Question

2. Let $L$ be an array of size $4n$, let $L[i]$ denote the $i$-th key of $L$, let $x$ be the key being searched for in $L$, and let $p(i)$ be the probability for $x = L[i]$.
Suppose that $x$ is always found in $L$ with the following probability:

$p(i) = 2ci$ for $1 \leq i \leq n$
$= ci + cn $ for $n + 1 \leq i \leq 2n$
$= 3cn $ for $2n + 1 \leq i \leq 4n$

where $c$ is a constant.

(a) Formulate the equation for computing the average time of the Sequential Search with the probability distribution given above in terms of $c$ and $n$. Do not compute. [15]
(b) Guess the average time in terms of $n$. [5]
(c) Compute the average time from (a). You do not have to compute the value of $c$ yet. [20]
(d) Determine the value of constant $c$ in terms of $n$ and express the average time in terms of $n$ only. What is the asymptotic average time ? [15]

You may use the following formulas if you need them:

$\Sigma_{i=1}^{n} i2^{i} = (n - 1) 2^{n + 1} + 2$, $\Sigma_{i=1}^{n} i^{2} = n( n + 1 )(2n + 1)/6$,
$\Sigma_{i=1}^{n} i^{3} = ( n( n + 1 )/2 )^{2}$, $\lg (n!) = \Theta(n\lg n)$.

Solution

(a) Average time is $\Sigma_{i=1}^{4n} ip(i)$
= $\Sigma_{i=1}^{n} 2ci^{2}$ + $\Sigma_{i=n+1}^{2n} (ci^{2} + nci)$ + $\Sigma_{i=2n+1}^{4n} 3nci$

(b) My guess would be "somewhere between 2n and 3n" because of the way x is distributed.
Since i also represents the number of comparisons used for each case, plotting p(i) against i gives us the probability distribution for the number of comparisons. By looking at that graph you can guess the average number of comparisons to be between 2n and 4n more toward 2n than toward 4n.

(c) Continueing from (a)
$\Sigma_{i=1}^{n} 2ci^{2}$ + $\Sigma_{i=n+1}^{2n} (ci^{2} + nci)$ + $\Sigma_{i=2n+1}^{4n} 3nci$
= $2c\Sigma_{i=1}^{n} i^{2}$ + $c\Sigma_{i=1}^{2n} i^{2}$ - $c\Sigma_{i=1}^{n} i^{2}$ + $nc\Sigma_{i=n+1}^{2n} i$ + $3nc\Sigma_{i=2n+1}^{4n} i$
= $2cn(n+1)(2n+1)/6$ + $2cn(2n+1)(4n+1)/6$ - $cn(n+1)(2n+1)/6$ + $cn^{2}(3n+1)/2$ + $6cn^{2} (6n+1)/2$
by using the formulas given above.
= $cn (135n^{2} + 36n + 3)/6$ (d) Since the probabilities add to 1, we have the following equation:
$\Sigma_{i=1}^{n} 2ci$ + $\Sigma_{i=n+1}^{2n} (ci + cn)$ + $\Sigma_{i=2n+1}^{4n} 3nc$ = $1$
The left hand side computes to $cn(18n + 3)/2$.
Hence $cn(18n + 3)/2$ = $1$ holds.
From this $c = 2/3n(6n+1)$

Substituting this value for $c$, the average time becomes $(139n^{2} + 38n + 3)/(54n + 9)$. This approaches $139n/54$, which is about $2.6n$.


Question

3. The length of a path in a graph is the number of edges in the path. The shortest path problem seeks to find a path of the shortest length between a give pair of vertices. Answer the following questions:

(a) Give a decision problem version of the shortest path problem. Let us call it the shortest path decision problem (SPDP). [5]
(b) Give two certificates for SPDP. [5]
(c) Prove that SPDP is in class NP. [10]

Solution

(a) Does a path of length $k$ or less exists between a given pair of vertives in a given graph ?

(b) Let $n$ denote the number of vertices of the given graph.
(1) A sequence of vertices (of length at most $k+1$)
(2) A sequence of edges (of length at most $k$)
(3) An algorithm to find a shortest path

(c) Let us use a sequence of vertices as a certificate.
Then since we need at most $n$ vertices in the sequence, its length is a polynomial in $n$. If this sequence represents a path of length $k$ between the given pair of vertices, then the answer to SPDP is yes.
So what we need to do to verify the certificate are:
(1) Check if the length is $k$ or not.
(2) Check if it is between the specified vertices.
(3) Check if an edge exists between every pair of consecutive vertices.
It should be easy to see that these can be checked in polynomial time.