Tải bản đầy đủ (.pdf) (10 trang)

Tài liệu Thuật toán Algorithms (Phần 45) ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (149.02 KB, 10 trang )

33. Network Flow
q
Weighted directed graphs are useful models for several types of applica-
tions involving commodities flowing through an interconnected network.
Consider, for example, a network of oil pipes of varying sizes, interconnected
in complex ways, with switches controlling the direction of flow at junctions.
Suppose further that the network has a single source (say, an oil field) and a
single destination (say, a large refinery) to which all of the pipes ultimately
connect. What switch settings will maximize the amount of oil flowing from
source to destination? Complex interactions involving material flow at junc-
tions make this problem, called the problem, a nontrivial problem
to solve.
This same general setup can be used to describe traffic flowing along
highways, materials flowing through factories, etc. Many different versions
of the problem have been studied, corresponding to many different practical
situations where it has been applied. There is clearly strong motivation to
find an efficient algorithm for these problems.
This type of problem lies at the interface between computer science
and the field of operations research. Operations researchers are generally
concerned with mathematical modeling of complex systems for the purpose
of (preferably optimal) decision-making. Network flow is a typical example
of an operations research problem; we’ll briefly touch upon some others in
Chapters 37-40.
As we’ll see, the modeling can lead to complex mathematical equations
that require computers for solution. For example, the classical solution to the
network flow problem given below is closely related to the graph algorithms
that we have been examining. But this problem is one which is still actively
being studied: unlike many of the problems that we’ve looked at, the “best”
solution has not yet been found and good new algorithms are still being
discovered.
433


434
33
The Network Flow Problem
Consider the following rather idealized drawing of a small network of oil pipes:
The pipes are of fixed capacity proportional to their size and oil can
in them only in the direction indicated (perhaps they run downhill or have
unidirectional pumps Furthermore, switches at each junction control how
much of the oil goes in each direction. No matter how the switches are set,
the system reaches a state of equilibrium when the amount of oil flowing into
the system at the left is equal to the amount flowing out of the system at the
right (this is the quantity that we want to maximize) and when the amount
of oil flowing in at each junction is equal to the amount of oil flowing out. We
measure both flow and pipe capacity in terms of integral “units” (say, gallons
per second).
It is not immediately obvious that the switch settings can really affect the
total flow: the following example will illustrate that they can. First, suppose
that all switches are open so that the two diagonal pipes and the top and
bottom pipes are full. This gives the following configuration:
The total into and out of the network in this case is less than half the
capacity of the input pipe, only slightly more than half the capacity of the
output pipe. Now suppose that the upward diagonal pipe is shut off. This
shuts flow equal to its capacity out of the bottom, and the top is unaffected
because there’s room to replace its flow from the input pipe; thus we have:
NETWORK FLOW 435
The total flow into and out of the network is increased to substantially.
This situation can obviously be modeled using a directed graph, and it
turns out that the programs that we have studied can apply. Define a network
as a weighted directed graph with two distinguished vertices: one with no
edges pointing in (the source); one with no edges pointing out (the sink). The
weights on the edges, which we assume to be non-negative, are called the edge

capacities. Now, a flow is defined as another set of weights on the edges such
that the on each edge is equal to or less than the capacity, and the flow
into each vertex is equal to the flow out of that vertex. The value of the flow
is the flow into the source (or out of the sink). The network problem is
to find a flow with maximum value for a given network.
Networks can obviously be represented using either the adjacency matrix
or adjacency list representations that we have used for graphs in previous
chapters. Instead of a single weight, two weights are associated with each
edge, the size and the Aow. These could be represented as two fields in an
adjacency list node, as two matrices in the adjacency matrix representation,
or as two fields within a single record in either representation. Even though
networks are directed graphs, the algorithms that we’ll be examining need
to traverse edges in the “wrong” direction, so we use an undirected graph
representation: if there is an edge from x to y with size s and flow we also
keep an edge from y to x with size and flow -f. In an adjacency list
representation, it is necessary to maintain links connecting the two list nodes
which represent each edge, so that when we change the flow in one we can
update it in the other.
Ford-Fulkerson Method
The classical approach to the network flow problem was developed by L. R.
Ford and D. R. Fulkerson in 1962. They gave a method to improve any legal
flow (except, of course, the maximum). Starting with a zero flow, we apply
the method repeatedly; as long as the method can be applied, it produces an
increased if it can’t be applied, the maximum flow has been found.
Consider any directed path through the network (from source to sink).
Clearly, the flow can be increased by at least the smallest amount of unused
capacity on any edge on the path, by increasing the flow in all edges on the
436
33
path by that amount. In our example, this rule could be applied along the

path ADEBCF:
then along the path ABCDEF:
C
F
E
and then along the path ABCF:
Now all directed paths through the network have at least one edge which
is filled to capacity. But there is another way to increase the flow: we can
consider arbitrary paths through the network which can contain edges which
point the “wrong way” (from sink to source along the path). The flow can
NETWORK FLOW
437
be increased along such a path by increasing the flow on edges from source
to sink and decreasing the flow on edges from sink to source by the same
amount. To simplify terminology, we’ll call edges which flow from source to
sink along a particular path forward edges and edges which flow from sink to
source backward edges. For example, the in the network above can be
increased by 2 along the path ABEF.
This corresponds to shutting off the oil on the pipe from E to B; this allows 2
units to be redirected from E to F without losing any flow at the other end,
because the 2 units which used to come from E to B can be replaced by 2
units from A.
Notice that the amount by which the flow can be increased is limited by
the minimum of the unused capacities in the forward edges and the minimum
of the in the backward edges. Put another way, in the new flow, at
least one of the forward edges along the path becomes full or at least one of
the backward edges along the path becomes empty. Furthermore, the flow
can’t be increased on any path containing a full forward edge or an empty
backward edge.
The paragraph above gives a method for increasing the flow on any

network, provided that a path with no full forward edges or empty backward
edges can be found. The crux of the Ford-Fulkerson method is the observation
that if no such path can be found then the flow is maximal. The proof of
this fact goes as follows: if every path from the source to the sink has a full
forward edge or an empty backward edge, then go through the graph and
identify the first such edge on every path. This set of edges cuts the graph in
two parts, as shown in the diagram below for our example.

×