Вы находитесь на странице: 1из 135

Operations Research

Dr. Paul G. Johannsen


2015
Operations Research

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 3 Graphs and Networks


3.1 Defining Terms 14
3.2 Prims Algorithm 17
3.3 The Traveling Salesman Problem 21
3.4 Dijkstras Algorithm 25
3.5 The Route Inspection Problem 30
3.6 Matchings 36

Chapter 4 Digraphs
4.1 Flows in Networks 41
4.2 Critical Path Analysis 43

Chapter 5 Introduction to Linear Programming


5.1 Linear Inequalities in Two Variables 47
5.2 Systems of Inequalities 50
5.3 Linear Programming: Two Variables 52
5.4 The Simplex Algorithm 59
5.5 Minimization Problems 65

Chapter 6 Nonlinear Optimization Problems


6.1 A Nelder-Mead Procedure 68
6.2 Least-Squares Fit: Logistic Growth 71

Appendix A Problems Solved Step by Step 74


B Practice Problems 83
C Visual Basic Program Listings 108
D C Program Listings 125
Chapter Operations Research 1

1
Introduction

1.1 Introduction to Operations Research

Decisions in business management are not


seldom made by trial and error: repeated
attempts (usu. varied and unsystematic) are
continued until successful.

Operations Research (or operations re-


search, also decision mathematics) is the
application of scientific principles to business
management, providing a quantitative basis
for complex decisions. That is, the goal of
operations research is to find better solutions
than by the trial and error method, or, if an
optimum solution exists, to find this optimum
solution.

An algorithm is a finite sequence of precise


instructions used to solve a problem. Algo-
rithms play a fundamental role throughout
decision mathematics.

Generally, algorithms are meant to be imple-


mented as a or part of a computer program.
For almost every algorithm, presented in this
module, the implementation by a C program
is discussed.
Operations Research 1. Introduction 2

1.2 Introductory Example: Binary Search

The binary search algorithm provides a sys-


tematic way of searching through an ordered flow no flow

list to find an element that matches your crite- A


1 2 3 4 5 6 7

rion. For example, you may need to look 8


through a set of index cards to find the contact B
details of a customer. 15 14 13 12 11 10 9

Step 1 Find the element in the middle of the Figure 3

list. If this element matches your criterion


then stop. If it does not, then use the middle We finally remove bulb 6 (O. K.). As there is
term to decide in which half of the list the no flow between sockets 6 and 8, bulb 7 must
required element must lie. be the defective one.

Step 2 Repeat Step 1 for the selected half. flow

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

know) in a series circuit of 15 bulbs using an


Figure 4
Ohm-meter to check for electric flow.

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

1.3 Introductory Example: The Traveling Solution:


Salesman Problem
TABLE 1

The traveling salesman problem (TSP) is Town Distance/km


the problem of finding a route of minimum Lippetal 17
distance that visits every town or village and Beckum 29
returns to the starting point. Ahlen 19
Hamm 28
An approach to find a "good" solution is to Werl 23
use the nearest-neighbor algorithm. Arnsberg 12
Meschede 24
Step 1 Choose any starting point. Warstein 12
Erwitte 20
Step 2 Move from your present position to Lippstadt 11
the nearest town or village not yet Erwitte 11
visited. Soest 20
Welver 21
Step 3 Repeat step 2 until every destination Soest 21
has been visited. Return to the start- 268
ing point.

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

We have found a solution, however, we have


the suspicion that there could be better solu-
tions. Well come back to this problem in
Section 3.3.
1. Introduction 1.3 The Traveling Salesman Problem 4

Now Work Problem 1.3.1


Find a route in the road map of Figure 3,
starting in Soest, using the nearest neighbor-
algorithm.

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

2.1 Flow Diagrams and Visual Basic Pro-


grams Example (2)
In a Visual Basic program, we have access to
One way to define an algorithm is to simply a spread-sheet cell by the syntax:
list all of the necessary steps. In more com-
plex situations, the process may be easier to Worksheets(IO).Cells(2, 5)
follow if the algorithm is presented as a flow
diagram. This means the cell E2 in sheet IO. In a paper-
and-pencil version we may use the abbrevia-
Example (1) tion
Implement the algorithm given by the flow
W(IO, 2, 5)
chart.
Write a Visual Basic source code for the flow
diagram of Example (1).
START
Solution: Using the above abbreviation, we
i :=1, j :=1 have:

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

Now Work Problem 2.1.1 Now Work Problem 2.1.3


Given the following flow chart: Given the following part of a program:
Option Explicit
Public n
START Sub NWP213()
init
reverse
i :=1, j :=1 End Sub

Sub init()
Write the n = Worksheets("IO").Cells(1, 2)
value of j End Sub

yes Sub reverse


i =9? STOP Dim i
For i = 1 to n
no
Next i
i :=i +1 End Sub

j :=2i Find the subroutine reverse, which reverses


the order of the five numbers in row 4 of the
Excel spreadsheet.
a) What numbers does the algorithm gener-
ate?
A B C D E
b) Write a Visual Basic source code. mean=
1 n= 5
2 23 35 12 86 45
Now Work Problem 2.1.2 3
Given the following flow chart: 4

Now Work Problem 2.1.4


START For the program of Problem 2.1.3
Option Explicit
i :=1 Public n
d :=x 1 Sub NWP213()
init
reverse
i :=i +1 worksheets(IO).cells(1,5) = mean
End Sub
yes
i =n +1 STOP include a function
no
no Function mean
xi< d
Dim i, x
yes
d :=x i
mean = x
End Function

a) What is the purpose of this algorithm?


which calculates the arithmetic mean of the
five numbers.
b) Given the following part of an Excel
spread sheet, write a Visual Basic source code
for the algorithm.

A B C D E
1 n= 5 d=
2 23 35 12 86 45
Operations Research 2. Algorithms 7

2.2 The Bubble Sort Example (2)


Sort the list 8, 6, 9, 3, 7, 5 into ascending or-
As its name suggests, the bubble sort is an der.
algorithm for sorting a list in a particular or-
der.
Solution: The 1st pass gives
Step 1 Compare the first two elements of the
list and switch them if they are in the wrong 8 6 9 3 7 5 6 8 9 3 7 5
order. 6 8 9 3 7 5
6 8 9 3 7 5 6 8 3 9 7 5
Step 2 Compare the second and third element 6 8 3 9 7 5 6 8 3 7 9 5
of the list and, again, switch them if they are 6 8 3 7 9 5 6 8 3 7 5 9
in the wrong order.
The 2nd pass produces
Step 3 Continue in the same way until you
reach the end of the list. This completes the 6 8 3 7 5 9
first pass through the list.
6 8 3 7 5 9 6 3 8 7 5 9
6 3 8 7 5 9 6 3 7 8 5 9
Step 4 Make repeated passes through the list
until a pass produces no change. 6 3 7 8 5 9 6 3 7 5 8 9

The 3rd pass gives


Example (1)
6 3 7 5 8 9 3 6 7 5 8 9
Write the letters DCQPB in alphabetical order
3 6 7 5 8 9
using the bubble sort algorithm.
3 6 7 5 8 9 3 6 5 7 8 9
Solution: The 1st pass gives
The 4th pass produces
D C Q P B C D Q P B 3 6 5 7 8 9
C D Q P B
3 6 5 7 8 9 3 5 6 7 8 9
C D Q P B C D P Q B
C D P Q B C D P B Q In the 5th pass, no more changes are produced.
The 2nd pass produces

C D P B Q
C D P B Q
C D P B Q C D B P Q

The 3rd pass gives

C D B P Q
C D B P Q C B D P Q

In the 4th and final pass:

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.1


Sort the List
exchanged = 1
End If
4, 8, 2, 0, 6 Next i
n = n - 1
If exchanged = 1 And n >= 1 Then GoTo 0
into ascending order. End Sub

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

2.3 The Quick Sort Example (2)


Sort the numbers i in Table 1 in such a way,
Step 1 Take the first element in the list as the that the corresponding PCodes are sorted into
pivot. ascending order.

Step 2 Consider each of the other elements in Table 1


turn and place any with order less than or i PCode Name 1st Name Data Files
equal to the pivot on its one side and any with 1 34113 Mouse Mickey data1.dat
order greater than the pivot on its other side. 2 21567 Duck Donald data2.dat
Do this without re-ordering the numbers on 3 55678 Kong King data3.dat
either side of the pivot. 4 83123 Duck Daisy data4.dat
5 12234 Brown Charles data5.dat
Step 3 Repeat steps 1 and 2 for each sub-list 6 42456 Windsor Charles data6.dat
until each one contains a single element.
Solution:
Example (1)
Sort the list 6
1 2 3 4 5 6
34, 21, 55, 83, 12, 42
2 1 3
2 5 1 3 4 6
into ascending order.
1 1 1 1 1 1
Solution: 5 2 1 6 3 4

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

Now Work Problem 2.3.1


Sort the list

44 63 35 92 51 17 78 86 29

into ascending order.

Now Work Problem 2.3.2


Sort the list

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

2.4 Bin Packing Example (1) Project


A project involves activities A H with dura-
The bin packing algorithm is used to solve tions in hours as given in the table.
problems that can be represented by the need
to pack some boxes of equal cross-section but A B C D E F G H
different heights into bins, with the same 3 1 5 4 2 3 4 2
cross-section as the boxes, using as few bins
as possible. The project is to be completed in 8 hours.

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.

c) Find the solution of the full-bin decreasing


First-fit algorithm algorithm.

Take each box in turn from the order given Solution:


and pack it into the first available bin.
a) AB, C, DE, FG, H
31 5 42 34 2
Full-bin algorithm1
b) ABD, CF, EGH
Step 1 Considering the boxes in the given
314 53 242
order, use the first available combination that
will fill a bin.
c) We first sort the activities into descending
order:
Step 2 Repeat Step 1 until no more bins can
be filled.
C D G A F E H B
5 4 4 3 3 2 2 1
Step 3 Implement the first-fit algorithm for
the remaining boxes.
The result of the full-bin algorithm then is:

Full-bin decreasing algorithm


CA, DG, FEHB
53 44 3221
Step 1 Arrange the boxes in decreasing order
of size.

Step 2 Implement the full-bin algorithm


starting with the largest box.

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

a) Show that a minimum of four disks is re- 900


840
quired.
610 580
b) Use the first fit algorithm to allocate the
files on disk.

c) Use the full-bin decreasing algorithm to


show how all of the files may be stored using
Figure 2
four disks.
Now Work Problem 2.4.1
A project involves activities A H with dura-
tion in hours as given in the table.
Solution:
A B C D E F G H
a) Total memory to transfer: 4 2 6 5 3 4 5 3

4520 Kb = 4.52 Mb 4.52/1.4 3.23 Find the minimum number of workers


needed, if each worker works 8 hours.
Three disks do not have sufficient capacity to
hold all the files. A minimum of four disks is
required.
TABLE 2
b) File A B C D E F G
1.4 Mb Kb 489 368 555 432 834 478 898

Now Work Problem 2.4.2


532 Table 2 shows the amount of memory taken
468 up by some files on a computer system. The
840 900 files are to be transferred onto disk with a
capacity of 1.4 Mb. Show how all of the files
580 610 590 may be stored using three disks.

