4

I am trying to solve the following problem but my algorithm is too slow. That's because I am using Edmonds - Karp algorithm to find maximum flow which when applied to bipartite graphs gives maximum matching as well. It's running time is n^5. I would like to know any faster algorithms to solve this problem (for bipartite graphs specifically). One algorithm that I am currently studying is Relabel to Front which is n^3.

Varun Sharma
  • 2,591
  • 8
  • 45
  • 63
  • [This algorithm](https://github.com/niklasb/tcr/blob/master/graphentheorie/BipartiteMatching2.cpp), which is an efficient Ford-Fulkerson implementation, has an O(n^3) runtime – Niklas B. Apr 14 '14 at 19:14
  • 1
    I get AC in 0.018 seconds with that. – Niklas B. Apr 14 '14 at 19:32
  • Oh thanks Niklas ! As usual you solved it again lol ! Alright, I will debug your code to understand it but I thought we should not be using Ford Fulkerson because it uses DFS. Instead we should use BFS for finding augmenting paths. On the other hand, looks like your approach is doing lot more than just finding augmenting paths as I see "greedy matching in comment". – Varun Sharma Apr 14 '14 at 22:30
  • No I didn't use a greedy matching. And DFS is in fact better for bipartite matching for some reason – Niklas B. Apr 14 '14 at 22:34
  • you mean your algorithm is just plain ford-fulkerson? just keep on finding augmenting paths till you can't find anymore? – Varun Sharma Apr 14 '14 at 22:39
  • Yes exactly. You can read about it [here](http://www.geeksforgeeks.org/maximum-bipartite-matching/) – Niklas B. Apr 14 '14 at 22:42
  • Oh Okay thanks. That is strange, because you need to run dfs, find a path, find bottleneck capacity and then update the graph. You did all that in 20 lines!!!!!!!. My normal ford fulkerson/edmond karp implementation is around 100 lines (bfs + find path + find bottleneck + update the graph). – Varun Sharma Apr 14 '14 at 22:45
  • Oh thanks. This link is great. It has got some great tutorials for dynamic programming (my weakest point) !! Thanks buddy. – Varun Sharma Apr 14 '14 at 22:46
  • It's less code because augmenting paths in bipartite matchings have a very special structure. You can just greedily find an odd-length path and the bottleneck capacity is always 1. And obviously DFS is always a bit shorter because the data structure (stack) is represented implicitly using recursion – Niklas B. Apr 14 '14 at 23:07
  • Ya True, didn't think carefully, bottleneck is 1 and recursion removes the code that we will normally have in iterative DFS. But need to learn the odd length path stuff. Today I will get time to understand your code. Thanks for the effort and time :-). – Varun Sharma Apr 15 '14 at 23:40
  • Thanks Niklas, I got AC! Actually initially my program was taking 0.58 seconds but then I passed the graph (2D vector) as const reference and suddenly time went down to 0.018. This is a big thing I didn't take seriously earlier. Thanks to you, because your program ran so fast and as a result of that I was trying to tweak mine. – Varun Sharma Apr 18 '14 at 03:57
  • Good job. Surprisingly not even Dinic can beat that time. 18ms is probably the granularity of the runtime measurement or the graph is just too small to see the difference – Niklas B. Apr 18 '14 at 04:10
  • And Kudos for writing your own implementation and sticking with it. There's no better way to learn about an algorithm than to write it from scratch and optimize the heck out of it :) Much too many people in competitive programming just reuse other people's ideas and code – Niklas B. Apr 18 '14 at 04:20
  • Thanks Niklas, Ya it took me some debugging/reading to understand why this algorithm actually works. Luckily there was a 1 hour video explaining the algorithm in that link you gave. But ya you are the most unique person I have seen on stackoverflow. Normally people either downvote these kind of algorithm questions or explain the idea. On the other hand, you not only explain the idea mathematically (which is more important) but you actually go and solve the problem and get AC :-) as well. I have never seen this kind of thing before :-). Unbelievable. – Varun Sharma Apr 18 '14 at 20:52
  • I take every chance I get to practice since we want to win the ICPC regionals this or next year. It's not really surprising that other people don't feel the same way :) But there are some, for example [Gassa](http://stackoverflow.com/users/1488799/gassa) – Niklas B. Apr 18 '14 at 20:57
  • Oh okay, all the best. I am pretty sure you can make it to the world finals. If you were at our university - you would have won the regionals single handedly :-). Not sure how hard the competition is in Germany!! – Varun Sharma Apr 20 '14 at 21:15
  • Antarctica... I can see how the competition might be weaker there :D – Niklas B. Apr 21 '14 at 05:42
  • Lol no, I am from New Zealand :-), but Antartica is only 8 hours flight from here !!! – Varun Sharma Apr 25 '14 at 23:30

