Let G (U u V, E) be a weighted directed bipartite graph (i.e. U and V are the two sets of nodes of the bipartite graph and E contains directed weighted edges from U to V or from
This problem can be solved in polynomial time using the Hungarian Algorithm. The "proof" by Vor above is wrong.
The method of structuring the problem for the above example is as follows:
D E F
A # 7 9
B 1 # #
C # 3 #
where "#" means negative infinity. You then resolve the matrix using the Hungarian algorithm to determine the maximum matching. You can multiply the numbers by -1 if you want to find a minimum matching.
Define a new undirected graph G'
from G
as follows.
G'
has a node (A, B)
with weight w
for each directed edge (A, B)
with weight w
in G
G'
has undirected edge ((A, B),(B, C))
if (A, B) and (B, C) are both directed edges in Ghttp://en.wikipedia.org/wiki/Line_graph#Line_digraphs
Now find a maximal (weighted) independent vertex set in G'
.
http://en.wikipedia.org/wiki/Vertex_independent_set
Typically this would be an NP-hard problem. However, G'
is a bipartite graph -- it contains only even cycles. Finding the maximal (weighted) independent vertex set in a bipartite graph is not NP-hard.
The algorithm you will run on G'
is as follows.
G'
, say H_1, H_2, ..., H_k
H_i
do a 2-coloring (say red and blue) of the nodes. The cookbook approach here is to do a depth-first search on H_i
alternating colors. A simple approach would be to color each vertex in H_i
based on whether the corresponding edge in G
goes from U
to V
(red) or from V
to U
(blue).H_i
are either all the red nodes or all the blue nodes. Choose the colored node set with higher weight. For example, the red node set has weight equal to H_i.nodes.where(node => node.color == red).sum(node => node.w)
. Call the higher-weight node set N_i
.union(N_1, N_2, ..., N_k)
.Since each vertex in G'
corresponds to one of the directed edges in G
, you have your maximal DirectionalMatching.