-3

I am stuck with a maximal matching in a bipartite graph problem. The problem goes something like this:

Given a board with m circular holes and given a set of n circular discs. Holes are numbered as h1, ..., hm, and discs as d1, ..., dn.

We have a matrix A of m rows and n columns. A[i][j] = 1 if hi can fit dj (i.e., diameter of hi ≥ diameter of dj), and 0 otherwise.

Given the condition that any hole can contain at most one disc, I need to find the configuration for which holedisc fitting is maximal.

I have read that this problem can be modelled into network flow problem, but could not exactly follow how. Can someone explain how to do this? Also, is there any C code for this that I might be able to look at?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
rits
  • 19
  • 1
  • 6
  • 3
    You are unlikely to find people willing to just provide you with an entire C program here. Try it yourself and if it doesn't work, people will make suggestions for changes. – Daniel Aug 29 '11 at 16:17

1 Answers1

5

The reduction from bipartite matching to maximum flow is actually quite beautiful. When you are given a bipartite graph, you can think of the graph as two columns of nodes connected by edges from the first column to the second:

  A ----- 1
  B --\   2
  C    \- 3
 ...     ...
  Z       n

To reduce the problem to max-flow, you begin by directing all of the edges from the first column to the second column so that flow can only move from the left column to the right. After you do this, you introduce two new nodes s and t that act as the source and terminal nodes. You position s so that it is connected to all of the nodes on the left side and t so that each node in the right side is connected to it. For example:

     A ----- 1
 /   B --\   2   \
s-   C    \- 3   - t
 \  ...     ...  /
     Z       n

The idea here is that any path you can take from s to t must enter one of the nodes in the left column, then cross some edge to the right column, and from there to t. Thus there is an easy one-to-one mapping from an edge in a matching and an s-t path: just take the path from s to the source of the edge, then follow the edge, then follow the edge from the endpoint to the node t. At this point, our goal is to find the way to maximize the number of node-disjoint paths from s to t. We can accomplish this using a maximum-flow as follows. First, set the capacity of each edge leaving s to be 1. This ensures that at most one unit of flow enters each of the nodes in the first column. Similarly, set the capacity of each edge crossing the two columns to be one, ensuing that we either pick the edge or don't, rather than possibly picking it with some multiplicity. Finally, set the capacity of the edges leaving the second column into t to be one as well. This ensures that each node in the right-hand side is only matched once, since we can't push more than one unit of flow past it.

Once you've constructed the flow network, compute a maximum flow using any of the standard algorithms. Ford-Fulkerson is a simple algorithm that performs well here, since the maximum flow in the graph is equal to the number of nodes. It has a worst-case performance of O(mn). Alternatively, the highly optimized Hopcroft-Karp algorithm can do this in O(m√n) time, which can be much better.

As for a C implementation, a quick Google search for the Ford-Fulkerson step turned up this link. You'd need to construct the flow network before passing it into this code, but the construction isn't too complex and I think that you shouldn't have much trouble with it.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065