A Visual Basic source code is available in


Figure 1
Appendix C, and a C source code in Appen-
dix D.

3
1.000 Kb = 1 Mb
Operations Research 2. Algorithms 12

2.5 Binary Search

Now Work Problem 2.5.1 A B C


Analyze the program listing and find the out- 1 15 91
2 72 83
put which the program produces. Please note
3 72
that the dimension in cell A1 of the Excel 4 66
user-interface sheet IO must be the next 5 64
highest power of 2 minus 1 ( 24 1 = 15 ) with 6 53
respect to the number of data (12). In cell A2 7 46
the target is given. The program requires a 8 35
9 21
second sheet a. A C source code is also 10 16
available in Appendix D. 11 14
12 4

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

3.1 Defining Terms


Figure 4 Subgraph.

A graph is a set of points, called vertices A subgraph of some graph G is a graph


or nodes, connected by lines called edges consisting entirely of vertices and edges
or arcs. that belong to G.

A C

Figure 1 A simple graph.


Figure 5 Digraph with directed edges.
A simple graph is one that has no loops
and in which no pair of vertices are con-
nected by more than one edge.
A directed edge is an edge that has an
associated direction shown by an arrow. A
digraph is a graph in which the edges are
B directed.

A C

Figure 2 A non-simple graph: Two edges between A


and B.

The number of edges incident on a vertex


is called its order, degree or valency. A
Figure 6 Path.
vertex may be odd or even depending on
whether its order is odd or even.
A path is a finite sequences of edges such
that the end vertex of one edge is the start
vertex of the next edge. No edge is in-
3 cluded more than once.
2 1

Figure 3 Order of vertices.


3. Graphs and Networks 3.1 Defining Terms 15

An Eulerian cycle is one that traverses all


of the edges of the graph.

Example (1)
Find an Eulerian cycle of the given graph of
Figure 7 Connected graph. Figure 10.

A connected graph is one in which every


pair of vertices is connected by a path.

K4 K5 Figure 10

Solution:
Figure 8 Complete graphs.

A complete graph is one in which every


pair of vertices is connected by an edge. A 2,5 4,7
complete graph with n vertices is denoted
by Kn.
1,8 6,9
A planar graph is one that can be drawn
in a plane such that no two edges meet Figure 11
except at a vertex. K4 is planar but K5 is
not planar.

Figure 9 Cycle. Figure 12 Hamiltonian cycle.

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.

A minimum spanning tree (MST) of a


network is a spanning tree of minimum
possible weight. It is sometimes called a
minimum connector.

Figure 14 Spanning tree.

A spanning tree is a tree whose vertices


are all of the vertices of the graph.

G 2 F
1 E
3
1
3
4 2
H 4 1
D
2 1 C
2
B
A
Figure 15 Network.

A network is a graph that has a number


(weight) associated with every edge.
Operations Research 3. Graphs and Networks 17

3.2 Prim's Algorithm


C

Prim's algorithm may be used to find a


7
minimum spanning tree (MST) of a network.
B
Step 1 Choose a starting vertex.

Step 2 Connect it to the vertex that will A D


make an edge of minimum weight. Figure 2

Step 3 Connect one of the remaining verti-


ces to the tree formed so far, in such Step 3 Connect one of the remaining verti-
a way that the minimum extra ces to the tree formed so far, in such
weight is added to the tree. a way that the minimum extra
weight is added to the tree: A (Fig-
Step 4 Repeat step 3 until all of the vertices ure 3).
are connected. Give the sum of the
weights of the edges.
C

Example (1): Spanning Tree 7

Find the spanning tree of the network of Fig- B


ure 1. 5

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

Step 2 Vertex that will make an edge of Figure 4

minimum weight: B (Figure 2).


Step 4 All of the vertices are connected.
The sum of the weights is 26.
3. Graphs and Networks 3.2 Prim's Algorithm 18

The network given above may be represented Example (2)


by the matrix shown: Find the minimum spanning tree from Table
1.
TABLE 1
A B C D
Solution:
A 5 11 15
B 5 7 20
Step 1:
C 11 7 14
1
D 15 20 14
A B C D
A 5 11 15
A version of Prim's algorithm may be used to
B 5 7 20
find the minimum spanning tree directly from
C 11 7 14
the matrix:
D 15 20 14
Step 1 Choose a starting vertex. Delete the
Step 2:
corresponding row and write 1 above
1
the corresponding column as a la-
bel.1 A B C D
A 5 11 15
Step 2 Mark the smallest undeleted value in B 5 7 20
the labelled column an delete the C 11 7 14
row in which it lies. D 15 20 14

Step 3 Label the column, corresponding to Step 3:


the vertex of the deleted row, with
the next label number. 1 2
A B C D
Step 4 Mark the smallest undeleted value of A 5 11 15
all the values in the labelled columns B 5 7 20
and delete the row in which it lies. C 11 7 14
D 15 20 14
Step 5 Repeat steps 3 and 4 until all of the
rows are deleted. The marked values Step 4:
then define the edges of the mini- 1 2 3
mum spanning tree. Give the sum of A B C D
the weights of the edges. A 5 11 15
B 5 7 20
C 11 7 14
D 15 20 14

Next (and last) Step:


1 2 3
A B C D
A 5 11 15
B 5 7 20
C 11 7 14
D 15 20 14
1
Although any of the vertices may be chosen as a
starting vertex, let us adopt the convention to always
The sum of the weights is 26.
choose the first vertex. This convention makes the
results comparable.
3. Graphs and Networks 3.2 Prim's Algorithm 19

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

The sum of the weights is 28.


3. Graphs and Networks 3.2 Prim's Algorithm 20

Now Work Problem 3.2.2 Now Work Problem 3.2.3


Use Prim's algorithm to find a minimum Use the matrix form of Prim's algorithm to
spanning tree for the network of Figure 9. find a minimum spanning tree for the network
of Figure 9. For the matrix, use the following
sequence of vertices: Ah, Ha, Be, Wr, Wl, Ll,
Ahlen 19 Beckum
So, Lt, and Er.
28 28 26
27 29
Hamm Lippetal
30 Lippstadt
Welver 32 37
11
23 17
18
21
Erwitte A Visual Basic source code of Prims algo-
20
Werl
20 Soest rithm is available in Appendix C, and a C
source code in Appendix D.
Figure 9 Driving times (min).

Now Work Problem 3.2.4


A new airline plans to connect seven European capitals. Use the matrix form of Prim's algorithm to
find a minimum spanning tree for the given network. What will be the main route and on which
routes shuttle flights should be planned?

TABLE 3 Distances (km) between European capitals.


Berlin London Madrid Moscow Paris Rome Vienna
Berlin 890 1600 870 520
London 890 1270 2480 340
Madrid 1270 1060 1340
Moscow 1600 2480 2380 1680
Paris 870 340 1060 1040 1000
Rome 1340 2380 1040 740
Vienna 520 1680 1000 740

Moscow

London Berlin

Vienna
Paris

Madrid Rome

Figure 10
Operations Research 3. Graphs and Networks 21

3.3 The Traveling Salesman Problem Example (1)


Find an upper and lower bound for the TSP in
E the network of Figure 1.
F 16
34

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.

Finding upper and lower bounds


We find the length of the MST of 137 (see
Figure 2). Thus, an upper bound for the TSP
Step 1 Find the MST of the network using is 2137 = 274.
the matrix version of Prims algo-
rithm. An upper bound for the total
distance involved in the TSP is given Step 2
by twice the minimum spanning
tree. The last vertex taken into account in Prims
algorithm was F. The second smallest weight
Step 2 Consider again the matrix of Step 1 in the corresponding row is 32. Thus, a lower
and find the second shortest edge bound for the TSP is 137 + 32 = 169.
connected to the last vertex taken
into account. Add the weight of this
edge to the length of the MST. This
gives a lower bound for the TSP.

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.

Finding a Solution of the TSP


A tourist company plans a tour, including
seven European capitals. Find the upper and
Starting with the MST, we now replace edges lower bound for the costs of such a tour, if the
in the MST by others, in order to find a cycle company calculates average costs of 10 /km.
in the network, which connects all the verti-
ces. Solution:

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)

Table 3 Matrix after the application of Prims algorithm


The total length of the edges in this cycle is 1 5 6 7 4 3 2
186, which is quite close to the lower bound Be Lo Ma Mo Pa Ro Vi
of 169. Thus, we are quite confident that we Be 890 1600 870 520
have found a good solution of the travelling Lo 890 1270 2480 340
salesman problem. Ma 1270 1060 1340
Mo 1600 2480 2380 1680
Pa 870 340 1060 1040 1000
Ro 1340 2380 1040 740
E Vi 520 1680 1000 740
F 16
34

32 30 With a length of the MST of 5130 km. There-


D
29 fore, an upper bound for the costs is
G
25
45 49 22 2 5130 km 10 /km = 102 600
20
C
23
The last vertex considered was the one at
24 Moscow. The second shortest edge in the cor-
A B
responding row has a length of 1680 km. For
a lower bound we get therefore
Figure 3 Solution of the TSP.
(5230 km + 1680 km) 10 /km = 69 100
3. Graphs and Networks 3.3 The Traveling Salesman Problem 23

Moscow Including the short-cut London-Berlin, we


find a solution of the TSP (total length 7.651
2480 km), which appears to be the optimum solu-
1600 tion. (Figure7).
London Berlin 1680
890
340 870 520 Moscow
2380
1000 Vienna
1270 Paris
741
1060 1040 1680
1600
Madrid 1340 London Berlin
Rome
890
340

Figure 5 MST of Example (2). Vienna


Paris
741
1060

Madrid 1340 Rome

Example (3) Figure 7 Solution of the TSP.


Find a route for Example (2).

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

Figure 6 Two short-cuts. Figure 8 Distances (km).


3. Graphs and Networks 3.3 The Traveling Salesman Problem 24

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

3.4 Dijkstra's Algorithm


Step 1 Give the start vertex a label of 0 and
Dijkstra's algorithm is used to find the short- write its order of labeling as 1.
est distance between a chosen start vertex and
any other vertex in a network. Step 2 For each vertex directly connected to
the start vertex, enter the distance
The algorithm is designed for use by comput- from the start vertex as a working
ers but, when implementing it without the aid value. Enter the smallest working
of a computer, a system of labeling is re- value as a label for that vertex. Rec-
quired. ord the order in which it was labeled
as 2.
Order of la- O L Label (the minimum distance
beling from the start vertex)
Step 3 For each vertex directly connected to
W Record your 'Working values' the one that was last given a label,
here
add the edge distance onto the label
value to obtain a total distance. This
The steps in the algorithm below refer to this distance becomes the working value
system of labeling. Every vertex is labeled in for the vertex unless a lower one has
the same way. already been found.

Step 4 Find the vertex with the smallest


working value not yet labeled and
label it. Record the order in which is
was labeled.

Step 5 Repeat steps 3 and 4 until the largest


vertex is labeled. The values of the
labels are the minimum distances
from the start vertex.

Step 6 To find the path that gives the short-


