Sie sind auf Seite 1von 8

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

Chapter 11

The square root algorithm


To begin, Ill use
647.791 for demonstration. The square root algorithm works like this:

Algorithm
and Optimization
Algorithm and Optimization ( )
Algorithm is a set of instructions for solving a problem, or completing a procedure, that can be finished in finite steps. Euclids algorithm and extended Euclids algorithm you learnt in Chapter 4, or even synthetic division in Chapter 1, are examples of algorithms. In this chapter, we will introduce the algorithm for finding square root of a number here. Optimization is a process to find a solution that can achieve the greatest reward (output) with the given conditions. We will introduce greedy algorithm, linear programming and dynamic programming here.

Square Root Finding


Finding the square root of a real number is quite important to estimate a result. Here follows two algorithms for finding square root of a real number. Although there are methods for finding the square root, it is better for you to memorize some of the important values:
2 1.414 3 1.732 5 2.236

First, group the digits in two, from the decimal point. 6 47. 79 10 Start off from the far left group. Find a perfect square that is less than or equal to that value of that group. In our case, it is 22. Put 2 on the above the root sign. 2 6 47. 79 10 22 = 4. Put 4 under 6. Subtract and it gives 2. 2 6 47. 79 10 4 2 47 Double the number on the top rights now (2), and put it besides the newly formed integer (247). 2 6 47. 79 10 4 2 47 (4 _ )

Fill in the blank with a such that 4a a 247 , where a is the maximum possible integral solution for this. In this case, a = 5. Put it on the top, and subtract the product ( 4a a ) from 247. 2 5 6 47. 79 10 4 2 47 2 25 22 79

82

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

504 4 < 2279. 2 5. 6 47. 4 2 47. 2 25. 22. 20. 2. 5085 5 < 26310. 2 5. 6 47. 4 2 47. 2 25. 22. 20. 2. 2. 4 5 79 10 4 79 10

With only three iterations, the result is already good to four decimal points. Newtons iteration is very fast, but calculating t/xn is very troublesome. Also, Newtons iteration is sometimes not very stable.

79 16 63 10

Greedy Algorithm ( )
Greedy algorithm is an optimization algorithm. Greedy algorithm makes use of the principle of always choosing the best choice first to find an optimum.

Suppose you want to find a shortest path from A to J: (number indicates distance)
B

7 8

79 16 63 10 54 25 8 85

5
A

9 2
J

We get 647.791 25.5 . If you like, you can continue. The most time consuming and difficult part of this algorithm is the fill in the blank part. This is also very slow, yet stable.

7 6

5 3

3 2
G

Newtons Iteration
Newtons iteration makes use of the Newtons method, which will be introduced in Chapter 24. To calculate x = t , first start off with an initial guess x0, which should be as near as the exact x as possible. Then repeat calculating t 1 xn +1 = xn + 2 xn From n = 0, 1, 2, until the result is desired.

From A, by greedy algorithm, we found that going to B is the shortest at the moment, so we choose B. Similarly, we go to E, H then finally J. The total distance for choosing this path is 27. Greedy algorithm is an easy and direct way to find out a solution, but it doesnt promise the solution is necessarily optimal. For example, the shortest distance is actually 14 in the above case. The difference is there because greedy algorithm does not consider the consequence when choosing the best way to go. Therefore, greedy algorithm may not be a good choice for solving these kinds of questions.

For example, to calculate 647.791.

647.791 , we start from x0 = 25, as 252 = 625 is similar to

n 0 1 2 3

xn 25 25.4558 25.4517 25.4517

83

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

Applying greedy algorithm in diophantine equations


We knew that greedy algorithm is not suitable for finding shortest path, but it has its use in solving simple diophantine equations in order 1 of many variables.
Example: McDonalds Chicken McNuggets () was came in boxes of 6, 9 or 20. How can we consume 62 McNuggets?

Linear Programming ( )
Linear programming is an optimum searching process, which the cost/profit are linear functions. Example: Consider a company that produces two types of products P1 and P2. Production of these products is supported by two workstations W1 and W2, with each station visited by both product types. If workstation W1 is dedicated completely to the production of product type P1, it can process 40 units per day, while if it is dedicated to the production of product P2, it can process 60 units per day. Similarly, workstation W2 can produce daily 50 units of product P1 and 50 units of product P2, assuming that it is dedicated completely to the production of the corresponding product. If the companys profit by disposing one unit of product P1 is $200 and that of disposing one unit of P2 is $400, and assuming that the company can dispose its entire production, how many units of each product should the company produce on a daily basis to maximize its profit?

