Calculate maximum flow in directed graph
用于计算有向图中的最大流
如果大家遇到求无向图中最大流的时候可以先将有向图转换成无向图再进行求解。
[MaxFlow, FlowMatrix, Cut] = graphmaxflow(G, SNode, TNode)
[...] = graphmaxflow(G, SNode, TNode, ...'Capacity', CapacityValue, ...)
[...] = graphmaxflow(G, SNode, TNode, ...'Method', MethodValue, ...)
GN-by-N | sparse matrix that represents a directed graph. Nonzeroentries in matrix G represent the capacities ofthe edges. |
SNode | Node in G. |
TNode | Node in G. |
Capacity | ValueColumn vector that specifies custom capacities for the edgesin matrix G. It must have one entry forevery nonzero value (edge) in matrix G.The order of the custom capacities in the vector must match the orderof the nonzero values in matrix G whenit is traversed column-wise. By default, graphmaxflow getscapacity information from the nonzero entries in matrix G. |
MethodValue | String that specifies the algorithm used to find the minimalspanning tree (MST). Choices are: |
‘Edmonds’ — Uses the Edmondsand Karp algorithm, the implementation of which is based on a variationcalled the labeling algorithm. Time complexityis O(N*E^2), where N and E arethe number of nodes and edges respectively. | |
‘Goldberg’ — Default algorithm.Uses the Goldberg algorithm, which uses the generic method known as preflow-push.Time complexity is O(N^2*sqrt(E)), where N and E arethe number of nodes and edges respectively. |
Tip : For introductory information on graph theory functions, see Graph Theory Functions.
[MaxFlow, FlowMatrix, Cut] = graphmaxflow(G, SNode, TNode) calculatesthe maximum flow of directed graph G fromnode SNode to node TNode.Input G is an N-by-N sparse matrix thatrepresents a directed graph. Nonzero entries in matrix G representthe capacities of the edges. Output MaxFlow isthe maximum flow, and FlowMatrix is a sparsematrix with all the flow values for every edge. FlowMatrix(X,Y)is the flow from node X to node Y.Output Cut is a logical row vector indicatingthe nodes connected to SNode after calculatingthe minimum cut between SNode and TNode.If several solutions to the minimum cut problem exist, then Cut isa matrix.
Tip: The algorithm that determines Cut,all minimum cuts, has a time complexity of O(2^N),where N is the number of nodes. If this informationis not needed, use the graphmaxflow functionwithout the third output.
[…] = graphmaxflow(G, SNode, TNode, …’PropertyName’, PropertyValue, …) calls graphmaxflow withoptional properties that use property name/property value pairs. Youcan specify one or more properties in any order. Each PropertyName mustbe enclosed in single quotes and is case insensitive. These propertyname/property value pairs are as follows:
[…] = graphmaxflow(G, SNode, TNode, …’Capacity’, CapacityValue, …) lets you specify custom capacitiesfor the edges. CapacityValue is a columnvector having one entry for every nonzero value (edge) in matrix G.The order of the custom capacities in the vector must match the orderof the nonzero values in matrix G whenit is traversed column-wise. By default, graphmaxflow getscapacity information from the nonzero entries in matrix G.
[…] = graphmaxflow(G, SNode, TNode, …’Method’, MethodValue, …) lets you specify the algorithmused to find the minimal spanning tree (MST). Choices are:
‘Edmonds’ — Uses the Edmondsand Karp algorithm, the implementation of which is based on a variationcalled the labeling algorithm. Time complexityis O(N*E^2), where N and E arethe number of nodes and edges respectively.
‘Goldberg’ — Default algorithm.Uses the Goldberg algorithm, which uses the generic method known as preflow-push.Time complexity is O(N^2*sqrt(E)), where N and E arethe number of nodes and edges respectively.
Create a directed graph with six nodes and eight edges.
cm = sparse([1 1 2 2 3 3 4 5],[2 3 4 5 4 5 6 6],...
[2 3 3 1 1 1 2 3],6,6)cm =
(1,2) 2
(1,3) 3
(2,4) 3
(3,4) 1
(2,5) 1
(3,5) 1
(4,6) 2
(5,6) 3
Calculate the maximum flow in the graph from node1 to node 6.
[M,F,K] = graphmaxflow(cm,1,6)
M =
4
F =
(1,2) 2
(1,3) 2
(2,4) 1
(3,4) 1
(2,5) 1
(3,5) 1
(4,6) 2
(5,6) 2
K =
1 1 1 1 0 0
1 0 1 0 0 0
Notice that K is a two-row matrix becausethere are two possible solutions to the minimum cut problem.
View the graph with the original capacities.
h = view(biograph(cm,[],'ShowWeights','on'))
View the graph with the calculated maximum flows.
view(biograph(F,[],'ShowWeights','on'))
Show one solution to the minimum cut problem in theoriginal graph.
set(h.Nodes(K(1,:)),'Color',[1 0 0])
Notice that in the three edges that connect the source nodes(red) to the destination nodes (yellow), the original capacities andthe calculated maximum flows are the same.
[1] Edmonds, J. and Karp, R.M. (1972). Theoreticalimprovements in the algorithmic efficiency for network flow problems.Journal of the ACM 19, 248-264.
[2] Goldberg, A.V. (1985). A New Max-FlowAlgorithm. MIT Technical Report MIT/LCS/TM-291, Laboratory for ComputerScience, MIT.
[3] Siek, J.G., Lee, L-Q, and Lumsdaine, A.(2002). The Boost Graph Library User Guide and Reference Manual, (UpperSaddle River, NJ:Pearson Education).