稳定婚姻算法java代码
我需要在稳定婚姻类中实现makeMatches方法。 代码需要遵循以下算法:稳定婚姻算法java代码,java,list,stable-marriage,Java,List,Stable Marriage,我需要在稳定婚姻类中实现makeMatches方法。 代码需要遵循以下算法: set each person to be free; while (some man m with a nonempty preference list is free) { w = first woman on m's list; if (some man p is engaged to w) { set p to be free } set m and w to b
set each person to be free;
while (some man m with a nonempty preference list is free) {
w = first woman on m's list;
if (some man p is engaged to w) {
set p to be free
}
set m and w to be engaged to each other
for (each successor q of m on w's list) {
delete w from q's preference list
delete q from w's preference list
}
}
但是我找不到我的错误。该计划让夫妇们依赖于他们的第一选择,但之后就不会继续下去了。
你能看看我的代码,告诉我有什么问题吗
Person.java由讲师提供:
import java.util.*;
public class Person {
public static final int NOBODY = -1;
private String name;
private List<Integer> preferences;
private List<Integer> oldPreferences;
private int partner;
public Person(String name) {
this.name = name;
preferences = new ArrayList<Integer>();
oldPreferences = new ArrayList<Integer>();
erasePartner();
}
public void erasePartner() {
partner = NOBODY;
}
public boolean hasPartner() {
return partner != NOBODY;
}
public int getPartner() {
return partner;
}
public void setPartner(int partner) {
this.partner = partner;
}
public String getName() {
return name;
}
public boolean hasChoices() {
return !preferences.isEmpty();
}
public int getFirstChoice() {
return preferences.get(0);
}
public void addChoice(int person) {
preferences.add(person);
oldPreferences.add(person);
}
public List<Integer> getChoices() {
return preferences;
}
public int getPartnerRank() {
return oldPreferences.indexOf(partner) + 1;
}
}
import java.util.*;
公共阶层人士{
公共静态final int NOBODY=-1;
私有字符串名称;
私人列表偏好;
个人喜好;
私人国际合作伙伴;
公众人物(字符串名称){
this.name=名称;
首选项=新的ArrayList();
oldPreferences=new ArrayList();
删除合作伙伴();
}
公共合作伙伴(){
伙伴=无人;
}
公共布尔hasPartner(){
返回合作伙伴!=无人;
}
public int getPartner(){
返回伙伴;
}
公共无效设置合作伙伴(int合作伙伴){
这个。合伙人=合伙人;
}
公共字符串getName(){
返回名称;
}
公共布尔选择(){
return!preferences.isEmpty();
}
public int getFirstChoice(){
返回首选项。获取(0);
}
公共无效添加选择(整数人){
偏好。添加(个人);
oldPreferences.add(person);
}
公共列表getChoices(){
返回偏好;
}
public int getPartnerRank(){
返回oldPreferences.indexOf(partner)+1;
}
}
而StablaMarriage.java是我需要在其中实现代码的工具
import java.io.*;
import java.util.*;
public class StableMarriage {
public static final String LIST_END = "END";
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.print("What is the input file? ");
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));
System.out.println();
List<Person> men = readHalf(input);
List<Person> women = readHalf(input);
makeMatches(men, women);
writeList(men, women, "Matches for men");
writeList(women, men, "Matches for women");
}
public static Person readPerson(String line) {
int index = line.indexOf(":");
Person result = new Person(line.substring(0, index));
Scanner data = new Scanner(line.substring(index + 1));
while (data.hasNextInt()) {
result.addChoice(data.nextInt());
}
return result;
}
public static List<Person> readHalf(Scanner input) {
List<Person> result = new ArrayList<Person>();
String line = input.nextLine();
while (!line.equals(LIST_END)) {
result.add(readPerson(line));
line = input.nextLine();
}
return result;
}
public static void makeMatches(List<Person> list1, List<Person> list2) {
for (Person eachMan : list1){
eachMan.setPartner(-1);
}
for (Person eachWoman : list2){
eachWoman.setPartner(-1);
}
for (Person man : list1){
for (Person woman : list2){
while (man.hasChoices() && !man.hasPartner()){
int choosenWoman = man.getFirstChoice();
for (Person p : list1){
if (p.getPartner()== choosenWoman){
p.setPartner(-1);
}
}
man.setPartner(choosenWoman);
list2.get(choosenWoman).setPartner(list1.indexOf(man));
List manList= man.getChoices();
List womanList = woman.getChoices();
for (int q= womanList.size()-1; q>=0; q--){
if(q>womanList.indexOf(list1.indexOf(man)))
manList.remove(manList.indexOf(man.getFirstChoice()));
womanList.remove(q);
}
}
}
}
}
public static void writeList(List<Person> list1, List<Person> list2,
String title) {
System.out.println(title);
System.out.println("Name Choice Partner");
System.out.println("--------------------------------------");
int sum = 0;
int count = 0;
for (Person p : list1) {
System.out.printf("%-15s", p.getName());
if (!p.hasPartner()) {
System.out.println(" -- nobody");
} else {
int rank = p.getPartnerRank();
sum += rank;
count++;
System.out.printf("%4d %s\n", rank,
list2.get(p.getPartner()).getName());
}
}
System.out.println("Mean choice = " + (double) sum / count);
System.out.println();
}
}
import java.io.*;
导入java.util.*;
公共阶级稳定婚姻{
公共静态最终字符串列表\u END=“END”;
公共静态void main(字符串[]args)引发FileNotFoundException{
扫描仪控制台=新扫描仪(System.in);
System.out.print(“输入文件是什么?”);
字符串文件名=console.nextLine();
扫描仪输入=新扫描仪(新文件(文件名));
System.out.println();
列表人员=readHalf(输入);
列出女性=readHalf(输入);
制造火柴(男人、女人);
作家(男性、女性,“男性匹配”);
作家(女性,男性,“女性匹配”);
}
公共静态Person readPerson(字符串行){
int index=line.indexOf(“:”);
人员结果=新人员(第行子字符串(0,索引));
扫描仪数据=新扫描仪(行子字符串(索引+1));
while(data.hasNextInt()){
result.addChoice(data.nextInt());
}
返回结果;
}
公共静态列表readHalf(扫描仪输入){
列表结果=新建ArrayList();
String line=input.nextLine();
而(!line.equals(LIST_END)){
结果.添加(readPerson(行));
line=input.nextLine();
}
返回结果;
}
公共静态void makeMatches(列表1、列表2){
对于(每个人:列表1){
eachMan.setPartner(-1);
}
适用于(每人:列表2){
每个女人都有一个伴侣(-1);
}
用于(人员:列表1){
个人(女性:列表2){
while(man.hasChoices()&&!man.hasPartner()){
int choosenWoman=man.getFirstChoice();
对于(人员p:list1){
if(p.getPartner()==choosenWoman){
p、 setPartner(-1);
}
}
男。设置伙伴(选择女);
列表2.get(choosenWoman.setPartner(列表1.indexOf(man));
List manList=man.getChoices();
List womanList=woman.getChoices();
对于(int q=womanList.size()-1;q>=0;q--){
if(q>womanList.indexOf(list1.indexOf(man)))
remove(manList.indexOf(man.getFirstChoice());
女性主义者。移除(q);
}
}
}
}
}
公共静态无效写列表(列表1、列表2、,
字符串标题){
系统输出打印项次(标题);
System.out.println(“名称选择合作伙伴”);
System.out.println(“---------------------------------------------”);
整数和=0;
整数计数=0;
对于(人员p:list1){
System.out.printf(“%-15s”,p.getName());
如果(!p.hasPartner()){
System.out.println(“--nobody”);
}否则{
int rank=p.getPartnerRank();
总和+=秩;
计数++;
System.out.printf(“%4d%s\n”,秩,
清单2.get(p.getPartner()).getName());
}
}
System.out.println(“平均值选择=”+(双)和/计数);
System.out.println();
}
}
伪代码从不在女性列表上循环,而您的实现会循环。对于(w列表上m的每个继任者q){对于伪代码的这一部分,我需要在女性列表上循环。不是吗?不是。w
是“m列表上的第一个女性”,所以“w列表”是一个男性列表(男性w
喜欢).无论如何,这不是所有男人的名单也不是所有女人的名单。