est distance, start at the target vertex
and work back towards the start
vertex in such a way that an edge is
only included if its distance equals
the change in the label values.
3. Graphs and Networks 3.4 Dijkstra's Algorithm 26

Example (1) Example (2)


Use Dijkstra's algorithm to find the shortest Use Dijkstra's algorithm to find the shortest
distance from A to F in this network and state distance from G to D.
the route used.
B
2 D
B E 5
12 4
5 7 3 C
8
7 5 A
A 2 F 5
7 2 8
8 9
C 4 D 3 7
G F E

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

Now Work Problem 3.4.1 Matrix Version of Dijkstras Algorithm


Use Dijkstra's algorithm to find the shortest
distance from A to B. Step 1 Give the start vertex a label of 0. For
each vertex directly connected to the
start vertex, enter the distance from
B the start vertex as a working value.

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.

Step 4 Copy the labels into the working


B E column.
10
5 7 3
A 3 4 5 F Step 5 To find the path that gives the short-
9 9 est distance, start at the target vertex
C2 D and work back towards the start
vertex in such a way that an edge is
only included if its distance equals
the change in the label values.
Figure 6

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

Solution: Step 3 Repeat Step 2, until all vertices are


labeled.
Ah Be Ldt Er So We Ha Wv Ltl
20 0 17
Ah 19 28
Ah Be Ldt Er So We Ha Wv Ltl
Be 19 26 28 26 29
Ah 19 28
Ldt 26 11 37
46 Be 19 26 28 26 29
Er 11 20
31 Ldt 26 11 37
So 20 20 21 17
Er 11 20
We 20 23 18
So 20 20 21 17
Ha 28 28 23 30
20 We 20 23 18
Wv 26 21 18 30 32
Ha 28 28 23 30
Ltl 29 37 17 32
21 Wv 26 21 18 30 32
Ltl 29 37 17 32
Step 1 Give the start vertex a label of 0. For
each vertex directly connected to the 20 0 20 17
Ah Be Ldt Er So We Ha Wv Ltl
start vertex, enter the distance from Ah 19 28
the start vertex as a working value. 46 Be 19 26 28 26 29
31 Ldt 26 11 37
Er 11 20
0 So 20 20 21 17
Ah Be Ldt Er So We Ha Wv Ltl We 20 23 18
Ah 19 28 43 Ha 28 28 23 30
Be 19 26 28 26 29 21 Wv 26 21 18 30 32
Ldt 26 11 37 Ltl 29 37 17 32
20 Er 11 20
So 20 20 21 17
20 We 20 23 18 20 0 20 21 17
Ha 28 28 23 30 Ah Be Ldt Er So We Ha Wv Ltl
21 Wv 26 21 18 30 32 Ah 19 28
17 Ltl 29 37 17 32 46 Be 19 26 28 26 29
31 Ldt 26 11 37
Er 11 20
Step 2 Delete the smallest working value So 20 20 21 17
We 20 23 18
and enter it as a label for that vertex. 43 Ha 28 28 23 30
For each of the vertices directly Wv 26 21 18 30 32
connected to it, add the edge dis- Ltl 29 37 17 32

tances onto the label value to obtain


31 20 0 20 21 17
total distances. These distances be- Ah Be Ldt Er So We Ha Wv Ltl
come the working values for these Ah 19 28
46 Be 19 26 28 26 29
vertices, unless lower values have Ldt 26 11 37
already been found. Er 11 20
So 20 20 21 17
We 20 23 18
0 17
43 Ha 28 28 23 30
Ah Be Ldt Er So We Ha Wv Ltl
Wv 26 21 18 30 32
Ah 19 28
Ltl 29 37 17 32
46 Be 19 26 28 26 29
54 Ldt 26 11 37
20 Er 11 20 31 20 0 20 43 21 17
So 20 20 21 17 Ah Be Ldt Er So We Ha Wv Ltl
20 We 20 23 18 71 Ah 19 28
Ha 28 28 23 30 46 Be 19 26 28 26 29
21 Wv 26 21 18 30 32 Ldt 26 11 37
Ltl 29 37 17 32 Er 11 20
So 20 20 21 17
We 20 23 18
Ha 28 28 23 30
Wv 26 21 18 30 32
Ltl 29 37 17 32
3. Graphs and Networks 3.4 Dijkstra's Algorithm 29

46 31 20 0 20 43 21 17 In this way we can of cause find the shortest


Ah Be Ldt Er So We Ha Wv Ltl
65 Ah 19 28 path from the start vertex (Soest) to any target
Be 19 26 28 26 29 vertex. We demonstrate this for the path from
Ldt 26 11 37
Er 11 20 Soest to Lippstadt:
So 20 20 21 17
We 20 23 18 65 46 31 20 0 20 43 21 17
Ha 28 28 23 30 Ah Be Ldt Er So We Ha Wv Ltl
Wv 26 21 18 30 32 65 Ah 19 28
Ltl 29 37 17 32 46 Be 19 26 28 26 29
31 Ldt 26 11 37
20 Er 11 20
Step 4 Copy the labels into the working 0 So 20 20 21 17
column. 20 We 20 23 18
43 Ha 28 28 23 30
21 Wv 26 21 18 30 32
65 46 31 20 0 20 43 21 17 17 Ltl 29 37 17 32
Ah Be Ldt Er So We Ha Wv Ltl
65 Ah 19 28
46 Be 19 26 28 26 29 Soest Erwitte Lippstadt
31 Ldt 26 11 37
20 Er 11 20
0 So 20 20 21 17
20 We 20 23 18
43 Ha 28 28 23 30
21 Wv 26 21 18 30 32 Now Work Problem 3.4.3
17 Ltl 29 37 17 32 Use the matrix version of Dijkstras algorithm
to find the shortest distance from M to Ah.
Step 5 To find the path that gives the short-
est distance, start at the target vertex
and work back towards the start
vertex in such a way that an edge is H Ah Be We S E L Ar M Wa
H 18 32 17
only included if its distance equals Ah 18 15
the change in the label values. Be 32 15 33 33 24
We 17 33 15 16
S 33 15 18 20 36 30
65 46 31 20 0 20 43 21 17 E 18 11 21
Ah Be Ldt Er So We Ha Wv Ltl L 24 11
65 Ah 19 28 Ar 16 20 31
46 Be 19 26 28 26 29 M 36 31 13
31 Ldt 26 11 37 Wa 30 21 13
20 Er 11 20
0 So 20 20 21 17
20 We 20 23 18
43 Ha 28 28 23 30
21 Wv 26 21 18 30 32
17 Ltl 29 37 17 32

The path that gives the shortest distance from


Soest to Ahlen is therefore: A Visual Basic Source Code is available in
Appendix C and a C source code in Appendix
Soest Lippetal Beckum Ahlen D.
Operations Research 3. Graphs and Networks 30

3.5 The Route Inspection Problem b)

The route inspection problem is to find a


route of minimum total length that traverses
every edge of the network, at least once, and
returns to the start vertex.

This is also known as the Chinese postman


problem. Figure 2

The algorithm for solving this problem is c)


based on the idea of a traversable graph. A
graph is traversable, if it can be drawn in one
continuous movement without going over the
same edge more than once. If all of the verti-
ces of the graph are even, then any of them
may be used as the starting point and the same
point will be returned to at the end of the
Figure 3
movement. Such a graph is said to be Eule-
rian.1 The only other possibility for a traver-
sable graph is that it has exactly two odd ver- d)
tices. In this situation, one of the vertices is
the start point and the other is the finish point.
This type of graph is said to be Semi-Eule-
rian.

Example (1) Figure 4


Find whether the following graphs are traver-
sable, Eulerian, or Semi-Eulerian. Draw the e)
graphs in one continuous movement without
going over the same edge more than once, if
possible.

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:

a) The graph has even vertices only. It is d) Semi-Eulerian graph.


therefore Eulerian. Figure 7 gives a possible
route.
3 7
11 10 4 5 6
12 9
4 3 1 9
2 8
1 2
5 8

6 7
Figure 10
Figure 7

e) All vertices are odd. The vertices can be


b) Eulerian graph. grouped into three pairs. Each pair defines an
edge that must be repeated to solve the prob-
8 lem.
12 4
11 5
7 10 9
6
9
1 6 3 7/8
2
12 3

Figure 8 10/13 11 4 2/5

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

The route inspection algorithm is: Solution:

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.

For the set [BC, DE]: BC = 7 and DE = 4.

Total length of the extra paths for this set is


7 + 4 = 11.

Example (2) Step 4 of the algorithm is to keep going until


The diagram represents the system of path- all the sets have been considered.
ways that a security guard must patrol during
his course of duty. The weights on the edges For the set [BD, CE]:
represent the time taken, in minutes, to patrol
each pathway. The shortest route from B to D is
BA + AD = 16. CE = 6. Total length of the
Find the minimum time required to patrol extra paths for this set is 16 + 6 = 22.
every pathway at least once and give a possi-
ble route. For the set [BE, CD]:

D 10 C The shortest route from B to E is


BC + CE = 13. CD = 10. Total length of the
6 extra paths for this set is 13 + 10 = 23.
4 E F
6 7 The minimum total corresponds to the set
5 4 [BC, DE] which means that the edges BC and
8
DE will be repeated.
10
A B A possible route is ABCDAFBCEDEA.
Figure 13
The time taken will be 60 + 11 = 71.
3. Graphs and Networks 3.5 The Route Inspection Problem 33

Example (3)
Make a plan for winter road clearance. Find
the minimum number of kilometers required
to clear away snow.

Ahlen 15 Beckum Ahlen 7 Beckum

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

Figure 14 Distances in km. The total single length is Figure 15


272 km.

Solution:

There are four odd vertices: Werl (3), Welver


(5), Beckum (5), and Lippstadt (3). Applying
the route inspection algorithm to possible sets
of pairs gives the following extra paths:

[We/Wv, Be/Ldt]: 11 km + 24 km = 35 km.

[We/Be, Wv/Ldt]: 33 km + 35 km = 68 km.

[We/Ldt, Be/Wv]: 43 km + 21 km = 64 km.

Thus, the set of pairs: Werl/Welver and


Beckum/Lippstadt gives the minimum extra
paths of 35 km. The respective edges have to
be repeated. A possible route is:

Welver, Soest, Lippetal, Welver, Beckum,


Hamm, Ahlen, Beckum, Lippstadt, Lippetal,
Beckum, Lippstadt, Erwitte, Soest, Werl,
Welver, Hamm, Werl, Welver.

Total number of kilometers:

272 km + 35 km = 307 km.


3. Graphs and Networks 3.5 The Route Inspection Problem 34

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

Figure 16 Distances in km. Total length: 448 km. Figure 17

Solution:

There are eight odd vertices, which makes the


application of the route inspection algorithm
somewhat impractical: Beckum, Welver,
Soest, Arnsberg, Meschede, Warstein, Er-
witte, Lippstadt.

We try to find a "good" solution. From all the


possible combinations of pairs of odd verti-
ces, we only consider neighboring pairs. In
this connection we observe that the odd verti-
ces form a cycle. We therefore check only the
following two combinations:

[Be/Wv, So/Ar, Me/Wa, Er/Li]


and
[Wv/So, Ar/Me, Wa/Er, Li/Be]

