Problém stabilného priradenia

Created: 2008-06-19 - 16:49

Preferencny zoznam muzov:
Znamena to, ze 0. muz ma zoznam zien v poradi: 2.,1.,0.,... atd

2 1 0 3 4
3 2 1 0 4
2 3 4 1 0
3 1 4 0 2
2 1 4 3 0

Preferencny zoznam zien:
2 1 3 0 4
4 1 2 0 3
0 1 2 3 4
2 3 1 0 4
1 0 3 2 4

Zoznamy su ulozene v 2 textakoch, vid kod.

import java.io.File;
import java.util.Scanner;

public class ZufaleManzelstva {
	static int manzelky[] = new int[5];
	static int manzelia[] = new int[5];
	static int pocetPonukMuza[] = new int[5];

	static int prefZoznamMuza[][] = new int[5][5];
	static int prefZoznamZeny[][] = new int[5][5];
	static int inverznyZoznamZeny[][] = new int[5][5];

	public static void stabilnePriradenie() {

	}

	public static void main(String[] args) {
		Scanner sc = new Scanner("null");
		Scanner sc2 = new Scanner("null");
		//-1 bude znamenat ze nema nikoho priradeneho:
		for (int i = 0; i < 5; i++) {
			manzelia[i]=-1;
			manzelky[i]=-1;
		}		
		
		//nacitat zo suboru a spravit inverzny zoznam
		try {
			File zoznamMuzi = new File("zoznamMuzi.txt");
			File zoznamZeny = new File("zoznamZeny.txt");
			sc = new Scanner(zoznamMuzi);
			sc2 = new Scanner(zoznamZeny);
		} catch (Exception e) {
			System.out.print("subor nenajdeny");
		}
		int i = 0;
		while (sc.hasNextLine()) {
			for (int j = 0; j < 5; j++) {
				prefZoznamMuza[i][j] = sc.nextInt();
				prefZoznamZeny[i][j] = sc2.nextInt();
			}
			i++;
		}
		for (i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				inverznyZoznamZeny[i][prefZoznamZeny[i][j]] = j;
			}
		}
		//+pekne vypisat co mame:
		for (i = 0; i < 5; i++) {
			System.out.print(i+". zena:");
			for (int j = 0; j < 5; j++) {
				System.out.print(prefZoznamZeny[i][j]);
			}
			System.out.println();
		}			System.out.println();

		for (i = 0; i < 5; i++) {
			System.out.print(i+". muz:");
			for (int j = 0; j < 5; j++) {
				System.out.print(prefZoznamMuza[i][j]);
			}
			System.out.println();
		}
		System.out.println();
		//utvor pary z danych udajov:
		utvorPary();
		System.out.print("(muz,zena):");
		for (i = 0; i < 5; i++) {
			System.out.print("(" + i + "," + manzelia[i] + ")");
		}
	}

	public static void utvorPary() {
		int m = 0;
		int i = 0;
		m = ktoryJeNesparovanyManzel(manzelia);
		while (existujeNesparovanyManzel(manzelia)) {
			int w = prefZoznamMuza[m][i];
			if (manzelky[w] == -1) {
				manzelia[m] = w;
				manzelky[w] = m;
				i = 0;
			} else if (manzelkaPreferujeTohtoPredTymCoMa(w, m)) {
				int mm = manzelky[w];
				manzelky[w] = m;
				manzelia[m] = w;
				manzelia[mm] = -1;
				i = 0;
			} else {
				i++;
			}
			m = ktoryJeNesparovanyManzel(manzelia);
		}
	}

	public static boolean manzelkaPreferujeTohtoPredTymCoMa(int w,
			int ponukanyMuz) {
		if (inverznyZoznamZeny[w][ponukanyMuz] < inverznyZoznamZeny[w][manzelky[w]]) {
				return true;
			}
		return false;
	}

	public static int ktoryJeNesparovanyManzel(int[] manzelia) {
		for (int i = 0; i < 5; i++) {
			if (manzelia[i] == -1)
				return i;
		}
		return -1;
	}
	
	public static boolean existujeNesparovanyManzel(int[] manzelia) {
		for (int i = 0; i < 5; i++) {
			if (manzelia[i] == -1)
				return true;
		}
		return false;
	}
}