Find maximum matching in a given Binary Tree
Given a Tree with N nodes values from 1 to N and N – 1 edges. The task is to find the maximum matching in the given tree.
A matching in a tree is a collection of edges such that no pair of edges share a common node. Matching with the most edges is known as a maximum matching.
Examples:
Input: Below is the given graph:
Output: 3
Explanation:
Set of Edges in the above graph for maximum matching:
(4, 5), (1, 2), (7, 8)
Input: Below is the given graph:
Output: 3
Explanation:
Set of Edges in the above graph for maximum matching:
(4, 5), (2, 3), (1, 7)
Approach: This problem can be solved using Greedy Approach and the idea is to use post-order traversal in the tree and start with leaf edges and go up the order. Below are the steps:
- Perform DFS Traversal on the given tree with rooted node 1 and make parent as 0 and pass current nodes as a parent of the node in recursive DFS traversal.
- While performing traversal, for each node U and its parent node P if these nodes are unvisited then mark these nodes as visited and increment the maximum matching count by 1.
- Print the count of a maximum matching in the above step after DFS Traversal.
The Greedy algorithm is to repeatedly take any leaf-edge.
TreeMatch(F:forest)
M <- []
while F nonempty do {
select any leaf-edge e
M <- M + [e]
F <- F - both ends of e
}
Why the greedy algorithm works correctly?
Let’s, assume E is a leaf edge and consider any maximum matching N. Suppose N does not contain E. Then if we add E to N, only one vertex now has two edges incident with it. So we can delete one of the edges of N and attain a maximum matching containing E.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 10000
vector< int > adj[N];
int used[N];
int max_matching;
void AddEdge( int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void Matching_dfs( int u, int p)
{
for ( int i = 0;
i < adj[u].size(); i++) {
if (adj[u][i] != p) {
Matching_dfs(adj[u][i], u);
}
}
if (!used[u] and !used[p] and p != 0) {
max_matching++;
used[u] = used[p] = 1;
}
}
void maxMatching()
{
Matching_dfs(1, 0);
cout << max_matching << "\n" ;
}
int main()
{
int n = 5;
AddEdge(1, 2);
AddEdge(1, 3);
AddEdge(3, 4);
AddEdge(3, 5);
maxMatching();
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int N = 10000 ;
@SuppressWarnings ( "unchecked" )
static Vector<Integer>[] adj = new Vector[N];
static int used[] = new int [N];
static int max_matching;
static void AddEdge( int u, int v)
{
adj[u].add(v);
adj[v].add(u);
}
static void Matching_dfs( int u, int p)
{
for ( int i = 0 ; i < adj[u].size(); i++)
{
if (adj[u].get(i) != p)
{
Matching_dfs(adj[u].get(i), u);
}
}
if (used[u] == 0 &&
used[p] == 0 && p != 0 )
{
max_matching++;
used[u] = used[p] = 1 ;
}
}
static void maxMatching()
{
Matching_dfs( 1 , 0 );
System.out.print(max_matching + "\n" );
}
public static void main(String[] args)
{
for ( int i = 0 ; i < adj.length; i++)
adj[i] = new Vector<Integer>();
AddEdge( 1 , 2 );
AddEdge( 1 , 3 );
AddEdge( 3 , 4 );
AddEdge( 3 , 5 );
maxMatching();
}
}
|
Python3
N = 10000
adj = {}
used = [ 0 for i in range (N)]
max_matching = 0
def AddEdge(u, v):
if u not in adj:
adj[u] = []
if v not in adj:
adj[v] = []
adj[u].append(v)
adj[v].append(u)
def Matching_dfs(u, p):
global max_matching
for i in range ( len (adj[u])):
if (adj[u][i] ! = p):
Matching_dfs(adj[u][i], u)
if ( not used[u] and not used[p] and p ! = 0 ):
max_matching + = 1
used[u] = 1
used[p] = 1
def maxMatching():
Matching_dfs( 1 , 0 )
print (max_matching)
n = 5
AddEdge( 1 , 2 )
AddEdge( 1 , 3 )
AddEdge( 3 , 4 )
AddEdge( 3 , 5 )
maxMatching()
|
C#
using System;
using System.Collections.Generic;
class GFG{
static readonly int N = 10000;
static List< int >[] adj = new List< int >[N];
static int []used = new int [N];
static int max_matching;
static void AddEdge( int u, int v)
{
adj[u].Add(v);
adj[v].Add(u);
}
static void Matching_dfs( int u, int p)
{
for ( int i = 0; i < adj[u].Count; i++)
{
if (adj[u][i] != p)
{
Matching_dfs(adj[u][i], u);
}
}
if (used[u] == 0 &&
used[p] == 0 && p != 0)
{
max_matching++;
used[u] = used[p] = 1;
}
}
static void maxMatching()
{
Matching_dfs(1, 0);
Console.Write(max_matching + "\n" );
}
public static void Main(String[] args)
{
for ( int i = 0; i < adj.Length; i++)
adj[i] = new List< int >();
AddEdge(1, 2);
AddEdge(1, 3);
AddEdge(3, 4);
AddEdge(3, 5);
maxMatching();
}
}
|
Javascript
<script>
let N = 10000;
let adj = new Array(N);
let used = new Array(N);
used.fill(0);
let max_matching = 0;
function AddEdge(u, v)
{
adj[u].push(v);
adj[v].push(u);
}
function Matching_dfs(u, p)
{
for (let i = 0; i < adj[u].length; i++)
{
if (adj[u][i] != p)
{
Matching_dfs(adj[u][i], u);
}
}
if (used[u] == 0 &&
used[p] == 0 && p != 0)
{
max_matching++;
used[u] = used[p] = 1;
}
}
function maxMatching()
{
Matching_dfs(1, 0);
document.write(max_matching + "</br>" );
}
for (let i = 0; i < adj.length; i++)
adj[i] = [];
AddEdge(1, 2);
AddEdge(1, 3);
AddEdge(3, 4);
AddEdge(3, 5);
maxMatching();
</script>
|
Time Complexity: O(V + E), where V is the number of edges and E is the number of edges.
Auxiliary Space: O(V)
Bottom-up DFS approach:
Another intuitive approach to solve this problem is to use DFS in a bottom-up manner and return two values at each level
Maximum matching including the current node
Maximum matching excluding the current node
We will recurse on the left and right subtrees and get these values for both of them. We can then calculate new values for the current level based on these values.
Let left_included denote maximum matching including root of left subtree and left_excluded denote the maximum matching excluding the root of left subtree. Similarly, for right_included and right_excluded.
If we include the current node in maximum matching, then we have to exclude one of either left sub tree root or right sub tree root. Including both will cause overlapping on current node which is not allowed. By excluding either left or right sub tree root we can increase the maximum matching by 1 by including one of the edges from current_node -> left sub tree root or current_node -> right sub tree root.
Thus maximum matching including current node will be given by
current_including = max(max(left_including, right_excluding) + 1, max(left_excluding, right_including) + 1)
If we exclude the current node then we can include both left and right subtree root. As matchings in left and right subtrees are independent of each other we can get maximum value by adding both matchings.
Thus maximum matching excluding current node will be given by
current_excluding = left_including + right_including
We will return both these values from the current recursion level to upper recursion levels. After the recursion completes we will receive two values, maximum matching including root node and maximum matching excluding root node.
The maximum of those two will give the maximum matching in the tree.
C++
#include <iostream>
#include <algorithm>
using namespace std;
class Node {
public :
Node* left;
Node* right;
int val;
Node( int key) {
this ->left = nullptr;
this ->right = nullptr;
this ->val = key;
}
};
int * maxMatchingHelper(Node* root) {
if (root == nullptr) {
int * arr = new int [2];
arr[0] = 0;
arr[1] = 0;
return arr;
}
if (root->left == nullptr && root->right == nullptr) {
int * arr = new int [2];
arr[0] = 0;
arr[1] = 0;
return arr;
}
int * left = maxMatchingHelper(root->left);
int * right = maxMatchingHelper(root->right);
int currIncluded = max(max(left[0], right[1]) + 1, max(left[1], right[0]) + 1);
int currExcluded = left[0] + right[0];
return new int [2] { currIncluded, currExcluded };
}
int maxMatching(Node* root) {
int * result = maxMatchingHelper(root);
return max(result[0], result[1]);
}
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(7);
root->left->left = new Node(3);
root->left->right = new Node(4);
root->left->right->left = new Node(5);
root->left->right->right = new Node(6);
root->right->left = new Node(8);
root->right->right = new Node(9);
cout << maxMatching(root) << endl;
return 0;
}
|
Java
class Node {
Node left, right;
int val;
public Node( int key) {
this .left = null ;
this .right = null ;
this .val = key;
}
}
public class Main {
public static void main(String[] args) {
Node root = new Node( 1 );
root.left = new Node( 2 );
root.right = new Node( 7 );
root.left.left = new Node( 3 );
root.left.right = new Node( 4 );
root.left.right.left = new Node( 5 );
root.left.right.right = new Node( 6 );
root.right.left = new Node( 8 );
root.right.right = new Node( 9 );
System.out.println(maxMatching(root));
}
public static int [] maxMatchingHelper(Node root) {
if (root == null ) {
return new int []{ 0 , 0 };
}
if (root.left == null && root.right == null ) {
return new int []{ 0 , 0 };
}
int [] left = maxMatchingHelper(root.left);
int [] right = maxMatchingHelper(root.right);
int currIncluded = Math.max(
Math.max(left[ 0 ], right[ 1 ]) + 1 ,
Math.max(left[ 1 ], right[ 0 ]) + 1
);
int currExcluded = left[ 0 ] + right[ 0 ];
return new int []{currIncluded, currExcluded};
}
public static int maxMatching(Node root) {
int [] result = maxMatchingHelper(root);
return Math.max(result[ 0 ], result[ 1 ]);
}
}
|
Python3
class Node:
def __init__( self , key):
self .left = None
self .right = None
self .val = key
def max_matching_helper(root):
if not root:
return ( 0 , 0 )
if not root.left and not root.right:
return ( 0 , 0 )
left_included, left_excluded = max_matching_helper(root.left)
right_included, right_excluded = max_matching_helper(root.right)
curr_included = max ( max (left_included, right_excluded) + 1 , max (left_excluded, right_included) + 1 )
curr_excluded = left_included + right_included
return (curr_included, curr_excluded)
def max_matching(root):
root_including, root_excluding = max_matching_helper(root)
return max (root_including, root_excluding)
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 7 )
root.left.left = Node( 3 )
root.left.right = Node( 4 )
root.left.right.left = Node( 5 )
root.left.right.right = Node( 6 )
root.right.left = Node( 8 )
root.right.right = Node( 9 )
print (max_matching(root))
|
C#
using System;
public class Node {
public Node left;
public Node right;
public int val;
public Node( int key) {
this .left = null ;
this .right = null ;
this .val = key;
}
}
public class MaximumMatching {
private static int [] MaxMatchingHelper(Node root) {
if (root == null ) {
int [] arr = new int [2];
arr[0] = 0;
arr[1] = 0;
return arr;
}
if (root.left == null && root.right == null ) {
int [] arr = new int [2];
arr[0] = 0;
arr[1] = 0;
return arr;
}
int [] left = MaxMatchingHelper(root.left);
int [] right = MaxMatchingHelper(root.right);
int currIncluded = Math.Max(Math.Max(left[0], right[1]) + 1, Math.Max(left[1], right[0]) + 1);
int currExcluded = left[0] + right[0];
return new int [2] { currIncluded, currExcluded };
}
public static int MaxMatching(Node root) {
int [] result = MaxMatchingHelper(root);
return Math.Max(result[0], result[1]);
}
public static void Main() {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(7);
root.left.left = new Node(3);
root.left.right = new Node(4);
root.left.right.left = new Node(5);
root.left.right.right = new Node(6);
root.right.left = new Node(8);
root.right.right = new Node(9);
Console.WriteLine(MaxMatching(root));
}
}
|
Javascript
class Node {
constructor(key) {
this .left = null ;
this .right = null ;
this .val = key;
}
}
function maxMatchingHelper(root) {
if (!root) {
return [0, 0];
}
if (!root.left && !root.right) {
return [0, 0];
}
const [leftIncluded, leftExcluded] = maxMatchingHelper(root.left);
const [rightIncluded, rightExcluded] = maxMatchingHelper(root.right);
const currIncluded = Math.max(
Math.max(leftIncluded, rightExcluded) + 1,
Math.max(leftExcluded, rightIncluded) + 1
);
const currExcluded = leftIncluded + rightIncluded;
return [currIncluded, currExcluded];
}
function maxMatching(root)
{
const [rootIncluding, rootExcluding] = maxMatchingHelper(root);
return Math.max(rootIncluding, rootExcluding);
}
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(7);
root.left.left = new Node(3);
root.left.right = new Node(4);
root.left.right.left = new Node(5);
root.left.right.right = new Node(6);
root.right.left = new Node(8);
root.right.right = new Node(9);
console.log(maxMatching(root));
|
Time Complexity: O(V + E), where V is the number of edges and E is the number of edges.
Auxiliary Space: O(V)
Last Updated :
28 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...