## Exercises
#### Section 3.1
1. Establish Equality 3.1 given in this section.
2. Use induction on *n* to show that the divide-and-conquer algorithm for the Binomial Coefficient problem ([Algorithm 3.1](LiB0025.html#258)), based on Equality 3.1, computes 2 ![](https://box.kancloud.cn/496d8e4a3956aae833cf09a296075269_47x46.jpg) − 1 terms to determine ![](https://box.kancloud.cn/475599a20c36a2c791f00ff44d5e5eee_41x47.jpg).
3. Implement both algorithms for the Binomial Coefficient problem ([Algorithm 3.1](LiB0025.html#258) and [Algorithm 3.2](LiB0025.html#263)) on your system and study their performances using different problem instances.
4. Modify [Algorithm 3.2](LiB0025.html#263) (Binomial Coefficient Using Dynamic Programming) so that it uses only a one-dimensional array indexed from 0 to *k*.
#### Section 3.2
1. Use Floyd's algorithm for the Shortest Paths problem 2 ([Algorithm 3.4](LiB0026.html#280)) to construct the matrix *D*, which contains the lengths of the shortest paths, and the matrix *P*, which contains the highest indices of the intermediate vertices on the shortest paths, for the following graph. Show the actions step by step.
[![Click To expand](https://box.kancloud.cn/34b87227c79c51c427e838bed70b19c9_350x259.jpg)](figu133_3_0.jpg)
2. Use the Print Shortest Path algorithm ([Algorithm 3.5](LiB0026.html#283)) to find the shortest path from vertex *v*7 to vertex *v*3, in the graph of Exercise 5, using the matrix *P* found in that exercise. Show the actions step by step.
3. Analyze the Print Shortest Path algorithm ([Algorithm 3.5](LiB0026.html#283)) and show that it has a linear time complexity.
4. Implement Floyd's algorithm for the Shortest Paths problem 2 ([Algorithm 3.4](LiB0026.html#280)) on your system, and study its performance using different graphs.
5. Can Floyd's algorithm for the Shortest Paths problem 2 ([Algorithm 3.4](LiB0026.html#280)) be modified to give just the shortest path from a given vertex to another specified vertex in a graph? Justify your answer.
6. Can Floyd's algorithm for the Shortest Paths problem 2 ([Algorithm 3.4](LiB0026.html#280)) be used to find the shortest paths in a graph with some negative weights? Justify your answer.
#### Section 3.3
1. Find an optimization problem in which the principle of optimality does not apply and therefore that the optimal solution cannot be obtained using dynamic programming. Justify your answer.
#### Section 3.4
1. Find the optimal order, and its cost, for evaluating the product *A*1 × *A*2 × *A*3 × *A*4 × *A*5, where
![](https://box.kancloud.cn/a86ea2c3e177522e3117da593e45586a_114x130.jpg)
2. Implement the Minimum Multiplications algorithm ([Algorithm 3.6](LiB0028.html#301)) and the Print Optimal Order algorithm ([Algorithm 3.7](LiB0028.html#306)) on your system, and study their performances using different problem instances.
3. Show that a divide-and-conquer algorithm based on Equality 3.5 has an exponential time complexity.
4. Establish the equality
![](https://box.kancloud.cn/b20860b0d0b51fad2b67d259f6724ec7_400x53.jpg)
This is used in the every-case time complexity analysis of [Algorithm 3.6](LiB0028.html#301).
5. Show that to fully parenthesize an expression having *n* matrices we need *n* − 1 pairs of parentheses.
6. Analyze [Algorithm 3.7](LiB0028.html#306) and show that it has a linear time complexity.
7. Write an efficient algorithm that will find an optimal order for multiplying *n* matrices *A*1 x *A*2 x … x *An*, where the dimension of each matrix is 1 × 1, 1 × *d, d* × 1, or *d* × *d* for some positive integer *d.* Analyze your algorithm and show the results using order notation.
#### Section 3.5
1. How many different binary search trees can be constructed using six distinct keys?
2. Create the optimal binary search tree for the following items, where the probability occurrence of each word is given in parentheses: CASE (.05), ELSE (.15), END (.05), IF (.35), OF (.05), THEN (.35).
3. Find an efficient way to compute ![](https://box.kancloud.cn/0e84a6b5d6470d2a12f9b87f8d68279a_59x58.jpg), which is used in the Optimal Binary Search Tree algorithm ([Algorithm 3.9](LiB0029.html#322)).
4. Implement the Optimal Binary Search Tree algorithm ([Algorithm 3.9](LiB0029.html#322)) and the Build Optimal Binary Search Tree algorithm ([Algorithm 3.10](LiB0029.html#325)) on your system, and study their performances using different problem instances.
5. Analyze [Algorithm 3.10](LiB0029.html#325), and show its time complexity using order notation.
6. Generalize the Optimal Binary Search Tree algorithm ([Algorithm 3.9](LiB0029.html#322)) to the case in which the search key may not be in the tree. That is, you should let *q**i*, in which *i* = 0, 1, 2, …, *n*, be the probability that a missing search key can be situated between *Key**i* and *Key**i*+1. Analyze your generalized algorithm and show the results using order notation.
7. Show that a divide-and-conquer algorithm based on Equality 3.6 has an exponential time complexity.
#### Section 3.6
1. Find an optimal circuit for the weighted, direct graph represented by the following matrix *W.* Show the actions step by step.
![](https://box.kancloud.cn/ab9aaf16826beaa299d133d37c5fc4fb_190x122.jpg)
2. Write a more detailed version of the Dynamic Programming algorithm for the Traveling Salesperson problem ([Algorithm 3.11](LiB0030.html#340)).
3. Implement your detailed version of [Algorithm 3.11](LiB0030.html#340) from Exercise 27 on your system and study its performance using several problem instances.
#### Additional Exercises
1. Like algorithms for computing the *n*th Fibonacci term (see Exercise 34 in [Chapter 1](LiB0008.html#16)), the input size in [Algorithm 3.2](LiB0025.html#263) (Binomial Coefficient Using Dynamic Programming) is the number of symbols it takes to encode the numbers *n* and *k.* Analyze the algorithm in terms of its input size.
2. Determine the number of possible orders for multiplying *n* matrices *A*1, *A*2, …, *An.*
3. Show that the number of binary search trees with *n* keys is given by the formula
![](https://box.kancloud.cn/fc5ffb0bfc89dc3dbab6813fd3689849_111x47.jpg)
4. Can you develop a quadratic-time algorithm for the Optimal Binary Search Tree problem ([Algorithm 3.9](LiB0029.html#322))?
5. Use the dynamic programming approach to write an algorithm to find the maximum sum in any contiguous sublist of a given list of *n* real values. Analyze your algorithm, and show the results using order notation.
6. Let us consider two sequences of characters *S*1 and *S*2. For example, we could have *S*1− A$CMA\*MN and *S*2 = AXMC4ANB. Assuming that a subsequence of a sequence can be constructed by deleting any number of characters from any positions, use the dynamic programming approach to create an algorithm that finds the longest common subsequence of *S*1 and *S*2. This algorithm returns the maximum-length common subsequence of each sequence.
- Table of Contents
- BackCover
- Foundations of Algorithms Using C++ Pseudocode, Third Edition
- Preface
- Chapter Contents
- Pedagogy
- Course Outlines
- Acknowledgments
- Errors
- Chapter 1: Algorithms - Efficiency, Analysis, and Order
- 1.2 The Importance of Developing Efficient Algorithms
- 1.3 Analysis of Algorithms
- 1.4 Order
- 1.5 Outline of This Book
- Exercises
- Chapter 2: Divide-and-Conquer
- 2.1 Binary Search
- 2.2 Mergesort
- 2.3 The Divide-and-Conquer Approach
- 2.4 Quicksort (Partition Exchange Sort)
- 2.5 Strassen's Matrix Multiplication Algorithm
- 2.6 Arithmetic with Large Integers
- 2.7 Determining Thresholds
- 2.8 When Not to Use Divide-and-Conquer
- Exercises
- Chapter 3: Dynamic Programming
- 3.1 The Binomial Coefficient
- 3.2 Floyd's Algorithm for Shortest Paths
- 3.3 Dynamic Programming and Optimization Problems
- 3.4 Chained Matrix Multiplication
- 3.5 Optimal Binary Search Trees
- 3.6 The Traveling Salesperson Problem
- Exercises
- Chapter 4: The Greedy Approach
- 4.1 Minimum Spanning Trees
- 4.2 Dijkstra's Algorithm for Single-Source Shortest Paths
- 4.3 Scheduling
- 4.4 Huffman Code
- 4.5 The Greedy Approach versus Dynamic Programming: The Knapsack Problem
- Exercises
- Chapter 5: Backtracking
- 5.2 The n-Queens Problem
- 5.3 Using a Monte Carlo Algorithm to Estimate the Efficiency of a Backtracking Algorithm
- 5.4 The Sum-of-Subsets Problem
- 5.5 Graph Coloring
- 5.6 The Hamiltonian Circuits Problem
- 5.7 The 0-1 Knapsack Problem
- Exercises
- Chapter 6: Branch-and-Bound
- 6.1 Illustrating Branch-and-Bound with the 0 - 1 Knapsack problem
- 6.2 The Traveling Salesperson Problem
- 6.3 Abductive Inference (Diagnosis)
- Exercises
- Chapter 7: Introduction to Computational Complexity - The Sorting Problem
- 7.2 Insertion Sort and Selection Sort
- 7.3 Lower Bounds for Algorithms that Remove at Most One Inversion per Comparison
- 7.4 Mergesort Revisited
- 7.5 Quicksort Revisited
- 7.6 Heapsort
- 7.6.1 Heaps and Basic Heap Routines
- 7.6.2 An Implementation of Heapsort
- 7.7 Comparison of Mergesort, Quicksort, and Heapsort
- 7.8 Lower Bounds for Sorting Only by Comparison of Keys
- 7.8.1 Decision Trees for Sorting Algorithms
- 7.8.2 Lower Bounds for Worst-Case Behavior
- 7.8.3 Lower Bounds for Average-Case Behavior
- 7.9 Sorting by Distribution (Radix Sort)
- Exercises
- Chapter 8: More Computational Complexity - The Searching Problem
- 8.1 Lower Bounds for Searching Only by Comparisons of Keys
- 8.2 Interpolation Search
- 8.3 Searching in Trees
- 8.4 Hashing
- 8.5 The Selection Problem: Introduction to Adversary Arguments
- Exercises
- Chapter 9: Computational Complexity and Interactability - An Introduction to the Theory of NP
- 9.2 Input Size Revisited
- 9.3 The Three General Problem Categories
- 9.4 The Theory of NP
- 9.5 Handling NP-Hard Problems
- Exercises
- Chapter 10: Number-Theoretic Algorithms
- 10.1 Number Theory Review
- 10.2 Computing the Greatest Common Divisor
- 10.3 Modular Arithmetic Review
- 10.4 Solving Modular Linear Equations
- 10.5 Computing Modular Powers
- 10.6 Finding Large Prime Numbers
- 10.7 The RSA Public-Key Cryptosystem
- Exercises
- Chapter 11: Introduction to Parallel Algorithms
- 11.1 Parallel Architectures
- 11.2 The PRAM Model
- Exercises
- Appendix A: Review of Necessary Mathematics
- A.2 Functions
- A.3 Mathematical Induction
- A.4 Theorems and Lemmas
- A.5 Logarithms
- A.6 Sets
- A.7 Permutations and Combinations
- A.8 Probability
- Exercises
- Appendix B: Solving Recurrence Equations - With Applications to Analysis of Recursive Algorithms
- B.2 Solving Recurrences Using the Characteristic Equation
- B.3 Solving Recurrences by Substitution
- B.4 Extending Results for n, a Power of a Positive Constant b, to n in General
- B.5 Proofs of Theorems
- Exercises
- Appendix C: Data Structures for Disjoint Sets
- References
- Index
- Index_B
- Index_C
- Index_D
- Index_E
- Index_F
- Index_G
- Index_H
- Index_I
- Index_J
- Index_K
- Index_L
- Index_M
- Index_N
- Index_O
- Index_P
- Index_Q
- Index_R
- Index_S
- Index_T
- Index_U
- Index_V
- Index_W-X
- Index_Y
- Index_Z
- List of Figures
- List of Tables
- List of Algorithms, Examples, and Theorems
- List of Sidebars