Introduction To Algorithms Second Edition Solutions Manual: Christopher Clark University of Virginia Summer 2010

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Introduction to Algorithms Second Edition Solutions Manual

Christopher Clark University of Virginia Summer 2010

Contents

1 The 1.1 1.2 1.3

Role of Algorithms in Computing Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Algorithms as a technology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3 3 4 5 7 7 7 8 8

2 Getting Started 2.1 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 Analyzing Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Elementary Graph Algorithms 3.1 Representations of Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Preface
This solutions manual is totally kickass because it has a preface. There are no mistakes. Eveything is obviously perfect. If you nd an error, you are wrong, but let me know so that I can nd a way to incorporate your ineptitude into this document. Or you can correct the error yourself, and send me the new .tex and .pdf les via [email protected]. A L TEX
A I wrote this document partly to practice algorithms and partly to practice L TEX. I used exclusively Emacs + AUCTEX in Linux. I would very much appreciate any critique whatsoever regarding the A L TEX source, the presentation of the material, or anything else that I havent thought about. I A would consider myself at most an advanced beginner with L TEX, and I have much to learn.

Chapter 1
1.1 Algorithms

1.1-1 Give a real-world example in which one of the following computational problems appears: sorting, determining the best order for multiplying matrices, or nding the convex hull. Solution: Sorting: to perform a binary search, the input must be in order. Multiplying matrices: Certain orders create smaller matrices. For example, A (3 1) and a (1 3) matrix make a (3 3) matrix. A (1 3) and a (3 1) matrix make a (1 1) matrix. Finding convex hull : Well, say you want to stretch a rubber band around the smallest number of nails sticking out of a board.

1.1-1 Other than speed, what other measures of eciency might one use in a real-world setting? Solution: Memory usage, power usage, ability to be broken up into parts (for parallel computing).

1.1-3 Select a data structure that you have seen previously, and discuss its strengths and limitations. Solution: Linked List: Pros: Keeps a denite ordering, iterators are predictable. Cons: Uses memory to maintain pointers between every node, linear search time. 1.1-4 How are the shortest-path and traveling-salesman problems given above similar? How are

they dierent? Solution: A solution to the travelling-salesman problem would nd a shortest path starting from a start node, then hitting every other node, and nally returning to the start node. A solution to the shortest-path problem would be a shortest path from a start node to an end node. It is not necessary that all nodes be visited, nor is it necessary that the start node and the end node be the same.

1.1-5 Come up with a real-world problem in which only the best solution will do. Then come up with one in which a solution that is approximately the best is good enough. Solution: If you are trying to prove that you found the shortest path among a set of points, then you must show you have an exact answer. If you are actually a travelling salesman, then you will be content nding an approximate answer to the traveling-salesman problem.

1.2

Algorithms as a technology

1.2-1 Give an example of an application that requires algorithmic content at the application level, and discuss the function of the algorithms involved. Solution: An application that searches the Web to nd pages with certain keywords must have a searching algorithm. The algorithm should be very fast and should order the results by their level of applicability.

1.2-2 Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For input sizes of n, insertion sort runs in 8n2 steps, while merge sort runs in 64n lg n steps. For which values of n does insertion sort beat mege sort? Solution: Insertion sort will run slower than merge sort for all values of n above a certain value. We can nd this value by nding when insertion sort and merge sort run in the same amount of time by solving the equaiton 8n2 = 64n lg n Which yields n = 43.559. Thus merge sort is faster for all input sizes of 44 or more. Using l33t gnuplot skillz, we see that insertion sort is initially faster than merge sort, but insertion sorts running time grows faster.

Merge Sort vs Insertion Sort 25000 20000 15000 10000 5000 0 -5000 0 10 20 30 40 50 Number of Items Insertion Sort Merge Sort

1.2-3 What is the smallest value of n such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n on the same machine? Solution: An algorithm that runs in exponential time will eventually run slower than an algorithm that runs in n2 time. We can nd the point where they cross over by solving 100n2 = 2n This yields n = 14.327. Thus the smallest value of n for which an algorithm with running time 100n2 runs faster than an algorithm with running time 2n is n = 15.

1.3

Chapter 1 Problems

1-1 For each function f (n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem takes f (n) microseconds. Solution: Hint : Use Google to nd microseconds in a time, then Mathematica to evaluate an expression for each cell. For example, for the cell at the intersection of n lg n and 1 century, a search for microsecods in a century yields 3.1556926 1015 , and the appropriate Mathematica code would be ScientificForm [ NSolve [ n Log2 [ n ] == 3 1 5 5 6 9 2 6 0 0 0 0 0 0 0 0 , n ] ]

Computations

1 second lg n n n n lg n n2 n3 2
n

1 minute 26.010
7

1 hour 23.610
9

1 day 28.610
10

1 month 22.610
12

1 year 23.210
13

1 century 23.210
15

21,000,000 1012 106 6.3 104 1,000 100 19 9

3.61015 6.0 107 2.8 106 7,745 391 25 11

1.31019 3.6 109 1.3 108 60,000 1,532 31 12

7.41021 8.61010 2.8 109 293,939 4,420 36 13

6.81024 2.61012 7.31010 1.6 106 13,802 41 15

1.01027 3.21013 8.01011 5.6 106 31,600 44 16

1.01031 3.21015 6.91013 5.6 107 146,678 51 17

n!

Chapter 2
2.1 Insertion Sort

2.1-1 Using Figure 2.2 as a model, illustrate the operation of Insertion Sort on the array A = 31, 41, 59, 26, 41, 58 . Solution:

2.1-2 Rewrite the Insertion-Sort procedure to sort into nonincreasing instead of nondecreasing order. Solution:

2.1-3 Consider the searching problem: Input: A sequence of n numbers A = a1 , a2 , . . . , an and a value v. Output: An index i such that v = A[i] or the special value nil if v does not appear in A. Write pseudocode for linear search, which scans through the sequence, looking for v. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fullls the three necessary properties. Solution:

2.1-4 Consider a problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n + 1)-element array C. State the problem formally and write pseudocode for adding the two integers. Solution:

2.2

Analyzing Algorithms

Chapter 22
3.1 Representations of Graphs

22.1-1 Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree of every vertex? How long does it take to compute the in-degrees? Solution: Finding the out-degree of any particular vertex entails counting the number of vertices in the adjecency list for that vertex, which takes (n) time (because for a particular vertex there can be up to n 1 items in its adjecency list). To nd the in-degree we must look at each vertexs adjacency list. Since there are n vertices and each can have up to n 1 items in its adjacency list, nding the in-degree takes O n2 time. Many graphs are sparse and will take a much shorter time. In general it will take (V + E) time.

22.1-2 Give an adjacenty-list representation for a complete binary tree on 7 vertices. Give an equivalent adjacency-matrix representation. Assume that vertices are numbered from 1 to 7 as in a binary heap. Solution:

22.1-3 The transpose of a directed graph G = (V, E) is th graph GT = (V, E T ), where

You might also like