• Home
  • Textbooks
  • Foundations of Algorithms using C++ Pseudocode
  • Divide-and-Conquer

Foundations of Algorithms using C++ Pseudocode

Richard Neapolitan, Kumarss Naimipour

Chapter 2

Divide-and-Conquer - all with Video Answers

Educators


Chapter Questions

02:30

Problem 1

Use Binary Search, Recursion (Algorithm 2.1) to search for the integer 120 in the following list (array) of integers. Show the actions step by step.
$\begin{array}{lllllllll}12 & 34 & 37 & 45 & 57 & 82 & 99 & 120 & 134\end{array}$

RO
Reynald Oliveria
Numerade Educator
01:18

Problem 2

Suppose that, even unrealistically, we are to search a list of 700 million items using Binary Search, Recursion (Algorithm 2.1). What is the maximum number of comparisons that this algorithm must perform before finding a given item or concluding that it is not in the list?

Narayan Hari
Narayan Hari
Numerade Educator
02:58

Problem 3

Let us assume that we always perform a successful search. That is, in Algorithm 2.1 the item $x$ can always be found in the list $S .$ Improve Algorithm 2.1 by removing all unnecessary operations.

James Chok
James Chok
Numerade Educator
02:25

Problem 4

Show that the worst-case time complexity for Binary Search (Algorithm 2.1) is given by
$W(n)=[\lg n]+1$
When $n$ is not restricted to being a power of $2 .$ Hint. First show that the recurrence equation for $W(n)$ is given by
$W(n)=1+W\left(\left\lfloor\frac{n}{2}\right\rfloor\right) \quad$ for $n>1$
$W(1)=1$
To do this, consider even and odd values of $n$ separately. Then use induction to solve the recurrence equation.

James Kiss
James Kiss
Numerade Educator
04:22

Problem 5

Suppose that, in Algorithm 2.1 (line 4), the splitting function is changed to $m i d=$ low.. Explain the new search strategy. Analyze the performance of this strategy and show the results using order notation.

Bryan Lynn
Bryan Lynn
Numerade Educator
07:42

Problem 6

Write an algorithm that searches a sorted list of $n$ items by dividing it into three sublists of almost $n / 3$ items. This algorithm finds the sublist that might contain the given item and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Analyze your algorithm and give the results using order notation.

Bryan Lynn
Bryan Lynn
Numerade Educator
02:34

Problem 7

Use the divide-and-conquer approach to write an algorithm that finds the largest item in a list of $n$ items. Analyze your algorithm, and show the results in order notation.

Adriano Chikande
Adriano Chikande
Numerade Educator
01:12

Problem 8

Use Mergesort (Algorithms 2.2 and 2.4 ) to sort the following list. Show the actions step by step.
$\begin{array}{llllllll}123 & 34 & 189 & 56 & 150 & 12 & 9 & 240\end{array}$

Nick Johnson
Nick Johnson
Numerade Educator
07:02

Problem 9

Give the tree of recursive calls in Exercise 8.

Lucas Gagne
Lucas Gagne
Numerade Educator
02:34

Problem 10

Write for the following problem a recursive algorithm whose worst-case time complexity is not worse than $\Theta(n \ln n)$. Given a list of $n$ distinct positive integers, partition the list into two sublists, each of size $n / 2,$ such that the difference between the sums of the integers in the two sublists is maximized. You may assume that $n$ is a multiple of 2.

Adriano Chikande
Adriano Chikande
Numerade Educator
01:49

Problem 11

Write a nonrecursive algorithm for Mergesort (Algorithms 2.2 and 2.4 ).

SS
Sarvesh Somasundaram
Numerade Educator
02:25

Problem 12

Show that the recurrence equation for the worst-case time complexity for Mergesort (Algorithms 2.2 and 2.4 ) is given by
$W(n)=W\left(\left\lfloor\frac{n}{2}\right\rfloor\right)+W\left(\left\lceil\frac{n}{2}\right\rceil\right)+n-1$
when $n$ is not restricted to being a power of 2

James Kiss
James Kiss
Numerade Educator
04:31

Problem 13

Write an algorithm that sorts a lists of $n$ items by dividing it into three sublists of about $n / 3$ items, sorting each sublist recursively and merging the three sorted sublists. Analyze your algorithm, and give the results under order notation.

Bryan Lynn
Bryan Lynn
Numerade Educator
01:38

Problem 14

Given the recurrence relation
\[\begin{array}{l}
T(n)=7 T\left(\frac{n}{5}\right)+10 n \quad \text { for } n>1 \\T(1)=1\end{array}\]
find $T(625)$

Adriano Chikande
Adriano Chikande
Numerade Educator
16:32

Problem 15

Consider algorithm solve given below. This algorithm solves problem $P$ by finding the output (solution) $O$ corresponding to any input $l$.
void solve (input I, output& O)
{
if (size (I) == 1)
find solution O directly;
else{
partition I into 5 inputs I1, I2, I3, I4, I5, where
size (Ij) = size (I)/3 for j = 1, ..., 5;
for (j = 1; j < = 5; j++)
solve (Ij, Oj);
combine O1, O2, O3, O4, O5 to get O for P with input I;
}
}
Assume $g(n)$ basic operations for partitioning and combining and no basic operations for an instance of size 1
a. Write a recurrence equation $T(n)$ for the number of basic operations needed to solve $P$ when the input size is $n$
b. What is the solution to this recurrence equation if $g(n) \in \Theta(n) ?$ (Proof is not required.)
c. Assuming that $g(n)=n^{2}$, solve the recurrence equation exactly for $n=27$
d. Find the general solution for $n$ a power of 3

Jacquelyn Trost
Jacquelyn Trost
Numerade Educator
01:32

Problem 16

Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size $n$ of a problem into 10 subinstances of size $n / 3$, and the dividing and combining steps take a time in $\Theta\left(n^{2}\right)$. Write a recurrence equation for the running time $T(n),$ and solve the equation for $T(n)$.

Akash M
Akash M
Numerade Educator
14:24

Problem 17

Write a divide-and-conquer algorithm for the Towers of Hanoi problem. The Towers of Hanoi problem consists of three pegs and $n$ disks of different sizes. The object is to move the disks that are stacked, in decreasing order of their size, on one of the three pegs to a new peg using the third one as a temporary peg. The problem should be solved according to the following rules: (1) when a disk is moved, it must be placed on one of the three pegs;
(2) only one disk may be moved at a time, and it must be the top disk on one of the pegs; and (3) a larger disk may never be placed on top of a smaller disk.
a. Show for your algorithm that $S(n)=2^{n}-1$. (Here $S(n)$ denotes the number of steps (moves), given an input of $n$ disks.)
b. Prove that any other algorithm takes at least as many moves as given in part (a).

Ibrahima Barry
Ibrahima Barry
Numerade Educator
01:24

Problem 18

When a divide-and-conquer algorithm divides an instance of size $n$ of a problem into subinstances each of size $n / c,$ the recurrence relation is typically given by
\[\begin{array}{l}T(n)=a T\left(\frac{n}{c}\right)+g(n) \quad \text { for } n>1 \\
T(1)=d\end{array}\]
where $g(n)$ is the cost of the dividing and combining processes, and $d$ is a constant. Let $n=c^{k}$
a. Show that
\[T\left(c^{k}\right)=d \times a^{k}+\sum_{j=1}^{k}\left[a^{k-j} \times g\left(c^{j}\right)\right]\]
b. Solve the recurrence relation given that $g(n) \in \Theta(n)$

James Kiss
James Kiss
Numerade Educator
02:00

Problem 19

Use Quicksort (Algorithm 2.6) to sort the following list. Show the actions step by step.
$\begin{array}{llllllll}123 & 34 & 189 & 56 & 150 & 12 & 9 & 240\end{array}$

Bryan Lynn
Bryan Lynn
Numerade Educator
07:02

Problem 20

Give the tree of recursive calls in Exercise 19

Lucas Gagne
Lucas Gagne
Numerade Educator
02:25

Problem 21

Show that if
\[W(n) \leq \frac{(p-1)(p-2)}{2}+\frac{(n-p)(n-p-1)}{2}+n-1\]
then
\[W(n) \leq \frac{n(n-1)}{2} \quad \text { for } 1 \leq p \leq n\]
This result is used in the discussion of the worst-case time complexity analysis of Algorithm 2.6 (Quicksort).

James Kiss
James Kiss
Numerade Educator
03:09

Problem 22

Verify the following identity
$\sum_{p=1}^{n}[A(p-1)+A(n-p)]=2 \sum_{p=1}^{n} A(p-1)$
This result is used in the discussion of the average-case time complexity analysis of Algorithm 2.6 (Quicksort).

Brittany Knowlton
Brittany Knowlton
Numerade Educator
01:12

Problem 23

Write a nonrecursive algorithm for Quicksort (Algorithm 2.6). Analyze your algorithm, and give the results using order notation.

Nick Johnson
Nick Johnson
Numerade Educator
02:41

Problem 24

Assuming that Quicksort uses the first item in the list as the pivot item:
a. Give a list of $n$ items (for example, an array of 10 integers) representing the worst-case scenario.
b. Give a list of $n$ items (for example, an array of 10 integers) representing the best-case scenario.

Zack Spears
Zack Spears
Numerade Educator
View

Problem 25

Show that the number of additions performed by Algorithm 1.4 (Matrix Multiplication) can be reduced to $n^{3}-n^{2}$ after a slight modification of this algorithm.

Nick Johnson
Nick Johnson
Numerade Educator
03:22

Problem 26

In Example $2.4,$ we gave Strassen's product of two $2 \times 2$ matrices. Verify the correctness of this product.

Kayleah Tsai
Kayleah Tsai
Numerade Educator
03:43

Problem 27

How many multiplications would be performed in finding the product of two $64 \times 64$ matrices using the standard algorithm?