For the first combination we find an extra


length of 65 km, whereas for the second com-
bination we find 90 km. A possible route is
shown in Figure 17 with a total length of 448
km + 65 km = 513 km.
3. Graphs and Networks 3.5 The Route Inspection Problem 35

Now Work Problem 3.5.1


Show that the graph of Figure 18 is Semi-
Eulerian and how to traverse the graph in one
continuous movement.
Moscow

London Berlin

Vienna
Paris

Madrid Rome

Figure 18

Now Work Problem 3.5.2


Make a plan for winter road clearance. Find
the minimum number of kilometers required
to clear away snow.
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 19
Operations Research 3. Graphs and Networks 36

3.6 Matchings

Matchings and Graphs

A bipartite graph is a graph in which the


vertices are divided into two sets such that no
pair of vertices in the same set is connected
by an edge.

In Figure 1, the two sets are {A, B, C} and


{p, q, r, s}.

Some vertices in a bipartite graph may not be


connected to another vertex.
p
A
q
B
r
C
s

Figure 1 Bipartite graph.

A matching between two sets may be repre-


sented by a bipartite graph in which there is at
most one edge connecting a pair of vertices.

A maximal matching is a matching which


has the maximum number of edges. This oc-
curs when every vertex in one of the sets is
connected to a vertex in the other set. The
bipartite graph of Figure 1 represents a maxi-
mal matching.

A complete matching is a matching in which


every vertex is connected to another vertex.
This can only occur when the two sets contain
the same number of vertices.
3. Graphs and Networks 3.6 Matchings 37

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.

In an alternating path, the edges alternate


A between those that are not in the initial
1 matching and those that are.
B
Step 2 Each edge on the alternating path ,
2
not included in the initial matching,
C is now included and each edge origi-
3 nally included is removed.
D
Step 3 Repeat steps 1 and 2 using the latest
Figure 3 matching in place of the initial
matching until no further alternating
Figure 3 is a bipartite graph representing an path can be found.
initial matching.

An initial matching may be improved by in- A


creasing the number of connections. This is 1
the purpose of the matching improvement B
algorithm.
2
C
3
D

Figure 5 Maximal matching.


3. Graphs and Networks 3.6 Matchings 38

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

Figure 11 Complete matching.


2 Q

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

Example (2) D Thu


Table 1 shows the names of five employees of E Fri
a company and the days when they are each
available to work a late shift. The days that Figure 13 Initial matching.
have been highlighted represent a first attempt
at matching the employees to the available
days. c) Obviously, we do need an alternating path
that connects Dave and Tuesday:
a) Use a bipartite graph to represent the re-
lationship between employees and days avail- A Mon
able.
B Tue
b) Use a second bipartite graph to represent
an initial matching based on the days high- C Wed
lighted.
D Thu
c) Implement a matching improvement algo-
E Fri
rithm to obtain a complete matching. Indicate
the alternating path used.
Figure 14

Solution:

a) A Mon

A Mon B Tue

B Tue C Wed

C Wed D Thu

D Thu E Fri

E Fri Figure 15 Complete matching.

Figure 12 Bipartite Graph.


3. Graphs and Networks 3.6 Matchings 40

Now Work Problem 3.6.1 Now Work Problem 3.6.2


Starting with an initial matching in which A is Table 2 shows the names of six employees of
connected to 1, B is connected to 2, and E is a company and the weeks when they are each
connected to 5, use the improvement algo- available to work in the holiday season. The
rithm to establish a complete matching for the weeks that have been highlighted represent a
bipartite graph of Figure 16. Indicate the al- first attempt at matching the employees to the
ternating paths used. available weeks. Use a bipartite graph to rep-
resent the relationship between employees
and weeks available. Use a second bipartite
A 1 graph to represent an initial matching based
on the weeks highlighted. Implement a
B 2
matching improvement algorithm to obtain a
C 3 complete matching. Indicate the alternating
path used.
D 4

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

4.1 Flows in Networks

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

Figure 1 This edge has a capacity of 10 and a flow of 7. (i)


4
5
The set of flows for a network is feasible if:
15 14
B
The total output from all source vertices is 8
equal to the total input for all sink vertices
(most will have just one source vertex and
S 10 C
one sink vertex).

The input for each vertex other than a Figure 2 Cuts.


source or sink vertex is equal to its output.

The flow along each edge is less than or


equal to its capacity.
4. Digraphs 4.1 Flows in Networks 42

The Maximum Flow Minimum Cut Theorem

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

It follows from the theorem that if you find a


flow that is equal to the capacity of some cut 11 6 19
16
then it must be the maximum flow that can be
established through the network. S T
12 C
(ii) 9
11 18
A 8 D 9 T
8 8
4
4 D 15 E
14
5
15 14 Figure 4
B
12 8
4
10
S 10 C

Figure 3

For example, the capacity of cut (ii) for the


network in Figure 3 has already been found to
be 22. The circled figures in Figure3 show
one way of achieving this flow and the
theorem now tells us that this must be the
maximum possible flow through the network.
Operations Research 4. Digraphs 43

4.2 Critical Path Analysis

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.

In Table 1, the figures in brackets represent Figure 1 Activity Network.


the duration of each activity, i. e. the time
required, in hours, for its completion.

TABLE 1 Precedence Table Earliest event times


Activity Depends on
A(3) - The earliest event time for vertex i is de-
B(5) - noted by ei and represents the earliest time of
C(3) A arrival at event i with all dependent activities
D(2) A completed. These times are calculated using a
E(5) B, C forward scan from the start vertex to the ter-
F(4) D minal vertex.
G(1) F, E

A precedence table can be used to produce an


activity network. In the network, activities Latest event times
are represented by arcs and events are repre-
sented by vertices. (See Figure 1) The latest event time for vertex i is denoted
by li and represents the latest time that event i
The vertices are numbered from 0 at the start may be left without extending the time for the
vertex and finishing at the terminal vertex. project. These times are calculated using a
backward scan from the terminal vertex back
The direction of the arrows shows the order in to the start vertex.
which the activities must be completed.

There must only be one activity between each


pair of events in the network. The notation
(i, j) is used to represent the activity between
events i and j.
4. Digraphs 4.2 Critical Path Analysis 44

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

The total float of an activity is the maximum


B(5) G(1) time that the activity may be delayed without
2 4 5 affecting the length of the critical path. It is
given by:
66 E(5) 11 11 12 12
latest finish time

Figure 2 Critical path: ACEG.


earliest start time

duration of the activity


The critical path is the longest path through
the network. The activities on this path are the The total float of any critical activity is al-
critical activities. If any critical activity is ways zero.
delayed then this will increase the time
needed to complete the project. The events on Typically, the purpose of scheduling is to de-
the critical path are critical events and for termine the number of workers needed to
each of these ei = li. complete the project in a given time, or to
determine the minimum time required for a
It is useful to add the information about earli- given number of workers to complete the
est and latest times to the network. The criti- project.
cal path is then easily identified. (See Figure
2) The critical activities are shown as a line in
Figure 3. The diagram illustrates the degree of
Notice that B(5) lies between two critical flexibility in starting activities B, D, and F.
events but is not a critical activity. Remember that F cannot be started until D
has been completed.

Scheduling
0 1 2 3 4 5 6 7 8 9 10 11 12

The process of allocating activities to workers A C E G


for completion, within all of the constraints of
B(5)
the project, is known as scheduling.
D(2)
The information regarding earliest and latest
times for each activity is crucial when con-
structing a schedule. This information may be F(4)

presented as a table (see Table 2) or as a


chart. (See Figure 3) Figure 3
4. Digraphs 4.2 Critical Path Analysis 45

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

Now Work Problem 4.2.1 Now Work Problem 4.2.4


Given the Precedence Table 5 (see Figure 7). Given the Precedence Table 6. Find the activ-
Find the critical activities, and the critical ity network, the critical activities, and the
path. Give the length of the critical path. critical path. Give the length of the critical
path.
Now Work Problem 4.2.2
Construct a schedule for the project of prob- Now Work Problem 4.2.5
lem 4.2.1 in form of a table. (Like, for exam- Construct a schedule for the project of prob-
ple, Table4.) lem 4.2.4 in form of a table.

Now Work Problem 4.2.3


Give the schedule of Problem 4.2.1 as a chart. Now Work Problem 4.2.6
(See, for example, Figure 6.) Give the schedule of Problem 4.2.4 as a chart.

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.

4x + 5y = 100 where x, y 0. Geometrically, the solution (or graph) of a


linear inequality in x and y consists of all
The solution of this equation, called a budget points (x, y) in the plane whose coordinates
equation, gives the possible combinations of satisfy the inequality. For example, a solution
A and B that can be purchased for 100. The of Equation (1) is the point (2, 3), because
graph of the equation is the budget line in substitution gives
Figure 1. Note that (10, 12) lies on the line.
This means that if 10 kg of A are purchased, 4(2) + 5(3) 100
then 12 kg of B must be bought, for a total
cost of 100. 23 100, which is true.

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

For a vertical line x = a (see Figure 3), we y 4 2x.


speak of a half plane to the right (x > a) of the
line or to the left (x < a). Since any linear ine- From the aforementioned statement 3, we
quality (in two variables) can be put into one conclude that the solution includes the half
of the forms we have discussed, we can say plane below the line. Thus, if (x0, y0) is any
that the solution of a linear inequality must be point in this region, then its ordinate y0 is less
a half plane. than the number 4 2x0. For example,
(2, 1) is in the region, and

1 4 2(2)

1 8.

y x=a y

4
x<a x>a

2
x x

Figure 3 A vertical line determines two half planes. Figure 4 Graph of 2x + y 4.


5. Introduction to Linear Programming 5.1 Linear Inequalities in Two Variables 49

Example (2) Solving a Linear Inequality


Find the region defined by the inequality

y 5.

Solution: Since x does not appear, the ine-


quality is assumed to be true for all values of
x. The region consists of the line y = 5, to-
gether with the half plane below it. (See Fig-
ure 5.)

y
5

Figure 5 Graph of y 5.

Now Work Problem 5.1.1


Solve the inequality

2(2x y) 2(x + y) 4.
Operations Research 5. Introduction to Linear Programming 50

5.2 Systems of Inequalities Example (1): Solving a System of Linear


Inequalities
The solution of a system of inequalities con- Solve the system
sists of all points whose coordinates simulta- y 2x + 4
neously satisfy all of the given inequalities.
Geometrically, it is the region that is common yx2
to all the regions determined by the given
inequalities. For example, let us solve the Solution: The solution consists of all points
system that are simultaneously on or above the line
y = 2x + 4 and on or above the line y = x 2.
2x + y 3 It is the shaded region in Figure2.

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

Now we sketch the corresponding lines


y = 2x + 3, y = x and y = 12 . We then shade
the region that is simultaneously above the Figure 2 Solution of system of linear inequalities.
first line, below the second line, and above
the third line. (See Figure 1.)

y=x
3
2
y = 1/2 1

x
1 2 3

y = - 2x + 3

Figure 1 Solution of system of linear inequalities.


5. Introduction to Linear Programming 5.2 Systems of Inequalities 51

Example (2): Solving a System of Linear


