Академический Документы
Профессиональный Документы
Культура Документы
CONTENTS
Chapter 1 Introduction
1.1 Introduction to Operations Research 1
1.2 Introductory Example: Binary Search 2
1.3 Introductory Example: The Traveling Salesman Problem 3
Chapter 2 Algorithms
2.1 Flow Diagrams and Visual Basic Programs 5
2.2 The Bubble Sort 7
2.3 The Quick Sort 9
2.4 Bin Packing 10
2.5 Binary Search 12
Chapter 4 Digraphs
4.1 Flows in Networks 41
4.2 Critical Path Analysis 43
1
Introduction
1 2 3 4 5 6 7
A
8
Example (1) B
Find the defective bulb (No. 7, what we don't 15 14 13 12 11 10 9
1 2 3 4 5 6 7
A
8 Now Work Problem 1.2.1
B
15 14 13 12 11 10 9
When we have taken a bulb from its socket,
by means of an Ohm-meter we can check on
Figure 1 the one hand side the bulb itself, on the other
hand, we are also able to check the branches
Solution: We remove bulb 8, which is O. K., of the circuit between the socket and other
and check for electric flow from socket 8 to A measuring points. In a circuit with exactly one
and to B. We find that there is no electric flow defective bulb, show that
between socket 8 and point A.
a maximum number of 1 bulb has to be
no flow
checked in a circuit with up to 3 bulbs,
1 2 3 4 5 6 7
A a maximum number of 2 bulbs have to be
8 checked in a circuit with up to 7 bulbs,
B
15 14 13 12 11 10 9
flow a maximum number of 3 bulbs have to be
Figure .2
checked in a circuit with up to 15 bulbs,
We remove bulb 4, which is O. K., and check to identify the defective one.
for electric flow between socket 4 and point A
(flow) and between sockets 4 and 8 (no flow). Give a general rule.
Operations Research 1. Introduction 3
Example (2)
Find a solution of the travelling salesman
problem using the nearest neighbor algorithm
for the road map of Figure 1, starting in Soest.
Ahlen 19
Ahlen 19 Beckum
Beckum
28 28
28 28 26
26 29
29 Hamm 26
Hamm 26 37 Lippstadt
37 Lippstadt 30 32 Lippetal
30 32 11
Lippetal 23
11 Welver 17
23 Welver 17 18 20 Erwitte
18 20 Erwitte 21
21
Werl 20 Soest
Werl 20 Soest
20
20 29 34
29 34
12
12
44 Warstein
44 Warstein
Arnsberg
Arnsberg 12
12 24
24
Meschede
Meschede
Figure 2
Figure 1
Ahlen 19 Beckum
28 28 26
26 29
Hamm Lippetal
30 Lippstadt
Welver 32 37 11
23 17
21
18 Erwitte
20
20 Soest
Werl
Figure 3
Chapter Operations Research 5
2
Algorithms
Sub Example1()
Write the Dim i, j
value of j i = 1
j = 1
yes 0: W(IO, 1, i) = j
Is i =6? STOP If i = 6 Then GoTo 1
no i = i + 1
j = j + i
i :=i +1 GoTo 0
1: End Sub
j :=j +i
The corresponding executable source code
would be:
Figure 1 Flow chart.
Sub Example1()
Dim i, j
Solution: Table 1 shows how the values of i i = 1
and j change as the algorithm is implemented. j = 1
0: Worksheets("IO").Cells(1, i) = j
TABLE 1 If i = 6 Then GoTo 1
i = i + 1
i 1 2 3 4 5 6 j = j + i
j 1 3 6 10 15 21 GoTo 0
1: End Sub
2. Algorithms 2.1 Flow Diagrams and C Programs 6
Sub init()
Write the n = Worksheets("IO").Cells(1, 2)
value of j End Sub
A B C D E
1 n= 5 d=
2 23 35 12 86 45
Operations Research 2. Algorithms 7
C D P B Q
C D P B Q
C D P B Q C D B P Q
C D B P Q
C D B P Q C B D P Q
C B D P Q B C D P Q
2. Algorithms 2.2 The Bubble Sort 8
Example (3)
Sort the list 3, 7, 9, 1, 5 into descending order. Now Work Problem 2.2.3
Given the following part of a program
Option Explicit
Solution: Public n
Sub BubbleSort()
3 7 9 1 5 7 3 9 1 5 init
sort
7 3 9 1 5 7 9 3 1 5 End Sub
7 9 3 1 5
Sub init()
7 9 3 1 5 7 9 3 5 1 Dim i
n = Worksheets("IO").Cells(1, 1)
For i = 1 To n
7 9 3 5 1 9 7 3 5 1 Worksheets("IO").Cells(4, i) _
= Worksheets("IO").Cells(2, i)
9 7 3 5 1 Next i
9 7 3 5 1 9 7 5 3 1 End Sub
Sub sort()
Dim i, x, exchanged
0: exchanged = 0
For i = 1 To n - 1
If Worksheets("IO").Cells(4, i) _
> Worksheets("IO").Cells(4, i + 1) Then
Now Work Problem 2.2.2 Complete the subroutine sort, which sorts
Sort the List the data values of row 2 into ascending order
in row 4, using the bubble sort algorithm.
4, 8, 2, 0, 6
A B C D E F
into descending order. 1 6
2 77 48 55 12 7 1
3
4
Operations Research 2. Algorithms 9
6
34 21 55 83 12 42 i PCode Name 1st Name Data Files
5 12234 Brown Charles data5.dat
2 1 3 2 21567 Duck Donald data2.dat
21 12 34 55 83 42
1 34113 Mouse Mickey data1.dat
1 1 1 1 1 1
6 42456 Windsor Charles data6.dat
12 21 34 42 55 83 3 55678 Kong King data3.dat
4 83123 Duck Daisy data4.dat
44 63 35 92 51 17 78 86 29
44 63 35 92 51 17 78 86 29
A Visual Basic source code is available in
into descending order. Appendix C.
Operations Research 2. Algorithms 10
There is no known algorithm that will always a) Use the first-fit algorithm to try to find the
provide the best solution. You need to be fa- maximum number of workers needed.2
miliar with the three algorithms below that
attempt to provide a good solution. Such al- b) Find a better solution, using the full-bin
gorithms are known as heuristic algorithms. algorithm.
1 2
The full bin algorithm is only practical when the You can represent each worker as a 'bin' of 'height' 8
number of bins and boxes is small. hours.
2. Algorithms 2.4 Bin Packing 11
TABLE 1
File A B C D E F G c) Writing the memory sizes in order gives
Kb 580 468 610 532 840 590 900
900, 840, 610, 590, 580, 532, 468.
Example (2)
Table 1 shows the amount of memory taken 1.4 Mb
up by some files on a computer system. The
files are to be transferred onto disk with a 468 532
capacity of 1.4 Mb.3 590
3
1.000 Kb = 1 Mb
Operations Research 2. Algorithms 12
Option Explicit
Public n, target, mid, min, max
Sub BinarySearch()
init
search
finaloutput
End Sub
Sub init()
Dim i
n = Worksheets("IO").Cells(1, 1)
target = Worksheets("IO").Cells(2, 1)
For i = 1 To n
Worksheets("a").Cells(i, 1) = Worksheets("IO").Cells(i, 2)
Next i
End Sub
Sub search()
Dim tag
min = 1
max = n
mid = (min + max) / 2
0: output
If target = Worksheets("a").Cells(mid, 1) Then
tag = 1
Else
If target < Worksheets("a").Cells(mid, 1) Then
min = mid + 1
Else
max = mid - 1
End If
End If
mid = (min + max) / 2
If tag = 0 Then GoTo 0
target = mid
End Sub
Operations Research 2. Algorithms 13
Sub output()
Dim i
For i = 1 To n
Worksheets("IO").Cells(i, 3) = ("")
Next i
Worksheets("IO").Cells(min, 3) = ("<---")
Worksheets("IO").Cells(mid, 3) = ("<---")
Worksheets("IO").Cells(max, 3) = ("<---")
End Sub
Sub finaloutput
Dim i
For i = 1 to n
Worksheets("a").Cells(i, 3) = ("")
Worksheets("IO").Cells(i, 3) = ("")
Next i
Worksheets("IO").Cells(target, 3) = ("<---")
End Sub
Chapter Operations Research 14
3
Graphs and Networks
A C
A C
Example (1)
Find an Eulerian cycle of the given graph of
Figure 7 Connected graph. Figure 10.
K4 K5 Figure 10
Solution:
Figure 8 Complete graphs.
A cycle is a path that starts and finishes at A Hamiltonian cycle is one that passes
the same vertex. through every vertex once only.
3. Graphs and Networks 3.1 Defining Terms 16
G 2
F
1 E
1
H 1
D
2 1 C
Figure 13 Tree.
2 B
A
A tree is a graph with no cycles. Figure 16 Minimum spanning tree of Figure 15.
G 2 F
1 E
3
1
3
4 2
H 4 1
D
2 1 C
2
B
A
Figure 15 Network.
A D
C Figure 3
7
14 Step 3 Connect one of the remaining verti-
11
B ces to the tree formed so far, in such
a way that the minimum extra
5 20 weight is added to the tree: D (Fig-
ure 4).
15
A D
Figure 1 C
7
14
B
Solution:
5
Step 1 Starting vertex: C A D
Example (3)
Find the minimum spanning tree. Now Work Problem 3.2.1
A new theme park has its major attractions at
G 8 F A, B, C, D, E, F, and G as shown in the net-
5 E work below. The edges of the network show
2 the possible routes of paths connecting the
10
9 attractions.
7 4
H 3
D
11
6 12 C B
1 B A
A
G
Figure 5 F
Solution (1): E
C D
G 8
F
5 E Figure 7
2 10
9 The cost of laying each of these paths, in hun-
7 4
D dreds of euros, is given in Table 2. The paths
H 3
11 that are actually laid must form a connected
6 12 C graph. Use Prim's algorithm, starting by de-
1 B leting row A, to find the minimum cost of
A laying the necessary paths.
Figure 6 TABLE 2
A B C D E F G
A 50 40 61 38
Solution (2): B 50 47 51
C 40 35
1 2 6 7 4 8 5 3 D 61 35 42
A B C D E F G H E 42 25 10
A 1 11 6 F 38 47 25 63
B 1 12 G 51 10 63
C 12 3 4
D 3 10
E 11 4 10 5 2 7
F 5 8
G 2 8 9
H 6 7 9
Moscow
London Berlin
Vienna
Paris
Madrid Rome
Figure 10
Operations Research 3. Graphs and Networks 21
32 30 D
29
Step 1
G
49 22
25 Table 1 Matrix for Prims algorithm
45
A B C D E F G
20 C A 24 45
23 B 24 23 49 20
A 24 B C 23 25 22
D 25 34 29
Figure 1 A small network. E 34 16 30
F 45 49 16 32
The traveling salesman problem (TSP)1 is G 20 22 29 30 32
the problem of finding a route of minimum
distance that visits every vertex and returns to
the start vertex. For a small network it is pos- Table 2 Matrix after the application of Prims algorithm
sible to produce an exhaustive list of all pos- 1 2 4 5 6 7 3
sible routes and choose the one that mini- A B C D E F G
mizes the total distance. For a large network A 24 45
an exhaustive check is not feasible, even with B 24 23 49 20
a computer, because the number of possible C 23 25 22
routes grows so rapidly. D 25 34 29
E 34 16 30
It is useful to know within what limits the F 45 49 16 32
total distance must lie and there are algo-
G 20 22 29 30 32
rithms that can be used for this purpose.
1
This is based on the classic situation of a salesman
who wishes to visit a number of towns and return home
using the shortest possible route.
3. Graphs and Networks 3.3 The Traveling Salesman Problem 22
Example (2)
E Moscow
F 16
34
2480
32 30 D
29 1600
G London 890
Berlin 1680
25
45 49 22 340 870 520
20 2380
C 1270 Paris
1000 Vienna
23 741
24 1060 1040
A B
Madrid 1340 Rome
Figure 2 MST for the network of Figure 1.
Figure 4 Distances between European capitals in km.
We replace the edge EG by DE and add the In Problem 3.2.4 we found the following ma-
edge AF, which completes a cycle. trix (see also Figure 5)
Solution:
Now Work Problem 3.3.1
When we use the two short-cuts Moscow- a) Find the upper and lower bounds for the
Vienna and Rome-Madrid, we find an upper TSP of Figure 8.
bound of 7.971 km. (Figure 6) b) Find a solution of the TSP using shortcuts.
For Prims matrix use the sequence Ah, Be,
Moscow Ldt, E, S, We, H, Ltl, Wv.
1680
1600 Ahlen 15 Beckum
London Berlin
19 32 24
340 870
21 20
Hamm Lippetal
Vienna 16 Lippstadt
Paris Welver 12 23
741 10
17 13
1060 14
11 Erwitte
18
Madrid 1340 Rome 15 Soest
Werl
B
48
A 85
82 67 G
79 62 F 71
52
H 31 Now Work Problem 3.3.3
77
C 44 53
63
Sketch the TSP of Table 4. Find upper and
51 lower bound and a solution of the TSPs.
D 78 E
Now Work Problem 3.3.4
Figure 9
Develop a subroutine for the Visual Basic
Now Work Problem 3.3.2
program of Prims algorithm to solve the
a) Find the upper and lower bounds for the TSP.
TSP of Figure 9.
b) Find a solution of the TSP using shortcuts. A C source code is available in Appendix D.
TABLE 4
Hamburg Bremen Hannover Dortmund Mnster Kassel F'furt a.M.
Hamburg 127 154
Bremen 127 130 167
Hannover 154 130 212 189 166
Dortmund 212 71 164 224
Mnster 167 189 71
Kassel 166 164 191
F'furt a.M. 224 191
Operations Research 3. Graphs and Networks 25
Figure 1 Figure 3
Solution: Solution:
Dijkstra's algorithm produces the diagram in The shortest distance from G to D is 16. The
Figure 2. The shortest distance from A to F is route is G, F, A, B, C, D.
19 (given by the label at F). Tracing the route
backwards using step 6 gives F, E, D, C, B,
A, so the required route is A, B, C, D, E, F.
2 5 5 16 4 10 B
10
5 17 16
6 12 7 16
B 12 E 13 12 18 16
5 2
5 3 4
3 5 8
C D
A 2 7 5 F 7 5 A
1 0 8 9 6 19 2 5
20 19 7 8
C 4 D 1 0 3 7
3 7 4 11 2 3 5 10
8 7 12 11 G F 3
E 10
Figure 4
Figure 2
3. Graphs and Networks 3.4 Dijkstra's Algorithm 27
18 2 D
4
Step 2 Delete the smallest working value
C and enter it as a label for that vertex.
16
A For each of the vertices directly
2 6 8 2
connected to it, add the edge dis-
3 3 tances onto the label value to obtain
G F E total distances. These distances be-
come the working values for these
Figure 5
vertices, unless lower values have
already been found.
Now Work Problem 3.4.2
Use Dijkstra's algorithm to find the shortest
Step 3 Repeat Step 2, until all vertices are
distance from A to F.
labeled.
Example (3)
Use the matrix version of Dijkstra's algorithm
to find the shortest distance from Soest to
Ahlen.
Ahlen 19 Beckum
28 28 26
26 29
Hamm Lippetal
30 Lippstadt
Welver 32 37
11
23 17
21
18 Erwitte
20
20 Soest
Werl
Figure 7
3. Graphs and Networks 3.4 Dijkstra's Algorithm 28
a)
Figure 5
f)
Figure 1
Figure 6
1
Leonhard Euler was the most prolific mathematician
of all time. One of his many contributions to the sub-
ject was to introduce graph theory as a means of solv-
ing problems.
3. Graphs and Networks 3.5 The Route Inspection Problem 31
Solution:
6 7
Figure 10
Figure 7
c) Semi-Eulerian graph.
Figure 11
8
f)
4 18
3 9 7 17
10
5 6 13 14
2 16
1 12 10 15
11/14 13 6/9
7
11 12
8
3 2
Figure 9 15 5
1/4
Figure 12
3. Graphs and Networks 3.5 The Route Inspection Problem 32
Step 1 List all of the odd vertices. Step 1 of the algorithm is to list the odd verti-
ces.
Step 2 Form the list into a set of pairs of
odd vertices. Find all such sets. Vertex A B C D E F
Order 4 3 3 3 3 2
Step 3 Choose a set. For each pair find a
path of minimum length that joins The odd vertices are B, C, D, and E.
them. Find the total length of these
paths for the chosen set. Step 2 of the algorithm is to find the sets of
pairs of odd vertices. Each odd vertex appears
Step 4 Repeat steps 3 until all sets have once in each set. The sets of pairs of odd ver-
been considered. tices are:
Step 5 Choose the set that gives the mini- [BC, DE] [BD, CE] [BE, CD]
mum total. Each pair in the set de-
fines an edge that must be repeated Step 3 of the algorithm is to find the extra
in order to solve the problem. length introduced for each set.
Example (3)
Make a plan for winter road clearance. Find
the minimum number of kilometers required
to clear away snow.
19 6 5
32 24 8/11
21 20 4 10
Hamm Lippetal Hamm Lippetal
16 Lippstadt 16 Lippstadt
Welver 12 Welver 3 9
23 12
10
17 13 17 2
14 1
11 Erwitte 15/18 Erwitte
18 13
15 Soest 14 Soest
Werl Werl
Solution:
Example (4)
Find a solution of the route inspection prob-
lem.
Ahlen 15 Ahlen
Beckum 2 Beckum
19 32 3 4
24 12
20 1/5 11
Hamm 21 Hamm 16
23 Lippstadt 6 10 Lippstadt
16 12 Lippetal
Lippetal 13/17
11
17 Welver 13 7 Welver 15
11 18 Erwitte 27 14 Erwitte
14 9
Werl Soest Werl Soest
15 8
21 18
21
20 30
16 26
24/25
36 Warstein 20 Warstein
Arnsberg Arnsberg
13 19/22
31 23
Meschede Meschede
Solution:
London Berlin
Vienna
Paris
Madrid Rome
Figure 18
3.6 Matchings
The Matching Improvement Algorithm Step 1 In the initial matching, start from a
A vertex that is not connected and look
for an alternating path to a vertex in
1
the other set that is not connected.
B
2
A
C
1
3
B
D
2
Figure 2
C
Figure 2 is a bipartite graph showing the pos- 3
sible connections between two sets. It does D
not represent a matching because some verti-
ces have more than one connection. Figure 4 Alternating path connecting C and 3.
Example (1)
Starting with an initial matching in which 1 is 1 P
connected to R and 2 is connected to P, use
the improvement algorithm to establish a
complete matching for the bipartite graph of 2 Q
Figure 6.
1 P 3 R
2 Q 4 S
Figure 9 New initial matching.
3 R
4 S 1 P
Figure 6
2 Q
Solution:
3 R
1 P
4 S
2 Q Figure 10 Alternating path connecting 3 and Q.
3 R
1 P
4 S
Figure 7 Initial matching. 2 Q
3 R
1 P 4 S
3 R
4 S
Figure 8 Alternating path connecting 4 and S.
3. Graphs and Networks 3.6 Matchings 39
TABLE 1 b)
Names Days available
Abby Mon, Tue, Fri A Mon
Ben Mon, Wed, Thu
Colin Mon, Fri B Tue
Dave Wed, Fri
Emma Thu, Fri C Wed
Solution:
a) A Mon
A Mon B Tue
B Tue C Wed
C Wed D Thu
D Thu E Fri
E 5 TABLE 2
Week Names
Figure 16
30, 32 Anne
29 Bill
30, 33 Carl
29, 34 Daisy
31, 32 Ed
33 Fritz
Chapter Operations Research 41
4
Digraphs
The flows referred to may be flows of liquids, A cut divides the vertices into two sets, one
gases or any measurable quantities. The edges set containing the source and the other con-
may represent such things as pipes, wires or taining the sink.
roads that carry the quantities between the
points identified as vertices. The capacity of a cut is equal to the sum of
the capacities of the edges that cross the cut,
A typical vertex has a flow into it and a flow taken in the direction from the source set to
out of it. The exceptions are a source vertex the sink set.
which has not input and a sink vertex which
has no output.
In Figure 2, the capacity of cut (i) is 15 + 10 =
Each edge of the network has a capacity 25. The capacity of cut (ii) is 8 + 14 = 22.
which represents the maximum possible flow Notice that, for the second cut, the capacity of
along that edge. In the usual notation, the ca- 4 is not included because it does not go from
pacity is written next to the edge and the flow the source set to the sink set.
is shown in a circle.
10
7
(ii)
A 8 D 9 T
The maximum flow minimum cut theo- Now Work Problem 4.1.1
rem states that the maximum value of the Find the maximum flow through the network
flow through a network is equal to the capac- of Figure 4. Verify your answer using the
ity of the minimum cut. This is a bit like say- maximum flow minimum cut theorem.
ing that the minimum cut is the bottle-neck of
the network. A 10 B
Figure 3
Activity Networks
D(2)
A Complex project may be divided into a 1 3
number of smaller parts called activities. The A(3)
completion of one or more activities is called
event.
F(5)
0 C(3)
Activities often rely on the completion of oth-
ers before they can be started.
B(5)
The relationship between these activities can 2 4 5
be represented in a precedence table, some-
E(5) G(1)
times called dependency table.
TABLE 2
Start Finish
Activity Duration Earliest Latest Earliest Latest Float
33 D(2) 57 A(0, 1) 3 0 0 3 3 0
B(0, 2) 5 0 1 5 6 1
1 3 C(1, 2) 3 3 3 6 6 0
A(3) D(1, 3) 2 3 5 5 7 2
E(2, 4) 5 6 6 11 11 0
00
F(3, 4) 4 5 7 9 11 2
F(4)
0 C(3) G(4, 5) 1 11 11 12 12 0
Scheduling
0 1 2 3 4 5 6 7 8 9 10 11 12
TABLE 3
Activity Depends on 33 59
C(2)
A(3) -
1 3
B(5) - A(3)
C(2) A
D(3) A 00
dummy
E(3) B, D D(3) G(1)
0
F(5) C, E
G(1) C
H(2) F, G B(5) F(5) H(2)
2 4 5 6
Example (1) 66 E(3) 9 9 14 14 16 16
Given the Precedence Table 3. Find the activ-
ity network, the critical activities, and the
critical path. Construct a chart for the purpose Figure 5 Critical path: ADEFH
of scheduling.
TABLE 4
Solution: Start Finish
Activity Duration Earliest Latest Earliest Latest Float
A dummy activity is one that has zero dura- A(0, 1) 3 0 0 3 3 0
tion. A dummy is needed in the network of B(0, 2) 5 0 1 5 6 1
C(1, 3) 2 3 7 5 9 4
Figure 4 to show that G depends on C D(1, 2) 3 3 3 6 6 0
whereas F depends on C and E. E(2, 4) 3 6 6 9 9 0
F(4, 5) 5 9 9 14 14 0
G(3, 5) 1 5 13 6 14 8
H(5, 6) 2 14 14 16 16 0
C(2)
1 3
A(3) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A D E F H
dummy
D(3) G(1)
0 B(5)
C(2)
B(5)
2 4 5 6
E(3) F(5) H(2) G(1)
Figure 4 Activity network. Figure 6 Remember that G cannot be started until C has
been completed.
4. Digraphs 4.2 Critical Path Analysis 46
TABLE 5
Activity Depends on TABLE 6
A(4) - Activity Depends on
B(6) - A(12) -
C(3) A B(10) A
D(2) A C(8) A
E(8) B, C D(9) C
F(9) B, C E(7) D, F
G(6) D, E F(6) A
H(5) D, E G(11) B, E
I(5) H H(3) D, F
J(3) F, G I(12) H
J(4) G, I
D(2) H(5)
1 3 5
A(4)
0 C(3) I(5)
E(8) G(6)
B(6)
2 4 6
F(9) J(3)
Figure 7
Chapter Operations Research 47
5
Introduction to Linear Programming Definition
5.1 Linear Inequalities in Two Variables A linear inequality in the variables x and y is
an inequality that can be written in the form
Suppose a consumer receives a fixed income
of 100 per week and uses it all to purchase ax + by + c < 0 (or 0, 0, > 0)
products A and B. If x kilograms of A cost 4
per kilogram and y kilograms of B cost 5 per where a, b, and c are constants and not both a
kilogram, then and b are zero.
On the other hand, suppose the consumer Clearly, there are infinitely many solutions,
does not necessarily wish to spend all of the which is typical of every linear inequality.
100. In this case, the possible combinations
are described by the inequality To consider linear inequalities in general, we
first note that the graph of a nonvertical line
(1) 4x + 5y 100 where x, y 0. y = mx + b separates the plane into three dis-
tinct parts ( see Figure 2):
y y y = mx + b
25
20 y > mx + b
15
(10, 12) x
10
5 y < mx + b
0 x
0 5 10 15 20 25
Figure 1 Budget line. Figure 2 A nonvertical line determines two half planes.
5. Introduction to Linear Programming 5.1 Linear Inequalities in Two Variables 48
1. the line itself, consisting of all points (x, y) Example (1): Solving a Linear Inequality
whose coordinates satisfy the equation Find the region defined by the inequality
y = mx + b;
2x + y 4.
2. the region above the line, consisting of all
points (x, y) whose coordinates satisfy the
inequality y > mx + b (this region is called an Solution: From our previous discussion, we
open half plane); know that the solution is a halve plane. To
find it, we begin by replacing the inequality
3. the open half plane below the line, consist- symbol by an equality sign and then graphing
ing of all points (x, y) whose coordinates sat- the resulting line, 2x + y = 4. This is easily
isfy the inequality y < mx + b. done by choosing two points on the line for
instance, the intercepts (2, 0) and (0, 4). (See
In the situation, where the strict inequality "<" Figure 4.)
is replaced by "", the solution of y mx + b
consists of the line y = mx + b, as well as the We must now determine whether the solution
half plane below it. In this case we say that includes the have plane above the line or the
the solution is a closed halve plane. A similar one below it. This can be done by solving the
statement can be made when ">" is replaced inequality for y. Once y is isolated, the appro-
by "". priate half plane will be apparent. We have
1 4 2(2)
1 8.
y x=a y
4
x<a x>a
2
x x
y 5.
y
5
Figure 5 Graph of y 5.
2(2x y) 2(x + y) 4.
Operations Research 5. Introduction to Linear Programming 50
xy
y
2y 1 0
y = - 2x + 4
We first rewrite each inequality so that y is
isolated. This gives the equivalent system
y 2x + 3
x
yx
y=x- 2
y 1
2
y=x
3
2
y = 1/2 1
x
1 2 3
y = - 2x + 3
x+ y 1
x x + 3y 3
y = 1/3x - 3 5.2.3 x 3y 3
4x + 3y 12
5.2.4 2x + 4y 8
3x 4y 12
3x + y 3
Operations Research 5. Introduction to Linear Programming 52
y y
160 160
2x +y =180
120 120
x +y =100
80 80
E P = 600
A
40 x +2y =160 40
Feasible
region P = 300 B
C
x
x 0 D
40 80 120 160
0 40 80 120 160
Figure 2 Isoprofit lines and feasible region.
Figure 1 Feasible region.
From Figure 2, we see that A lies on both the This gives the point B = (80, 20). In a similar
line x + y = 100 and the line x + 2y = 160. way, we obtain all the corner points:
Thus, its coordinates may be found by solving
the system A = (40, 60) B = (80, 20) C = (90, 0)
2x + y = 180
x + y = 100
5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 55
2x + y = 8
2x + 3y = 12
Z(A) = 3(0) + 0 = 0
Z(B) = 3(4) + 0 = 12
Z(C) = 3(3) + 2 = 11
Z(D) = 3(0) + 4 = 4
4x + 3y 72 20 3x- 4y = 4
15
3x 4y 4
10
C
x, y 0
5 4x +3y = 72
A
x
Solution: We solve the equations 0 B 5 10 15 20 25
Figure 4 A, B, C, and D are corner points of the feasible
4x + 3y = 72 region.
3x 4y = 4
A = (0, 0) B = (4/3, 0)
The next example illustrates a situation where If a feasible region is unbounded, and if the
no optimum solution exists. objective function has a maximum (or mini-
mum) value, then that value occurs at a corner
Example (3): Empty Feasible Region point.
Maximize the objective Function Z = 8x + 3y,
subject to the constraints Example (4): Unbounded Feasible Region
Minimize the cost function C = 2x + 2y,
x + 3y 21 subject to the constraints
x+ y5 5x + 2y 50
x, y 0 x + 2y 20
x, y 0
Solution: The portion of the lines for which
x 0 and y 0 are shown in Figure 5. They
remain dashed lines until we determine Solution:
whether or not they are to be included in the
feasible region. A feasible point (x, y) must y
have x 0 and y 0, and must lie both on or
above the top dashed line and on or below the 25 A
bottom dashed line. However, no such point
20 5x +2y = 50
exists. Hence, the feasible region is empty,
and the problem has no optimum solution. 15
10
y B
x+ 2y = 20
5
8 C
x
-x + 3y = 21 0 5 10 15 20 25
6
Empty feasible region
4 Minimum cost at corner point B of an
Figure 6
unbounded feasible region.
2 x +y =5
x
0 2 4 6 8
Figure 5 Empty feasible region.
5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 58
x, y 0
27x + 6y 81
6x + 8y 48
x, y 0
5.3.3 Z = 5x + 5y
2x + 5y 100
6x + 3y 180
x, y 0
Operations Research 5. Introduction to Linear Programming 59
Z = c1 x1 + c2 x2
a11 a12 b1
subject to a21 a22 b2
a31 a32 b3
a11x1 + a12x2 b1 -c1 -c2 0
The entries in the last row1 are called indicators.
a21x1 + a22x2 b2
The 0th row is a working row and the 0th
a31x1 + a32x2 b3 column a working column.2
1
Also called objective row.
2
We now profit from our convention about the 0th row
and column in a matrix.
5. Introduction to Linear Programming 5.4 The Simplex Algorithm 60
4. Mark the entry in the pivot column that 8. The other entries are calculated according
corresponds to the smallest quotient in step 3. to the following scheme:
This is the pivot entry and the row in which
it is located is the pivot row. If, e. g., b2/a22 is new = old pivot column working row
the smallest quotient, we have
This means for the new entry in the upper left
corner:
b1/a12 a11 a12 b1 a
b2/a22 new = a11 a12 21
a21 a22 b2 a22
b3/a32 a31 a32 b3
-c1 -c2 0 a21/a22 1/a22 b2/a22
b1/a12 a11 a12 b1
5. Now fill the entries of the working row: the b2/a22 a21 a22 b2
entry in the pivot column becomes 1/a22, b3/a32 a31 a32 b3
while the other entries become the ratios of
-c1 -c2 0
the entries of the pivot row and the pivot,
which completes the first tableau:
a11 - a12a21/a22 -a12/a22
a21/a22 1/a22 b2/a22
b1/a12 a11 a12 b1 a21/a22 1/a22 b2/a22
b2/a22 -a32/a22
a21 a22 b2
b3/a32 a31 a32 b3 c2/a22
-c1 -c2 0
6. Set up the second tableau. First copy the a21/a22 1/a22 b2/a22
b1/a12 a11 a12 b1
entries of the old working row into the old
b2/a22 a21 a22 b2
pivot row:
b3/a32 a31 a32 b3
-c1 -c2 0
Example (1)
Maximize 5. Now fill the entries of the working row: the
entry in the pivot column becomes 2/7, while
Z = 300x + 200y the other entries become the ratios of the en-
tries of the pivot row and the pivot, which
subject to the constraints completes the first tableau:
x + 3y 30 2/7 4/7 10
30/1 = 30 1 3 30
33/2 = 16.5 2 3 33
2x + 3y 33
35/3.5 = 10 7/2 2 35
7
2
x + 2 y 35 -300 -200 0
Solution:
6. Set up the second tableau. First copy the
entries of the old working row into the old
1. Set up the initial simplex tableau:
pivot row:
1 3 30
2 3 33
7/2 2 35
2/7 4/7 10
-300 -200 0
30/1 = 30 1 3 30
33/2 = 16.5 2 3 33 9. The indicator in the second column is
35/3.5 = 10 7/2 2 35 negative, so we repeat the process, beginning
-300 -200 0 with step 2 applied to the new tableau.
5. Introduction to Linear Programming 5.4 The Simplex Algorithm 63
6/13 -17/13 3
-4/13 7/13 7
6/13 -4/13 6
1000/13 200/13 3200
5. Introduction to Linear Programming 5.4 The Simplex Algorithm 64
Example (2)
Maximize Now Work Problem 5.4.1
Maximize
Z = x + 2y + 4z
Z = 4x + 6y
subject to the constraints
subject to the constraints
10x + 2y + z 36
2x + y 180
2x + 25y + 2z 120
x + 2y 160
x + y + 25z 206
x + y 100
Solution:
2 y
1 C 4
D
x 3
0 1 2 3 4
2
A'
Figure 1 Unbounded feasible region and graphical solu- 1 B'
tion of the minimization problem.
C'
x
0 1 2 3 4
Note that the origin of the coordinate system
is not a point of the feasible region. There-
Figure 2 A nave application of the simplex algorithm
fore, a nave application of the simplex algo- does not lead to a solution of the minimization prob-
rithm does not lead to a solution of the mini- lem.
mization problem, as is illustrated by the fol-
lowing tableaus and Fig. 2
5. Introduction to Linear Programming 5.5 Minimization Problems 66
y I. 3(3 u ) + 4 v 4
2 1 0 3u v 9
u A
II. 8(3 u ) + 6(4 v ) 19
1
B
8u 6v 29
2
III. 3 u + 2(4 v ) 3
C
3
D
u 2v 8
x
4. Multiply the constraints by (1) which re-
v verses the inequality sign and transforms the
Figure 3 Strategy: Make an appropriate point of the minimization problem into a maximization
feasible region the origin of a new coordinate system problem.
with new variables u and v, thus transforming the
minimization into a maximization problem. Then apply
the simplex algorithm. Finally transform the solution I. 3u + v 9
for u and v back to the original variables x and y.
II. 8u + 6v 29
1. Find the intercepts forms of the constraints:
III. u + 2v 8
x y
I. + 1
1.33 4.00
x y
II. + 1
2.38 3.16
x y
III. + 1
3.00 1.50
x = 3
y = 4
x = x u = 3 u
y = y v = 4 v
5. Introduction to Linear Programming 5.5 Minimization Problems 67
v y
4 4 A
D
C
3 3
B
2 2
B
1 1 C
A D
u x
0 1 2 3 4 0 1 2 3 4
Simplex algorithm:
1/2 1 5/2
1/5 3/5 1
1/10 4/5 7/2
1/5 7/5 17
The final tableau corresponds to point C in
Fig. 4.
u=1 v = 3.5 Z = 17
x=3u=2
y = 4 v = 0.5
Z = 3x + 4y = 8
Chapter Operations Research 68
6
Nonlinear Optimization Problems
The algorithm does not make use of a specific indicator that the function is not computable at a
point. If the function is not computable, therefore, a large value should be assigned to the function
larger than the function value at any of the vertices in the current simplex. This will usually, but not
always, force the Nelder-Mead process away from the point in question. Note that the algorithm
must be stopped if the initial point is not feasible.
#
J. A. Nelder and R. Mead 1965 Comput. J 7, 308
J. C. Nash COMPACT NUMERICAL METHODS FOR COMPUTERS: linear algebra and function minimization
Adam Hilger (Bristol 1979)
6 Nonlinear Optimization Problems 6.1 A Nelder-Mead Procedure 69
Step 9 is the point at which the algorithm continues after a general contraction of the simplex.
Let ss = size to save the simplex size measure.
9 Now compute the function value at each vertex.
10 For j = 1 to n + 1
11 If j = L, goto step 13 to skip the evaluation, since the function value is already
known at vertex L.
12 For i = 1 to n, let b[i] = X[i, j], end loop on i.
Compute P = S(b), let count = count + 1.
Let X[n + 1, j] = P.
13 End loop on j.
Now perform tests on the function values to order them. Step 14 is the point at which the
Nelder-Mead algorithm continues in all instances except a general contraction of the simplex.
Let L = 1, let H = 1, let M = 1, let VL = X[n + 1, 1], let VH = VL.
14 This initializes the pointers to the lowest, highest and next-to-highest vertices and sets initial
values for the lowest and highest.
15 For j = 2 to n + 1
16 Let T = X[n + 1, j]
17 If T < VL, let VL = T and let L = j since a new lowest value has been found.
18 If T >= VH, let M = H, also let H = j, let VH = T. A new highest value has been
found and the pointer to the next-to-highest reset.
19 End loop on j.
Note that in steps 17 and 18 other forms of the test may be easier to implement. At this point in
the algorithm, the progress of the minimization may be monitored by printing count, VL, and
VH.
20 If VL =VH, goto step 40 since procedure has converged.
21 For i = 1 to n
Let T = X[i, H].
For j = 1 to n + 1; let T = T + X[i, j]; end loop on j.
Let X[i, C] = T/n to save the centroid of all points but the highest in the right-most
Column of X.
End loop on i.
22 Reflection to bR.
For i = 1 to n
Let b[i] = (1 + alpha )*X[i, C] alpha*X[i, H]
End loop on i.
Compute P = S(b); let count = count + 1.
Perform tests on the new function value.
23 If P < VL; goto step35 to extend the simplex since a new lowest has been found at bR.
24 If P < X[n + 1, M]; goto step 39 to replace bH with the new point bR.
25 If P >= VH; goto step 27 to reduce the simplex by finding a new point on the line bet-
Ween bC and bH.
Should the above condition not true, then S(bR) > VH but is greater than the next-to-
Highest function value. Therefore the reduction is performed along the line between bC and
bR by means of the following replacement of the highest vertex.
26 For i = 1 to n; let X[i, H] = b[i]; end loop on i. Let X[n + 1, H] = P.
6 Nonlinear Optimization Problems 6.1 A Nelder-Mead Procedure 70
M
(1) f (t ) =
1 + be ct
Time t
#
s. Business Bathematics II
6 Nonlinear Optimization Problems 6.2 Least Squares Fits: Logistic Growth 72
Month 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
Units 6675 7325 10550 11765 14725 15885 19637 19875 22879 23123
50000
40000
Number of Units Sold
30000
20000
10000
0
0 5 10 15 20
Months
Figure 2
An essential question for the management of the company is: how many units should be produced
in order to optimize the supply in view of the expected maximum demand?
Mathematically spoken: we want to find the parameters M, b, and c of function (1) that best de-
scribe the empirical data. Then M will be the maximum demand.
20
V (M , b, c ) = ( N i f (t i )) ,
2
(2)
i =1
which is a function of the three variables M, b, and c. In mathematics, extrema of functions of sev-
eral variables can be determined using the methods of calculus, which involve the partial deriva-
tives of the function in question.# However, we would not even think to find the partial derivatives
of function (2) with respect to the variables M, b, and c.
#
see Business Mathematics II
6 Nonlinear Optimization Problems 6.2 Least Squares Fits: Logistic Growth 73
The idea of a least-squares fit# is to vary the parameters M, b, and c in a systematic way, so that
function (2) achieves a minimum. This is the purpose of the Nelder-Mead procedure of section 6.1.
A Visual Basic source code of the algorithm is available in Appendix C.
The application of the Nelder-Mead algorithm gives the values of the parameters that best fit the
empirical data:
Fig. 3 shows the graph of the function. The result of this analysis is, that the management of the
company should not plan to produce more than 30 000 units of their product.
50000
40000
Number of Units Sold
30000
20000
10000
0
0 5 10 15 20
Months
Figure 3
#
since function (2) is a sum of squares
Appendix Operations Research 74
A
Problems Solved Step by Step
1.2.1
Solution:
1.3.1
Solution:
TABLE 1
Town Distance/km
Lippetal 17
Beckum 29
Ahlen 19
Hamm 28
Werl 23
Welver 18
Soest 21
Erwitte 20
Lippstadt 11
Erwitte 11
Soest 20
217
Ahlen 19 Beckum
28 28 26
26 29
Hamm Lippetal
30 Lippstadt
Welver 32 37
11
23 17
21
18 Erwitte
20
20 Soest
Werl
Appendix A Problems Solved Step by Step 75
2.1.1 2.1.3
Solution: Solution:
Sub reverse()
a) The algorithm generates the powers of 2 Dim i
from 1 to 512. For i = 1 To n
Worksheets("IO").Cells(4, i) _
= Worksheets("IO").Cells(2, n + 1 - i)
b) Next i
End Sub
Sub NWP211()
Dim i, j
i = 1
2.1.4
j = 1
0: Worksheets("IO").Cells(1, i) = j
Solution:
If i = 9 Then GoTo 1
i = i + 1 Option Explicit
j = 2 ^ i Public n
GoTo 0 Sub NWP213()
1: End Sub
init
reverse
Worksheets("IO").Cells(1, 5) = mean
End Sub
2.1.2
Solution: Sub init()
n = Worksheets("IO").Cells(1, 2)
End Sub
a) The algorithm finds the smallest number
Sub reverse()
out of n. Dim i
For i = 1 To n
Worksheets("IO").Cells(4, i) _
b) = Worksheets("IO").Cells(2, n + 1 - i)
Next i
Sub NWP212() End Sub
Dim i, n
i = 1 Function mean()
n = Worksheets("IO").Cells(1, 2) Dim i, x
Worksheets("IO").Cells(1, 5) _ x = 0
= Worksheets("IO").Cells(2, i) For i = 1 To n
0: i = i + 1 x = x + Worksheets("IO").Cells(2, i)
If i = n + 1 Then GoTo 1
i = i + 1 Next i
If Worksheets("IO").Cells(2, i) _ x = x / n
< Worksheets("IO").Cells(1, 5) Then mean = x
Worksheets("IO").Cells(1, 5) _ End Function
= Worksheets("IO").Cells(2, i)
End If
GoTo 0
1: End Sub
A B C D E A B C D E
1 n= 5 mean= 40.2
1 n= 5 d= 12
2 23 35 12 86 45 2 23 35 12 86 45
3
4 45 86 12 35 23
Appendix A Problems Solved Step by Step 76
2.2.1 2.2.3
Solution: Solution:
Option Explicit
4 8 2 0 6 Public n
4 8 2 0 6 4 2 8 0 6
Sub BubbleSort()
4 2 8 0 6 4 2 0 8 6 init
4 2 0 8 6 4 2 0 6 8 sort
End Sub
4 2 0 6 8 2 4 0 6 8 Sub init()
Dim i
2 4 0 6 8 2 0 4 6 8 n = Worksheets("IO").Cells(1, 1)
For i = 1 To n
2 0 4 6 8 Worksheets("IO").Cells(4, i) _
= Worksheets("IO").Cells(2, i)
Next i
2 0 4 6 8 0 2 4 6 8 End Sub
0 2 4 6 8
Sub sort()
Dim i, x, exchanged
0 2 4 6 8 0: exchanged = 0
For i = 1 To n - 1
If Worksheets("IO").Cells(4, i) _
> Worksheets("IO").Cells(4, i + 1) Then
x = Worksheets("IO").Cells(4, i + 1)
2.2.2 Worksheets("IO").Cells(4, i + 1) _
Solution: = Worksheets("IO").Cells(4, i)
Worksheets("IO").Cells(4, i) = x
exchanged = 1
4 8 2 0 6 8 4 2 0 6 End If
Next i
8 4 2 0 6 n = n - 1
If exchanged = 1 And n >= 1 Then GoTo 0
8 4 2 0 6 End Sub
8 4 2 0 6 8 4 2 6 0
A B C D E F
8 4 2 6 0 1 6
8 4 2 6 0 2 77 48 55 12 7 1
8 4 2 6 0 8 4 6 2 0 3
4 1 7 12 55 48 77
8 4 6 2 0
8 4 6 2 0 8 6 4 2 0
8 6 4 2 0
Appendix A Problems Solved Step by Step 77
2.3.1 2.4.1
Solution: Solution:
1 1 1 1 1 1 1 1 1
17 29 35 44 51 63 78 86 92
2.3.2 2.4.2
Solution: Solution:
1 1 1 1 1 1 1 1 1
92 86 78 63 51 44 35 29 17
Appendix A Problems Solved Step by Step 78
2.5.1 3.2.1
Solution: Solution: Applying the matrix form of Prim's
algorithm gives:
While the program is running, three arrows
point to actual min, mid, and max cells. E. g.: 1 7 5 6 3 2 4
A B C D E F G
A B C A 50 40 61 38
1 15 91 <--- B 50 47 51
2 72 83
3 72
C 40 35
4 66 <--- D 61 35 42
5 64 E 42 25 10
6 53 F 38 47 25 63
7 46 <--- G 51 10 63
8 35
9 21
10 16
11 14 B
12 4 A
G
In the final output, the arrow points to the F
target cell:
A B C E
1 15 91
2 72 83 C D
3 72 <---
4 66
5 64 Figure 1
6 53
7 46 The minimum cost of laying the paths is
8 35 19500.
9 21
10 16
11 14
12 4
Appendix A Problems Solved Step by Step 79
3.2.2 3.2.4
Solution: Solution:
1 5 6 47 3 2
Be Lo Ma Mo
Pa Ro Vi
Ahlen
19 Beckum Be 890 1600
870 520
28 28 26
Lo 890 1270 2480
340
27 29 Ma 1270 1060 1340
Hamm Lippetal Mo 1600 2480 2380 1680
30 Lippstadt
Welver 32 37 11 Pa 870 340 1060 1040 1000
23 17
21 Ro 1340 2380 1040 740
18 Erwitte
20 Vi 520 1680 1000 740
20 Soest
Werl
3.2.3
Solution:
London Berlin
Matrix for Prims algorithm:
Vienna
Ah Ha Be Wr Wl Ll So Lt Er Paris
Ah 28 19
Ha 28 28 30 23
Madrid
Be 19 28 27 29 26 Rome
Wr 30 27 18 32 21
Wl 23 18 20 Figure 3
Ll 29 32 17 37
So 21 20 17 20 The length of the MST is 5,130 km. The main
Lt 26 37 11 route may be Madrid, Paris, Berlin, Moscow,
Er 20 11 with shuttle flights from London to Paris and
from Rome to Berlin, via Vienna.
1 9 2 8 7 6 5 3 4
Ah Ha Be Wr Wl Ll So Lt Er
Ah 28 19
Ha 28 28 30 23
Be 19 28 27 29 26
Wr 30 27 18 32 21
Wl 23 18 20
Ll 29 32 17 37
So 21 20 17 20
Lt 26 37 11
Er 20 11
3.3.1 3.3.2
Solution: Solution:
Ah Be Ldt E S We H Ltl Wv a)
Ah 15 19 A B C D E F G H
Be 15 24 32 20 21
Ldt 24 10 23
A 48 79 82 62
E 10 18 B 48 67 85
S 18 15 13 14 C 79 51 52
We 15 17 11 D 51 78 53 44
H 19 32 17 16 E 78 63 77
Ltl 20 23 13 12
Wv 21 14 11 16 12
F 82 67 53 63 71 31
G 85 77 71
1 2 9 8 7 5 3 6 4 H 62 52 44 31
Ah Be Ldt E S We H Ltl Wv
Ah 15 19 1 2 6 5 7 4 8 3
Be 15 24 32 20 21 A B C D E F G H
Ldt 24 10 23 A 48 79 82 62
E 10 18
B 48 67 85
S 18 15 13 14
We 15 17 11 C 79 51 52
H 19 32 17 16 D 51 78 53 44
Ltl 20 23 13 12 E 78 63 77
Wv 21 14 11 16 12 F 82 67 53 63 71 31
G 85 77 71
a) The length of the MST is 114 km. Thus, H 62 52 44 31
the upper bound is 2114 km = 228 km. The
last deleted vertex was the one at Lippstadt Upper bound: 2370 = 740.
and the second shortest edge connected to it Lower bound: 370 + 77 = 447.
has a length of 23 km. Therefore, the lower
bound is 114 km + 23 km = 137 km. B
48
A 85
Ahlen 15 Beckum
82 67 G
19 32 79 62 F
24 71
21 20
52
H 31
Hamm Lippetal 77
16 Lippstadt 63
Welver 12 23
10 C 44 53
17 13 51
14
11 Erwitte 78
18 D E
15 Soest
Werl
Figure 6 MST.
Figure 4 MST of Problem 3.3.1.
b) B
48
A 85
Ahlen 15 Beckum
82 67 G
19 32 79 62
24 F 71
Hamm
21 20
Lippetal 52
H 31
16 Lippstadt 63 77
Welver 12 23
13
10 C 44 53
17
14 51
11 Erwitte
18 78
15 Soest D E
Werl
3.3.3
Solution:
HH HB H DO M K F
HH 127 154
HB 127 130 167
H 154 130 212 189 166
DO 212 71 164 224
M 167 189 71
K 166 164 191
F 224 191
1 2 3 5 6 4 7
HH HB H DO MS KS F
HH 127 154
HB 127 130 167
H 154 130 212 189 166
DO 212 71 164 224
MS 167 189 71
KS 166 164 191
F 224 191
Route:
3.4.4
Solution:
Sub bounds()
Dim i, r, c, x, exchanged, nsmd
r = 0
For i = 1 To n
If Worksheets("a").Cells(n + 1, i) = n Then r = i
Next i
c = 0
For i = 1 To n
If Worksheets("IO").Cells(r + 1, i + 1) > 0 Then
c = c + 1
Worksheets("a").Cells(r, c) = Worksheets("IO").Cells(r + 1, i + 1)
End If
Next i
exchanged = 1
0: exchanged = 0
For i = 1 To c - 1
If Worksheets("a").Cells(r, i) > Worksheets("a").Cells(r, i + 1) Then
x = Worksheets("a").Cells(r, i + 1)
Worksheets("a").Cells(r, i + 1) = Worksheets("a").Cells(r, i)
Worksheets("a").Cells(r, i) = x
exchanged = 1
End If
Next i
c = c - 1
If exchanged = 1 And c >= 1 Then GoTo 0
nsmd = Worksheets("a").Cells(r, 2)
Worksheets("io").Cells(2 * n + 8, 1) = ("UBound:")
Worksheets("io").Cells(2 * n + 8, 2) = 2 * length
Worksheets("io").Cells(2 * n + 10, 1) = ("LBound:")
Worksheets("io").Cells(2 * n + 10, 2) = length + nsmd
End Sub
3.4.1 3.4.3
Solution: Solution:
64 82 69 47 36 34 45 31 0 13
H Ah Be We S E L Ar M Wa
7 16 64 H 18 32 17
18 16 B 82 Ah 18 15
6 14 5 10 69 Be 32 15 33 33 24
16 14 10 47 We 17 33 15 16
18
2 4 36 S 33 15 18 20 36 30
1 0 16 C D 34 E 18 11 21
A 45 L 24 11
8 31 Ar 16 20 31
2 6 2
0 M 36 31 13
2 2 3 3 13 Wa 30 21 13
2 3 5 4 8
G F 6 5
E 8
Figure 8 64 82 69 47 36 34 45 31 0 13
H Ah Be We S E L Ar M Wa
64 H 18 32 17
Dijkstra's algorithm produces the diagram in 82 Ah 18 15
Figure 8. The shortest distance from A to B is 69 Be 32 15 33 33 24
47 We 17 33 15 16
16 (given by the label at B). Tracing the route 36 S 33 15 18 20 36 30
backwards using step 6 gives B, C, D, E, F, 34 E 18 11 21
45 L 24 11
G, A, so the required route is A, G, F, E, D, 31 Ar 16 20 31
C, B. 0 M 36 31 13
13 Wa 30 21 13
Route: M Ar We H - Ah
2 5 5 14
5 15 14
B E
5 7
10
3
Ah 15 Be
A 3 4 5 F 18 32 24
1 0 9 9 6 17
H 33
L
C 2 D 18 17 33
11
17 18
3
9
8
8
4 9
9 15 S E
We 21
30
Figure 9 20
16
36 Wa
Dijkstra's algorithm produces the diagram in
Ar 13
Figure 9. The shortest distance from A to F is 31
17 (given by the label at F). Tracing the route M
backwards using step 6 gives F, E, D, B, A, so Figure 10
the required route is A, B, D, E, F.
Appendix A Problems Solved Step by Step 84
3.5.1
Solution:
Moscow
12
11
13
London Berlin 9
2 14
10
6 8
3 Vienna
Paris
5 7
1
Madrid 4 Rome
Figure 11 Solution.
3.5.2
Solution: There are three neighboring pairs of
odd vertices: Ar M, Wa E, and H Be.
The length of the total network is 383 km.
Adding the weights of the edges between the
pairs of odd vertices gives the minimum
length:
383 km + 31 km + 21 km + 32 km = 467 km
Possible Route: M S Ar M Wa E S
Wa E L Be So We Be H Ah
Be H We Ar M.
Ah 15 Be
18 32 24
H 33
L
33
11
17 18
15 S E
We 21
30
20
16
36 Wa
Ar 13
31
M
Figure 12 Solution.
Appendix A Problems Solved Step by Step 85
3.6.1
Solution:
A 1 A 1
B 2 B 2
C 3 C 3
D 4 D 4
E 5 E 5
A 1 A 1
B 2 B 2
C 3 C 3
D 4 D 4
E 5 E 5
A 1
B 2
C 3
D 4
E 5
3.6.2
Solution:
A 29 A 29
B 30 B 30
C 31 C 31
D 32 D 32
E 33 E 33
F 34 F 34
A 29
B 30
C 31
D 32
E 33
F 34
A 29
B 30
C 31
D 32
E 33
F 34
4.1.1
Solution:
34 44
A 10 B
11 6 19
16
S T
12 C
9
11
18
D 15 E
43 43
A 10 B
10
16
11 6 19
16
6 6 16
S 9 T
12 C 9 18
9
11
18
9
9
D 15 E
4.2.1 4.2.4
Solution: Solution:
4 4 15 15 20 20 36 36 47 47
G(11)
1 3 5
4 6
B(10) J(4)
D(2) H(5) E(7) I(12)
0 0
A(4)
A(12) F(6) H(3)
0 C(3) 0 1 3 5 7
E(8) G(6) I(5)
0 0 12 12 29 29 32 35 51 51
B(6) F(9) J(3) C(8) D(9)
2 4 6
2
7 7 21 22 25 25 20 20
Figure 24 Activity network with critical path ACEHI, Figure 26 Activity network with critical path ACDEGJ,
length: 25. length: 51.
4.2.2 4.2.5
Solution: Solution:
TABLE 1 TABLE 2
Start Finish Start Finish
Activity Duration Earliest Latest Earliest Latest Float Activity Duration Earliest Latest Earliest Latest Float
A(0, 1) 4 0 0 4 4 0 A(0, 1) 12 0 0 12 12 0
B(0, 2) 6 0 1 6 7 1 B(1, 4) 10 12 26 22 36 14
C(1, 2) 3 4 4 7 7 0 C(1, 2) 8 12 12 20 20 0
D(1, 3) 2 4 13 6 15 9 D(2, 3) 9 20 20 29 29 0
E(2, 3) 8 7 7 15 15 0 E(3, 4) 7 29 29 36 36 0
F(2, 4) 9 7 13 16 22 6 F(1, 3) 6 12 23 18 29 11
G(3, 4) 6 15 16 21 22 1 G(4, 6) 11 36 36 47 47 0
H(3, 5) 5 15 15 20 20 0 H(3, 5) 3 29 32 32 35 3
I(5, 6) 5 20 20 25 25 0 I(5, 6) 12 32 35 44 47 3
J(4, 6) 3 21 22 24 25 1 J(6, 7) 4 47 47 51 51 0
4.2.3 4.2.6
Solution: Solution:
0 5 10 15 20 25 0 5 10 15 20 25 30 35 40 45 50
A C E H I A C D E G J
B(6) B(10)
D(2) F(6)
F(9) H(3)
G(6) I(12)
J(3)
5.1.1
Solution: We first solve the inequality for y,
so that the appropriate half plane is obvious.
The inequality is equivalent to
4x 2y 2x + 2y 4
4x 4y 2x 4
4y 2x 4
x
y + 1.
2
1
x
-2
5.2.1 5.2.3
Solution: Solution:
y 2x + 7.5 y 1/3x 1
y 2x + 1.5 y 4/3x + 4
y 0.5 y 2x 4
y y
2x+ 1.5
-4/3x +4
2x -4
3 3
2 2
y = 1/2 1 1
x x
1 2 3 1 2 3
1/3x -1
- 2x + 7.5
5.2.2 5.2.4
Solution: Solution:
y 2/3x 2 y 1/2x + 2
y x + 1 y 3/4x 3
y 1/3x + 1 y 3x + 3
y y
3 3
-1/2x +2
2 2
-1/3x +1 1 1
x x
1 2 3 1 2 3
2/3x -2 -3x +3
-x+ 1 3/4x -3
5.3.1 5.3.3
Solution: Solution:
y A = (0, 0) y A = ( 0, 0)
B = (3, 0) B = (30, 0)
8 C = (3, 2) C = (25, 10)
x =3 D = (1, 4) 60 D = ( 0, 20)
6 E = (0, 4) 50
y =4 40
6x +3y =180
4 ED 30
y =-0.5x +4.5 20 5x +5y =175
2 C D
10 C
2x +5y =100
A B
x +y =5 A B
x x
0 2 4 6 8 0 10 20 30 40 50 60
5.3.2 5.3.4
Solution: Solution:
5.4.1 5.4.2
Maximize Maximize
Z = 4x + 6y Z = 3x + 5y + 4z
2x + y 180 x + 2y + 10z 58
x + 2y 160 10x + y + z 47
x + y 100 2x + 5y + z 23
Solution: 5x + y + 5z 47
5.5.1
Minimize Z = 2 x + y subject to the con-
straints
I. 2x + 3y 6
II. 4x + y 4
III. 2x + 2 y 5
Solution:
x y
I. + 1
3 2
x y
II. + 1
1 4
x y
III. + 1
2.5 2.5
x = x u = 3 u
y = y v = 4 v
I. 2u 3v 12
II. 4u v 12
III. 2u 2v 9
1/4 1/4 3
6 2 3 12
3 4 1 12
4.5 2 2 9
-2 -1 0
-1/3 2/3 2
2.4 -1/2 5/2 6
12 1/4 1/4 3
0.5 -1/2 3/2 3
1/2 -1/2 6
1/3 -5/3 1
1/3 -1/6 5/2
-1/3 2/3 2
1/3 1/3 7
x = x u = 3 2.5 = 0.5
y = y v = 4 2 = 2
Z = 2x + y = 3
Appendix Operations Research 94
B
Practice Problems
B1.1 B1.5
Write a Visual Basic source code, which finds Sort the numbers i in Table 1 in such a way,
the greatest number out of that the corresponding PCodes are sorted into
descending order, using the quick sort algo-
23, 35, 12, 86, 45 rithm.
Table 1
B1.2 i PCode Name 1st Name Data Files
For the program of Problem 2.1.4 write the 1 34113 Mouse Mickey data1.dat
Visual Basic source code of a function, which 2 21567 Duck Donald data2.dat
calculates the standard deviation of the given 3 55678 Kong King data3.dat
numbers. 4 83123 Duck Daisy data4.dat
5 12234 Brown Charles data5.dat
6 42456 Windsor Charles data6.dat
B1.3
Use the paper and pencil version of the
bubble sort algorithm to sort the numbers
B1.6
23, 35, 12 ,86, 45 A project involves activities A I with dura-
tion in hours as given in the table.
into ascending order.
A B C D E F G H I
3 4 5 2 6 2 5 3 4
B1.4
Sort the numbers Find the minimum number of workers
needed, if each worker works 8 hours. Use the
45, 35, 67, 76, 12, 23, 52 full-bin decreasing algorithm.
TABLE 2
File A B C D E F G
Mb 1489 368 555 1232 734 378 898
Appendix B Practice Problems 95
B2.1 B2.3
Find the minimum spanning tree (MST) for Find the MST for the network of Fig. 3.
the network of Fig. 1.
Ah 15 Be
18 32 24
D
H 33
L
33
3 7 11
17 18
C 8 15 S E
E We 21
5 30
20
7 4 16
5 H F 36 Wa
8 6
B 1
5
Ar 13
2 31
3 G
M
5 Figure 3
A
Figure 1
B2.2
Find the MST for the network of Fig. 2.
F 95 E
44 75
A B 42
60 65
45
41
73 39
C 91 D
Figure 2
Appendix B Practice Problems 96
B2.4 Use the matrix version of Prims algorithm to find the MST of Fig. 1.
A B C D E F G H
A 3 8 5 2
B 3 5
C 8 5 3 8 7
D 3 7
E 8 7 5 6 4
F 5 5
G 5 6 5 1
H 2 7 4 1
B2.5 Use the matrix version of Prims algorithm to find the MST of Fig. 2.
A B C D E F
A 45 41 75 44
B 45 73 39 42
C 41 73 91 60
D 39 91 65
E 75 42 65 95
F 44 60 95
B2.6 Use the matrix version of Prims algorithm to find the MST of Fig. 3.
H Ah Be We S E L Ar M Wa
H 18 32 17
Ah 18 15
Be 32 15 33 33 24
We 17 33 15 16
S 33 15 18 20 36 30
E 18 11 21
L 24 11
Ar 16 20 31
M 36 31 13
Wa 30 21 13
Appendix B Practice Problems 97
B3.1 We fount the MST of Fig. 1 in Problem B2.6. On the basis of this answer find the upper and
lower bound of the TSP. Also find a solution of the TSP using shortcuts.
Ah 15 Be
18 32 24
H 33
L
33
11
17 18
15 S E
We 21
30
20
16
36 Wa
Ar 13
31
M
Figure 1
B3.2 We found the MST of Fig. 2 in Problem B2.5. On the basis of this answer find the upper and
lower bound of the TSP. Also find a solution of the TSP using shortcuts.
F 95 E
44 75
A B 42
60 65
45
41
73 39
C 91 D
Figure 2
Appendix B Practice Problems 98
Bielefeld
Mnster
Wesel
Hamm Paderborn
Dortmund
Duisburg
Meschede
Dsseldorf
Wuppertal
Siegen
Figure 3
B3.3 Use the matrix form of Prims algorithm to find the MST of Fig. 3.
B3.4 Find the upper and lower bound of the TSP. Find a solution of the TSP using shortcuts.
Wesel Mnste Bielefel Duisbu Dortmu Hamm Paderb Dssel Wuppe Siegen Mesch
Wesel 103 38 80
Mnster 103 82 71 47
Bielefeld 82 83 43
Duisburg 38 66 24 46
Dortmund 80 71 66 41 52 107
Hamm 47 83 41 92 123 66
Paderborn 43 92 73
Dsseldorf 24 39
Wuppertal 46 52 39 98
Siegen 107 123 98 79
Meschede 66 73 79
Appendix B Practice Problems 99
Dijkstras Algorithm
Bielefeld
Mnster
Wesel
Hamm Paderborn
Dortmund
Duisburg
Meschede
Dsseldorf
Wuppertal
Siegen
Wesel Mnster Bielef. Duisb. Dortm. Hamm Paderb. Ddorf Wtal Siegen Mesch.
Wesel 103 38 80
Mnster 103 82 71 47
Bielef. 82 83 43
Duisb. 38 66 24 46
Dortm. 80 71 66 41 52 107
Hamm 47 83 41 92 123 66
Paderb. 43 92 73
Ddorf 24 39
Wtal 46 52 39 98
Siegen 107 123 98 79
Mesch. 66 73 79
For the given network, find the shortest paths between the following vertices:
B
D A 48
85
3 7 82 67 G
C 8 79 62 F
E H 71
5 52 31
77
63
7 4 C 44
5 H F 53
8 6 51
B 1 D 78 E
2 5
3 G Figure 3 The road map for a road maintenance team
5
A (weights on the edges in kilometers).
Matchings
B6.1 Table 1 shows the names of five em- B6.3 In Table 3, the numbers that have been
ployees of a company and the days when they highlighted represent a first matching attempt.
are each available to work a late shift. The Implement matching improvement algorithms
days that have been highlighted represent a to obtain a complete matching. Indicate the
first attempt at matching the employees to the alternating paths used.
available days. Implement a matching im-
TABLE 3
provement algorithm to obtain a complete
matching. Indicate the alternating paths used. A 2, 5
B 1
C 2, 4
TABLE 1 D 5
Names Days available E 3, 6
Abby Mon, We F 6
Ben Mon, Thu G 3, 7
Colin Tue
Dave Tue, Fr
Emma Thu B6.4 Table 4 shows the names of 7 employees
of a company and the weeks when they are
each available to work in the holiday season.
The weeks that have been highlighted, repre-
B6.2 Table 2 shows the names of sic employ- sent a first attempt at matching the employees
ees of a company and the weeks when they to the available weeks. Implement matching
are each available to work in the holiday sea- improvement algorithms to obtain a complete
son. The weeks that have been highlighted matching. Indicate the alternating paths used.
represent a first attempt at matching the em-
TABLE 4
ployees to the available weeks. Implement a
matching improvement algorithm to obtain a Names Weeks
complete matching. Indicate the alternating Anne 28, 29
path used. Bill 29, 30
Carl 28, 30, 31
Daisy 30, 32
TABLE 2 Ed 31, 34
Names Week Fritz 32, 34
Alex 31 Gunter 33, 34
Bud 29, 32
Cary 29, 30
Donald 30, 31
Elisa 32, 34
Funda 31, 33
Appendix B Practice Problems 102
Flows in Networks
A 5 D 12 T
2
7
15 10
B
9
S 12 C
Figure 1
A 15 B
13 12 11
17
S T
12 C
5
22
18
D 10 E
Figure 2
Systems of Inequalities
Find the feasible regions.
3 x y 3 x + y 130
x 2y 4 5 x + y 450
x0
B8.2 x 3 y 6
y0
x + 4y 1
x+ y3 B8.6 2 x + y 100
8 x + 9 y 560
B8.3 x y 3
10 x + 7 y 700
x + 4 y 12
3x + y 120
y 2
x0
x4
y0
B8.4 x y 4
x 4 y 1
7 x + 4 y 12
y0
x0
Appendix B Practice Problems 104
B9.1 Z = 3x + y B9.5 Z = 3x + 8 y
x + 3 y 1500 3x + y 15
2 x + y 1000 x + 4 y 16
x 0, y 0 x 0, y 0
B9.2 Z = x + 4y
x + 2 y 80
2 x + y 100
x 0, y 0
B9.3 Z = 4x + 5 y
4 x + 3 y 24
2 x + 3 y 15
x 0, y 0
B9.4 Z = x + 2y
3x + 2 y 135
2 x + 5 y 200
x 0, y 0
Appendix B Practice Problems 105
B10.1 Z = 3x + y B10.5 Z = 3x + 8 y
x + 3 y 1500 3x + y 15
2 x + y 1000 x + 4 y 16
x 0, y 0 x 0, y 0
B10.2 Z = x + 4y
x + 2 y 80
2 x + y 100
x 0, y 0
B10.3 Z = 4x + 5 y
4 x + 3 y 24
2 x + 3 y 15
x 0, y 0
B10.4 Z = x + 2y
3x + 2 y 135
2 x + 5 y 200
x 0, y 0
Appendix B Practice Problems 106
Simplex Algorithm
Maximize the function Z subject to the given
constraints, using the Simplex Algorithm.
B11.1 Z = 2x + y
4 x + y 180
x + 2 y 160
x 0, y 0
B11.2 Z = x + 2y
x + 3 y 12
3x + y 12
x 0, y 0
B11.3 Z = 3x + y
5 x + y 125
x + 3 y 150
x 0, y 0
B11.4 Z = x + 3y
x + 5 y 15
8 x + 2 y 29
x 0, y 0
Appendix B Practice Problems 107
Minimization Problems
B12.1 B12.3
Minimize Z = 6x + 9 y Minimize Z = 12 x + 9 y
8 x + 2 y 16 8 x + 4 y 24
4 x + 4 y 20 3x + 6 y 18
2 x + 4 y 12 4 x + 16 y 32
x 0, y 0 x 0, y 0
B12.2
Minimize Z = 10 x + 5 y
4 x + 8 y 32
4 x + 4 y 24
10 x + 2 y 20
x 0, y 0
Appendix Operations Research 108
C
Visual Basic Program Listings
A B C D E F
1 6
2 45 23 29 33 12 25
3
4
A B C D E F
1 6
2 45 23 29 33 12 25
3
4 12 23 25 29 33 45
Option Explicit
Public n, ende, ll, pp
Sub QuickSort()
init
sort
output
End Sub
Sub init()
Dim i
n = Worksheets("IO").Cells(1, 1)
Worksheets("m").Cells(1, 1) = n
ende = 0
For i = 1 To n
Worksheets("m").Cells(2, i) = Worksheets("IO").Cells(2, i)
Next i
End Sub
Sub sort()
0: ende = 1
sortrow
swap
check
If ende = 0 Then GoTo 0
End Sub
Visual Basic Program Listings 2.3 The Quick Sort 109
Sub sortrow()
pp = 1
0: ll = Worksheets("m").Cells(1, pp)
If ll = 0 Then
pp = pp + 1
Else
If ll = 1 Then
Worksheets("m").Cells(3, pp) = 1
Worksheets("m").Cells(4, pp) = Worksheets("m").Cells(2, pp)
pp = pp + 1
Else
sortlist
End If
End If
If pp <= n Then GoTo 0
End Sub
Sub sortlist()
Dim j, pivot, count1, count2
count1 = 0
count2 = 0
pivot = Worksheets("m").Cells(2, pp)
For j = 1 To ll - 1
If Worksheets("m").Cells(2, pp + j) <= pivot Then
Worksheets("m").Cells(4, pp + count1) = Worksheets("m").Cells(2, pp + j)
count1 = count1 + 1
End If
Next j
Worksheets("m").Cells(3, pp) = count1
Worksheets("m").Cells(3, pp + count1) = 1
Worksheets("m").Cells(4, pp + count1) = pivot
count1 = count1 + 1
If count1 < ll Then
For j = 1 To ll - 1
If Worksheets("m").Cells(2, pp + j) > pivot Then
Worksheets("m").Cells(4, pp + count1 + count2) = Worksheets("m").Cells(2, pp + j)
count2 = count2 + 1
End If
Next j
End If
If count2 <> 0 Then Worksheets("m").Cells(3, pp + count1) = count2
pp = pp + count1 + count2
End Sub
Sub swap()
Dim i, j
For i = 1 To n
Worksheets("m").Cells(1, i) = Worksheets("m").Cells(3, i)
Worksheets("m").Cells(3, i) = ("")
Worksheets("m").Cells(2, i) = Worksheets("m").Cells(4, i)
Worksheets("m").Cells(4, i) = ("")
Next i
End Sub
Function check()
Dim i
For i = 1 To n
If Worksheets("m").Cells(1, i) <> 1 Then ende = 0
Next i
End Function
Sub output()
Dim i
For i = 1 To n
Worksheets("IO").Cells(4, i) = Worksheets("m").Cells(2, i)
Worksheets("m").Cells(1, i) = ("")
Worksheets("m").Cells(2, i) = ("")
Next i
End Sub
Appendix C Visual Basic Program Listings 110
A B C D E F
1 7 489
2 1400 369
3 555
4 432
5 834
6 478
7 898
A B C D E F
1 7 489 898 834 478
2 1400 369 489 555 432
3 555 369
4 432
5 834
6 478
7 898
Visual Basic Program Listings 2.4 Bin Packing 111
Option Explicit
Public n, max, pi, pk
Sub BinPacking()
init
bubblesort
pack
output
End Sub
Sub init()
Dim i
n = Worksheets("IO").Cells(1, 1)
max = Worksheets("IO").Cells(2, 1)
For i = 1 To n
Worksheets("a").Cells(i, 1) = Worksheets("IO").Cells(i, 2)
Next i
End Sub
Sub bubblesort()
Dim i, x, bn, exchanged
bn = n
exchanged = 1
0: exchanged = 0
For i = 1 To bn - 1
If Worksheets("a").Cells(i, 1) < Worksheets("a").Cells(i + 1, 1) Then
x = Worksheets("a").Cells(i + 1, 1)
Worksheets("a").Cells(i + 1, 1) = Worksheets("a").Cells(i, 1)
Worksheets("a").Cells(i, 1) = x
exchanged = 1
End If
Next i
bn = bn - 1
If exchanged = 1 And bn >= 1 Then GoTo 0
End Sub
Sub pack()
Dim j, empt
pi = 1
j = 1
0: empt = 1
For pk = 1 To n
If check <= max And Worksheets("a").Cells(pk, 1) <> 0 Then
Worksheets("b").Cells(j, pi) = Worksheets("a").Cells(pk, 1)
Worksheets("a").Cells(pk, 1) = ("")
empt = 0
j = j + 1
End If
Next pk
pi = pi + 1
j = 1
If empt = 0 Then GoTo 0
End Sub
Function check()
Dim j, sum
sum = 0
For j = 1 To n
sum = sum + Worksheets("b").Cells(j, pi)
Next j
sum = sum + Worksheets("a").Cells(pk, 1)
check = sum
End Function
Sub output()
Dim i, j
For i = 1 To n
For j = 1 To n
If Worksheets("b").Cells(j, i) <> 0 Then
Worksheets("IO").Cells(j, 3 + i) = Worksheets("b").Cells(j, i)
Worksheets("b").Cells(j, i) = ("")
End If
Next j
Next i
End Sub
Appendix C Visual Basic Program Listings 112
A B C D E F G H
1 7 Berlin London Madrid Moscow Paris Rome Vienna
2 Berlin 890 1600 870 520
3 London 890 1270 2480 340
4 Madrid 1270 1060 1340
5 Moscow 1600 2480 2380 1680
6 Paris 870 340 1060 1040 1000
7 Rome 1340 2380 1040 740
8 Vienna 520 1680 1000 740
9
10
11 Berlin London Madrid Moscow Paris Rome Vienna
12 Berlin
13 London
14 Madrid
15 Moscow
16 Paris
17 Rome
18 Vienna
19
20 Length:
A B C D E F G H
1 7 Berlin London Madrid Moscow Paris Rome Vienna
2 Berlin 890 1600 870 520
3 London 890 1270 2480 340
4 Madrid 1270 1060 1340
5 Moscow 1600 2480 2380 1680
6 Paris 870 340 1060 1040 1000
7 Rome 1340 2380 1040 740
8 Vienna 520 1680 1000 740
9
10 1 5 6 7 4 3 2
11 Berlin London Madrid Moscow Paris Rome Vienna
12 Berlin
13 London 340
14 Madrid 1060
15 Moscow 1600
16 Paris 870
17 Rome 740
18 Vienna 520
19
20 Length: 5130
Visual Basic Program Listings 3.2 Prims Algorithm 113
Option Explicit
Public n, min, count, row, length
Sub Prim()
init
findmst
output
End Sub
Sub init()
Dim i, j
n = Worksheets("IO").Cells(1, 1)
For i = 1 To n
For j = 1 To n
Worksheets("a").Cells(i, j) = Worksheets("IO").Cells(i + 1, j + 1)
Next j
Next i
min = 10000
count = 1
End Sub
Sub findmst()
Dim i
row = 1
delrow
For i = 2 To n
count = count + 1
check
Next i
length = mst
End Sub
Sub delrow()
Dim j
For j = 1 To n
Worksheets("a").Cells(row, j) = ("")
Next j
Worksheets("a").Cells(n + 1, row) = count
End Sub
Sub check()
Dim i, j, imin, jmin
For j = 1 To n
For i = 1 To n
If Worksheets("a").Cells(n + 1, j) <> 0 _
And Worksheets("a").Cells(i, j) <= min _
And Worksheets("a").Cells(i, j) > 0 Then
min = Worksheets("a").Cells(i, j)
imin = i
jmin = j
End If
Next i
Next j
Worksheets("b").Cells(imin, jmin) = min
row = imin
delrow
min = 10000
End Sub
Function mst()
Dim i, j, sum
sum = 0
For i = 1 To n
For j = 1 To n
sum = sum + Worksheets("b").Cells(i, j)
Next j
Next i
mst = sum
End Function
Visual Basic Program Listings 3.2 Prims Algorithm 114
Sub output()
Dim i, j
For i = 1 To n
For j = 1 To n
Worksheets("IO").Cells(i + n + 4, j + 1) = Worksheets("b").Cells(i, j)
Next j
Next i
For i = 1 To n
Worksheets("io").Cells(n + 3, i + 1) = Worksheets("a").Cells(n + 1, i)
Next i
Worksheets("io").Cells(2 * n + 6, 2) = length
For i = 1 To n + 1
For j = 1 To n + 1
Worksheets("a").Cells(i, j) = ("")
Worksheets("b").Cells(i, j) = ("")
Next j
Next i
End Sub
Appendix C Visual Basic Program Listings 115
A B C D E F G H
1 7 Berlin London Madrid Moscow Paris Rome Vienna
2 Berlin 890 1600 870 520
3 London 890 1270 2480 340
4 Madrid 1270 1060 1340
5 Moscow 1600 2480 2380 1680
6 Paris 870 340 1060 1040 1000
7 Rome 1340 2380 1040 740
8 Vienna 520 1680 1000 740
9
10 1 5 6 7 4 3 2
11 Berlin London Madrid Moscow Paris Rome Vienna
12 Berlin
13 London 340
14 Madrid 1060
15 Moscow 1600
16 Paris 870
17 Rome 740
18 Vienna 520
19
20 Length: 5130
UBound: 10260
LBound: 6810
Appendix C Visual Basic Program Listings 116
A B C D E F G H I J K L
1 10 9
2 2 H1 Ah 2 Be 3 We 4 S5 E6 L7 Ar 8 M9 Wa 19
3 H1 18 32 17
4 Ah2 18 15
5 Be 3 32 15 33 33 24
6 We 4 17 33 15 16
7 S5 33 15 18 20 36 30
8 E6 18 11 11 21
9 L7 24
10 Ae 8 16 20 31
11 M9 36 31 13
12 Wa 10 39 21 13
13
14
15 Route:
A B C D E F G H I J K L
1 10 9 64 82 69 47 36 34 45 31 0 13
2 2 H1 Ah 2 Be 3 We 4 S5 E6 L7 Ar 8 M9 Wa 19
3 64 H1 18 32 17
4 82 Ah2 18 15
5 69 Be 3 32 15 33 33 24
6 47 We 4 17 33 15 16
7 36 S5 33 15 18 20 36 30
8 34 E6 18 11 11 21
9 45 L7 24
10 31 Ae 8 16 20 31
11 0 M9 36 31 13
12 13 Wa 10 39 21 13
13
14
15 Route: 9 8 4 1 2
Visual Basic Program Listings 3.4 Dijkstras Algorithm 117
Option Explicit
Public n, count, start, target
Sub Dijkstra()
init
step1
step2
step3
step4
step5
output
End Sub
Sub init()
Dim i, j
n = Worksheets("IO").Cells(1, 1)
For i = 1 To n
For j = 1 To n
Worksheets("m").Cells(i, j) = Worksheets("IO").Cells(i + 2, j + 2)
Next j
Next i
start = Worksheets("IO").Cells(1, 2)
target = Worksheets("IO").Cells(2, 1)
End Sub
Sub step1()
Dim i
Worksheets("m").Cells(n + 1, start) = 0
For i = 1 To n
Worksheets("m").Cells(i, n + 1) = Worksheets("m").Cells(i, start)
Next i
Worksheets("m").Cells(start, n + 1) = -1
End Sub
Sub step2()
Dim i, nxt, dum
nxt = smallestworkingvalue
Worksheets("m").Cells(n + 1, nxt) = Worksheets("m").Cells(nxt, n + 1)
Worksheets("m").Cells(nxt, n + 1) = -1
For i = 1 To n
If Worksheets("m").Cells(i, nxt) > 0 And Worksheets("m").Cells(i, n + 1) > -1 Then
dum = Worksheets("m").Cells(n + 1, nxt) + Worksheets("m").Cells(i, nxt)
If Worksheets("m").Cells(i, n + 1) = 0 Then
Worksheets("m").Cells(i, n + 1) = dum
Else
If dum < Worksheets("m").Cells(i, n + 1) Then
Worksheets("m").Cells(i, n + 1) = dum
End If
End If
End If
Next i
End Sub
Sub step3()
Dim i
For i = 1 To n - 2
step2
Next i
End Sub
Sub step4()
Dim i
For i = 1 To n
Worksheets("m").Cells(i, n + 1) = Worksheets("m").Cells(n + 1, i)
Next i
End Sub
Visual Basic Program Listings 3.4 Dijkstras Algorithm 118
Sub step5()
Dim i, nxt, dum
nxt = target
count = 1
Worksheets("m").Cells(n + 3, count) = target
0: For i = 1 To n
dum = Worksheets("m").Cells(n + 1, nxt) - Worksheets("m").Cells(i, nxt)
If dum = Worksheets("m").Cells(i, n + 1) And nxt <> i Then
nxt = i
count = count + 1
Worksheets("m").Cells(n + 3, count) = nxt
End If
Next i
If nxt <> start Then GoTo 0
End Sub
Function smallestworkingvalue()
Dim i, j, min
min = 10000
For i = 1 To n
If Worksheets("m").Cells(i, n + 1) > 0 And Worksheets("m").Cells(i, n + 1) < min Then
min = Worksheets("m").Cells(i, n + 1)
j = i
End If
Next i
smallestworkingvalue = j
End Function
Sub output()
Dim i
For i = 1 To n
Worksheets("IO").Cells(1, i + 2) = Worksheets("m").Cells(n + 1, i)
Worksheets("IO").Cells(i + 2, 1) = Worksheets("m").Cells(n + 1, i)
Next i
For i = 1 To count
Worksheets("IO").Cells(n + 5, i + 2) = Worksheets("m").Cells(n + 3, count + 1 - i)
Next i
End Sub
Appendix C Visual Basic Program Listings 119
A B C D
1 5 1 4 40
2 3 2 5 53
3 8 5 92
4 4 1 40
5 -1 -2 0
6
7
8
9
10
11
12
13
14 x y
15
A B C D
1 5 1 4 40
2 3 2 5 53
3 8 5 92
4 4 1 40
5 -1 -2 0
6
7 0.2666667 -0.066667 8
8 -0.166667 0.1666667 6.5
9 -0.9 0.1 1.5
10 0.4 -0.6 6
11 0.3666667 0.0333333 22.5
12
13
14 x y
15 6.5 8
Visual Basic Program Listings 5.4 The Simplex Algorithm 120
Option Explicit
Public nr, nc, pr, pc
Sub lop()
init
simplex
output
End Sub
Sub init()
Dim i, j
nr = Worksheets("IO").Cells(1, 1)
nc = Worksheets("IO").Cells(2, 1)
For i = 1 To nr
For j = 1 To nc
Worksheets("d1").Cells(i, j) = Worksheets("IO").Cells(i, j + 1)
Next j
Next i
End Sub
Sub simplex()
Dim ci, count
count = 0
0: ci = 0
pr = 0
pc = 0
setuptableau
If count <= nc - 2 Then
count = count + 1
Worksheets("v").Cells(count, 1) = pr
Worksheets("v").Cells(count, 2) = pc
End If
makenewtableau
ci = checkindicators
If ci = 1 Then GoTo 0
End Sub
Sub setuptableau()
pivotcolumn
If pc > 0 Then
pivotrow
workingrow
End If
End Sub
Sub makenewtableau()
Dim i, j, r, c
r = nr - 1
c = nc - 1
For i = 1 To nr
For j = 1 To nc
Worksheets("d2").Cells(i, j) = Worksheets("d1").Cells(i, j) _
- Worksheets("d1").Cells(nr + 1, j) _
* Worksheets("d1").Cells(i, pc)
Next j
Next i
For i = 1 To nr
Worksheets("d2").Cells(i, pc) = -Worksheets("d1").Cells(i, pc) _
/ Worksheets("d1").Cells(pr, pc)
Next i
For j = 1 To nc
Worksheets("d2").Cells(pr, j) = Worksheets("d1").Cells(nr + 1, j)
Next j
swap
End Sub
Visual Basic Program Listings 5.4 The Simplex Algorithm 121
Sub pivotcolumn()
Dim j, min
min = 0
For j = 1 To nc - 1
If Worksheets("d1").Cells(nr, j) < min Then
min = Worksheets("d1").Cells(nr, j)
pc = j
End If
Next j
End Sub
Sub pivotrow()
Dim i, min
min = 10000
For i = 1 To nr - 1
If Worksheets("d1").Cells(i, pc) > 0 Then
Worksheets("d1").Cells(i, nc + 1) _
= Worksheets("d1").Cells(i, nc) / Worksheets("d1").Cells(i, pc)
End If
Next i
For i = 1 To nr - 1
If Worksheets("d1").Cells(i, nc + 1) > 0 _
And Worksheets("d1").Cells(i, nc + 1) < min Then
min = Worksheets("d1").Cells(i, nc + 1)
pr = i
End If
Next i
End Sub
Sub workingrow()
Dim j
For j = 1 To nc
Worksheets("d1").Cells(nr + 1, j) _
= Worksheets("d1").Cells(pr, j) / Worksheets("d1").Cells(pr, pc)
Next j
Worksheets("d1").Cells(nr + 1, pc) = 1 / Worksheets("d1").Cells(pr, pc)
End Sub
Function checkindicators()
Dim j, tag
tag = 0
For j = 1 To nc - 1
If Worksheets("d1").Cells(nr, j) < 0 Then tag = 1
Next j
checkindicators = tag
End Function
Sub swap()
Dim i, j
For i = 1 To nr + 1
For j = 1 To nc + 1
Worksheets("d1").Cells(i, j) = Worksheets("d2").Cells(i, j)
Worksheets("d2").Cells(i, j) = ("")
Next j
Next i
End Sub
Visual Basic Program Listings 5.4 The Simplex Algorithm 122
Sub output()
Dim i, j, pr, pc
For i = 1 To nr
For j = 1 To nc
Worksheets("IO").Cells(nr + 1 + i, j + 1) _
= Worksheets("d1").Cells(i, j)
Next j
Next i
For i = 1 To nc - 1
pr = Worksheets("v").Cells(i, 1)
pc = Worksheets("v").Cells(i, 2)
Worksheets("IO").Cells(2 * nr + 5, pc + 1) _
= Worksheets("d1").Cells(pr, nc)
Next i
For i = 1 To nr
For j = 1 To nc
Worksheets("d1").Cells(i, j) = ("")
Worksheets("v").Cells(i, j) = ("")
Next j
Next i
End Sub
Appendix C Visual Basic Program Listings 123
Option Explicit
Sub Simplex()
Dim i, j, n, C, alpha, beta, gamma, count, ss, size, H, L, M, P, T, VH, VL
0: n = Worksheets("IO").Cells(1, 3)
C = n + 2
alpha = 1
beta = 0.5
gamma = 2
1: P = Worksheets("IO").Cells(3, 3)
count = 1
2: Worksheets("X").Cells(n + 1, 1) = P
For i = 1 To n
Worksheets("X").Cells(i, 1) = Worksheets("IO").Cells(i, 1)
Next i
L = 1
size = 0
3: For j = 2 To n + 1
4: For i = 1 To n
Worksheets("X").Cells(i, j) = Worksheets("IO").Cells(i, 1)
Next i
T = Worksheets("IO").Cells(2, 3)
5: Worksheets("X").Cells(j - 1, j) = Worksheets("IO").Cells(j - 1, 1) + T
6: If Worksheets("X").Cells(j - 1, j) = Worksheets("IO").Cells(j - 1, 1) Then
T = 10 * T
GoTo 5
End If
7: size = size + Abs(T)
8: Next j
9: ss = size
10: For j = 1 To n + 1
11: If j = L Then GoTo 13
12: For i = 1 To n
Worksheets("IO").Cells(i, 1) = Worksheets("X").Cells(i, j)
Next i
P = Worksheets("IO").Cells(3, 3)
count = count + 1
Worksheets("X").Cells(n + 1, j) = P
13: Next j
Worksheets("IO").Cells(4, 3) = size
Worksheets("IO").Cells(5, 3) = count
14: L = 1
H = 1
M = 1
VL = Worksheets("X").Cells(n + 1, 1)
VH = VL
15: For j = 2 To n + 1
16: T = Worksheets("X").Cells(n + 1, j)
17: If T < VL Then
VL = T
L = j
End If
18: If T >= VH Then
M = H
H = j
VH = T
End If
19: Next j
20: If VL = VH Then GoTo 40
21: For i = 1 To n
T = -Worksheets("X").Cells(i, H)
For j = 1 To n + 1
T = T + Worksheets("X").Cells(i, j)
Next j
Worksheets("X").Cells(i, C) = T / n
Next i
Visual Basic Program Listings 6.1 The Nelder-Mead Algorithm 124
22: For i = 1 To n
Worksheets("IO").Cells(i, 1) _
= (1 + alpha) * Worksheets("X").Cells(i, C) - alpha * Worksheets("X").Cells(i, H)
Next i
P = Worksheets("IO").Cells(3, 3)
count = count + 1
Worksheets("IO").Cells(4, 3) = size
Worksheets("IO").Cells(5, 3) = count
23: If P < VL Then GoTo 35
24: If P < Worksheets("X").Cells(n + 1, M) Then GoTo 39
25: If P >= VH Then GoTo 27
26: For i = 1 To n
Worksheets("X").Cells(i, H) = Worksheets("IO").Cells(i, 1)
Next i
Worksheets("X").Cells(n + 1, H) = P
27: For i = 1 To n
Worksheets("IO").Cells(i, 1) _
= (1 - beta) * Worksheets("X").Cells(i, H) + beta * Worksheets("X").Cells(i, C)
Next i
P = Worksheets("IO").Cells(3, 3)
count = count + 1
Worksheets("IO").Cells(4, 3) = size
Worksheets("IO").Cells(5, 3) = count
28: If P < Worksheets("X").Cells(n + 1, H) Then GoTo 39
29: size = 0
For j = 1 To n + 1
30: If j = L Then GoTo 32
31: For i = 1 To n
Worksheets("X").Cells(i, j) _
= beta * (Worksheets("X").Cells(i, j) - Worksheets("X").Cells(i, L)) _
+ Worksheets("X").Cells(i, L)
size = size + Abs(Worksheets("X").Cells(i, j) - Worksheets("X").Cells(i, L))
Next i
32: Next j
33: If size < ss Then GoTo 9
34: GoTo 40
35: For i = 1 To n
T = gamma * Worksheets("IO").Cells(i, 1) + (1 - gamma) * Worksheets("X").Cells(i, C)
Worksheets("X").Cells(i, C) = Worksheets("IO").Cells(i, 1)
Worksheets("IO").Cells(i, 1) = T
Next i
Worksheets("X").Cells(n + 1, C) = P
36: P = Worksheets("IO").Cells(3, 3)
count = count + 1
Worksheets("IO").Cells(4, 3) = size
Worksheets("IO").Cells(5, 3) = count
37: If P < Worksheets("X").Cells(n + 1, C) Then GoTo 39
38: For i = 1 To n
Worksheets("IO").Cells(i, 1) = Worksheets("X").Cells(i, C)
Next i
P = Worksheets("X").Cells(n + 1, C)
39: For i = 1 To n
Worksheets("X").Cells(i, H) = Worksheets("IO").Cells(i, 1)
Next i
Worksheets("X").Cells(n + 1, H) = P
GoTo 14
40: End Sub
Appendix Operations Research 125
D
C Program Listings
#include <stdio.h>
#include <stdlib.h>
int a[8]={0,489,368,555,432,834,478,898};
int dim=7, size=8, max=1400, b[8][8];
int main()
{bubblesort(dim,a);
packing(a,b);
outm(b);
return 0;}
#include <stdio.h>
#include <stdlib.h>
int main()
{int number, target=72;
number=search(target);
output(number);
return 0;}
int search(int x)
{int min=1, mid, max=dim, tag=0;
mid=(min+max)/2;
while(tag==0)
{if(x==a[mid]) tag=1;
else if(x<a[mid]) min=mid+1;
else max=mid-1;
mid=(min+max)/2;
return mid;}
void output(int x)
{printf(\n%4d%4d\n,x,a[x]);}
Appendix D C Program Listings 127
#include <stdio.h>
#include <stdlib.h>
int i,j,min=10000,count=1,dim=7,size=8,marks[8][8];
int main()
{int i, sum;
i=1;
del(i,m);
for (i=2;i<=dim;++i)
{count=count+1;
check(m,marks);}
sum=mst(m,marks);
outm(marks);
printf("Length of MST:%5d",sum);
return 0;}
#include <stdio.h>
#include <stdlib.h>
int main()
{int i, stl, lb, ub;
i=1;
del(i,m);
for (i=2;i<=dim-1;++i)
{count=count+1;
check(m,marks);}
stl=secondtolast(m);
count=count+1;
check(m,marks);
lb=mst(m,marks)+stl;
ub=2*mst(m,marks);
outm(marks);
printf("Lower bound: %3d",lb); printf("\n");
printf("Upper bound: %3d",ub); printf("\n");
return 0;}
#include <stdio.h>
#include <stdlib.h>
int main()
{int i;
step1(start,m);
step2(m);
step3(m);
step4(m);
outm(m);
step5(start,target,m);
return 0;}
#include <stdio.h>
#include <stdlib.h>
int main()
{simplex(data);
output(data);
outputvariables(data);
return 0;}