Gale-Shapely 匹配算法

老师说要用map

GG

这个算法的匹配思想很简单:

就是男士从最喜欢的女士开始表白

如果女士没有被配对,就在一起,如果女士配对了,看之前配对的男士和当前的男士哪个更优

一直进行到所有人都匹配了为止

package GSFianl;

public class Quee {
	public Node head;
	public Node tail;
	public Node pointer;
	public int size;
  public Quee(){
	   head=new Node();
	  tail=head;
	  size=0;
  }
  public void Add(String name)
  {
	  Node node=new Node(name);
	  tail.LinkNext(node);
	  tail=node;
	  size++;
  }
  public void Mov()
  {
	  head.LinkNext(head.next().next());
	  size--;
	 
  }
}
class Node{
	Node next;
	String value;
	public Node()
	{
		
	}
	public Node(String value){
		this.value=value;
	}
	 public void LinkNext(Node next) {  
	        this.next = next;  
	    }  
	public Node next()
	{
		return this.next;
	}
	public String value()
	{
		return this.value;
	}
}

package GSFianl;
import java.util.*;
public class Human {
   public String name;//名字
   public ArrayListmap_expect=new ArrayList();//偏好表
   public String company="";//当前约会对象
   public boolean isFree=true;//是否自由,初始为自由
   public int rank=0;//求婚求到第几个人
   //方法
   public void date (String name)//与女孩配对
   {
	   company =name;
	   isFree =false;
	}
   public void part(String name )//与女孩分手
   {
	   company ="";
	   isFree =true;
	   rank++;
   }
}
class MatchingProcessing{
	public ArrayList map_men;		
	public ArrayList map_women;
	public  MatchingProcessing()
	{
	}
	public void Matching()
	{
		 map_men =new ArrayList();
		 map_women =new ArrayList();
	   Scanner input=new Scanner(System.in);
	   System.out.println("请输入对数"); 
	   int n=input.nextInt();
	   input.nextLine();
	   Human[] man=new Human[n];
	   for(int i=0;i0)
		 {//存在男人m是自由的且还没对每个女人都求过婚
		 // 从队列头取出第一个男人名,在man数组中找到信息
		 String name = queue.head.next().value;
		 Human choosenman=new Human();//被选召的男人
		   for(int i=0;i> iter = choosenman.map_expect.entrySet().iterator();
		   String woname;
		    while (choosenman.map_expect.entrySet().iterator().hasNext()) {
		       woname = choosenman.map_expect.entrySet().iterator().next().getKey();
		       break;
		    }*/
            name=choosenman.map_expect.get(choosenman.rank);
            Human choosenwoman=new Human();
            for(int i=0;i

package GSFianl;
import java.util.Map;
import java.util.Scanner;
public class MainMethod {
	public static void main(String args[])
	{
      MatchingProcessing ma=new MatchingProcessing();
      ma.Matching();
		
	}

}


你可能感兴趣的:(数据结构与算法分析(java))