Inequalities
Solve the system Now Work Problems 5.2.1 to 5.2.4
3x 2y < 6 In Problems 5.2.1 to 5.2.4 solve the system of
inequalities.
x 3y > 9
5.2.1 4x + 2y 13
Solution:
4x 2y 3
y
2y 1
y = 3/2x - 3
5.2.2 2x 3y 6

x+ y 1
x x + 3y 3

y = 1/3x - 3 5.2.3 x 3y 3

4x + 3y 12

Figure 3 Solution of system of linear inequalities. 4x 2y 8

5.2.4 2x + 4y 8

3x 4y 12

3x + y 3
Operations Research 5. Introduction to Linear Programming 52

5.3 Linear Programming: Two Variables TABLE 1


Hours
Sometimes it is desired to maximize or mini- Manual Electric Available
mize a function, subject to certain restrictions A 2 hr 1 hr 180
B 1 hr 2 hr 160
(or constraints). For example, a manufacturer
C 1 hr 1 hr 100
may want to maximize a profit function, sub- Profit/Unit 4 6
ject to production restrictions imposed by
limitations on the use of machinery and labor. We consider the following problem. A com-
pany produces two types of widgets: manual
We shall now consider how to solve such and electric. Each requires in its manufacture
problems when the function to be maximized the use of three machines: A, B, and C. Table
or minimized is linear. A linear function in x 1 gives data relating to the manufacture of
and y has the form these widgets. Each manual widget requires
the use of machine A for 2 hours, machine B
Z = ax + by for 1 hour, and machine C for 1 hour. An
electric widget requires 1 hour on A, 2 hours
where a and b are constants. We shall also on B, and 1 hour on C. Furthermore, suppose
require that the corresponding constraints be the maximum numbers of hours available per
replaced by a system of linear inequalities month for the use of machines A, B, and C
(involving "" or "") or linear equations in x are 180, 160, and 100, respectively. The profit
and y, and that all variables be nonnegative. A on a manual widget is 4, and on an electric
problem involving all of these conditions is widget is 6. If the company can sell all the
called a linear programming problem. widgets it can produce, how many of each
type should it make in order to maximize the
In a linear programming problem, the func- monthly profit?
tion to be maximized or minimized is called
objective function. Although there are usu- To solve the problem, let x and y denote the
ally infinitely many solutions to the system of number of manual and electric widgets, re-
constraints (these are called feasible solutions spectively, that are made in a month. Since
or feasible points), the aim is to find one such the number of widgets made is not negative,
solution that is an optimum solution (that is,
one that gives the maximum or minimum x0 and y 0.
value of the objective function).
For machine A, the time needed for working
We shall now give a geometrical approach to on x manual widgets is 2x hours, and the time
linear programming. Section 5.4 will discuss needed for working on y electric widgets is 1y
a matrix approach that will enable us to work hours. The sum of these times cannot be
with more than two variables and, hence, a greater than 180, so
wide range of problems.
2x + y 180

Similarly, the restrictions for machines B and


C give

x + 2y 160 and x + y 100.


5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 53

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.

Since the objective function, P = 4x + 6y, is


The profit function is a function of x and y
equivalent to
and is given by
2 P
P = 4x + 6y. y= x+
3 6
Summarizing, we want to maximize the ob-
jective function it defines a so-called family of parallel lines,
one for each possible value of P, each having
(1) P = 4x + 6y slope of 2/3 and y-intercept (0, P/6). For
example, if P = 600, then we obtain the line
subject to the condition that x and y must be a
solution of the system of constraints 2
y = x + 100
3
(2) 2x + y 180
shown in Figure2. This line, called an iso-
(3) x + 2y 160 profit line, gives all possible combinations of
x an y that yield the same profit, 600. Note
(4) x + y 100 that this isoprofit line has no point in common
with the feasible region, whereas the isoprofit
(5) x0 line for P = 300 has infinitely many such
points. Let us look for the member of the
(6) y0 family that contains a feasible point an whose
P-value is maximum. This will be the line
Thus, we have a linear programming problem. whose y-intercept is farthest from the origin
Constraints (5) and (6) are called nonnegat- (giving a maximum value of P) and that has at
ive conditions. The region simultaneously least one point in common with the feasible
satisfying constraints (2) (6) is shaded in region. It is not difficult to observe that such a
Figure 1. Each point in this region represents line will contain the corner point A. Any iso-
a feasible solution, and the region is called the profit line with a greater profit will contain no
feasible region. Although there are infinitely points of the feasible region.
many feasible solutions, we must find one
that maximizes the profit function.
5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 54

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)

x + y = 100 D = (0, 0) E = (0, 80)

x + 2y = 160 We now evaluate the objective function


P = 4x + 6y at each point
This gives x = 40 and y = 60. Substituting
these values into the equation P = 4x + 6y, we P(A) = 4(40) + 6(60) = 520
find that the maximum profit subject to the
constraints is 520, which is obtained by pro- P(B) = 4(80) + 6(20) = 440
ducing 40 manual widgets and 60 electric
widgets per month. P(C) = 4(90) + (0) = 360

If a feasible region can be contained within a P(D) = 4(0) + 6(0) = 0


circle, such as the region in Figure 2, it is
called a bounded feasible region. Otherwise, P(E) = 4(0) + 6(80) = 480
it is unbounded. When a feasible region
contains at least one point, it is said to be Thus, P has a maximum value of 520 at A,
nonempty. Otherwise, it is empty. The re- where x = 40 and y = 60.
gion in Figure 2 is a nonempty bounded fea-
sible region. The optimum solution to a linear program-
ming problem is given by the optimum value
It can be shown that: of the objective function and the point where
the optimum value of the objective function
A linear function defined on a nonempty occurs.
bounded feasible region has a maximum
(minimum) value, and this value can be found
at a corner point.

This statement gives us a way of finding an


optimum solution without drawing isoprofit
lines, as we did previously: We simply evalu-
ate the objective function at each of the corner
points of the feasible region and then choose a
corner point at which the function is opti-
mum.

For example, in Figure 2 the corner points are


A, B, C, D, and E. We found A before to be
(40, 60). To find B, we see from Figure 1 that
we must solve

2x + y = 180

x + y = 100
5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 55

Example (1): Solving a linear Program-


ming Problem y A = (0, 0)
Maximize the objective function Z = 3x + y B = (4, 0)
subject to the constraints C = (3, 2)
8
D = (0, 4)
2x + y 8
6 2x +y = 8
2x + 3y 12 4
D
x0 2 C 2x +3y = 12
y0 A B
x
0 2 4 6 8
Figure 3 A, B, C, and D are corner points of the feasible
Solution: In Figure 3, the feasible region is region.
nonempty and bounded. Thus, Z is maximum
at one of the four corner points. The coordi-
nates of A, B, and D are obvious on inspec-
tion. To find the coordinates of C, we solve
the equations

2x + y = 8

2x + 3y = 12

simultaneously, which gives x = 3, and y = 2.


Thus,

A = (0, 0) B = (4, 0) C = (3, 2) D = (0, 4)


Evaluating Z at these points, we obtain

Z(A) = 3(0) + 0 = 0

Z(B) = 3(4) + 0 = 12

Z(C) = 3(3) + 2 = 11

Z(D) = 3(0) + 4 = 4

Hence, the maximum value of Z, subject to


the constraints, is 12, and it occurs when x = 4
and y = 0.
5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 56

Example (2): Solving a linear Program-


ming Problem y
Maximize the objective function Z = 9x 2y 9x- 2y = 92
subject to the constraints 25 D

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

simultaneously, which gives x = 12, and y = 8.


Thus,

A = (0, 0) B = (4/3, 0)

C = (12, 8) D = (0, 24)

Evaluating Z at these points, we obtain

Z(A) = 9(0) 2(0) = 0

Z(B) = 9(4/3) 2(0) = 12

Z(C) = 9(12) 2(8) = 92

Z(D) = 9(0) 2(24) = 48

Hence, the maximum value of Z, subject to


the constraints, is 92, and it occurs when
x = 12 and y = 8.
5. Introduction to Linear Programming 5.3 Linear Programming: Two Variables 57

Empty Feasible Region Unbounded Feasible Region

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

Now Work Problems 5.3.1 to 5.3.3 Now Work Problem 5.3.4


Maximize the function Z subject to the given Minimize the function
constraints.
Z=x+y
5.3.1 Z = x + 2y
subject to the constraints
x+y5
5x + 2y 50
x3
x + 2y 20
y4

x, y 0

5.3.2 Z = 15x + 10y

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

5.4 The Simplex Algorithm

Problem: Simplex Algorithm:

Maximize 1. Set up the initial simplex tableau:

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

2. If all the indicators in the last row are non-


where x1, x2 and b1, b2, b3 are nonnegative. negative, then Z has a maximum when x1 = 0
and x2 = 0. The maximum value is 0. If there
are any negative indicators, locate the column
in which the most negative indicator appears.
This is the pivot column. (If more than one
column contains the most negative indicator,
the choice of pivot column is arbitrary.)

3. Divide each positive entry above the objec-


tive row in the pivot column into the corre-
sponding b-values. We write the results into
the working column. If, e. g., -c2 < -c1, we
have

b1/a12 a11 a12 b1


b2/a22 a21 a22 b2
b3/a32 a31 a32 b3
-c1 -c2 0

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

a21/a22 1/a22 b2/a22


a11 - a12a21/a22 -a12/a22 b1 - a12b2/a22

a21/a22 1/a22 b2/a22


-a32/a22
7. For the entries of the second column (the c2/a22
old pivot column) we divide the entries of the
pivot column by the negative pivot:
a21/a22 1/a22 b2/a22
b1/a12 a11 a12 b1
-a12/a22 b2/a22 a21 a22 b2
a21/a22 1/a22 b2/a22 b3/a32 a31 a32 b3
-a32/a22 -c1 -c2 0
c2/a22

a11 - a12a21/a22 -a12/a22 b1 - a12b2/a22

a21/a22 1/a22 b2/a22


a31 - a32a21/a22 -a32/a22
c2/a22
5. Introduction to Linear Programming 5.4 The Simplex Algorithm 61

a21/a22 1/a22 b2/a22 Finally, the new tableau is completed:


b1/a12 a11 a12 b1
b2/a22 a21 a22 b2
b3/a32 a11 - a12a21/a22 -a12/a22 b1 - a12b2/a22
a31 a32 b3
-c1 -c2 0 a21/a22 1/a22 b2/a22
a31 - a32a21/a22 -a32/a22 b3 - a32b2/a22
-c1 (-c2)a21/a22 c2/a22 0(-c1)b2/a22
a11 - a12a21/a22 -a12/a22 b1 - a12b2/a22

a21/a22 1/a22 b2/a22


a31 - a32a21/a22 -a32/a22 b3 - a32b2/a22 9. If the indicators of the new tableau are all
c2/a22 nonnegative, we have an optimum solution.
The maximum value of Z is the entry in the
last row and the last column. It occurs when
a21/a22 1/a22 b2/a22 the variables are equal to the corresponding
b1/a12 a11 a12 b1 entries in b-column. If at least one of the indi-
b2/a22 cators is negative, repeat the process, begin-
a21 a22 b2
b3/a32 ning with step 2 applied to the new tableau.
a31 a32 b3
-c1 -c2 0

