Algorithm Lesson Note Series (vi)--figure (PART2)

Source: Internet
Author: User

Last week went to a trip to say walk on the journey of treatment, so algorithm class are not able to on. :( However, life has been such a time, is enough. As long as you always do what you think is right, there will be no cross-over.

Like last week, this week's content contains a few small parts, namely, the shortest path dynamic planning, the shortest path between all point pairs, and the network flow.

The first part: the Shortest path dynamic programming

For a forward graph g= (V, E), each edge weight is CVW (weight can be negative), the problem is to find the shortest path from node s to T. The Dijkstra method does not apply if there is a negative value in the weight of the edge. So we think of a way to add a normal number to each weight so that the weights of each edge are non-negative, and this method is not possible.

This defines a negative (cost) ring. As shown, as the name implies, all sides of the weight value and is negative.

If a path from S to T contains a negative (cost) ring, there is no shortest path between the s-t, otherwise there is a shortest path. Such as.

The following is a dynamic plan to solve the shortest path problem.

Define OPT (I, V) to represent the shortest v to T path p using the most I edges.

1) use up to i-1 edges in P

Opt (i, v) = opt (i-1, v)

2) p Use the edge of article I

if (V, W) is the first edge, then opt uses (V, W) and then selects the best w-t path using up to i-1 edges

Through previous observations, it can be concluded that if there is no negative ring, then opt (n-1, v) is the shortest length of the v-t path.

The implementation code is as follows:

The time complexity is O (MN) and the space complexity is O (n^2).

An improvement is presented below.

Keep a matrix m[v] for the shortest v-t path we can find today. Unless M[v] has changed in the previous iteration, it is not necessary to check the edges formed by (V, W). Here is a theorem, in the whole algorithm, M[v] is the length of the v-t path, after the I-wheel update, M[V] will not be greater than the length of the shortest path with no more than I edge. Memory Consumption is O (m+n), the time complexity is O (MN) worst case, but actually faster.

The Bellman-ford implementation algorithm is as follows:

Using the above algorithm can prove that if for all v have opt (n, v) = opt (n-1, v), then there is no negative ring.

If opt (n, v) < opt (n-1, v), then any shortest path from V to T contains a ring W, and W contains the weighted edge of the negative number.

The above two can be used to detect negative loops. The time complexity of the Bellman-ford algorithm is O (MN) and the space complexity is O (m+n).


Part Two: Shortest path between all pairs of points

The shortest path contains the single source shortest path problem and the shortest path problem between all point pairs. Single Source Shortest path problem contains Dijkstra algorithm (O (E+VLGV)) with nonnegative edge weights, general algorithm Bellman-ford algorithm (O (VE)) and DAG algorithm for a bellman-ford change (O (v+e)). The shortest path problem between all point pairs includes the Dijkstra algorithm | V| times: O (VE+V2LGV), and general method (three algorithms to be described below).

Give a figure g= (V, E), | v| = N, the function relationship between the edge and the weight is w:e→r. The output is the NXN matrix for the shortest path length, for all I, j∈v.

Consider running the Bellman-ford algorithm at each vertex at a time of O (v2e)

Dynamic planning

Consider an adjacency matrix of a NXN graph, defined as the weight of a shortest path that uses up to m edges from I to J. Because we get

For any m =,..., n-1,

To prove the above, the condition can be relaxed, for any k from 1 to N, if, then the DIJ is updated to

The absence of a negative ring means that

The following are three algorithms for half the situation between all pairs of points:

1. Matrix multiplication

Calculate C=a b, where C,a and B are the matrices of the NXN,

Using traditional algorithms requires the time complexity of O (n^3).

If we set the + reason to min and map to +, then there is

Such

The unit matrix I is expressed as

(min,+) multiplication is associative, in real numbers, it forms a set structure called a closed half loop. Therefore, we can calculate

Concluded

Time complexity is not better than n-times Bellman-ford algorithm.

Thus, an improved matrix multiplication algorithm is a constant square,

In this way, you only need to calculate the square of O (LGN) times,

The complexity of time becomes

In order to detect the presence of a negative weight ring, it is necessary to check whether the diagonal contains negative values that require additional O (n) time.

2. Floyd-warshall algorithm

The algorithm is also a dynamic programming, but has a faster speed.

The definition is the weight of the shortest path from I to J that contains the set as {,... k} as the middle node

Then there is, and,

In the process of looking for the most-segment path, it occurs

Loop for

Time complexity is, and is more effective.

3. Johnson algorithm

For each v∈v, given a label H (v), re-assigns weights to each edge (U, v) ∈e

This way, the paths between the same two vertices will be re-weighted by the same value.

A Find a vertex labeled H so that for all (U, v) ∈e is established by using the Bellman-ford algorithm to address different limitations

or determine whether a negative weight ring exists, the time required is O (VE).

B. For each vertex used run Dijsktra algorithm, time complexity

C. For each shortest path length the shortest path length of the original graph is generated by re-weighting value , the complexity of time is

Therefore, the total time is

Part Three network flow

Maximum flow and minimum cut are two very common algorithmic problems.

Minimum cutting problem:

can be abstracted to flow through the material of the edge.

There is a direction to figure g= (V, E) without parallel sides, two different nodes s for source,t for Sink,c (e) is the capacity of the side E.

Defining S-T Cutting is a division of V of S∈a,t∈b (A, B). The capacity of cut (A, B) is

Therefore, the problem of minimum s-t partitioning is to find a S-T partition with a minimum capacity.

The problem with the stream is:

The first definition of a s-t stream is a function that satisfies the condition:

For each edge e∈e:; For each vertex v∈v-{s,t},

The value of stream F is defined as, as shown in

So the maximum flow problem is finding the maximum value of the s-t stream.

The flow value theorem is: make f a stream of any kind, (a, B) is any s-t cut. Then, the network stream passed through the cut equals the total amount of leave S, i.e.

Here is an example of an image:

Weak duality: makes f a stream of any kind, (a, B) is any s-t cut. Then, the value of the stream is up to cut capacity, i.e. V (f) <= cap (A, B)

The corollary is: if V (f) = Cap (A, B), then f is the largest flow, (a, B) is the smallest cut

The following greedy algorithm is used to find a maximum flow algorithm:

First, all edges are e∈e, and all have F (E) =0. Find a S-T path P has each edge F (e) < C (e). Split the stream along the path p, repeating the operation until it is unable to proceed. It's not going to work here anymore. It is important to note that local optimality is not able to introduce global optimality.


Here is an overview of the remaining graphs,

The original side is e= (U, v) ∈e, Stream F (e), and the capacity is C (e). The remaining capacity is defined as

The remaining figure is

The remaining edges have positive remaining capacity, so

The Ford-fulkerson algorithm can be used to find the maximum stream. Also known as the split path algorithm.

Split path theorem: If there is no split path algorithm, Flow f is a maximum stream.

Algorithm Lesson Note Series (vi)--figure (PART2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.