This is the same as solving the diophantine equation 6a + 9b + 20c = 62, where a, b, c > 0. By greedy algorithm, first take the best choice, i.e., buying the 20 only. Solving the equation gives c = 3.1. But since a, b and c are integers, we can only buy three 20s. There are still 2 chicken left. What to do? It is impossible to obtain 62 from 20 ( 20 / 62 ), so lets try retreating one 20, | and take the rest 22 by the 9s only. This gives b = 2.4 , which is, again, impossible. If we take {a, b, c} = {0, 2, 2}, leaving 4 behind. Dispose a 9, and try the 6s. We have {2, 1, 2} then, but just one left. Remove one more 9, and now is {3, 0, 2}, but there should be four more! Now reconsider: should we need so much 20s? Maybe we can restart from having one 20 only. So the guess is now {0, 4, 1}. We need to consume six more. It is obvious that buying one more 6 will give you exactly 62 McNuggets. Therefore: 61 + 94 + 201 = 62. This is a systematic trial-and-error way to solve a diophantine equation.
Note: Any integers that can be expressed in the form 6a + 9b + 20c are called McNugget numbers.

First, convert this long and tedious text into mathematical languages. Assume p1 is the number of P1 produced each day, and p2 is for P2. We define the objective function ( ) f to be: f ( p1, p2 ) = 200 p1 + 400 p2 To maximize the profit, we need to maximize f. However, there are some technical constraints () in it. For W1, it can only produce at most 40 P1s or 60 P2s a day: p1 p2 + 1 40 60 Similarly, for W2, p1 p2 + 1 50 50 Moreover, there is a sign restriction () for p1 and p2, that is: p1, p2 0 And, of course, p1 and p2 need to be integers. We have set up the linear programming problem mathematically. The next thing is to solve it. There are two methods.

84

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

Graphical method ( )
Firstly, we plot out the four inequalities.
y 50

Next is to plot f(p1, p2). But we cannot simply plot it on the 2D Cartesian plane. The plot of f is actually a 3D surface. So we need to graph out f(p1, p2) = a by different as to visualize it.
y 50

40

40

This way: Increasing value of f(p1, p2)

30

30

20

20

Feasible Region Constrained Areas

Feasible Region Constrained Areas

10

10

Note that the horizontal axis is p1, and the vertical is p2.

10

20

30

40

The yellow lines are graphs of f = 0, f = 2500, f = 20000. From the graph, we see that f achieves the maximum within the feasible region when p1 = 0 and p2 = 50. Therefore, out solution is: the company should only produce P2 at a rate of 50 items per day. This will give a profit of $20,000.
Note: The plotting method for f is known as contour plotting ().

10

20

30

40

Only the area enclosed by the four inequalities, marked Feasible Region (), is considered.

85

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

That should be simpler


Graphical method is easy to understand but there are too many graphing works. In fact, the graph f has no need to be graphed out. First we introduce the fundamental theorem of linear programming (): If a linear programming problem has a bounded () optimal solution, then there exists an extremum (or vertex ) of the feasible region which is optimal. That means, we can just look up to the vertices of the polygon formed from the feasible region to check if that is maximum! The vertices of our problem are (0, 0), (0, 40), (20, 30) and (0, 50). The values of f in each case are 0, 16000, 16000 and 20000, which gives the optimal solution p1 = 0, p2 = 50 in a breeze.

For our example, the goal is J. So start from there.


From J Best To J 0 0 J The Last column means the last decision. No decision has made yet for now. The column J means the cost (distance) made by choosing that decision. Since the distance for going from J to J is zero, the cost is 0. Best is the cost of the decision that can make the optimum solution at there. Next is the best decision. From J Best To H 9 9 J I 2 2 J

We can go to J from H or I. The cost from H is 9, and the cost from I is 2. Although going from I is much cheaper, we cannot discard H. Otherwise it is the same as greedy algorithm, except the direction is changed. From E or F one can go to H, and F, G can go to I. Note the values needed to be added up with the previous choices. If there is no way going from a point to another point (e.g. E I), ignore it. From H I Best To E 15 - 15 H F 11 5 5 I G -3 3 I
From B C D E 22 F G Best 23 - 22 10 - 10 8 12 8 To E F F