3 Answers3

7

I write bipartite matching using dinitz's algorithm. Also there is a theorem that for the graphs of the type of the maximum bipartite matching problems it has the same complexity as relabel to front(and it is way easier to implement).

In networks arising during the solution of bipartite matching problem, the number of phases is bounded by O(\sqrt{V}), therefore leading to the O(\sqrt{V} E) time bound. The resulting algorithm is also known as Hopcroft–Karp algorithm. More generally, this bound holds for any unit network — a network in which each vertex, except for source and sink, either has a single entering edge of capacity one, or a single outgoing edge of capacity one, and all other capacities are arbitrary integers.

Unfortunately the wikipedia article on the algorithm is way not enough to implement it and I could not find any better resource online. I have my own implementation, but I have created it using guidance from others in my university a long time ago.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Hopcroft-Karp has rather good pseudocode: http://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm – IVlad Apr 14 '14 at 13:39
  • @IVlad But Dinic is almost the same code, just as fast and happens to work for general graphs as well – Niklas B. Apr 14 '14 at 17:38
  • @NiklasB. - I was only suggesting Hopcroft-Karp because it has detailed pseudocode, and the answer mentioned that that's hard to find for Dinic. – IVlad Apr 14 '14 at 18:20
  • That is true. It is hard to find DINIC algorithm's explanation and implementation. I found this really good website. Thanks to google translator and now I can also read it :-) http://e-maxx.ru/algo/dinic – Varun Sharma Apr 14 '14 at 22:35
2

The so-called Hungarian algorithm for bipartite matching can be implemented with a lower runtime complexity.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • Okay I see it here - http://en.wikipedia.org/wiki/Hungarian_algorithm#The_algorithm_in_terms_of_bipartite_graphs but it says that running time is n^4. – Varun Sharma Apr 14 '14 at 12:16
  • Well there is an n^3 implementation explained here - http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=hungarianAlgorithm here. This should be good. – Varun Sharma Apr 14 '14 at 12:18
  • Hungerian is for weighted matchings, for bipartite matching it's pretty inefficient – Niklas B. Apr 14 '14 at 17:39
  • Oh right, thanks for that. I know there are some weighted bipartite matching problems as well on UVA Online Judge, may be for them I can try it. For now I will just understand your 20 liner code for maximum bipartite matching. Actually I was planning to around 5 - 6 bipartite matching problems but got stuck because of TLE on this one. This particular problem had a tricky case with 0 on the left/right side as well and you considered that as well :-). – Varun Sharma Apr 14 '14 at 22:40
  • @VVV Just as you can solve max matching using max flow, you can solve min-cost matching using min-cost flow. So in competitive programming, it makes sense to be familiar with the latter instead because it is more general. To be honest I've never needed Hungerian for that exact reason – Niklas B. Apr 15 '14 at 02:44
  • Oh ok. Thanks. Actually ya I need to practice those types (bellman ford to find min cost path repeatedly) as well. – Varun Sharma Apr 15 '14 at 23:39
1

Hopcroft–Karp algorithm provides the lowest time complexity for finding maximum matching (or minimum vertex cover) for Bipartite graph.

According to wikipidea, It runs in O(E * (V^0.5)), E is the total edges and V is the total vertices. At worst case (dense graph) this will be O(V^2.5).

User_67128
  • 1,230
  • 8
  • 21