Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
hopcroft_karp.cpp File Reference

Implementation of Hopcroft–Karp algorithm. More...

#include <iostream>
#include <cstdlib>
#include <queue>
#include <list>
#include <climits>
#include <memory>
#include <cassert>
Include dependency graph for hopcroft_karp.cpp:

Classes

class  graph::HKGraph
 Represents Bipartite graph for Hopcroft Karp implementation. More...
 

Namespaces

namespace  graph
 Graph Algorithms.
 

Functions

void tests ()
 
int main ()
 Main function.
 

Detailed Description

Implementation of Hopcroft–Karp algorithm.

The Hopcroft–Karp algorithm is an algorithm that takes as input a bipartite graph and produces as output a maximum cardinality matching, it runs in O(E√V) time in worst case.

Bipartite graph

A bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint and independent sets U and V such that every edge connects a vertex in U to one in V. Vertex sets U and V are usually called the parts of the graph. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

Matching and Not-Matching edges

Given a matching M, edges that are part of matching are called Matching edges and edges that are not part of M (or connect free nodes) are called Not-Matching edges.

Maximum cardinality matching

Given a bipartite graphs G = ( V = ( X , Y ) , E ) whose partition has the parts X and Y, with E denoting the edges of the graph, the goal is to find a matching with as many edges as possible. Equivalently, a matching that covers as many vertices as possible.

Augmenting paths

Given a matching M, an augmenting path is an alternating path that starts from and ends on free vertices. All single edge paths that start and end with free vertices are augmenting paths.

Concept

A matching M is not maximum if there exists an augmenting path. It is also true other way, i.e, a matching is maximum if no augmenting path exists.

Algorithm

1) Initialize the Maximal Matching M as empty. 2) While there exists an Augmenting Path P Remove matching edges of P from M and add not-matching edges of P to M (This increases size of M by 1 as P starts and ends with a free vertex i.e. a node that is not part of matching.) 3) Return M.

Author
Krishna Pal Deora

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
307{
308 tests(); // perform self-tests
309
310 int v1 = 0, v2 = 0, e = 0;
311 std::cin >> v1 >> v2 >> e; // vertices of left side, right side and edges
312 HKGraph g(v1, v2);
313 int u = 0, v = 0;
314 for (int i = 0; i < e; ++i)
315 {
316 std::cin >> u >> v;
317 g.addEdge(u, v);
318 }
319
320 int res = g.hopcroftKarpAlgorithm();
321 std::cout << "Maximum matching is " << res <<"\n";
322
323 return 0;
324
325}
Represents Bipartite graph for Hopcroft Karp implementation.
Definition hopcroft_karp.cpp:67
double g(double x)
Another test function.
Definition composite_simpson_rule.cpp:115
void tests()
Definition hopcroft_karp.cpp:255
Here is the call graph for this function:

◆ tests()

void tests ( )

Self-test implementation

Returns
none
255 {
256 // Sample test case 1
257 int v1a = 3, v1b = 5, e1 = 2; // vertices of left side, right side and edges
258 HKGraph g1(v1a, v1b); // execute the algorithm
259
260 g1.addEdge(0,1);
261 g1.addEdge(1,4);
262
263 int expected_res1 = 0; // for the above sample data, this is the expected output
264 int res1 = g1.hopcroftKarpAlgorithm();
265
266 assert(res1 == expected_res1); // assert check to ensure that the algorithm executed correctly for test 1
267
268 // Sample test case 2
269 int v2a = 4, v2b = 4, e2 = 6; // vertices of left side, right side and edges
270 HKGraph g2(v2a, v2b); // execute the algorithm
271
272 g2.addEdge(1,1);
273 g2.addEdge(1,3);
274 g2.addEdge(2,3);
275 g2.addEdge(3,4);
276 g2.addEdge(4,3);
277 g2.addEdge(4,2);
278
279 int expected_res2 = 0; // for the above sample data, this is the expected output
280 int res2 = g2.hopcroftKarpAlgorithm();
281
282 assert(res2 == expected_res2); // assert check to ensure that the algorithm executed correctly for test 2
283
284 // Sample test case 3
285 int v3a = 6, v3b = 6, e3 = 4; // vertices of left side, right side and edges
286 HKGraph g3(v3a, v3b); // execute the algorithm
287
288 g3.addEdge(0,1);
289 g3.addEdge(1,4);
290 g3.addEdge(1,5);
291 g3.addEdge(5,0);
292
293 int expected_res3 = 0; // for the above sample data, this is the expected output
294 int res3 = g3.hopcroftKarpAlgorithm();
295
296 assert(res3 == expected_res3); // assert check to ensure that the algorithm executed correctly for test 3
297
298
299
300}
Here is the call graph for this function: