## Solving Flows in Network Formats

Flows in Networks

Flows in Networks are used in problems such as traffic on roads and oil in pipelines. This graph is called a digraph where the arcs have a direction denoted by an arrow. The weights on the arcs are the capacities of the arc and show the limit of the flow that can be passes down that arc.

Consider a network with N nodes. Node 1 will be the source (all flow will come from node 1). Node N will be the sink (all flow will be to node N). For all the other nodes we will assume conservation of flow, i.e. no flow is lost.

The object is to maximise the total flow from node 1 to node N.

Intuitive Approach

Step 1. Find any flow-augmenting path from source to sink such that each of the arcs has a positive flow capacity.

Step 2. The arc with the smallest flow capacity limits the flow along the path. Assign (i.e. ‘send’) this flow (f) and reduce the capacities of the arcs on this path by f.

Step 3. Repeat steps 1 and 2 until no suitable path can be found.

e.g. Initial Flow-augmenting Path 1 ® 2 ® 4 ® 6 f1 = 4

Second Flow-augmenting Path 1 ® 3 ® 5 ® 6 f2 = 4

Third Flow-augmenting Path 1® 3 ® 4 ® 6 f3 = 1

Maximal flow = 4 + 4 + 1 = 9 (this is obviously maximal since all the arcs from the source are saturated).

It’s often difficult to see useful applications for these diagrams but they’re actually very useful in real life.   For instance creating such structure would have definitely helped programmers and designers who created the Netflix algorithm which creates your suggested option screens.  Using the data from previous selections it can use flow format theorems to create a likely pattern of other shows.  Just look carefully how the structure works, if you haven’t got Netflix access then just use this trial to watch the US version!

If however our initial flow-augmenting path was 1® 3 ® 4 ® 6, f1 = 5, then the network would be stuck but not at maximal flow. We must therefore build in a refinement to stop us getting stuck. The idea is to allow a ‘fictitious’ back flow in the wrong direction along an arc in order to cancel out all, or part, of a previously assigned flow. e.g. in the above stuck network we could send up to 5 from node 4 to node 3 and still end up with a real flow from node 3 to node 4. So the second path is 1 ® 2 ® 4 ® 3 ® 5® 6, f2 = 4. Now continuing as before we could again have a flow of 9. (The real flow along (3,4) is 1 from node 3 to node 4).

A different notation can be used called the labelling procedure and appears to be preferred by the examination board.  They are commonly used in network diagrams too if you want to illustrate the flow from something like a e-commerce server to an ATC proxy, or other network devices. In this method when the capacity is reduced after flow has been assigned a back flow in the opposite direction is shown. This can help in two ways; one is by showing the total flow after a few iterations along a certain arc, even if it is the “wrong” direction. The second is it may help with the fictitious flows mentioned above.

## Linear Programming – Graphical Methods

Graphical Solution

When the L.P. problem involves only two decision variables its solution may be obtained by graphical means. Consider the earlier example.

The shaded region OABCD contains all the points (x,y) which satisfy the constraints. The points form the set of feasible solutions. Clearly there are an infinite number of such points. We require that one which gives a maximum value of z. From z = 8x + 5y with rearranging we get y = – 8/5x + z/5, for different values of z this equation represents a family of straight lines each with a gradient -8/5. Z will be greatest when the family of parallel lines leave the feasible region i.e. at C (2.5,4.5). Point C’s values can be found using simultaneous equations of the lines which cross at that point. 3x +y = 12 and x + y = 7. By subtraction 2x = 5 \ x = 2.5 y = 4.5.

Hence Zmax = 8(2.5) + 5(4.5) = 42.5.

Integer Solutions

In the above problem we are producing bicycles and trucks. Yet our graphical solution gave us x being 2.5 and y being 4.5. Not very sensible, it is difficult to make half a truck! Some problems obviously must be integer solutions. In these cases we must “test” integer points within the feasible region. The integer point will usually be near the location were the family of profit lines left the feasible region. In our case near point C. Integer points near C yet still within the feasible region are (2,4), (2,5), (3,3). Putting these points in our objective function z = 8x + 5y gives z to be 36, 41 and 39 respectively. Hence our integer solution is a point (2,5) there the profit is £41.

If you find these difficult to follow there are lots more examples online.  Many of the best lessons used to come from the Open University Math’s models which were broadcast on the BBC.  I’m not sure if you can still find them as they are quite old now, but it might be worth checking the BBC’s archive – BBC iPlayer.   If you’re not able to access the site because of location restrictions then this site can help to access via a BBC iPlayer DNS service.

The Simplex Method

The Simplex Method is an algebraic rather than graphical approach to solving Liner Programming problems. The advantage of the Simplex Method is that it can cope with grater than 2 variables which cannot be solved graphically.

The first step is to state the general Linear Programming problem in a standard form.  We require a L.P. problem to be put into a form in which

1. the object function is to be maximised e.g. Maximise z =
2. all the variables are to be non-negative i.e. zero or positive because you cannot have negative amounts

III. all the constraints are to be equations (except for the non-negativity constraints) i.e. no >, , <,  but only =.

1. the right hand side constant of the constraints is positive or zero e.g. not x = 2 but x = 2 i.e. the RHS is +ve.

For further help see the below resources below:

Additional: BBC iPlayer Access – Free Trial Offer

## Real Life Maths – Bin Packing

Bin Packing

We have to fit a number of boxes of the same width and depth but different height into a rack. The rack is the same depth, divided into slots of the same width and of a fixed height.  There are 11 boxes, A to K, with heights as below

A – 8, B – 7, C – 4, D – 9, E – 6, F – 9, G – 5, H – 5, I – 6, J – 7, K – 9

The rack is 15 units high and we stack the boxes one on top of each other using as few slots as possible.

