VII. Applications: sciences, economics and optimization

52. Graphs, networks and logistics

Problems on graphs and networks are on the home screen in the "Applications" section, field Logistics and networks: the shortest path (Dijkstra's, Bellman–Ford and Floyd–Warshall algorithms), the minimum spanning tree (Kruskal, Prim), the maximum flow and minimum cut, currency arbitrage as a search for a negative cycle and a project's critical path (CPM, PERT).

You enter the graph in a multi-line field, one edge per line – A-B 4 is an undirected edge of weight 4, A->B 4 a directed one, for a flow A->B 3/7 a given flow and capacity; without a number an edge has weight 1. Instead of the edges you can paste a matrix of weights row by row (-, ∞ or 0 means no edge), optionally with a line of the node names above it. Nodes can be named by a letter or a word (PHA, Boston). A project's activities are given by their name, duration and predecessors (D 5 B C), for PERT with three estimates (B 3/4/11 A).

For an arbitrage you can choose today's rate table of the ČNB or the ECB instead of your own rates – just list the currencies (CZK EUR USD GBP). With one table an arbitrage never comes out (the steps show why); with both tables at once each pair of currencies takes the better rate and a cycle above 1 can come out. The “Fill in the rates to edit” button puts one table's rates into the Exchange rates field – edit them, for example, by an exchange office's buying and selling rates.

The steps show a table of the iterations as in textbooks – Dijkstra's permanent and temporary labels, the Bellman–Ford passes, the Floyd–Warshall matrix after each node, Kruskal's sorted edges, the augmenting paths of a flow, the CPM forward and backward pass. Below the result is the graph, which you go through step by step with the arrows: red is the step's change, blue what is already decided, green the result.

Examples

The shortest path by Dijkstra's algorithm.
Input: shortestpath(A-B 4; A-C 2; B-C 1; B-D 5; C-D 8; C-E 10; D-E 2; D-F 6; E-F 2; from=A; to=F; method=dijkstra)
shortestpath(A-B 4; A-C 2; B-C 1; B-D 5; C-D 8; C-E 10; D-E 2; D-F 6; E-F 2; from=A; to=F; method=dijkstra)
shortestpath(A-B 4; A-C 2; B-C 1; B-D 5; C-D 8; C-E 10; D-E 2; D-F 6; E-F 2; from=A; to=F; method=dijkstra)shortestpath(A-B 4; A-C 2; B-C 1; B-D 5; C-D 8; C-E 10; D-E 2; D-F 6; E-F 2; from=A; to=F; method=dijkstra)
A negative edge weight – the Bellman–Ford algorithm.
Input: shortestpath(S->A 4; S->B 5; B->A -2; A->C 3; C->D 2; B->D 6; from=S; to=D; method=bellmanford)
shortestpath(S->A 4; S->B 5; B->A -2; A->C 3; C->D 2; B->D 6; from=S; to=D; method=bellmanford)
shortestpath(S->A 4; S->B 5; B->A -2; A->C 3; C->D 2; B->D 6; from=S; to=D; method=bellmanford)shortestpath(S->A 4; S->B 5; B->A -2; A->C 3; C->D 2; B->D 6; from=S; to=D; method=bellmanford)
The distances between all pairs of nodes.
Input: shortestpath(A->B 3; B->C -1; A->C 4; C->A 2; C->D 2; D->B 1; method=floyd)
shortestpath(A->B 3; B->C -1; A->C 4; C->A 2; C->D 2; D->B 1; method=floyd)
shortestpath(A->B 3; B->C -1; A->C 4; C->A 2; C->D 2; D->B 1; method=floyd)shortestpath(A->B 3; B->C -1; A->C 4; C->A 2; C->D 2; D->B 1; method=floyd)
The minimum spanning tree by Kruskal's algorithm.
Input: spanningtree(A-B 7; A-D 5; B-C 8; B-D 9; B-E 7; C-E 5; D-E 15; D-F 6; E-F 8; E-G 9; F-G 11; method=kruskal)
spanningtree(A-B 7; A-D 5; B-C 8; B-D 9; B-E 7; C-E 5; D-E 15; D-F 6; E-F 8; E-G 9; F-G 11; method=kruskal)
spanningtree(A-B 7; A-D 5; B-C 8; B-D 9; B-E 7; C-E 5; D-E 15; D-F 6; E-F 8; E-G 9; F-G 11; method=kruskal)spanningtree(A-B 7; A-D 5; B-C 8; B-D 9; B-E 7; C-E 5; D-E 15; D-F 6; E-F 8; E-G 9; F-G 11; method=kruskal)
The shortest network joining regional capitals by Prim's algorithm.
Input: spanningtree(PHA-PLZ 93; PHA-LBC 110; PHA-HK 115; PHA-JI 128; PHA-BRN 205; LBC-HK 100; HK-OL 140; JI-BRN 88; BRN-OL 78; OL-OVA 100; BRN-OVA 170; PLZ-JI 210; start=PHA; method=prim)
spanningtree(PHA-PLZ 93; PHA-LBC 110; PHA-HK 115; PHA-JI 128; PHA-BRN 205; LBC-HK 100; HK-OL 140; JI-BRN 88; BRN-OL 78; OL-OVA 100; BRN-OVA 170; PLZ-JI 210; start=PHA; method=prim)
spanningtree(PHA-PLZ 93; PHA-LBC 110; PHA-HK 115; PHA-JI 128; PHA-BRN 205; LBC-HK 100; HK-OL 140; JI-BRN 88; BRN-OL 78; OL-OVA 100; BRN-OVA 170; PLZ-JI 210; start=PHA; method=prim)spanningtree(PHA-PLZ 93; PHA-LBC 110; PHA-HK 115; PHA-JI 128; PHA-BRN 205; LBC-HK 100; HK-OL 140; JI-BRN 88; BRN-OL 78; OL-OVA 100; BRN-OVA 170; PLZ-JI 210; start=PHA; method=prim)
The maximum flow and the minimum cut.
Input: maxflow(S->A 16; S->B 13; B->A 4; A->C 12; C->B 9; B->D 14; D->C 7; C->T 20; D->T 4; from=S; to=T)
maxflow(S->A 16; S->B 13; B->A 4; A->C 12; C->B 9; B->D 14; D->C 7; C->T 20; D->T 4; from=S; to=T)
maxflow(S->A 16; S->B 13; B->A 4; A->C 12; C->B 9; B->D 14; D->C 7; C->T 20; D->T 4; from=S; to=T)maxflow(S->A 16; S->B 13; B->A 4; A->C 12; C->B 9; B->D 14; D->C 7; C->T 20; D->T 4; from=S; to=T)
A profitable cycle of exchanges of three currencies.
Input: arbitrage(USD->EUR 0.9; EUR->GBP 0.8; GBP->USD 1.4; EUR->USD 1.1; GBP->EUR 1.24; USD->GBP 0.7)
arbitrage(USD->EUR 0.9; EUR->GBP 0.8; GBP->USD 1.4; EUR->USD 1.1; GBP->EUR 1.24; USD->GBP 0.7)
arbitrage(USD->EUR 0.9; EUR->GBP 0.8; GBP->USD 1.4; EUR->USD 1.1; GBP->EUR 1.24; USD->GBP 0.7)arbitrage(USD->EUR 0.9; EUR->GBP 0.8; GBP->USD 1.4; EUR->USD 1.1; GBP->EUR 1.24; USD->GBP 0.7)
Consistent rates as a matrix – no arbitrage.
Input: arbitrage(CZK EUR USD; 1 0.04 0.0432; 25 1 1.08; 23.148148 0.925926 1)
arbitrage(CZK EUR USD; 1 0.04 0.0432; 25 1 1.08; 23.148148 0.925926 1)
arbitrage(CZK EUR USD; 1 0.04 0.0432; 25 1 1.08; 23.148148 0.925926 1)arbitrage(CZK EUR USD; 1 0.04 0.0432; 25 1 1.08; 23.148148 0.925926 1)
The rates of one table (ČNB) – never an arbitrage.
Input: arbitrage(source=cnb; CZK EUR USD GBP)
arbitrage(source=cnb; CZK EUR USD GBP)
arbitrage(source=cnb; CZK EUR USD GBP)arbitrage(source=cnb; CZK EUR USD GBP)
The ČNB's and the ECB's rates together – a tiny “gain” between reference rates.
Input: arbitrage(source=cnb+ecb; CZK EUR USD GBP CHF)
arbitrage(source=cnb+ecb; CZK EUR USD GBP CHF)
arbitrage(source=cnb+ecb; CZK EUR USD GBP CHF)arbitrage(source=cnb+ecb; CZK EUR USD GBP CHF)
The critical path and the activities' slack.
Input: cpm(A 3; B 4 A; C 2 A; D 5 B C; E 3 C; F 2 D E)
cpm(A 3; B 4 A; C 2 A; D 5 B C; E 3 C; F 2 D E)
cpm(A 3; B 4 A; C 2 A; D 5 B C; E 3 C; F 2 D E)cpm(A 3; B 4 A; C 2 A; D 5 B C; E 3 C; F 2 D E)
PERT – the probability of finishing the project by a deadline.
Input: cpm(A 2/3/4; B 3/4/11 A; C 1/2/3 A; D 4/5/6 B C; T=15)
cpm(A 2/3/4; B 3/4/11 A; C 1/2/3 A; D 4/5/6 B C; T=15)
cpm(A 2/3/4; B 3/4/11 A; C 1/2/3 A; D 4/5/6 B C; T=15)cpm(A 2/3/4; B 3/4/11 A; C 1/2/3 A; D 4/5/6 B C; T=15)

Known limitations