Data Structures and Algorithm Analysis in C 4th Edition Mark A
Data Structures and Algorithm Analysis in C 4th Edition Mark A
Weiss
Solutions Manual
Full Doawnload:
Doawnload:
https://solutionsmanualban
https://solutionsmanualbank.com/dow
k.com/download/solution-m
nload/solution-manual-for-d
anual-for-data-
ata-
structures-and-algorithm-analysis-in-c-4-e-mark-a-weiss/
CHAPTER 2
Algorithm Analysis
2.1 2/ N
N , 37, N , N , N log log N log N , N log( N 2), N
log N , N log N ), N log2 N , N 1.5, N 2, N 2 log N
log N , N 3, 2 N /2
/2 N
,2 .
2
N log N
log N and N
and N log ( N ) grow at the same rate.
2 2
(c) False. A counterexample is T 1( N ) = N
= N , T 2( N ) = N
= N , and f
and f ( N ) = N
= N .
log N
log N
2.3 We claim that N is the slower growing function. To see this, suppose otherwise. Then, N
that N log N
log N is
2 2
then we are claiming that L grows slower than log L
log L,, or equivalently, that L grows
L grows slower than log L.
L.
2.4 Clearly, log k1 N o(log k 2 N ) if k 1 < k 2, so we need to worry only about positive integers. The claim is
logi N logi 1 N
lim lim i
N N N N
The second limit is zero by the inductive hypothesis, proving the claim.
2.5 Let f
Let f ( N ) = 1 when N
when N is
is even, and N
and N when N
when N is
is odd. Likewise, let g
let g ( N
N ) = 1 when N
when N is
is odd, and N
and N when N
when N is
is
2 N
2.6 (a) 2
(b) O(log log D
log D))
2.7 For all these programs, the following analysis will agree with a simulation:
2
(II) The running time is O( N ).
3
(III) The running time is O( N
N ).
2
(IV) The running time is O( N ).
2 2 2
(V) j can
j can be as large as i , which could be as large as N
as N . k can
can be as large as j,
j, which is N
is N . The running time
2 2 5
is thus proportional to N
to N N
N N , which is O( N ).
3 2
(VI) The if statement
statement is executed at most N
most N times, by previous arguments, but it is true only O( N ) times
2
(because it is true exactly i times for each i). Thus the innermost loop is only executed O( N
N ) times. Each
2.8 (a) It should be clear that all algorithms generate only legal permutations. The first two algorithms have tests
to guarantee no duplicates; the third algorithm works by shuffling an array that initially has no duplicates, so
none can occur. It is also clear that the first two algorithms are completely random, and that each permutation
is equally likely. The third algorithm, due to R. Floyd, is not as obvious; the correctness can be proved by
induction. See J. Bentley, “Programming Pearls,” Communications of the ACM 30 (1987), 754 – 757.
757. Note
then not all permutations are equally likely. To see this, notice that for N =
= 3, there are 27 equally likely ways
of performing the three swaps, depending on the three random integers. Since there are only 6 permutations,
and 6 does not evenly divide 27, each permutation cannot possibly be equally represented.
(b) For the first algorithm, the time to decide if a random number to be placed in a[i] has not been used
earlier is O(i). The expected number of random numbers that need to be tried is N /(
/( N – i). This is obtained as
N 1 N 1 N 1
Ni N 2 1 N
1
N i N i N 2
N i
N 2 O (N 2 log N )
j
i 0 i 0 i 0 j 1
The second algorithm saves a factor of i for each random number, and thus reduces the time bound to
O( N log N
log N ) on average. The third algorithm is clearly linear.
(c,d) The running times should agree with the preceding analysis if the machine has enough memory. If not,
the third algorithm will not seem linear because of a drastic increase for large N .
2.9 Algorithm 1 at 10,000 is about 38 minutes and at 100,000 is about 26 days. Algorithms 1 –
1 – 4 at 1 million are
approximately: 72 years, 4 hours, 0.7 seconds, and 0.03 seconds respectively. These calculations assume a
2
(b) O( N )
(c) The answer depends on how many digits past the decimal point are computed. Each digit costs O( N ).
).
1/3
(d) 12000 times as large a problem, or input size 2,289.
2
2.13 (a) O( N ).
2.14
poly = 0
poly = 3*0 + 4 = 4
poly = 3*4 + 8 = 20
poly = 3*20 + 1 = 61
poly = 3*61 + 2 = 185
b) ∑ = aN x N + aN-1 x N-1 + … + a1 x +
+ a0 =(x…( x ( x (aN) + aN-1) + aN-2) … a1) + a0
c) O(N )
2.15
bool indexIsValue( int A[], int low, int high)
{
if (low>high) return false;
else
{
int mid = (low+high)/2;
(low+high)/2;
if (A[mid] == mid) return true;
else if (A[mid] > mid) return indexIsValue(A,low,
indexIsValue(A,low, mid-1);
else return indexIsValue(A,mid+1,high);
indexIsValue(A,mid+1,high);
}
}// O(lg(n))
2.16
2.17
a)
/**
* Linear-time minimum contiguous subsequence sum algorithm.
*/
int minSubSum(
int minSubSum( const vector
vector<<int
int>
> & a )
{
int minSum
int minSum = a[0], thisSum = 0;
for( int
for( int j
j = 1; j < a.size( ); ++j )
{
thisSum += a[ j ];
b)
/**
* Quadratic time minimum positive contiguous subsequence sum algorithm.
*/
int minPosSubSum(
int minPosSubSum( const vector
vector<
<int
int>
> & a )
{
vector<
vector <int
int>> partialSum;
int minPosSum
int minPosSum = -1;
int sum;
int sum;
partialSum.push_back(a
partialSum.push_back( a[0]);
if (
if (a
a[0] > 0)
minPosSum = a[0];
for (
for (auto
auto i
i = 1; i < a.size(); i++)
{
partialSum.push_back(a
partialSum.push_back( a[i] + a[i-1]);
if (
if (aa[i] > 0 && minPosSum == -1)
minPosSum = a[i];
else if
if (
(a
a[i] > 0 && a[i] < minPosSum)
minPosSum = a[i];
}
for (
for (auto
auto i
i = 0 ; i < a.size(); i++)
for (
for (auto
auto j
j = i+1; j < a.size(); j++)
{
sum = partialSum[j]
partialSum[j] - partialSum[i];
partialSum[i];
if (sum
if (sum > 0 && sum < minPosSum)
minPosSum = sum;
}
return minPosSum;
return minPosSum;
}// O(n + n^2) = O(n^2)
c)
/**
* linear time maximum contiguous subsequence product algorithm.
*/
double maxSubProd(
double maxSubProd( const vector
vector< <double
double>
> & a )
{
double maxVal
double maxVal = 1, maxPos = 1, maxNeg = 0;
for (
for (auto
auto i
i = 0; i < a.size(); i++)
{
if (
if (aa[i] > 0)
{
maxPos = maxPos*a
maxPos* a[i];
maxNeg = maxNeg*a
maxNeg* a[i];
}
else if
if (
(a
a[i] < 0)
{
maxNeg = -maxPos*a
-maxPos* a[i];
maxPos = -maxNeg*a
-maxNeg* a[i];//
[i];// maxNeg == 0 will cause an empty positive suffix
}
else
{
maxPos =1;
maxNeg = 0;
}
if (maxPos
if (maxPos < 1) maxPos = 1;
if (maxPos
if (maxPos > maxVal) maxVal = maxPos;
}
return maxVal;
return maxVal;
} // O(n)
2.18
/*
bisection method
assumes (f(low)*f(high) < 0 and only 1 zero between low and high)
termination ensured by choosing a tolerance (tol).
*/
#include <iostream>
#include<iostream>
using namespace
namespace std;
std;
const double
double tol
tol = 0.00001;
double f(double
double f( double x)
{
return x*x - x;
}
double bisection(
double bisection( double
double (*
(*f
f)(double
)(double),
), double low
low,
, double high
high))
{
double mid
double mid = (high
( high +
+ low
low)/2.;
)/2.;
double value;
double value;
if ((
if ((high
high -
- low
low) ) < tol)
return (mid);
return (mid);
value = f(mid);
if (value*
if (value*ff(low
low) ) < 0)
return bisection(
return bisection(f f, low
low,
, mid);
else
return bisection(
return bisection(f f, mid, high
high);
);
}
int main()
int main()
{
cout<<bisection(f,
cout<<bisection(f, .1 , 10)<<endl;
return 0;
return 0;
}
2.19
struct MaxSeq
{
int value;
int value;
int startIndex;
int startIndex;
int endIndex;
int endIndex;
MaxSeq operator
MaxSeq operator + (const
( const MaxSeq
MaxSeq &
& rhs
rhs)
) const
{
MaxSeq sum;
MaxSeq sum;
sum.value = value + rhs
rhs.value;
.value;
sum.startIndex = min(startIndex, rhs
rhs.startIndex);
.startIndex);
sum.endIndex = max(endIndex, rhs
rhs.endIndex);
.endIndex);
return sum;
return sum;
}
};
MaxSeq max(
MaxSeq max(const
const MaxSeq
MaxSeq &
& a, const MaxSeq
MaxSeq &
& b)
{
if (
if (aa.value > b.value)
return a;
else
return b;
}
/**
* Cubic maximum contiguous subsequence sum algorithm.
*/
MaxSeq maxSubSum1(
MaxSeq maxSubSum1( const vector
vector<<int
int>
> & a )
{
MaxSeq maxSum;
MaxSeq maxSum;
maxSum.value
maxSum.value = 0;
for( int
for( int i
i = 0; i < a.size( ); ++i )
for(
for( int
int j
j = i; j < a.size( ); ++j )
{
int thisSum
int thisSum = 0;
for(
for( int
int k
k = i; k <= j; ++k )
thisSum += a[ k ];
/**
* Quadratic maximum contiguous subsequence sum algorithm.
*/
MaxSeq maxSubSum2(
MaxSeq maxSubSum2( const vector
vector<<int
int>
> & a )
{
MaxSeq maxSum;
MaxSeq maxSum;
maxSum.value
maxSum.value = 0;
for(
for ( int
int i
i = 0; i < a.size( ); ++i )
{
int thisSum
int thisSum = 0;
for(
for( int
int j
j = i; j < a.size( ); ++j )
{
thisSum += a[ j ];
if( thisSum > maxSum.value
if( maxSum.value )
{
maxSum.value
maxSum.value = thisSum;
maxSum.startIndex = i;
maxSum.endIndex = j;
}
}
return maxSum;
return maxSum;
}
/**
* Recursive maximum contiguous subsequence sum algorithm.
* Finds maximum sum in subarray spanning a[left..right].
* Does not attempt to maintain actual best sequence.
*/
MaxSeq maxSumRec(
MaxSeq maxSumRec( const vector
vector<
<int
int>> & a, int left
left,, int right
right )
)
{
MaxSeq maxSum;
MaxSeq maxSum;
maxSum.value = a[left
left];
];
maxSum.endIndex = right
right;
;
maxSum.startIndex = left
left;;
if(
if ( left
left ==
== right
right )
) // Base case
{
if(
if( a[ left
left ]
] > 0 )
return maxSum;
return maxSum;
else
{ maxSum.value = 0;
return maxSum;
return maxSum;
}
}
int center
int center = ( left
left +
+ right
right )
) / 2;
MaxSeq maxLeftSum
MaxSeq maxLeftSum = maxSumRec( a, leftleft,
, center );
MaxSeq maxRightSum
MaxSeq maxRightSum = maxSumRec( a, center + 1, right
right );
);
MaxSeq maxLeftBorderSum,
maxLeftBorderSum, leftBorderSum;
maxLeftBorderSum.value
maxLeftBorderSum.value = leftBorderSum.value = 0;
leftBorderSum.startIndex = left
left;;
leftBorderSum.endIndex
leftBorderSum.endIndex = center;
for(
for ( int
int i
i = center; i >= left
left;; --i )
{
leftBorderSum.value += a[ i ];
if(
if( leftBorderSum.value > maxLeftBorderSum.value
maxLeftBorderSum.value )
maxLeftBorderSum = leftBorderSum;
}
MaxSeq maxRightBorderSum,
maxRightBorderSum, rightBorderSum;
maxRightBorderSum.value = rightBorderSum.value = 0;
rightBorderSum.startInd
rightBorderSum.startIndex
ex = center+1;
rightBorderSum.endIndex = right
right;
;
for(
for ( int
int j
j = center + 1; j <= right
right;
; ++j )
{
rightBorderSum.value += a[ j ];
if(
if( rightBorderSum.value > maxRightBorderSum.value
maxRightBorderSum.value )
maxRightBorderSum
maxRightBorderSum = rightBorderSum;
rightBorderSum;
}
/**
* Linear-time maximum contiguous subsequence sum algorithm.
*/
MaxSeq maxSubSum4(
MaxSeq maxSubSum4( const vector
vector<
<int
int>
> & a )
{
MaxSeq maxSum,
MaxSeq maxSum, thisSum;
maxSum.value
maxSum.value = thisSum.value
thisSum.value = 0;
maxSum.startIndex
maxSum.startIndex = maxSum.endIndex
maxSum.endIndex = 0;
thisSum.startIndex
thisSum.startIndex = 0;
for(
for ( int
int j
j = 0; j < a.size( ); ++j )
{
thisSum.value
thisSum.value += a[ j ];
thisSum.endIndex
thisSum.endIndex = j;
if(
if ( thisSum.value > maxSum.value )
maxSum = thisSum;
else ifif(
( thisSum.value < 0 )
{
thisSum.value
thisSum.value = 0;
thisSum.startIndex
thisSum.startIndex = j;
}
}
return maxSum;
return maxSum;
}
(b) O( N ), assuming that all divisions count for one unit of time.
(c) B =
B = O(log N
(log N ).
).
B/2
B/2
(d) O(2 ).
2
(e) If a 20-bit number can be tested in time T , then a 40-bit number would require about T time.
(f) B is
B is the better measure because it more accurately represents the size
the size of the input.
log log N
log N ).
). See Knuth, Volume 2.
2 4 8 10 20 40 60 62
2.22 Compute X
Compute X , X , X , X , X , X , X , and X
and X .
2 4 2 log N
2.23 Maintain an array that can be filled in a for loop. The array will contain X , X , X , up to X . The binary
representation of N
of N (which
(which can be obtained by testing even or odd and then dividing by 2, until all bits are
2.24 For N
For N =
= 0 or N
or N =
= 1, the number of multiplies is zero. If b( N ) is the number of ones in the binary
representation of N
of N , then if N
if N > 1, the number of multiplies used is
log N b( N ) 1
2.25 (a) A.
A.
(b) B.
B.
(c) The information given is not sufficient to determine an answer. We have only worst-case bounds.
(d) Yes.
change this. Otherwise, the last element could be a majority. Thus if N is
is odd, ignore the last element. Run
the algorithm as before. If no majority element emerges, then return the N th element as a candidate.
(d) One copy of the original needs to be saved. After this, the B
the B array,
array, and indeed the recursion, can be
O(log N
(log N ) arrays are used; this guarantees only two copies.
2.27 Start from the top-right corner. With a comparison, either a match is found, we go left, or we go down.
(b, d) Similar solutions; (b) is described here. The maximum difference is at least zero ( i j),
j), so that can be
the initial value of the answer to beat. At any point in the algorithm, we have the current value j,
j, and the
a[i], reset the current low point to i. Start with i at index 0, j
0, j at
at index 0. j
0. j just
just scans the array, so the running
time is O( N
N ).
).
2.29 Otherwise, we could perform operations in parallel by cleverly encoding several integers into one. For
instance, if A = 001, B = 101, C = 111, D = 100, we could add A and B at the same time as C and D by
2.30
a) There are RC squares to start a search. For each starting square there are 8 directions in which to
create words to look up. Since the max length of a word is 10 letters, there are possible 80RC
words. Each of these needs to be looked up in the word list. So the running time is O(RCW)
b) If the word list is sorted, one can use a binary search to loop up potential words and the running
time becomes O(RC lg (W)).
2.32
/**
* Performs the standard binary search using two comparisons per level.
* Returns index where item is found or -1 if not found.
*/
template <
template <typename
typename Comparable
Comparable>>
int binarySearch(
int binarySearch( const vector
vector< <Comparable
Comparable>
> & a, const Comparable
Comparable &
& x )
{
int low
int low = 0, high = a.size( ) - 1;
Data Structures and Algorithm Analysis in C++ 4th edition Mark A. Weiss
Solutions Manual
Full Doawnload:
Doawnload:
https://solutionsmanualban
https://solutionsmanualbank.com/dow
k.com/download/solution-m
nload/solution-manual-for-d
anual-for-data-
ata-
structures-and-algorithm-analysis-in-c-4-e-mark-a-weiss/