Similar problems might be cutting lengths of wood from standard lengths, or fitting vehicles into lanes on ferries. In each case we are trying to make the best possible use of the space available and avoid waste ( in the form of unused space above, off-cuts of wood and unfilled lanes).

These problems can be solved by trial and error, but it is worth looking for a more systematic approach (an algorithm).

For the Bin Packing problem there is no known algorithm that will always produce an optimal solution. There are many algorithms that find a good solution – known as heuristic algorithms.

Three are

I. Full-bin algorithm

Look for combinations of boxes to fill bins. Pack these boxes. For the rest place the next box to the next available space.

II. First-fit algorithm

Taking the boxes in the order listed, place the next box to be packed in the first available space.

III. First-fit decreasing algorithm

Reorder the boxes in decreasing size. Then apply the first-fit algorithm to this reordered list.

A method you could use for the first-fit or first-fit decreasing algorithm is:

Define a list of numbers, P, for the heights of the packages (ordered if necessary)

For the bin-packing example, P = {8,7,4,9,6,9,5,5,6,7,8}

Define a second set of numbers, B, for the space remaining in the bins. At the very worst this list will need to be as long as the list of packages.

For example B = {15,15,15,15,……}

Step 1. Take the first entry in P.

Step 2. Is it less than or equal to the first entry in B? Yes – Step 4

No – Step 3

Step 3. Go to the next B entry. Is it less than or Yes – Step 4

equal to this entry in B? No – Step 3

Step 4. Reduce the B entry by this amount.

Step 5 Any more entries in P? Yes – Take the next entry go to Step 2

No – Stop

The result in applying this algorithm is

P = {8, 7, 4, 9, 6, 9, 5, 5, 6, 7, 8}

B = {15,15,15,15,15,15,……}

7, 11, 9, 10, 9, 7

0, 2, 0, 5, 2

You can see how many bins have been used and how much free space there is in each and what packages are in each bin.

For more real world maths problems, you’ll find many online.  One of our favorite resources is the BBC Bitesize site, which at the time of writing has lots of these sorts of problems and other maths resources.   Most of them should be available internationally however much of the website is geo-restricted i.e you’ll not be able to access directly with US residential IPs. There is a solution to this though by hiding your real location using a proxy or VPN you can bypass these sorts of blocks – there’s an example in this post about downloading from the BBC abroad.

## Algorithm Primer

An algorithm is simply a sequence of precise instructions to solve a problem.  Precise means that there should be no ambiguity to any instruction or which instruction is next.

An example of an algorithm is Zeller’s Algorithm which is used to work out which day of the week a date is

Zeller’s Algorithm                                        Example      15/05/1991

Let Day number = D                                                        D = 15

Month number = M                                                         M = 5

and Year = Y                                                                  Y = 1991

If M is 1 or 2 add 12 to M and subtract 1 from Y.

Let C be the first 2 digits of Y                                          C = 19

and Y’ be the last 2 digits of Y                                        Y’ = 91

Add together the integer parts of

(2.6M5.39),(Y’4) and (C4), then                             7+22+4+15

add on D and Y’ and subtract 2C                                    +9138=101

{Note: Integer part of 2.3 = 2 and 6.7 = 6

1.7= 2 and 3.1 = 4}

Find the remainder when this quantity is divided by 7        1017=14r3

If the remainder is 0 – Sunday, 1 – Monday etc.

Communicating an Algorithm

We can communicate algorithms by ordinary language as above, by pseudo-code (stylised English), computer languages or flow charts.

Let’s take the example of finding the real roots of the quadratic equation

Ax2 + Bx + C = 0  (assume A0)

Using the equation

Using pseudo-code we could write

calc ; note its value;

if this is negative    [no real roots – stop]

else                       [calc root of value

calc (B+root) (2A)

calc (Broot) (2A) – stop].

For programmable calculators we could write

Casio Graphic                                                       Texas TI-81

?A                                                                     :Input A

?B                                                                     :Input B

?C                                                                     :Input C

D                                                    : D

(B+D) (2A)                                                     : (B+D) (2A)

:Pause

(BD) (2A)                                                     (BD) (2A)

{Note: These assume real roots}

In BBC Basic we could write

10      Input “A,B,C”;A,B,C

20      D=B*B4*A*C

30      IF D<0 THEN PRINT “No real roots”: STOP

40      PRINT (B+SQR(D))/(2*A)

50      PRINT (BSQR(D))/(2*A)

Using a flow chart we diagrammatically represent the sequence of steps in an algorithm.

Three types of boxes are common

1. The start stop box
2. The instruction box
3. The question box

Russian Peasant’s Algorithm

Example

Write down side by side the two numbers to be multiplied           163    24

Repeatedly [beneath the LHS number, write down the number    81      48

which is half the number above, ignoring remainders, beneath     40      96

the RHS number write down the number doubled] until you        20     192

reach 1 on the LHS.                                                                  10     384

5       768

2     1536

1     3072

Delete those rows where the number in the LHS is a multiple

of 2.  Add up the numbers left on the RHS                                163      24

81      48

40      96

20     192

10     384

5       768

2     1536

1     3072

This number is the answer                                                                3912

There’s a great demonstration of the Russian Peasants algorithm on the BBC learning websites, and if you’re lucky a couple of explanations on the BBC iPlayer archive.  For many maths lessons for children and adult learners it’s a great resource.  If you’re having trouble accessing from outside the UK then this BBC iPlayer DNS option works great and allows people to access all UK media sites without restrictions.

Another alternative is to use a resource which supplies residential IP addresses, although these aren’t specifically designed for streaming video they work pretty well.  There’s an example of them in this post ATC proxies explained be aware that they can be quite expensive though,