Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

jjj

이분 매칭 본문

알고리즘/이론

이분 매칭

JJJJ123 2018. 10. 12. 23:11

이분 매칭

 

두 그룹 A와 B는 서로 연결될 수 있는 두 그래프이다. 이러한 이분 그래프에서 최대 매칭을 찾는 문제를 간단하게

 

이분 매칭이라고 한다.

 

// A 와 B의 정점의 개수
int n, m;

bool adj[MAX_N][MAX_M];

vector<int> aMatch, bMatch;

vector<bool> visited;

int dfs(int a) {
	if (visited[a]) return false;    
	visited[a] = true;
	
	for (int b = 0; b < m; b++) {
		if (adj[a][b]) {

			// 만약 b가 매칭되지 않았을때
			// or 매칭이 되었을 경우 b와 매칭된 A그룹의 정점이   
                        // 다른 B 그룹의 정점과 연결이 가능한지 파악  
			// 가능하다면 그 점과 연결한 후 a 와 b 매칭
			if (bMatch[b] == -1 || dfs(bMatch[b])) {
				// 증가 경로 발견 , a 와 b 매칭
				aMatch[a] = b;
				bMatch[b] = a;
				return true;
			}
		}

	}
	return false;
}

int bipartiteMatch() {
	
	aMatch = vector<int>(n, -1);
	bMatch = vector<int>(m, -1);
	int size = 0;
	for (int start = 0; start < n; start++) {
		visited = vector<bool>(n, false);

		if (dfs(start))
			size++;
	}
	return size;
}

 

'알고리즘 > 이론' 카테고리의 다른 글

네트워크 유량 ( 포드 풀커슨 )  (0) 2018.10.12
크루스칼 (Kruskal)  (0) 2018.10.12
플로이드 워셜  (0) 2018.10.12
상호 배타적 조합 ( DisjointSet )  (0) 2018.10.08
SCC(Strongly Connected Component)  (0) 2018.10.08
Comments