2

So I came across a problem in which there were 'n' pilots and 'm' airplanes. Each pilot had a list of airplanes which he could fly. And one pilot can fly only one airplane at a time. You had to determine the maximum number of planes that can fly at the same time. Standard bipartite matching problem(which I found out later).

In the contest, I came up with a greedy algorithm as follows :

While there are planes in the graph :

1)Select a plane which can be flown by minimum number of pilots

2)Greedily allocate a pilot to that plane (from the ones who can fly it)

3)Remove both the plane and the allocated pilot from the graph

In general, for a bipartite matching problem, I propose the following algorithm :

While there are nodes in the right set of the bipartite graph :

1)Select a node from the right set with minimum incoming degree

2)Greedily match it with ANY node from the left set (which has an edge to it)

3)Remove both these nodes from the graph( this would also involve decreasing the incoming degree of all nodes on the right to which this node has an edge to)

I am not mathematically proficient to prove the correctness of this algorithm and after a lot of thinking I have not been able to come up with a counter example. So my specific question is, is this a standard or known algorithm or am I making some blatant mistake which I cannot see?

Please feel free to edit my question to make it clearer if you feel so. Thank you.

Anirudh
  • 23
  • 1
  • 4

4 Answers4

2

The greedy approach will not work on bipartite matching. The problem as you could have guessed is with "selecting any node on the left".

Here is an example - nodes on the left are A, B, C and D and on the right are x, y, z, t. Connect each of A, B, C with each of x, y, z(so 9 edges here) then connect D with t and A with t. As a result you have 3 nodes on the right with in- degree 3(x, y, z) and one with in-degree 2(t). So you choose t and you choose one node on the left at random - this may be A or D. Problem is that if you select A, your max matching will be of size 3, while the real answer is 4(by selecting D).

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Thank you! Just out of curiosity, will the algorithm be correct if I include a condition like "for the node Y with minimum incoming degree on the right, pick a node x on the left with an edge to Y such that among all such x, it has least outgoing degree"? – Anirudh Nov 04 '15 at 23:10
  • No it won't. But it will take more time to find counter example – Ivaylo Strandjev Nov 05 '15 at 05:32
2

counter-example:

   a1  a2  a3  a4  a5
p1  x   x
p2  x   x   x   x         
p3  x   x   x   x   
p4                  x
p5          x   x   x

a5 is selected first. Randomly select pilot which MAY be p5. If it is, p4 doesn't have a plane.

Jur
  • 92
  • 6
0

There is no reason to use a greedy algorithm ! If you cannot prove its correctness, then it is false. And here for example, your greedy algorithm fails because its output depends on the order of the vertexes.

You should read this article : https://en.wikipedia.org/wiki/Matching_%28graph_theory%29#Algorithms_and_computational_complexity

There is for example an efficient algorithm for bipartite graphs : Hopcroft-Karp

Labo
  • 2,482
  • 2
  • 18
  • 38
  • I understand and completely agree that this algorithm is incorrect. However for the sake of closure, I wanted to find a specific counter example. – Anirudh Nov 04 '15 at 10:49
  • How do you choose the right and left sides in your graph ? – Labo Nov 04 '15 at 10:51
  • It wouldn't matter. You could take either side to be the left or the right set in the bipartite graph. – Anirudh Nov 04 '15 at 10:57
0

I also made a greedy solution to a bipartite matching that passed Google Code Jam 2018 round 2 tests and is generally extremely good, but not completely flawless. It would pass Jur's test case. The difference vs post author is that I choose amongst all nodes - in this case both pilots and planes. So in Jur's case p4 would be my first choice. If I have several vertices of similar importance I among these I select any node linked to a least-connected node. I connect to that least connected counterpart.

I've made a program to test it vs bipartite matching with flow and found that it does get wrong sometimes. Interestingly, how often it gets wrong on randomly generated data hugely depends on dimensions. For n=m=20 it got 220 of 200k randomly generated cases wrong with the more accurate version. For n=m=400 it was wrong in 1 of 500k such cases.

But it wasn't quicker than a classical flow-based solution.

Here's one case it got wrong

3 - 1 - - 
1 3 - - - 
- 1 3 1 1 
- - 1 3 1 
1 1 - - - 

3 stands for the edges selected by my greedy algorithm, 1 for the ones that were not. Here's my code in C++.

Íhor Mé
  • 896
  • 9
  • 13