Sie sind auf Seite 1von 8

Algorithm Diniz

Formulation of the problem


Given a network, ie, directed graph in which each edge
is assigned
bandwidth
, and identified two peaks - the source and drain .
It locates in the network flow

from the source to the drain maximum.

A bit of history
This algorithm was published by the Soviet (Israeli) scientist Efim Diniz (Yefim Dinic,
sometimes written as "Dinitz") in 1970, ie, even two years before the publication of
the Edmonds-Karp algorithm (though both algorithms have been independently
discovered in 1968).
In addition, it should be noted that some simplification of the algorithm were
produced Ewen Shimon (Shimon Even ) and his student Alon Itai (Alon Itai ) in 1979.
It is thanks to their ability to get a modern look: they are used to the idea of the
concept of blocking flows Diniz Alexander Karzanov (Alexander Karzanov, 1974),
and reformulated algorithm to the combination of bypass in width and in depth, which
now presents this algorithm anywhere.
Development of ideas in relation to the flow algorithms are extremely interesting to
consider, given the "iron curtain" of those years between the USSR and the
West. It is seen as sometimes similar ideas appeared about the same time (as in the
case of the algorithm Diniz and Edmonds-Karp algorithm), however, while having
varying degrees of success (the algorithm Diniz one order of magnitude
faster); sometimes, on the contrary, the emergence of the idea to one side "curtain"
ahead of a similar move on the other side for more than a decade (as an algorithm
Karzanov push in 1974, and the algorithm of Goldberg (Goldberg) push in 1985).

Necessary definition
We introduce three essential definitions (each of them is independent of the others),
which can then be used in the algorithm Diniz.
Residual network
in which each edge
two edges:

towards network and some flow therein is called a network


with a capacity
and stream
correspond to the

with bandwidth

with bandwidth
It should be noted that with this definition in the residual network may appear
multiple edges: if the original network were as a rib
and
.
The remaining edge can be intuitively understood as a measure of how much more
you can increase the flow along some edges. In fact, if an edge
with
bandwidth
stream flows
, it potentially can miss thereon more
units

of flow and in the opposite direction can be skipped to


canceling flow in the initial direction.

flow units, which means

Blocks the flow in the network is called a stream that any path from the source to
the drain contains rich edge of this stream. In other words, the network not find such
a path from the source to the drain along which can freely increase the flow.
Blocking the flow is not necessarily maximal. Ford-Fulkerson theorem says that the
flow will be maximized if and only if a network not find residual
paths; in
blocking the thread says nothing about the existence of the path of the edges that
appear in the residual network.
The layered network for this network is constructed as follows. First the length of
the shortest paths from source to all the other vertices; we call the level of
the top of its distance from the source. Then, in a layered network includes all the
edges
of the original network, which lead from one level to any other, later, a
level that is
(why in this case the difference between the
distances can not exceed unity, it follows from the properties of the shortest
distances). Thus, all edges are removed, located entirely within the levels as well as
ribs, leading back to the previous levels.
Obviously, a layered network is acyclic. In addition, any
network is the shortest path to the source network.

path in the layered

Build a layered network on the network is easy: it is necessary to start the tour in
width at the edges of the network, thereby considering each vertex value
, and
then make into a layered network is suitable ribs.
Note. The term "layered network" in Russian literature is not used; this design is
usually referred to simply as "auxiliary graph." However, English is commonly used,
the term "layered network".

Algorithm
Chart
The algorithm consists of several phases . In each phase, first built the residual
network, and then in relation to it being built layered network (bypass wide), and it
sought an arbitrary blocking the flow. Found blocking the flow is added to the current
thread, and that the next iteration ends.
This algorithm is similar to the Edmonds-Karp algorithm, but the main difference can
be understood as follows: for each iteration of the flow does not increase along one
of the shortest
path, and along the whole set of ways (after all these ways are
the way to block the flow of a layered network).

The correctness of the algorithm


We show that if the algorithm is completed, the output he gets the flow is maximum.
In fact, suppose that at some point in a layered network built for the residual network,
we could not find a blocking flow. This means that the stock does not achievable in
a layered network of the source . But as a layered network contains all the shortest
paths from the source in the residual network, which in turn means that there is no

residual network path from the source to the drain. Consequently, applying the
theorem of Ford-Fulkerson, we see that the current thread is in fact a maximum.

Estimating the number of phases


We show that the algorithm always performs Diniz less phases . To this end, we
prove two lemmas:
Lemma 1 . The shortest distance from the source to each vertex is not reduced with
the implementation of each iteration, that is,