Dynamic Programming ( )
Unlike linear programming, dynamic programming is the process for optimizing the problem that can be divided into stages. Consider the path problem again.
B

7 8

5
A

9 2
J

7 6

5 3

3 2
G

Last B C D Best Next A 27 17 14 14 D Thus, by dynamic programming, the shortest path is 14, for ADFIJ. 48% shorter than the one given by greedy algorithm!

The greedy algorithm cannot help us to optimize the solution. But dynamic programming can. Dynamic programming finds the optimum reversely, i.e., from the back (goal).

86

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

Revision
In this chapter, weve learnt: 1. What is algorithm and optimization 2. Finding square root of a number by the square root algorithm and Newtons iteration 3. Using greedy algorithm to solve a diophantine equation 4. Linear programming 5. Dynamic programming

5. Find the least time-consuming path from A to Z, if only leftward and rightward movement is allowed:
A

3
F

8 5
G

1 2
H

6 4
I

4 5
J

6
K

7 5
L

3 1
M

9 2
N

1 1
P

Exercise
In the followings, if not specified, x is the variable. 1. Find the square root of the follows without using calculators, correcting to 0.1. a) 100 b) 120 c) 140 d) 200 e) 567 2. Can Newtons iteration be thought as an algorithm? Present your idea. 3. Solve the diophantine equation 5a + 7b + 12c + 19d = 136. 4. A clothing company has a factory that produces shirts and pants. It has 500 employees. Five employees form a group. Each employee cannot work more than eight hours. The profit for selling a shirt is $79, and a pant is $99. In a test, it is found that one group of employees can make a pant in 20 minutes, or make a shirt in 16 minutes, and another group can make a pant and a shirt both in 18 minutes. a) Base on these samples, what strategy should the company carry out so it can maximize its profit? [Hint: Divide the employees into two types] b) What is the average working time for each group of employee? c) The company later found that since one of the production is halted, the price for that product is raised by 50%, and the one that is selling now is decreased by 50%. How should the company reconstruct their production plan? [Note: Remember that only integral solution is allowed]

1
Q

7 9
R

3 2
S

6 6
T

5 3
U

5
V

7 4
W

2 3
X

1 5
Y

4 3
Z

6. (PCMSIMC 2004, Modified) Approximate numerical answers to 2 decimal points. Mary is tying her shoelace (). There are 2 columns of holes, each with 7 holes on it. The holes are evenly distributed on a plane in a rectangular shape, with each of them at a distance of 1 cm from its neighbors. Mary always ties the shoelace from A1, then to a hole on column B, and then back to a hole on column A and so on, until she returns to B1. In order to save time, Mary will only tie the shoelace through exactly one of the two holes on the same row except for the first row (A1 and B1), i.e., the shoelace will not go through both A2 and B2, nor both A3 and B3, etc. The shoelace can only pass through a hole once. 1 cm A1 B1 A1 B1 A1 B1 1 cm A2 B2 A2 B2 A2 B2
A3 A4 A5 A6 A7 B3 B4 B5 B6 B7 A3 A4 A5 A6 A7 B3 B4 B5 B6 B7 A3 A4 A5 A6 A7 B3 B4 B5 B6 B7

87

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

a) The time passing though An is 15 n seconds, and Bn is n seconds. What is the shortest time that Mary needs to tie the shoelace? b) Find minimum length of the shoelace if shortest time in required. c) Find the minimum length of the shoelace from A1 to B1. d) Find the shortest time for Mary to tie the shoelace when the length is minimum.

88

Advanced Mathematics Training Class Notes

Chapter 11: Algorithm and Programming

Suggested Solutions for the Exercise


1a) 10 b) 11.0 c) 11.8 d) 14.1 e) 23.8 2) Free answer. 3) a = 1, b = 2, c = 5, d = 3. 4a) Selling pants alone at a rate of 1200 items per day. b) 7.6 hours (38/5 hours) c) Selling 1333 shirts each day solely. 5) ABCHMNPUZ (21) 6a) 51 s b) 25.02 cm (from 4 10 + 3 17 ) c) 57 s d) 14.01 cm (from 2 2 + 5 5 )

89

Das könnte Ihnen auch gefallen