Python implementation of network stream algorithm Push-relabel

Source: Internet
Author: User

The background of the network flow I will not say more, is in a graph to find the maximum flow, it is interesting that the problem of duality for the minimum cut, to find a slice, so that the flow of the two sides of the graph is the smallest, and usually dual problem is a lower bound of the original problem, but the minimum cut is equal to the maximum flow, That is, the cut edge is a combination of the saturated edges of each path in the maximum flow. It might be vague, but it's a good idea to read about it.

The most primitive and classical solution of the maximum flow is the FF algorithm, the algorithm complexity is O (MC), C is the sum of the capacity of the edge, and M is the number of sides. And today the Push-relabel algorithm is proposed in the 90 's efficient algorithm, the complexity of O (n^3), in fact, the most critical step of network flow is to add the opposite edge, to obtain the remaining figure. The other improvements are to be as greedy as possible when looking for an augmented path, as large as possible.

OK, start talking about the main idea of Push-relabel, first constructs a function excess, represents the traffic that each node saves, is equal to that node's incoming traffic-out traffic, normally, S's save the traffic is negative, the T saves the traffic is positive, the other node saves the traffic to be 0, The ultimate goal of the algorithm is this, and also defines a height function (h) that represents the heights of each node. Then, the initialization process is, H (s) =n,h (v) = 0, for all nodes that are not S, f (S, u) =c (S, u), for all edges starting from s are saturated by default, which is the upper bound. Then, is the process of Push-relabel, first traversing all nodes in the graph, if there is a non-t and excess greater than 0 node V, then view all the sides of V (V, W), if H (v) >h (w), you can label, that is, excess traffic, Passed to W, if the side is a positive edge, the size of the pass is Bottleneck=min{excess (v), C (V,w)-F (V, W)}, otherwise bottleneck=min{excess (V), F (V, W)}, after passing, Continue looking for nodes with excess greater than 0, note that if V has an edge but all edges are H (v)

The source code is as follows:

Note that the input format of the graph must satisfy the DIMACS format.

__author__ = ' Xanxus ' nodenum, edgenum = 0, 0arcs = []class Arc (object): Def __init__ (self): SELF.SRC = 1  SELF.DST = 1 Self.cap = -1s, t =-1, -1with open (' Sample.dimacs ') as F:for line in F.readlines (): Line            = Line.strip () if Line.startswith (' P '): tokens = Line.split (") nodenum = Int (tokens[2]) Edgenum = tokens[3] If Line.startswith (' n '): tokens = Line.split (") if tokens[2] = = ' s ': s = Int (tokens[1]) if tokens[2] = = ' t ': t = Int (tokens[1]) if line.            StartsWith (' a '): Tokens = Line.split (") Arc = arc () arc.src = Int (tokens[1]) ARC.DST = Int (tokens[2]) arc.cap = Int (tokens[3]) arcs.append (ARC) nodes = [-1] * nodenumfor i in RA Nge (S, T + 1): nodes[i-s] = Iadjacent_matrix = [[0 for I in range (Nodenum)] for J in Range (nodenum)]forward_matrix = [[0 for I in range (NodenUM)] for J in Range (Nodenum)]for arc in arcs:adjacent_matrix[arc.src-s][arc.dst-s] = Arc.cap Forward_matrix[arc . src-s][arc.dst-s] = Arc.capflow_matrix = [[0 for I in range (Nodenum)] to J in range (nodenum)]height = [0] * NODENUMH Eight[0] = nodenumfor i in range (len (Adjacent_matrix)): flow_matrix[0][i] = adjacent_matrix[0][i] adjacent_matrix[0] [I] = 0 adjacent_matrix[i][0] = flow_matrix[0][i]def Excess (v): in_flow, Out_flow = 0, 0 for I in range (Len (flow_ Matrix): In_flow + = Flow_matrix[i][v] Out_flow + flow_matrix[v][i] return in_flow-out_flowdef exist_e Xcess (): For-V in range (len (Flow_matrix)): If Excess (v) > 0 and V! = T-S: Return v return Non EV = Exist_excess () while v:has_lower_height = False for J in Range (Len (Adjacent_matrix)): If Adjacent_matrix [V]                [j]! = 0 and Height[v] > height[j]: has_lower_height = True if forward_matrix[v][j]! = 0: Bottleneck =MIN ([Excess (v), Adjacent_matrix[v][j]]) Flow_matrix[v][j] + bottleneck adjacent_matrix[v][j ]-= bottleneck adjacent_matrix[j][v] + = Bottleneck Else:bottleneck = min ([exces S (v), Flow_matrix[j][v]]) flow_matrix[j][v]-= bottleneck adjacent_matrix[v][j]-= Bottlenec K Adjacent_matrix[j][v] + = bottleneck if not has_lower_height:height[v] + = 1 v = exist_excess () for Arc in Arcs:print ' F%d%d '% (ARC.SRC, ARC.DST, Flow_matrix[arc.src-s][arc.dst-s])

We hope to help you.



Python implementation of network stream algorithm Push-relabel

Related Article

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.