a11 - a12a21/a22 -a12/a22 b1 - a12b2/a22

a21/a22 1/a22 b2/a22


a31 - a32a21/a22 -a32/a22 b3 - a32b2/a22
-c1 (-c2)a21/a22 c2/a22

a21/a22 1/a22 b2/a22


b1/a12 a11 a12 b1
b2/a22 a21 a22 b2
b3/a32 a31 a32 b3
-c1 -c2 0

a11 - a12a21/a22 -a12/a22 b1 - a12b2/a22

a21/a22 1/a22 b2/a22


a31 - a32a21/a22 -a32/a22 b3 - a32b2/a22
-c1 (-c2)a21/a22 c2/a22 0(-c1)b2/a22
5. Introduction to Linear Programming 5.4 The Simplex Algorithm 62

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

2. The most negative entry in the last row is in


the 1st column. Thus, the 1st column is the
7. For the entries of the 1st column (the old
pivot column.
pivot column) we divide the entries of the
pivot column by the negative pivot:
3. Divide each positive entry above the objec-
tive row in the pivot column into the corre-
sponding value of the last column. The results
are given on the left side of the tableau: -2/7
-4/7
2/7 4/7 10
30/1 = 30 1 3 30 600/7
33/2 = 16.5 2 3 33
35/3.5 = 10 7/2 2 35
-300 -200 0 8. The other entries are calculated:

4. Mark the entry in the pivot column that


corresponds to the smallest quotient in step 3. -2/7 17/7 20
This is the pivot entry and the row in which -4/7 13/7 13
it is located is the pivot row. 2/7 4/7 10
600/7 -200/7 3000

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

8.2 -2/7 17/7 20


7
y Isoprofit line
-4/7 13/7 13
17.5 2/7 4/7 10 300x + 200y = 3.200
600/7 -200/7 3000 10
8
-4/13 7/13 7 (6, 7)
8.2 -2/7 17/7 20 6
7 -4/7 13/7 13
17.5 2/7 4/7 10 4
600/7 -200/7 3000
2
0 x
-17/13 0 2 4 6 8 10
-4/13 7/13 7
-4/13 Figure 1 Graphical solution of Example (1).
200/13

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:

0.040 0.040 0.040 8.240


36 10 2 1 36 Now Work Problem 5.4.2
60 2 25 2 120 Maximize
8.24 1 1 25 206
-1 -2 -4 0 Z = 3x + 5y + 4z

subject to the constraints


0.077 0.040 -0.003 4.154
14.16 9.960 1.960 -0.040 27.760
4.15 x + 2y + 10z 58
1.920 24.920 -0.080 103.520
206 0.040 0.040 0.040 8.240
10x + y + z 47
-0.840 -1.840 0.160 32.960
2x + 5y + z 23
0.102 -0.008 -0.003 2.000
2 9.809 -0.079 -0.034 19.618
53.948 5x + y + 5z 47
0.077 0.040 -0.003 4.154
218.216 0.037 -0.002 0.040 8.074
-0.698 0.074 0.154 40.603

0.102 -0.008 -0.003 2.000


-0.008 0.041 -0.003 4.000 A Visual Basic program listing is available in
-0.004 -0.002 0.040 8.000 Appendix C and a C source code in Appendix
0.071 0.068 0.152 41.999 D.
Operations Research 5. Introduction to Linear Programming 65

5.5 Minimization Problems


1/2 1/2 3/2
Example (1) 4 3 1 4
Minimize Z = 3x + 4 y subject to the con- 3.2 8 6 19
straints 1.5 1 2 3
-3 -4 0
I. 3x + y 4
2/5 1/5 1
II. 8 x + 6 y 19 1 5/2 1/2 5/2
2 5 3 10
III. x + 2y 3 3 1/2 1/2 3/2
1 2 6
Solution: Note that this tableau corresponds to point A
in Fig. 2, which is not a point of the feasible
Graphical solution: minimum at the point region.
(2.0, 0.5)
2/5 1/5 1
(see Fig. 1).
2 2 5
1/5 3/5 1
Z = 3 x + 4 y = 3(2.0 ) + 4(0.5) = 8.0
2/5 9/5 7

y This final tableau corresponds to point B,


which, again, is not a point of the feasible
A
region. Therefore, a solution has not been
4 found.
3
B

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

2. Find the largest intercepts x and y :

x = 3

y = 4

3. Rewrite the constraints using the new vari-


ables u and v:

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

Figure 4 Figure 5 Solution of the minimization problem.

Simplex algorithm:

1/2 1/2 4 Now Work Problem 5.5.1


9 3 1 9 Minimize Z = 2 x + y subject to the con-
4.8 8 6 29 straints
4 1 2 8
3 4 0 I. 2x + 3y 6
II. 4x + y 4
1/5 3/5 1 III. 2x + 2 y 5
2 5/2 1/2 5
1 5 3 5
8 1/2 1/2 4
1 2 16
This tableau corresponds to point D in Fig. 4.

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

These are the coordinates of point C in Fig.5 .

Z = 3x + 4y = 8
Chapter Operations Research 68

6
Nonlinear Optimization Problems

6.1 A Nelder-Mead# Procedure


The algorithm requires a matrix X, n + 1 by n + 2, for a problem in n parameters, in which to store
the vertices of the simplex and the point bc as well as the function values of the simplex vertices.
Vertex j of the simplex is stored in column j of X with its associated function value in the element
X[n + 1, j]. A vector b of order n is also needed by the algorithm to save temporarily the coordinates
of a point in parameter space so that S(b) can be computed.

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.

0 Enter n, the number of parameters in the function.


Enter b[i], i = 1, 2, ... , n, the starting values of the parameters.
Enter step, a starting increment for all parameters. Note that if the parameters differ widely in
scale, it may be preferable to enter a different increment for each parameter.
Let C = n + 2.
Let alpha = 1, the reflection factor.
Let beta = 0.5, the contraction and reduction factor.
Let gamma = 2, the extension factor.
1 Compute P = S(b), let count = 1 to initialize function evaluation counter. If function is not
computable, stop.
2 Let X[n + 1, 1] = P to save initial function value.
For i = 1 to n, let X[i, 1] = b[i], end loop on i.
This saves the initial point as vertex 1 of the simplex.
Let L = 1 to save the index of the vertex as the lowest so that its function value is not recom-
puted in steps 10 to 13.
Generate rest of simplex, computing a measure of size at the same time.
Let size = 0.
3 For j = 2 to n + 1
4 For i = 1 to n, let X[i, j] = b[i], end loop on i.
This loop sets all vertices to b, now each will be altered in turn.
Let T = step.
5 Let X[j 1, j] = b[j 1] + T.
6 If X[j 1, j] = b[j 1], let T = 10*T, then goto step 5 to repeat the increment with a
larger step, since the parameter has jet to be altered.
7 Let size = size + ABS(T) to compute a measure of the simplex size.
8 End loop on j.

#
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

27 Reduction along a line.


For i = 1 to n.
Let b[i] = (1 beta)*X[i, H] + beta*X[i, C].
End loop on i.
Compute P(b), let count = count + 1
Now test the function value at the new point. Note that VH cannot be used in the test since the
highest vertex may have been replaced by the reflection point at step 26.
28 If P < X[n + 1, H], goto step 39 to save the new point.
Otherwise, all efforts have failed to lower the highest vertex in the simplex, so a general con-
traction towards the current lowest vertex is performed.
29 Let size = 0.
For j = 1 to n + 1
30 If j = L, goto step 32 since lowest vertex is not altered
31 For i = 1 to n.
Let X[i, j] = beta*(X[i, j] X[i, L]) X[i, L].
Let size = size + ABS(X[i, j] X[i, L]).
End loop on i.
Note that the first of the above two formulas for the value of the contraction factor
which is recommended.
32 End loop on j.
33 If site < ss, goto step 9 since simplex size has been successfully decreased. Otherwise Nelder-
Mead procedure cannot continue.
34 Goto step 40 the end of the program.
35 Extension of the simplex along a line. At the same time the point bR is saved in the right-most
column of the matrix X.
For i = 1 to n
Let T = gamma*b[i] + (1 gamma)*X[i, C].
Let X[i, C] = b[i].
Let b[i] = T.
End loop on i.
Let X[n + 1, C] = P.
36 Compute P = S(b), let count = count + 1.
37 If P < X[n + 1, C], goto step 39 to save the new point since it is the lowest so far. Otherwise
the extension point is not lower than the reflection point, and b must be reset to the latter.
38 For i = 1 to n; let b[i] = X[i, C]; end loop on i. Let P = X[n + 1, C].
39 For i = 1 to n; let X[i, H] = b[i]; end loop on i; Let X [n + 1, C] = P.
This replaces the current highest vertex with b.
Goto step 14
40 Stop. The Nelder-Mead algorithm can make no more progress in lowering the function.
Operations Research 6 Nonlinear Optimization Problems 71

6.1 Least-Squares Fits: Logistic Growth


In the Natural- and Life Sciences, in Economics, and various other fields, growth processes play an
important role. Such growth processes start, in general, exponentially, but then turn into a saturation
after a certain while: logistic growth.

A model function, which describes such a growth, is given by#

M
(1) f (t ) =
1 + be ct

The graph of this function is shown in Fig. 1.


Logistic Growth Function f (t )

Time t

Figure 1: Logistic Growth

#
s. Business Bathematics II
6 Nonlinear Optimization Problems 6.2 Least Squares Fits: Logistic Growth 72

Example: Market Penetration


Suppose a new product has been introduced in a market. The number of units of the product, sold in
the first ten months are given in Table 1 and the corresponding graphical display of the data is
shown in Fig. 2:
Table 1
Month 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
Units 420 510 1000 910 1105 1900 1920 3150 4250 4765

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.

In a statistical sense, we can define a variance

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:

M = 28 468 b = 105.08 c = 0.62286

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:

A maximum number of n-1 bulbs has to be


checked in a circuit with up to 2n 1 bulbs.

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:

9 We first find the decreasing order, using the


44 63 35 92 51 17 78 86 29 Bubble Sort Algorithm:
3 1 5 C D G A F E H B
35 17 29 44 63 92 51 78 86
6 5 5 4 4 3 3 2
2 1 1 1 1 3
17 29 35 44 51 63 92 78 86 1st worker: C(6), B(2)
2nd worker: D(5), E(3)
1 1 1 1 1 1 2 1 3rd worker: G(5), H(3)
17 29 35 44 51 63 78 86 92 4th worker: A(4), F(4)

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:

9 We first find the decreasing order, using the


44 63 35 92 51 17 78 86 29 Bubble Sort Algorithm:
5 1 3
63 92 51 78 86 44 35 17 29
898 834 555 489 478 432 368

3 1 1 1 1 2 1st disk: 898, 489


92 78 86 63 51 44 35 17 29 2nd disk:834, 555
3rd disk: 478, 432, 368
1 2 1 1 1 1 1 1
92 78 86 63 51 44 35 29 17

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

Figure 2 Minimum spanning tree. Moscow

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

The sum of the weights is 154 min.


Appendix A Problems Solved Step by Step 80

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

Figure 5 Solution of the TSP 3.3.1. Figure 7 Solution.


Appendix A Problems Solved Step by Step 81

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

Upper bound: 2849 km = 1698 km.


Lower bound: 849 km + 224 km = 1073 km.

Route:

HHH KSF DoMSHBHH


154 166 191 224 71 167 127

Length: 1100 km.


Appendix A Problems Solved Step by Step 82

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

Please note that this subroutine makes use of


the bubble-sort algorithm.

See also Appendix C.


Appendix A Problems Solved Step by Step 83

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

3.4.2 The back scan gives:


Solution:
Ah H We Ar M

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

Figure 13 Initial matching. Figure 16 Alternating path connecting D and 3.

A 1 A 1

B 2 B 2

C 3 C 3

D 4 D 4

E 5 E 5

Figure 14 Alternating path connecting C and 4. Figure 17 Complete matching.

A 1

B 2

C 3

D 4

E 5

Figure 15 Path connecting B and 2 is removed.


Appendix A Problems Solved Step by Step 86

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

Figure 18 Bipartite Graph. Figure 21 Complete matching.

A 29
B 30
C 31

D 32

E 33
F 34

Figure 19 Initial matching.

A 29
B 30
C 31

D 32

E 33
F 34

Figure 20 Alternating path connecting C and 31.


Appendix A Problems Solved Step by Step 87

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

Figure 22 Minimum cut: 34

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

Figure 23 Possible maximum flow.


Appendix A Problems Solved Step by Step 88

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)

Figure 25 Chart Figure 27 Chart


Appendix A Problems Solved Step by Step 89

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

Dividing both sides by 4 and reversing the


sense of the inequality

x
y + 1.
2

1
x
-2

Figure 28 Graph of y x/2 + 1.


Appendix A Problems Solved Step by Step 90

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

Figure 29 Solution of Problem 5.2.1 Figure 31 Solution of Problem 5.2.3

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

Figure 30 Solution of Problem 5.2.2 Figure 32 Solution of Problem 5.2.4


Appendix A Problems Solved Step by Step 91

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

Figure 33 Solution of Problem 5.3.1 Figure 35 Solution of Problem 5.3.3

5.3.2 5.3.4
Solution: Solution:

y A = (0, 0) y A = ( 0.00, 25.00)


B = (3, 0) B = ( 7.50, 6.25)
12 C = (2, 4.5) 25 A C = (20.00, 0.00)
10 D = (0, 6)
20
8 27x +6y = 81 5x +2y = 50
15
6 15x+ 10y = 75 x +y = 13,75
D
C
10
4 6x+ 8y = 48 B
5 x+ 2y = 20
2
C
A B
x x
0 2 4 6 8 10 12 0 5 10 15 20 25
Figure 34 Solution of Problem 5.3.2 Figure 36 Solution of Problem 5.3.4
Appendix A Problems Solved Step by Step 92

5.4.1 5.4.2
Maximize Maximize

Z = 4x + 6y Z = 3x + 5y + 4z

subject to the constraints subject to the constraints

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

1/2 1/2 80 Solution:


180 2 1 180
80 1 2 160 0.4 0.2 0.2 4.6
100 1 1 100 29 1 2 10 58
47 10 1 1 47
-4 -6 0
4.6 2 5 1 23
47 5 1 5 47
2 -1 40
66.7 3/2 -1/2 100 -3 -5 -4 0
160 1/2 1/2 80
40 1/2 -1/2 20 0.021 -0.042 0.104 5.083
5.083 0.2 -0.4 9.6 48.8
-1 3 480
53.000 9.6 -0.2 0.8 42.4
23.000 0.4 0.2 0.2 4.6
8.833 4.6 -0.2 4.8 42.4
-3 1 40
-1 1 60 -1.0 1.0 -3.0 23.0
2 -1 40
2 2 520 0.104 -0.017 -0.009 4.000
242.048 0.021 -0.042 0.104 5.083
y 4.000 9.583 -0.166 -0.083 38.334
9.048 0.396 0.208 -0.021 3.583
4.001
160 4.499 0.002 -0.500 18.001
-0.937 0.874 0.313 38.249
120
-0.002 -0.042 0.104 4.999
80
(40, 60) 0.104 -0.017 -0.009 4.000
4x + 6y = 520 -0.041 0.215 -0.017 1.999
40 -0.470 0.078 -0.460 0.005
0.100 0.858 0.305 41.997
x
0 40 80 120 160
Figure 37 Graphical solution of Problem 5.4.1. Refer
also to section 5.3.
Appendix A Problems Solved Step by Step 93

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.

into descending order using the quick sort


algorithm. B1.7
Table 2 shows the amount of memory taken
up by some files on a computer system. The
files are to be transferred onto memory sticks
with a capacity of 2 Gb each. Use the 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

The Travelling Salesman Problem

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:

B4.1 Dsseldorf Bielefeld

B4.2 Meschede Wesel

B4.3 Bielefeld Siegen

B4.4 Paderborn Wuppertal


Appendix B Practice Problems 100

The Route Inspection Problem

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).

B5.3 Find the minimum length of a tour to


Figure 1 Making a plan for winter road clearance. travel every road at least once (Fig. 3). Give a
possible route with start vertex H.
B5.1 Find the minimum number of kilometers
required to clear away snow (Fig. 1). Find a In a network with 2 odd vertices, A and B, the
possible route with start vertex C. set of all combinations of pairs of odd vertices
consists of 1 pair: A-B.

In a network with 4 odd vertexes, A, B, C,


A
and D, the set of all combinations of pairs of
4.2 odd vertexes consists of 3 combinations of
6.5 G
pairs:
B 6.8 4.1 A-B, C-D
D
A-C, B-D
5.0 2.5 7.2 A-D, B-C
4.5 6.2
8.2 E
C 7.5 In a network with 6 odd vertexes, A, B, C, D,
6.0 E, and F, the set of all combinations of pairs
4.0 H of odd vertexes consists of 15 combinations
9.5 of pairs. The first 6 may be:
F A-B, C-D, E-F
Figure 2 The diagram represents the system of path- A-B, C-E, D-F
ways that a security guard must patrol during his A-B, C-F, D-E
course of duty. The weights on the edges represent the A-C, B-D, E-F
time taken, in minutes, to patrol each pathway. A-C, B-E, D-F
A-C, B-F, D-E
B5.2 Find the minimum time required to
patrol every pathway at least once (Fig. 2). B5.4 Give the remaining 9 combinations of
Give a possible route with start vertex D. pairs.

B5.5 How many combinations of pairs of odd


vertices exist in a network with 8 odd ver-
texes? Give a general rule.
Appendix B Practice Problems 101

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

B7.1 Find the maximum flow through the


network of Figure 1 using the maximum flow
minimum cut theorem. Also find a way of
achieving maximum flow through the
network.

A 15 B

13 12 11
17

S T
12 C
5
22
18

D 10 E

Figure 2

B7.2 Find the maximum flow through the


network of Figure 2 using the maximum flow
minimum cut theorem. Also find a way of
achieving maximum flow through the
network.
Appendix B Practice Problems 103

Systems of Inequalities
Find the feasible regions.

B8.1 x + 2y 6 B8.5 x + 4 y 400

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

Linear Programming: Two Variables


Maximize the function Z subject to the given
constraints.

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

Linear Programming: Two Variables


Maximize the function Z subject to the given
constraints, using the Simplex Algorithm.

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

subject to the constraints subject to the constraints

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

subject to the constraints

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

2.3 The Quick Sort


The algorithm requires an Excel user-interface sheet IO and a second, empty sheet m. The pro-
gram sorts the list in the 2nd row of IO into ascending order and puts the result into the 4th row.
Note that the number of data must be given in cell A1. E. g., if the IO before running the program
is

A B C D E F
1 6
2 45 23 29 33 12 25
3
4

the result will be

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

2.4 Bin Packing


The algorithm requires an Excel user-interface sheet IO and a two empty sheets a and b,
respectively. The program sorts the list in the 2nd column of IO into descending order and puts it
into sheet a using the bubble-sort algorithm, and then applies the full-bin decreasing algorithm.
Note that the number of data must be given in cell A1, and the bin-storage capacity in cell A2. E. g.,
if the IO before running the program is

A B C D E F
1 7 489
2 1400 369
3 555
4 432
5 834
6 478
7 898

the result will be

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

3.2 Prims Algorithm


The algorithm requires an Excel user-interface sheet IO and a two empty sheets a and b,
respectively. Note that the dimension of the (square) matrix must be given in cell A1.

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:

After running the program, the output will be:

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

3.3 The Traveling Salesman Problem


In Problem 3.4.4 the following subroutine was developed:
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

If this subroutine is added to Prims algorithm, the output is:

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

3.4 Dijkstras Algorithm


The algorithm requires an Excel user-interface sheet IO and one empty sheet m.The dimension
of the (square) matrix must be given in cell A1, the start vertex in B1, and the target vertex in A2.

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:

After running the program the output will be:

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

5.4 The Simplex Algorithm


The program requires a user-interface sheet IO and three blank sheets, d1, d2, and v,
respectively. In cell A1, the number of rows must be given, and in A2, the number of columns.

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

After running the program, the output will be

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

6.1 The Nelder-Mead Algorithm

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

2.4 Bin Packing

#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];

void bubblesort(int n, int a[size]);


void packing(int a[size], int b[size][size]);
int checkfilling(int i, int count, int a[size], int b[size][size]);
void outm(int m[size][size]);

int main()
{bubblesort(dim,a);
packing(a,b);
outm(b);
return 0;}

void bubblesort(int n, int a[size])


{int i, x, exchanged=1;
while(exchanged==1 && n>=0)
{exchanged=0;
for(i=1;i<=n;++i) if(a[i]<a[i+1])
{x=a[i+1];
a[i+1]=a[i];
a[i]=x;
exchanged=1;}
n=n-1;}}

void packing(int a[size], int b[size][size])


{int i=1, j=1, k=1, empty=0;
while(empty==0)
{empty=1;
for (k=1;k<=dim;++k)
if(checkfilling(i,k,a,b)<=max && a[k]!=0)
{b[i][j]=a[k];
a[k]=0;
empty=0;
j=j+1;}
i=i+1;
j=1;}}

int checkfilling(int i, int k, int a[size], int b[size][size])


{int j, sum;
sum=0;
for(j=0;j<size;++j) sum=sum+b[i][j];
sum=sum+a[k];
return sum;}

void outm(int m[size][size])


{int i,j;
for (i=1;i<=dim;++i) if(m[i][1]!=0)
{for (j=1;j<=dim;++j) if(m[i][j]!=0) printf("%6d",m[i][j]);
printf("\n");}
printf("\n");}
Appendix D C Program Listings 126

2.5 Binary Search

/*2.5 Binary Search Example 1*/

#include <stdio.h>
#include <stdlib.h>

int a[16]={ 0,91,83,72,66,64,53,46,


35,21,16,14,4};
int dim=15;

int search(int x);


void output(int x);

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

3.2 Prims Algorithm

#include <stdio.h>
#include <stdlib.h>