James Chok
James Chok
Numerade Educator
03:43

Problem 28

How many multiplications would be performed in finding the product of two $64 \times 64$ matrices using Strassen's method (Algorithm 2.8)?

James Chok
James Chok
Numerade Educator
07:07

Problem 29

Write a recurrence equation for the modified Strassen's algorithm developed by Shmuel Winograd that uses 15 additions/subtractions instead of $18 .$ Solve the recurrence equation, and verify your answer using the time complexity shown at the end of Section 2.5.

Bryan Lynn
Bryan Lynn
Numerade Educator
01:49

Problem 30

Use Algorithm 2.10 (Large Integer Multiplication 2 ) to find the product of 1253 and 23,103.

Maurizia De Palma
Maurizia De Palma
Numerade Educator
00:11

Problem 31

How many multiplications are needed to find the product of the two integers in Exercise $30 ?$

Tiffany Tran
Tiffany Tran
Numerade Educator
06:04

Problem 32

Write algorithms that perform the operations
$u \times 10^{m} ; u$ divide $10^{m} ; u$ rem $10^{m}$
where $u$ represents a large integer, $m$ is a nonnegative integer, divide returns the quotient in integer division, and rem returns the remainder. Analyze your algorithms, and show that these operations can be done in linear time.

Audrey Fong
Audrey Fong
Numerade Educator
01:10

Problem 33

Modify Algorithm 2.9 (Large Integer Multiplication) so that it divides each $n$ digit integer into
a. three smaller integers of $n / 3$ digits (you may assume that $n=3^{k}$ ).
b. four smaller integers of $n / 4$ digits (you may assume that $n=4^{k}$ ).
Analyze your algorithms, and show their time complexities in order notation.

James Chok
James Chok
Numerade Educator
02:34

Problem 34

Implement both Exchange Sort and Quicksort algorithms on your computer to sort a list of $n$ elements. Find the lower bound for $n$ that justifies application of the Quicksort algorithm with its overhead.

Adriano Chikande
Adriano Chikande
Numerade Educator
00:22

Problem 35

Implement both the standard algorithm and Strassen's algorithm on your computer to multiply two $n \times n$ matrices $\left(n=2^{k}\right) .$ Find the lower bound for $n$ that justifies application of Strassen's algorithm with its overhead.

Clarissa Noh
Clarissa Noh
Numerade Educator
06:04

Problem 36

Suppose that on a particular computer it takes $12 n^{2}$ \mus to decompose and recombine an instance of size $n$ in the case of Algorithm 2.8 (Strassen). Note that this time includes the time it takes to do all the additions and subtractions. If it takes $n^{3}$ \mus to multiply two $n \times n$ matrices using the standard algorithm, determine thresholds at which we should call the standard algorithm instead of dividing the instance further. Is there a unique optimal threshold?

Audrey Fong
Audrey Fong
Numerade Educator
07:15

Problem 37

Use the divide-and-conquer approach to write a recursive algorithm that computes $n ! .$ Define the input size (see Exercise 34 in Chapter 1 ), and answer the following questions. Does your function have an exponential time complexity? Does this violate the statement of case 1 given in Section 2.8.

Lucas Gagne
Lucas Gagne
Numerade Educator
01:32

Problem 38

Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size $n$ of a problem into $n$ subinstances of size $n / 3$, and the dividing and combining steps take linear time. Write a recurrence equation for the running time $T(n),$ and solve this recurrence equation for $T(n)$ Show your solution in order notation.

Akash M
Akash M
Numerade Educator
02:06

Problem 39

Write an efficient algorithm that searches for a value in an $n \times m$ table (two-dimensional array). This table is sorted along the rows and columns that is.
\[\begin{array}{l}\text { Table }[i][j] \leq \text { Table }[i][j+1] \\
\text { Table }[i][j] \leq \text { Table }[i+1][j]\end{array}\]

Adriano Chikande
Adriano Chikande
Numerade Educator
01:19

Problem 40

Suppose that there are $n=2^{k}$ teams in an elimination tournament, in which there are $n / 2$ games in the first round, with the $n / 2=2^{k-1}$ winners playing in the second round, and so on.
a. Develop a recurrence equation for the number of rounds in the tournament.
b. (b) How many rounds are there in the tournament when there are 64 teams?
c. Solve the recurrence equation of part (a).

James Chok
James Chok
Numerade Educator
02:13

Problem 41

Write a recursive $\Theta(n \lg n)$ algorithm whose parameters are three integers $x, n,$ and $p,$ and which computes the remainder when $x^{n}$ is divided by $p$ For simplicity, you may assume that $n$ is a power of $2-$ that is, that $n=2^{k}$ for some positive integer $k$

Lucas Gagne
Lucas Gagne
Numerade Educator
04:03

Problem 42

Use the divide-and-conquer approach to write a recursive algorithm that finds the maximum sum in any contiguous sublist of a given list of $n$ real values. Analyze your algorithm, and show the results in order notation.

Lucas Gagne
Lucas Gagne
Numerade Educator