[Study notes] Hungary match

Hungarian matching algorithm

Summary

Hungarian matching algorithm can be used for target tracking, according to the prediction algorithm to predict box iou relationship on a box and can determine whether or not on a target.

It is also commonly used bipartite graph matching algorithms.

concept

  1. A graph G of the matching is a set consisting of a set of edges is not common endpoint is not circle.

  2. Perfect match : two portions set is considered in FIG. X = {x1, x2, ... } and Y = {y1, y2, ... } , and a perfect matching is to define a bijection from XY, followed by x1 , x2, ... xn find a pair of vertices
  3. Staggered way: Given a graph G M match, an alternate path if the edge occurs in M and M are not present, which we call a staggered way
  4. Augmenting paths: If a M- alternating path, it's neither endpoint associated with the M side, we call this path is called the M- augmenting path

Hungary essence of the algorithm is to find an augmenting path, before changing the results to find the target after completion of the final match of the match.

algorithm

1. empty set M

  2. Find an augmenting paths P, greater matching operation by negating M 'instead of M

  3. Repeat process until 2 can not find a route to augmenting

To put it bluntly, is that you find out from the bipartite graph in a path, let the start and end of the path is not yet match off point, and the path through which the connection is not to be a match, a match has been, then the next Thus not any match appear alternately. When you find such a path, it is clear that there was no connection path to be matched has matched more than one connection, then modify the map matching, the match had all the paths in connection remove the matching relationship, the connection does not match the change to match, so that it hits more than the original one. Continue to do so, until no such path so far.

Code

According to the explanation men and women understand the online matching write.

'''
@Descripttion: This is Aoru Xue's demo,which is only for reference
@version: 
@Author: Aoru Xue
@Date: 2019-08-27 02:24:35
@LastEditors: Aoru Xue
@LastEditTime: 2019-08-27 02:29:26
'''
import numpy as np

class Hungary():
    def __init__(self,graph):
        self.graph = graph
        self.n = len(graph)
        self.used = None
        self.nxt = None
    def find(self,x):
        for i in range(self.n): # 每个女生问一遍
            if self.graph[i][x] == 1 and self.used[i] == 0: # 如果对这个女生有好感,并且这个女生没有问过
                self.used[i] = 1
                if self.nxt[i] == -1 or self.find(self.nxt[i]): # 如果女生没有对象或者女生的对象还喜欢别人
                    self.nxt[x] = i
                    self.nxt[i] = x
                    return True
        return False

    def match(self):
        self.used = [False] * self.n
        self.nxt = [-1] * self.n
        sum = 0
        for i in range(self.n):
            if self.nxt[i] == -1:
                self.used = [False] * self.n # 每个男生问之前肯定都没问过任何女生
                self.find(i)
if __name__ == '__main__':
    hungary = Hungary([[0,0,0,1,1,1],
    [0,0,0,1,0,1],
    [0,0,0,1,0,0],
    [1,1,1,0,0,0],
    [1,0,0,0,0,0],
    [1,1,0,0,0,0]])
    hungary.match()
    print(hungary.nxt)
    

'''
[4, 5, 3, 2, 0, 1]
'''

Guess you like

Origin www.cnblogs.com/aoru45/p/11415984.html