Program to find out the number of accepted invitations in Python

This is a bipartite matching problem where we need to find the maximum number of boy-girl pairs for a party. Each boy can invite multiple girls, but each girl can accept only one invitation. We'll use a depth-first search (DFS) approach to solve this matching problem.

Problem Understanding

Given an m × n matrix where m boys invite n girls:

  • Matrix[i][j] = 1 means boy i sent an invitation to girl j
  • Matrix[i][j] = 0 means no invitation was sent
  • Each girl can accept at most one invitation
  • We need to find the maximum number of accepted invitations

For the input matrix ?

Girl 0 Girl 1 Girl 2
Boy 0 1 0 0
Boy 1 1 0 1
Boy 2 1 1 0

The optimal matching gives 3 accepted invitations:

  • Girl 0 accepts Boy 0's invitation
  • Girl 1 accepts Boy 2's invitation
  • Girl 2 accepts Boy 1's invitation

Algorithm Approach

We use the Hungarian algorithm with DFS to find maximum bipartite matching ?

  1. For each boy, try to find an available girl
  2. If a girl is already matched, try to find an alternative match for her current partner
  3. Use DFS to explore alternative matching possibilities

Implementation

def solve(grid):
    M, N = len(grid), len(grid[0])
    matching = [-1] * N  # matching[i] = boy matched to girl i
    
    def dfs(boy, seen):
        for girl in range(N):
            # If boy sent invitation to girl and girl not visited in this round
            if grid[boy][girl] and not seen[girl]:
                seen[girl] = True
                
                # If girl is free or we can find alternative match for her current partner
                if matching[girl] == -1 or dfs(matching[girl], seen):
                    matching[girl] = boy
                    return True
        return False
    
    result = 0
    for boy in range(M):
        seen = [False] * N  # Reset for each boy
        if dfs(boy, seen):
            result += 1
    
    return result

# Test with the given example
grid = [[1, 0, 0], 
        [1, 0, 1], 
        [1, 1, 0]]

print("Maximum accepted invitations:", solve(grid))
print("Final matching:")

# Show the matching details
M, N = len(grid), len(grid[0])
matching = [-1] * N

def dfs(boy, seen):
    for girl in range(N):
        if grid[boy][girl] and not seen[girl]:
            seen[girl] = True
            if matching[girl] == -1 or dfs(matching[girl], seen):
                matching[girl] = boy
                return True
    return False

for boy in range(M):
    seen = [False] * N
    dfs(boy, seen)

for girl in range(N):
    if matching[girl] != -1:
        print(f"Girl {girl} accepts Boy {matching[girl]}'s invitation")
Maximum accepted invitations: 3
Final matching:
Girl 0 accepts Boy 0's invitation
Girl 1 accepts Boy 2's invitation
Girl 2 accepts Boy 1's invitation

How the Algorithm Works

The DFS function tries to find an augmenting path for each boy ?

  1. For each boy: Try to find a girl who can accept his invitation
  2. If girl is free: Match them directly
  3. If girl is already matched: Try to find an alternative match for her current partner using recursion
  4. Backtracking: If alternative found, reassign the matches

Time and Space Complexity

Aspect Complexity Explanation
Time O(M × N²) M boys, each DFS visits N girls
Space O(N) Matching array and seen array

Alternative Example

# Example with different matching scenario
grid2 = [[1, 1, 0], 
         [0, 1, 1], 
         [1, 0, 1]]

print("Grid 2 result:", solve(grid2))

# Test edge case - no possible matches
grid3 = [[0, 0, 0], 
         [0, 0, 0], 
         [0, 0, 0]]

print("No invitations result:", solve(grid3))
Grid 2 result: 3
No invitations result: 0

Conclusion

This bipartite matching problem uses DFS to find the maximum number of boy-girl pairs. The algorithm efficiently handles reassignment of matches to maximize the total accepted invitations, achieving optimal results for party planning scenarios.

Updated on: 2026-03-26T14:36:09+05:30

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements