Exemplo n.º 1
0
int main()	{

int i,j;
for(i=0;i<MAXNUM;i++)	{
	m[i]=(int*) malloc(sizeof(int)*MAXNUM);
	w[i]=(int*) malloc(sizeof(int)*MAXNUM);
	
	printf("Enter pref for man[%d] ",i);
	for(j=0;j<MAXNUM;j++)	{
		scanf("%d",m[i]+j);
	}

	printf("Enter pref for women[%d] ",i);
	for(j=0;j<MAXNUM;j++)	{
		scanf("%d",w[i]+j);
	}
}

for(i=0;i<2*MAXNUM;i++)	{
	if(i%2==0)	
		pairings[i]=(i/2);
	else
		pairings[i]=-1;
}

int upid,partner,women;
while( (upid=isUnpaired()) >=0)	{
	women=*m[upid];
	m[upid]++;

	if( (partner=isEngaged(women)) >= 0)	{
		if(prefers(women,upid,partner))	{
			pairings[2*upid+1]=women;
			pairings[2*partner+1]=0;
		}
		else 
			continue;
	}

	else {
		pairings[2*upid+1]=women;
//		pairings[2*upid]=*women++;
	}
		
}

for(i=0;i<2*MAXNUM;i+=2)	{
	printf("Man[%d]-Women[%d]\n",(i/2),pairings[i+1]);
}


}
vector<int> MapsMerge::GaleShapleyMatcherStrategy::algGaleShapley(vector<vector<int>> matrix1, vector<vector<int>> matrix2) {
	if (matrix1.size() != matrix2.size()) {
		cerr << "Incompatible dimensions of matrices" << endl;
		return vector<int>(0, 0);
	}

	int N = matrix1.size();
	vector<int> wMatch(N, -1);
	vector<bool> mMatched(N, false);
	int freeCount = N;

	while (freeCount > 0) {
		int m;
		for (m = 0; m < N; m++)
			if (mMatched[m] == false)
				break;
		for (int i = 0; i < N && !mMatched[m]; i++) {
			int w = matrix1[m][i];

			if (wMatch[w] == -1) {
				wMatch[w] = m;
				mMatched[m] = true;
				freeCount--;
			} else {
				int m1 = wMatch[w];

				if (!prefers(matrix2, w, m, m1)) {
					wMatch[w] = m;
					mMatched[m] = true;
					mMatched[m1] = false;
				}
			}
		}
	}

	// Returns a vector with values corresponding to matrix1 and indices - to matrix2

	return wMatch;
}