int m[8][8]={{ 0, 0, 0, 0, 0, 0, 0, 0},


{ 0, 0, 890, 0,1600, 870, 0, 520},
{ 0, 890, 0,1270,2480, 340, 0, 0},
{ 0, 0,1270, 0, 0,1060,1340, 0},
{ 0,1600,2480, 0, 0, 0,2380,1680},
{ 0, 870, 340,1060, 0, 0,1040,1000},
{ 0, 0, 0,1340,2380,1040, 0, 740},
{ 0, 520, 0, 0,1680,1000, 740, 0}};

int i,j,min=10000,count=1,dim=7,size=8,marks[8][8];

void del(int i, int m[size][size]);


void check(int m[size][size], int marks[size][size]);
int mst(int m[size][size], int marks[size][size]);
void outm(int m[size][size]);

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;}

void del(int i, int m[size][size])


{int j;
for (j=1;j<=dim;++j) m[i][j]=0;
m[0][i]=count;}

void check(int m[size][size], int marks[size][size])


{int i,j,imin,jmin;
for (i=1;i<=dim;++i)
for (j=1;j<=dim;++j)
if(m[0][j]!=0 && m[i][j]<=min && m[i][j]>0)
{min=m[i][j];
imin=i;
jmin=j;}
marks[imin][jmin]=min;
del(imin,m);
min=10000;}

int mst(int m[size][size], int marks[size][size])


{int i,j,sum=0;
for (i=1;i<=dim;++i)
{marks[0][i]=m[0][i];
for (j=1;j<=dim;++j) sum=sum+marks[i][j];}
return sum;}

void outm(int m[size][size])


{int i,j;
for (i=1;i<=dim;++i) printf("%5d",m[0][i]);
printf("\n");
printf("\n");
for (i=1;i<=dim;++i)
{for (j=1;j<=dim;++j) printf("%5d",m[i][j]);
printf("\n");}
printf("\n");}
Appendix D C Program Listings 128

3.3 The Traveling Salesman Problem

#include <stdio.h>
#include <stdlib.h>

int min=1000, min0=1000, count=1, dim=7, size=8, marks[8][8] ;

int m[8][8]={{ 0, 0, 0, 0, 0, 0, 0, 0},


{ 0, 0,127,154, 0, 0, 0, 0},
{ 0,127, 0,130, 0,167, 0, 0},
{ 0,154,130, 0,212,189,166, 0},
{ 0, 0, 0,212, 0, 71,164,224},
{ 0, 0, 0,189, 71, 0, 0, 0},
{ 0, 0,167,166,164, 0, 0,191},
{ 0, 0, 0, 0,224, 0,191, 0}};

void del(int i, int m[size][size]);


void check(int m[size][size], int marks[size][size]);
int secondtolast(int m[size][size]);
int mst(int m[size][size], int marks[size][size]);
void outm(int m[size][size]);

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;}

void del(int i, int m[size][size])


{ int j;
for (j=1;j<=dim;++j) m[i][j]=0;
m[0][i]=count;}

void check(int m[size][size], int marks[size][size])


{int i,j,imin,jmin;
for (i=1;i<=dim;++i)
for (j=1;j<=dim;++j)
if(m[0][j]!=0 && m[i][j]>0 && m[i][j]<=min)
{min=m[i][j];
imin=i;
jmin=j;}
marks[imin][jmin]=min;
del(imin,m);
min=min0;}
C Program Listings 3.3 The Traveling Salesman Problem 129

int secondtolast(int m[size][size])


{int i,j,jmin,jstl;
for (j=1;j<=dim;++j) if(m[0][j]==0) i=j;
for (j=1;j<=dim;++j) if(m[i][j]>0 && m[i][j]<=min)
{jmin=j;
min=m[i][j];}
min=min0;
for (j=1;j<=dim;++j) if(m[i][j]>0 && m[i][j]<=min && j!=jmin)
{jstl=j;
min=m[i][j];}
min=min0;
return m[i][jstl];}

int mst(int m[size][size], int marks[size][size])


{int i,j,sum=0;
for (i=1;i<=dim;++i)
{marks[0][i]=m[0][i];
for (j=0;j<=size-1;++j) sum=sum+marks[i][j];}
return sum;}

void outm(int m[size][size])


{int i,j;
for (i=1;i<=dim;++i) printf("%4d",m[0][i]);
printf("\n");
printf("\n");
for (i=1;i<=dim;++i)
{for (j=1;j<=dim;++j) printf("%4d",m[i][j]);
printf("\n");}
printf("\n");}
Appendix D C Program Listings 130

3.4 Dijkstras Algorithm

#include <stdio.h>
#include <stdlib.h>

int m[10][10]={{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},


{ 0, 0,19, 0, 0, 0, 0,28, 0, 0},
{ 0,19, 0,26, 0, 0, 0,28,26,29},
{ 0, 0,26, 0,11, 0, 0, 0, 0,37},
{ 0, 0, 0,11, 0,20, 0, 0, 0, 0},
{ 0, 0, 0, 0,20, 0,20, 0,21,17},
{ 0, 0, 0, 0, 0,20, 0,23,18, 0},
{ 0,28,28, 0, 0, 0,23, 0,30, 0},
{ 0, 0,26, 0, 0,21,18,30, 0,32},
{ 0, 0,29,37, 0,17, 0, 0,32, 0}};

int start=5, target=1, size=10;

void step1(int s, int m[size][size]);


void step2(int m[size][size]);
void step3(int m[size][size]);
void step4(int m[size][size]);
void step5(int s, int t, int m[size][size]);
int smallestworkingvalue(int m[size][size]);
void outm(int m[size][size]);

int main()
{int i;
step1(start,m);
step2(m);
step3(m);
step4(m);
outm(m);
step5(start,target,m);
return 0;}

void step1(int s, int m[size][size])


{int i;
for (i=1;i<size;++i) m[i][0]=m[i][s];
m[s][0]=-1;};

void step2(int m[size][size])


{int i,next,dum;
next=smallestworkingvalue(m);
m[0][next]=m[next][0];
m[next][0]=-1;
for(i=1;i<size;++i) if((m[i][next]>0)&&(m[i][0]>-1))
{dum=m[0][next]+m[i][next];
if(m[i][0]==0) m[i][0]=dum;
else if(dum<m[i][0]) m[i][0]=dum;}};

void step3(int m[size][size])


{int i;
for(i=1;i<size-2;++i) step2(m);}

void step4(int m[size][size])


{int i;
for(i=1;i<size;++i) m[i][0]=m[0][i];}
C Program Listings 3.4 Dijkstras Algorithm 131

void step5(int s, int t, int m[size][size])


{int i,next=t,dum;
while(next!=s)
{for(i=1;i<size;++i)
{dum=m[0][next]-m[i][next];
if((dum==m[i][0])&&(next!=i))
{next=i;
printf("%3d",next);
printf("\n");}}}}

int smallestworkingvalue(int m[size][size])


{int i,j,min=100;
for(i=1;i<size;++i) if((m[i][0]>0)&&(m[i][0]<min))
{min=m[i][0];
j=i;}
return j;};

void outm(int m[size][size])


{int i,j;
for (i=0;i<size;++i)
{for (j=0;j<size;++j) printf("%3d",m[i][j]);
printf("\n");}
printf("\n");}
Appendix D C Program Listings 132

5.4 The Simplex Algorithm

#include <stdio.h>
#include <stdlib.h>

int pr, pc, sr=6, sc=5, variables[3][3];

float data[6][5]={{ 0, 0, 0, 0, 0},


{ 0, 1, 2,10, 58},
{ 0,10, 1, 1, 47},
{ 0, 2, 5, 1, 23},
{ 0, 5, 1, 5, 47},
{ 0,-3,-5,-4, 0}};

void simplex(float d[sr][sc]);


void setuptableau(float d[sr][sc]);
void makenewtableau(float d[sr][sc]);
int pivotcolumn(float d[sr][sc]);
int pivotrow(float d[sr][sc]);
void workingrow(float d[sr][sc]);
int checkindicators(float d[sr][sc]);
void swap(float d1[sr][sc], float d2[sr][sc]);
void output(float d[sr][sc]);
void outputvariables(float d[sr][sc]);

int main()
{simplex(data);
output(data);
outputvariables(data);
return 0;}

void simplex(float d[sr][sc])


{int ci=1, count=0, var=sc-2;
while(ci==1)
{pr=0; pc=0;
setuptableau(d);
output(d);
if(count<=var)
{++count;
variables[count][1]=pr;
variables[count][2]=pc;}
makenewtableau(d);
ci=checkindicators(d);}}

void setuptableau(float d[sr][sc])


{pc=pivotcolumn(d);
if(pc>0)
{pr=pivotrow(d); workingrow(d);}}

void makenewtableau(float d1[sr][sc])


{int i,j,r=sr-1,c=sc-1; float d2[sr][sc];
for(i=0;i<sr;++i)
for(j=0;j<sc;++j) d2[i][j]=0;
for(i=1;i<sr;++i)
for(j=1;j<sc;++j) d2[i][j]=d1[i][j]-d1[0][j]*d1[i][pc];
for(i=1;i<sr;++i) d2[i][pc]=-d1[i][pc]/d1[pr][pc];
for(j=1;j<sc;++j) d2[pr][j]=d1[0][j];
swap(d1,d2);}
C Program Listings 3.4 Dijkstras Algorithm 133

int pivotcolumn(float d[sr][sc])


{int j,r=sr-1,c=sc-1,pc,min=0;
for(j=1;j<c;++j) if(d[r][j]<min)
{min=d[r][j]; pc=j;}
return pc;}

int pivotrow(float d[sr][sc])


{int i,r=sr-1,c=sc-1,pr,min=1000;
for(i=1;i<=r;++i) if(d[i][pc]>0) d[i][0]=d[i][c]/d[i][pc];
for(i=1;i<=r;++i) if(d[i][0]>0 && d[i][0]<min)
{min=d[i][0]; pr=i;}
return pr;}

void workingrow(float d[sr][sc])


{int j,c=sc-1;
for(j=1;j<=c;++j) d[0][j]=d[pr][j]/d[pr][pc];
d[0][pc]=1/d[pr][pc];}

int checkindicators(float d[sr][sc])


{int j, tag=0; float min=0;
for(j=1;j<sc-1;++j) if(d[sr-1][j]<min) min=d[sr-1][j];
if(min<0) tag=1;
return tag;}

void swap(float d1[sr][sc], float d2[sr][sc])


{int i,j;
for(i=0;i<=sr;++i)
for(j=0;j<=sc;++j)
{d1[i][j]=d2[i][j]; d2[i][j]=0;}}

void output(float d[sr][sc])


{int i,j;
for(i=0;i<sr;++i)
{for(j=0;j<sc;++j) printf("%10.2f",d[i][j]);
printf("\n");}
printf("\n");}

void outputvariables(float d[sr][sc])


{int i,j;
for(i=1;i<=sc-2;++i)
{switch(variables[i][2])
{case 1: printf("x= "); break;
case 2: printf("y= "); break;
case 3: printf("z= "); break;}
printf("%10.2f\n\n",d[variables[i][1]][sc-1]);}
printf("Max= %10.2f\n\n",d[sr-1][sc-1]);}

Вам также может понравиться