Sie sind auf Seite 1von 7

CS 103, Homework Assignment 6 SOLUTIONS (in red and highlighted in yellow)

Your assignment is to do all 10 problems.


Searching problems
1. [Answer in English] Consider the list: L = [4 5 12 14 17 24 25 27 28
31 35 36 39].
Suppose you are using the binary search algorithm. For each of the following
targets, give a list of the numbers in B (not their indices) that would be compared
with the target in the order in which they would be compared with it.
a.
b.
c.
d.

24
4
17
15

ANS: 25, 12, 17, 24


ANS: 25, 12, 4
ANS: 25, 12, 17
ANS: 25, 12, 17, 14

2. [Answer in English] Suppose the binary-search algorithms are applied to unsorted


lists:
a. Will the recursive version ever respond that a target is on the list, when
that target is in fact not on the list?
b. Will the recursive version ever give the wrong index for a target that is on
the list?
c. When the recursive version responds that a target is not on the list, will it
ever be wrong?
d. Will the iterative version ever respond that a target is on the list, when that
target is in fact not on the list?
e. Will the iterative version ever give the wrong index for a target that is on
the list?
f. When the iterative version responds that a target is not on the list, will it
ever be wrong?
ANS: a-b NO, c YES, d-e NO, f YES.
Sorting Problems
3. [Answer in English] Suppose that in the function, selection_sort, the
following portion,
if m_min ~= m
temp = v(m);
v(m) = v(m_min);
v(m_min) = temp;
end
is replaced by

temp = v(m);
v(m) = v(m_min);
v(m_min) = temp;
a. How will the output be changed?
ANS: No change.
b. Will the average number of swaps be increased, decreased, or unchanged?
ANS: Increased.
c. Will the number of statement executions required for a sort always be
increased, always be decreased, or is it indeterminate (i.e., three possible
answers).
ANS: Indeterminate.
4. [Answer in English] The sort function, selection_sort, which is given in
Chapter 10, is applied to each of the following vectors. In each case, give the
vector after the 3rd swap.
a. 621, 687, 74, 479, 118, 199, 142, 467, 445
b. 136, 20, 28, 82, 345, 334, 303, 939, 298, 947
c. 249, 941, 705, 531, 718, 964, 449, 660, 478, 928, 841
d. 242, 853, 227, 860, 728, 140, 395, 148, 230, 883, 778, 561
ANS:
a. 74, 118, 142, 479, 687, 199, 621, 467, 445
b. 20, 28, 82, 136, 345, 334, 303, 939, 298, 947
c. 249, 449, 478, 531, 660, 964, 941, 718, 705, 928, 841
d. 140, 148, 227, 230, 728, 242, 395, 853, 860, 883, 778, 561
5. [Answer in English] In the merge_sort function given in Chapter 10, the
subfunction merge_sorted_lists(v1,v2) combines v1 and v2 into one
list that is sorted. Here is a partial listing of that function:
function x = merge_sorted_lists(x1,x2)
% The elements of x1 and x2 are assumed to be
% in ascending order. Combine them into
% ascending order and return them in x.
x = [];
while ~isempty(x1) & ~isempty(x2)
if x1(1) < x2(1)
x = [x, x1(1)]; % add x1(1) to x
x1 = Answer (a) ;
else
x = [x, x2(1)]; % add x2(1) to x
x2 = Answer (b) ;
end

end
x = [x,x1,x2];
a. What goes in place of Answer (a) ?
b. What goes in place of Answer (b) ?
ANS: a. x1 = x1(2:end); b. x2 = x2(2:end);
Problems on algorithmic complexity
6. [Answer in English]. Listed below is the worst-case time behavior t N of some
fictitious algorithms, where N is the size of the input. Give the order of each
algorithm using Big-Oh notation:
a. 2 N 3 14 N 2 3
3
b. a b log N

c. 6 log N 2 log log N


d. 4 3.2 N 2 N
3
3
ANS: a. O N , b. O log N -or- O log N , c. O log N , d. O N

Problems on symbolic mathematics:


7. [Answer in English] A car is traveling at night along a highway. The highway is
shaped like a parabola with its vertex at the origin. The car starts at a point 100
meters west and 100 meters north of the origin and travels in an easterly direction.
There is a statue located 100m east and 50m north of the origin.
(-100, 100)

(100, 50)
(x , y)

Let x represent the horizontal position of the car and y its vertical position. Give an
equation that relates y to x.
The cars path is shaped like a parabola with its vertex at the origin. By definition, a
parabola of this type has an equation of the form y = ax2. Our task then is to find a.
Since we already have a point on the parabola (the cars initial position is 100,100), we
can solve for a.
>>a=100/((100)^2)
a=
0.0100
So, the equation of the cars path is y = 0.01x2
8. [Answer in Matlab] Using Matlabs symbolic mathematics facility, give
commands that will determine the position of the car on the highway at which the
cars headlights will illuminate the statue. Assume that the statue is a point object.
The cars headlights project ahead of the vehicle in a straight line. As the car travels
around the parabola, the headlights illuminate the path directly aheadin other words,
they follow the tangent line of the parabola. To find the tangent line, we begin by finding
the slope. This is done by taking the first derivative of the parabola equation.
>>symsxy
>>y=0.01*x^2
>>tanslope=diff(y)
tanslope=
1/50*x
In other words, the slope of the tangent line = x/50. To find the actual value of the slope,
we have to plug in the value of x at the point along the parabola where the cars
headlights will strike the statue.
The temptation will be to plug in the x value of the statue.but that is NOT correct. This
equation only finds the slope for points along the parabola. If you plug in the x value of
the statue, youre finding the slope of the parabola at that x location (which is much
further north and east than the position you want).
The trick is realizing that slope is also equal to y / x. If we imagine that the point along
the parabola were looking for has coordinates (x,y) and we know the location of the
statue also falls along the tangent line, their difference will give us the slope. In other
words:

x
50 y

50 100 x

We know from the equation of the parabola that y = 0.01x2. Substituting that in for y, we
can solve for x.
>>slope_eqn=1/50*x(501/100*x^2)/(100x)
>>slope=solve(slope)
slope=
[100+50*2^(1/2)]
[10050*2^(1/2)]
>>vpa(slope)
ans=
[170.71]
[29.290]
From this answer, there are two possible x values that will cause the slope of the tangent
line to intersect the statue. If we examine those values against the graph, however, what
becomes obvious is that the first value of x works because the tangent line extends behind
the car. Since headlights only illuminate in front of the car, we can discard this solution.
The second answer, therefore, appears to be the right one.
Remember, however, that this is the value of the x-coordinate only. Our question asked
what point along the highway will cause the headlights to illuminate the statue, so we still
need a y coordinate.
>>subs(y,x,29.290)
ans=
8.5790
So, the coordinates are (29.290, 8.5790)
9. What is the equation of the headlights illumination when it strikes the statue?
Given the information we just found, we need the slope and y-intercept to describe the
tangent line. We find the slope by substituting the x-value into our first derivative.
>> subs(tanslope, x, 29.290)
ans =
0.5858
Now we need to find the y-intercept. We can use the point-slope formula to find the
intercept if we assume that one of the points is the intercept:
y y 0 m( x x 0 )

If we take (x,y) to be one of the points (either of the statue or the point on the parabola)
and (x0,y0) to be the y-intercept (where x=0 by definition), we can solve for y.
>>yint=50.5858*(1000)
yint=
8.500
>>ytanline=.5858*x8.500
So, the equation of our tangent line is: y = .5858x 8.500
10. [Answer in Matlab] When gas expands in a cylinder with radius r, the pressure at
any given time is a function of the volume: P = P(V). The force exerted by the
gas on the piston (as illustrated in the figure below) is the product of the pressure
and the area:

V
x
F = r2P. The work done by the gas when the volume expands from V1 to volume V2
is therefore:
V1

PdV

In a steam engine the pressure P and volume V of steam satisfy the equation PV1.4 = k,
where k is a constant. (This is true for adiabatic expansion that is, expansion in which
there is no heat transfer between the cylinder and its surroundings.) Using Matlabs
symbolic mathematics facility, give commands that calculate the work done by the engine
during a cycle when the steam starts at a pressure of 160 lb/in2 and a volume of 100 in3
and expands to a volume of 800 in3.
This is actually a fairly easy problem..youve just been given a lot of information to
sort through. The work done is calculated by taking the integral of Pressure, when
Pressure is dependent on Volume. In this case, the following information is needed to
solve the problem:
PV1.4 = k (where k is a constant)
P1 = 160 lb/in2

V1 = 100 in3
V2 = 800 in3
We need to begin by finding k.
>>k=160*100^1.4
k=
1.0095e+005
Now, set up P as a symbolic equation:
>>symspv
>>p=k/v^1.4;
Work is the integral of P from 100 to 800
>>work=int(p,800,100)
[work returns a really long and complicated answer. To make it readable, type:]
>>vpa(work,7)
ans=
22588.98