# Mathematics | Matching (graph theory)

**Prerequisite –** Graph Theory Basics

Given an **undirected graph**, a **matching** is a set of edges, such that no two edges share the same vertex. In other words, matching of a graph is a subgraph where each node of the subgraph has either **zero or one edge** incident to it.

A vertex is said to be **matched** if an edge is incident to it, **free** otherwise.

Possible matchings of , here the red edges denote the matching –

**Matching Terminology –**

**Maximal Matching –** A matching of graph is said to be maximal if on adding an edge which is in but not in , makes not a matching.

In other words, a maximal matching is not a proper subset of any other matching of . For example, the following graphs are maximal matchings –

Adding any edge to any of the above graphs would result in them no longer being a matching.

**Maximum Matching –** A matching of graph is said to be maximum if it is maximal and has the maximum number of edges.

There may be many possible maximum matchings of a graph.

Every maximum matching is a maximal matching but not every maximal matching is a maximum matching.

For example, in the first figure is a maximum matching and in the second figure, the second and third graphs are maximum matchings.

**Perfect Matching –** A matching of graph is said to be perfect if **every vertex** is connected to exactly one edge. Every perfect matching is a maximum matching but not every maximum matching is a perfect matching.

Since every vertex has to be included in a perfect matching, the number of edges in the matching must be where V is the number of vertices. Therefore, a perfect matching only exists if the number of vertices is even.

For example in the first figure, is a perfect matching.

A matching is said to be **near perfect** if the number of vertices in the original graph is odd, it is a maximum matching and it leaves out only one vertex. For example in the second figure, the third graph is a near perfect matching.

**Example –**Count the number of perfect matchings in a complete graph .**Solution –**If the number of vertices in the complete graph is odd, i.e. is odd, then the number of perfect matchings is 0.

But if is even then we can derive a general formula for counting the number of perfect matchings.

Since is even, we can denote it as for some positive integer .

Every vertex is connected to every other vertex in a perfect graph, therefore the degree of each vertex is . Out of these edges we have to choose 1 edge to include two vertices.

We can choose an edge in ways. Now vertices remain and edges remain since the edges connected to the already selected vertices cannot be selected because it is a matching.

So the number of ways of selecting an edge from edges is .

We can keep choosing edges in the same way, then by product rule-

Number of perfect matchings- .

This can also be written as-

So for a perfect graph with vertices the number of perfect matchings is-

**Bipartite Matching –**

Matching has many applications in flow networks, scheduling, and planning, graph coloring, neural networks etc. The most common of these is the scheduling problem where there are tasks which may be completed by workers. The tasks and workers represent the two sets of vertices in a bipartite graph, where a task is connected to a worker if the worker is able to complete it.

The problem therefore is to find a maximum matching.

More on this topic is discussed in the article – Maximum Bipartite Matching

**GATE CS Corner Questions**

Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them.

**References –**

Matching – Wikipedia

Discrete Mathematics and its Applications, by Kenneth H Rosen

This article is contributed by **Chirag Manwani**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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:

- Mathematics | Graph Theory Basics - Set 1
- Mathematics | Graph Theory Basics - Set 2
- Mathematics | Graph theory practice questions
- Mathematics | Independent Sets, Covering and Matching
- Mathematics | Set Operations (Set theory)
- Mathematics | Introduction of Set theory
- Regular Graph in Graph Theory
- Mathematics | Graph Isomorphisms and Connectivity
- Mathematics | Planar Graphs and Graph Coloring
- Mathematics | Walks, Trails, Paths, Cycles and Circuits in Graph
- Automata Theory | Set 7
- Automata Theory | Set 8
- Automata Theory | Set 9
- Automata Theory | Set 1
- Automata Theory | Set 2
- Automata Theory | Set 3
- Automata Theory | Set 4
- Automata Theory | Set 5
- Compiler Theory | Set 1
- Automata Theory | Set 10