Question
2. Let be an array of size , let denote the -th key of ,
let be the key being searched for in , and let be the probability
for .
Suppose that is always found in with the following probability:
for
for
for
where 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 and .
Do not compute. [15]
(b) Guess the average time in terms of . [5]
(c) Compute the average time from (a). You do not have to compute the value of yet. [20]
(d) Determine the value of constant in terms of and express the average
time in terms of only. What is the asymptotic average time ? [15]
You may use the following formulas if you need them:
,
,
,
.
Solution
(a) Average time is
=
+
+
(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)
+
+
=
+
-
+
+
=
+
-
+
+
by using the formulas given above.
=
(d) Since the probabilities add to 1, we have the following equation:
+
+
=
The left hand side computes to .
Hence = holds.
From this
Substituting this value for , the average time becomes
.
This approaches , which is about .
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 or less exists between a given pair of vertives in a given graph ?
(b) Let denote the number of vertices of the given graph.
(1) A sequence of vertices (of length at most )
(2) A sequence of edges (of length at most )
(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 vertices in the sequence, its
length is a polynomial in . If this sequence represents a path of length
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 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.