Maximum matching problem --- male and female matching problem (algorithm)

Nonsense:

During the review at the end of the term today, I found an algorithm that is very interesting. It is the problem of the maximum allocation of men and women. Several pairs of men and women can make up at most how many pairs of objects.
insert image description here
According to the core values ​​of socialism, it is best for us to implement monogamy and assign one person to one partner. It is our duty to do so.

Topic analysis:

There is one thing to say about this question. The analysis on the Internet is the Hungarian algorithm. I will stop talking about it because of me. . I don't quite understand either. For a deep understanding, please move to the systematic study of the Hungarian algorithm.
Combined with the analysis of netizens, I give my own understanding of this issue of male and female distribution.

The problem is to match as many men and women as possible. In layman's terms, this is the way to match as many couples as possible.

insert image description here
Take this picture as an example, male 1 and female 1 and female 2 like each other, and male 3 also likes female 1.

The best matching result is to let 男1和女2结合,男3和女1结合. In this way, two pairs can be formed, and if 女1和女1combined, only one pair can be improvised

We are in the process of matching, 有备胎的尽量给没备胎的让对象. In this way, everyone will be happy, and it is better to be happy alone than to be happy together.

We can solve it like this :

  1. Starting from male 1, find a partner for the boys in turn.
  2. If a guy likes someone who has already been matched, then you can ask the matched male compatriot to find out if there is a spare tire. If so, ask the already matched partner to find a spare tire and give this partner to me.

emm, it may sound a bit convoluted, let me demonstrate it with the animation above.
insert image description here
The following is a related question from Ningda University.

A university organizes n students to go to multiple units for internships. If all units can provide a total of m internship positions, the interns and the internship units are voluntary after negotiation, and the school hopes to maximize the number of interns. Assuming that the two parties are only willing or unwilling to each other, and have no priority, try to use an appropriate bipartite graph structure to represent the process, and design a reasonable algorithm to achieve the maximum matching of the bipartite graph and maximize the number of interns.

Same as above.

public class MaxMatching {
    
    
    private int n; // 学生数
    private int m; // 实习岗位数
    private boolean[][] graph; // 二分图的邻接矩阵
    private int[] match; // 存储每个学生匹配的实习岗位编号
    private boolean[] visited; // 存储每个学生是否已经被访问过

    public MaxMatching(int n, int m, boolean[][] graph) {
    
    
        this.n = n;
        this.m = m;
        this.graph = graph;
        this.match = new int[m];
        Arrays.fill(match, -1);
        this.visited = new boolean[n];
    }

    public int getMaxMatching() {
    
    
        int count = 0; // 记录匹配的学生数
        for (int i = 0; i < n; i++) {
    
    
            Arrays.fill(visited, false);
            if (find(i)) {
    
    
                count++;
            }
        }
        return count;
    }

    private boolean find(int student) {
    
    
        for (int i = 0; i < m; i++) {
    
    
            if (graph[student][i] && !visited[i]) {
    
    
                visited[i] = true;
                if (match[i] == -1 || find(match[i])) {
    
    
                    match[i] = student;
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        boolean[][] graph = new boolean[n][m];
        for (int i = 0; i < n; i++) {
    
    
            for (int j = 0; j < m; j++) {
    
    
                graph[i][j] = scanner.nextInt() == 1;
            }
        }
        MaxMatching maxMatching = new MaxMatching(n, m, graph);
        System.out.println(maxMatching.getMaxMatching());
    }
}

Conclusion:

The long algorithm road is too difficult! woo woo woo woo!

Guess you like

Origin blog.csdn.net/faker1234546/article/details/131402332