# Hopcroft–Karp Algorithm for Maximum Matching | Set 1 (Introduction)

A matching in a Bipartite Graph is a set of the edges chosen in such a way that no two edges share an endpoint. A maximum matching is a matching of maximum size (maximum number of edges). In a maximum matching, if any edge is added to it, it is no longer a matching. There can be more than one maximum matching for a given Bipartite Graph.

We have discussed importance of maximum matching and Ford Fulkerson Based approach for maximal Bipartite Matching in previous post. Time complexity of the Ford Fulkerson based algorithm is O(V x E).

Hopcroft Karp algorithm is an improvement that runs in O(√V x E) time. Let us define few terms before we discuss the algorithm

* Free Node or Vertex:* Given a matching M, a node that is not part of matching is called free node. Initially all vertices as free (See first graph of below diagram). In second graph, u2 and v2 are free. In third graph, no vertex is free.

* 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. In first graph, all edges are non-matching. In second graph, (u0, v1), (u1, v0) and (u3, v3) are matching and others not-matching.

* Alternating Paths:* Given a matching M, an alternating path is a path in which the edges belong alternatively to the matching and not matching. All single edges paths are alternating paths. Examples of alternating paths in middle graph are u0-v1-u2 and u2-v1-u0-v2.

* Augmenting path:* 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. In below diagram, augmenting paths are highlighted with blue color. Note that the augmenting path always has one extra matching edge.

The Hopcroft Karp algorithm is based on below 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*

So the idea is to one by one look for augmenting paths. And add the found paths to current matching.

**Hopcroft Karp Algorithm**

1) Initialize 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) 3) Return M.

Below diagram shows working of the algorithm.

In the initial graph all single edges are augmenting paths and we can pick in any order. In the middle stage, there is only one augmenting path. We remove matching edges of this path from M and add not-matching edges. In final matching, there are no augmenting paths so the matching is maximum.

Implementation of Hopcroft Karp algorithm is discussed in set 2.

Hopcroft–Karp Algorithm for Maximum Matching | Set 2 (Implementation)

References:

https://en.wikipedia.org/wiki/Hopcroft%E2%80%93Karp_algorithm

http://www.dis.uniroma1.it/~leon/tcs/lecture2.pdf

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Boruvka's algorithm for Minimum Spanning Tree
- Push Relabel Algorithm | Set 1 (Introduction and Illustration)
- Floyd Warshall Algorithm | DP-16
- Union-Find Algorithm | Set 2 (Union By Rank and Path Compression)
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Bellman–Ford Algorithm | DP-23
- Ford-Fulkerson Algorithm for Maximum Flow Problem
- Maximum Bipartite Matching
- Fleury's Algorithm for printing Eulerian Path or Circuit
- Find maximum number of edge disjoint paths between two vertices
- Johnson's algorithm for All-pairs shortest paths
- Graph Coloring | Set 1 (Introduction and Applications)
- Graph Coloring | Set 2 (Greedy Algorithm)
- Tarjan's Algorithm to find Strongly Connected Components
- Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)
- Flood fill Algorithm - how to implement fill() in paint?
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Boruvka's algorithm | Greedy Algo-9