where the subscript indicates the number of phases, which are taken to the values of
these variables.
Proof . We fix an arbitrary phase and arbitrary vertex and consider any
shortest
path in the network
network, taken before executing
is
.

(remember, so we denote the residual


th phase). Obviously, the length of the path

Note that in the residual network


may include only the edges of
, as well as
the edges, the edges of the reverse
(this follows from the definition of the
remaining networks). We consider two cases:

The path

contains only the edges of

is greater than or equal


(because
shortest path), which means that the inequality.

Way
the edge

. Then, of course, the path length


, by definition - the length of the

contains at least one edge that is not on

(but the reverse some of

). Consider the first such edge; let it be an edge

We can apply the lemma to the top , because it falls under the first case; So, we
get the inequality
.
Now, note that since the rib
appeared in the residual network only after th
phase, it follows that along the edge
was further passed some
thread;therefore, the edge
laminated owned network before -th phase and,
therefore
. We take into account that according to the
property of shortest paths
, and combining this with
the two previous inequalities, we obtain:

Now we can apply the same reasoning to the remaining path to (ie that each
inverted rib adds to
a minimum of two), and as a result we obtain the required
inequality.

Lemma 2 . The distance between the source and drain strictly increases after each
phase of the algorithm, ie .:

where the prime labeled value obtained in the next phase of the algorithm.
Proof : by contradiction. Suppose that after the current phase was found
that
. Consider the shortest path from the source to the drain; by
hypothesis, its length should remain unchanged. However, the residual net on the
next phase includes only the edges of the remaining network before executing the
current phase or reverse them. Thus, a contradiction: found a
path that does
not contain saturated edges, and has the same length as the shortest path. This path
had to be "locked" blocks the flow of what had happened, and what is a
contradiction, as required.
This lemma can be intuitively understood as follows: on th phase Diniz algorithm
identifies and fills all
the way length .
Since the length of the shortest path from a can not exceed
, then,
consequently, the algorithm performs Diniz maximum
phase .

Search for blocking the flow


To complete the construction of the algorithm Diniz, it is necessary to describe an
algorithm for finding blocking flow in layered network - a key place algorithm.
We consider three possible embodiment search blocking the flow:

Find

the way one by one, until such ways are. The path can be found for

the
bypass in depth and in all these ways to be
(since each way
saturates at least one edge). Summary of the asymptotic behavior of the search will
be blocking the flow

Similarly to the previous idea, but removed during the traversal of the graph in
depth all the "extra" edges, ie, edges along which will not reach the drain.
It is very easy to implement: just remove the edge after we watched it crawled into
the depths (except in the case when we passed along the edge and found a way to
drain). In terms of implementation, it is necessary to simply maintain the adjacency
list of each vertex pointer to the first undeleted edge and increase this point in the
cycle inside the bypass in depth.
We estimate the asymptotic behavior of the solutions. Each round is terminated in
the depth or saturation of at least one edge (if it has reached bypass flow) or moving
forward at least one direction (otherwise). One can understand that one start
crawling in depth from the main program works for
, where - the number
of promotions pointers. Taking into account that all bypass runs deep in the search of
blocking the flow is
where - the number of edges, it blocks the flow of
saturated, the entire ability to block the flow in return for
that, given that
all pointers in the amount passed away
, giving the asymptotic

behavior
. In the worst case, when the blocking flow saturates all edges,
the asymptotic behavior is obtained
; This asymptotic behavior and will be
used on.
We can say that this method of finding blocking flow is extremely effective in the
sense that in the search for ways of enhancing it spends
an average of
operations. Therein lies the difference by an order of efficient algorithms and Diniz
Edmonds-Karp (who is looking for a way of increasing one
).
This method of solution is still simple to implement, but very effective and therefore
most often used in practice.

You can apply special data structures - Dynamic trees Sletora (Sleator) and
Tarjan (Tarjan)). Then each block the flow can be found during the time

Asymptotics
Thus, the entire algorithm runs in Diniz
, if blocking the flow of the method
described above to look for
. The implementation using dynamic trees
Sletora Tarja will work during
.

Single Network
Network unit ("unit network") called such a network in which all the existing
capacities of edges equal to unity, and any node other than the source and drain, or
incoming or outgoing edge only.
This case is important enough, because the problem of finding maximum
matching network is built exactly the unit.
We prove that the algorithm Diniz single network, even in a simple realization (which
works off of an arbitrary graph
) runs during
reaching to the
problem of finding maximum matching one of the best known algorithms - HopcroftKarp algorithm.
First, we note that the above cited ability to block the flow, which is arbitrary networks
running during
, in networks with individual capacities to work for
: due
to the fact that each edge will not be seen more than once.
Second, we estimate the total number of phases that could occur in the case of
single chains.
Suppose that we have produced
the phase algorithm Diniz; then all the way
increasing the length of no more
have been discovered. Let - found the current
flow, and - the desired maximum flow; look at the difference between
them:
. It is a residual flux in the network
. This stream has a
magnitude
, and along each edge is zero or one. It can be decomposed
into a set of
paths from a and possibly cycles. Because the network is
trivial, all these paths can not have common vertices, therefore, given the above, the
total number of vertices in them
can be estimated as:

On the other hand, given that

which means that even after


maximum flow.

we get from here:

the phase algorithm Diniz guaranteed to find the

Consequently, the total number of phases of the algorithm Diniz performed on


individual networks can be assessed as
what we wanted to prove.

Realization
We give two implementations of the algorithm
running on the network,
given the adjacency matrix and adjacency list, respectively.

Implementation of the graphs in the form of co-occurrence


matrices
const int MAXN = ...; //
const int INF = 1000000000; // -
int n, c[MAXN][MAXN], f[MAXN][MAXN], s, t, d[MAXN], ptr[MAXN],
q[MAXN];
bool bfs() {
int qh=0, qt=0;
q[qt++] = s;
memset (d, -1, n * sizeof d[0]);
d[s] = 0;
while (qh < qt) {
int v = q[qh++];
for (int to=0; to<n; ++to)
if (d[to] == -1 && f[v][to] < c[v][to]) {
q[qt++] = to;
d[to] = d[v] + 1;
}
}
return d[t] != -1;
}
int dfs (int v, int flow) {
if (!flow) return 0;
if (v == t) return flow;
for (int & to=ptr[v]; to<n; ++to) {
if (d[to] != d[v] + 1) continue;
int pushed = dfs (to, min (flow, c[v][to] - f[v]
[to]));
if (pushed) {
f[v][to] += pushed;
f[to][v] -= pushed;

return pushed;
}
}
return 0;
}
int dinic() {
int flow = 0;
for (;;) {
if (!bfs()) break;
memset (ptr, 0, n * sizeof ptr[0]);
while (int pushed = dfs (s, INF))
flow += pushed;
}
return flow;
}
The network must be previously read: must be specified variables , , and is also
considered the matrix of capacities
. The main function of the decision which returns the value of the maximum flow found.

Implementation of the graphs in the form of adjacency lists


const int MAXN = ...; //
const int INF = 1000000000; // -
struct edge {
int a, b, cap, flow;
};
int n, s, t, d[MAXN], ptr[MAXN], q[MAXN];
vector<edge> e;
vector<int> g[MAXN];
void add_edge (int a, int b, int cap) {
edge e1 = { a, b, cap, 0 };
edge e2 = { b, a, 0, 0 };
g[a].push_back ((int) e.size());
e.push_back (e1);
g[b].push_back ((int) e.size());
e.push_back (e2);
}
bool bfs() {
int qh=0, qt=0;
q[qt++] = s;
memset (d, -1, n * sizeof d[0]);
d[s] = 0;
while (qh < qt && d[t] == -1) {
int v = q[qh++];
for (size_t i=0; i<g[v].size(); ++i) {

int id = g[v][i],
to = e[id].b;
if (d[to] == -1 && e[id].flow < e[id].cap)
{
q[qt++] = to;
d[to] = d[v] + 1;
}
}
}
return d[t] != -1;
}
int dfs (int v, int flow) {
if (!flow) return 0;
if (v == t) return flow;
for (; ptr[v]<(int)g[v].size(); ++ptr[v]) {
int id = g[v][ptr[v]],
to = e[id].b;
if (d[to] != d[v] + 1) continue;
int pushed = dfs (to, min (flow, e[id].cap e[id].flow));
if (pushed) {
e[id].flow += pushed;
e[id^1].flow -= pushed;
return pushed;
}
}
return 0;
}
int dinic() {
int flow = 0;
for (;;) {
if (!bfs()) break;
memset (ptr, 0, n * sizeof ptr[0]);
while (int pushed = dfs (s, INF))
flow += pushed;
}
return flow;
}
The network must be previously read: must be specified variables , , and added
to all edges (oriented) via function calls
. The main function of the decision
which returns the value of the maximum flow found.

Das könnte Ihnen auch gefallen