The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Correctness Next: Efficiency Up: Correctness and Efficiency Previous: Correctness and Efficiency Correctness It is seldom obvious whether a given algorithm correctly solves a given problem. This is why correct algorithms usually come with a proof of correctness, which is an explanation of why we know that the algorithm correctly takes all instances of the problem to the desired result. In this book, we will not stress formal proofs of correctness, primarily because they take substantial mathematical maturity to properly appreciate and construct. However, before we go further it is important to demonstrate why ``it's obvious'' never suffices as a proof of correctness and usually is flat-out wrong. To illustrate, let us consider a problem that often arises in manufacturing, transportation, and testing applications. Suppose we are given a robot arm equipped with a tool, say a soldering iron. In manufacturing circuit boards, all the chips and other components must be fastened onto the substrate. More specifically, each chip has a set of contact points (or wires) that must be soldered to the board. To program the robot arm to perform this soldering job, we must first construct an ordering of the contact points, so that the robot visits (and solders) the first contact point, then visits the second point, third, and so forth until the job is done. The robot arm must then proceed back to the first contact point to prepare for the next board, thus turning the tool-path into a closed tour, or cycle. Since robots are expensive devices, we want to find the tour that minimizes the time it takes to assemble the circuit board. A reasonable assumption is that the robot arm moves with fixed speed, so that the time it takes to travel between two points is the same as its distance. In short, we must solve the following algorithm problem: Input: A set S of n points in the plane. Output: What is the shortest cycle tour that visits each point in the set S? You are given the job of programming the robot arm. Stop right now and think about an algorithm to solve this problem. I'll be happy to wait until you find one. Several possible algorithms might come to mind to solve this problem. Perhaps the most popular idea is the nearest-neighbor heuristic. Starting from some point , we walk first to its nearest neighbor . From , we walk to its nearest unvisited neighbor, thus excluding only as a candidate. We now repeat this process until we run out of unvisited points, at which time we return to to close off the tour. In pseudocode, the nearest-neighbor heuristic looks like this: Figure: A good example for the nearest neighbor heuristic file:///E|/BOOK/BOOK/NODE8.HTM (1 of 5) [19/1/2003 1:28:06]
Correctness NearestNeighborTSP(P) Pick and visit an initial point from P i = 0 While there are still unvisited points Return to from i = i+1 Select to be the closest unvisited point to Visit This algorithm has a lot to recommend it. It is simple to understand and implement. It makes sense to visit nearby points before we visit faraway points if we want to minimize the total travel time. The algorithm works perfectly on the example in Figure . The nearest neighbor rule is very efficient, for it looks at each pair of points at most twice, once when adding to the tour, the other when adding . Against all these positives there is only one problem. This algorithm is completely wrong. Figure: A bad example for the nearest neighbor heuristic Wrong? How can it be wrong? The algorithm always finds a tour, but the trouble is that it doesn't necessarily find the shortest possible tour. It doesn't necessarily even come close. Consider the set of points in Figure , all of which lie spaced along a line. The numbers describe the distance that each point lies to the left or right of the point labeled `0'. When we start from the point `0' and repeatedly walk to the nearest unvisited neighbor, you will see that we keep jumping left-right-left-right over `0'. A much better (indeed optimal) tour for these points starts from the leftmost point and visits each point as we walk right before returning at the rightmost point. Try now to imagine your boss's delight as she watches a demo of your robot arm hopscotching left-right-left-right during the assembly of such a simple board. ``But wait,'' you might be saying. ``The problem was in starting at point `0'. Instead, why don't we always start the nearestneighbor rule using the leftmost point as the starting point ? By doing this, we will find the optimal solution on this file:///E|/BOOK/BOOK/NODE8.HTM (2 of 5) [19/1/2003 1:28:06]
- Page 53 and 54: References L. Adleman. Algorithmic
- Page 55 and 56: References AS89 Ata83 Ata84 problem
- Page 57 and 58: References BJL 91 A. Blum, T. Jiang
- Page 59 and 60: References BS81 BS86 BS93 BS96 BS97
- Page 61 and 62: References J. M. Chambers. Partial
- Page 63 and 64: References CT80 CT92 1971. G. Carpa
- Page 65 and 66: References K. Daniels and V. Milenk
- Page 67 and 68: References (FOCS), pages 60-69, 199
- Page 69 and 70: References FM71 M. Fischer and A. M
- Page 71 and 72: References History of Computing, 7:
- Page 73 and 74: References N. Gibbs, W. Poole, and
- Page 75 and 76: References Hof82 Hof89 Hol75 C. M.
- Page 77 and 78: References IK75 Ita78 O. Ibarra and
- Page 79 and 80: References Kir83 D. Kirkpatrick. Ef
- Page 81 and 82: References object in 2-dimensional
- Page 83 and 84: References LT79 LT80 8:99-118, 1995
- Page 85 and 86: References Mil97 V. Milenkovic. Dou
- Page 87 and 88: References Theoretical Computer Sci
- Page 89 and 90: References Pav82 T. Pavlidis. Algor
- Page 91 and 92: References Rei72 Rei91 Rei94 E. Rei
- Page 93 and 94: References Prentice Hall, Englewood
- Page 95 and 96: References SR95 SS71 SS90 SSS74 ST8
- Page 97 and 98: References Tin90 Mikkel Thorup. On
- Page 99 and 100: References Wel84 T. Welch. A techni
- Page 101 and 102: References Algorithms Mon Jun 2 23:
- Page 103: Correctness and Efficiency Next: Co
- Page 107 and 108: Correctness Figure: A bad example f
- Page 109 and 110: Efficiency Next: Expressing Algorit
- Page 111 and 112: Keeping Score Next: The RAM Model o
- Page 113 and 114: The RAM Model of Computation substa
- Page 115 and 116: Best, Worst, and Average-Case Compl
- Page 117 and 118: The Big Oh Notation Figure: Illustr
- Page 119 and 120: Logarithms Next: Modeling the Probl
- Page 121 and 122: Logarithms justified in ignoring th
- Page 123 and 124: Modeling the Problem Figure: Modeli
- Page 125 and 126: About the War Stories Next: War Sto
- Page 127 and 128: War Story: Psychic Modeling Next: E
- Page 129 and 130: War Story: Psychic Modeling have pu
- Page 131 and 132: War Story: Psychic Modeling Next: E
- Page 133 and 134: Exercises (b) If I prove that an al
- Page 135 and 136: Fundamental Data Types Next: Contai
- Page 137 and 138: Containers Next: Dictionaries Up: F
- Page 139 and 140: Dictionaries Next: Binary Search Tr
- Page 141 and 142: Binary Search Trees BinaryTreeQuery
- Page 143 and 144: Priority Queues Next: Specialized D
- Page 145 and 146: Specialized Data Structures Next: S
- Page 147 and 148: Sorting Next: Applications of Sorti
- Page 149 and 150: Applications of Sorting Figure: Con
- Page 151 and 152: Data Structures Next: Incremental I
- Page 153 and 154: Incremental Insertion Next: Divide
Correctness<br />
Next: Efficiency Up: Correctness and Efficiency Previous: Correctness and Efficiency<br />
Correctness<br />
It is seldom obvious whether a given algorithm correctly solves a given problem. This is why correct algorithms usually come<br />
with a proof of correctness, which is an explanation of why we know that the algorithm correctly takes all instances of the<br />
problem to the desired result. In this book, we will not stress formal proofs of correctness, primarily because they take<br />
substantial mathematical maturity to properly appreciate and construct. However, before we go further it is important to<br />
demonstrate why ``it's obvious'' never suffices as a proof of correctness and usually is flat-out wrong.<br />
To illustrate, let us consider a problem that often arises in manufacturing, transportation, and testing applications. Suppose we<br />
are given a robot arm equipped with a tool, say a soldering iron. In manufacturing circuit boards, all the chips and other<br />
components must be fastened onto the substrate. More specifically, each chip has a set of contact points (or wires) that must<br />
be soldered to the board. To program the robot arm to perform this soldering job, we must first construct an ordering of the<br />
contact points, so that the robot visits (and solders) the first contact point, then visits the second point, third, and so forth until<br />
the job is done. <strong>The</strong> robot arm must then proceed back to the first contact point to prepare for the next board, thus turning the<br />
tool-path into a closed tour, or cycle.<br />
Since robots are expensive devices, we want to find the tour that minimizes the time it takes to assemble the circuit board. A<br />
reasonable assumption is that the robot arm moves with fixed speed, so that the time it takes to travel between two points is<br />
the same as its distance. In short, we must solve the following algorithm problem:<br />
Input: A set S of n points in the plane.<br />
Output: What is the shortest cycle tour that visits each point in the set S? You are given the job of programming the robot arm.<br />
Stop right now and think about an algorithm to solve this problem. I'll be happy to wait until you find one.<br />
Several possible algorithms might come to mind to solve this problem. Perhaps the most popular idea is the nearest-neighbor<br />
heuristic. Starting from some point , we walk first to its nearest neighbor . From , we walk to its nearest unvisited<br />
neighbor, thus excluding only as a candidate. We now repeat this process until we run out of unvisited points, at which time<br />
we return to to close off the tour. In pseudocode, the nearest-neighbor heuristic looks like this:<br />
Figure: A good example for the nearest neighbor heuristic<br />
file:///E|/BOOK/BOOK/NODE8.HTM (1 of 5) [19/1/2003 1:28:06]