比较两个数据结构并建议最佳匹配

时间:2018-11-20 04:55:42

标签: java algorithm data-structures compare maps

大家好,我有两个结构Map<String,Map<String,String>>。第一个Map结构是医院偏好示例<Hospital name,<Student,Preferences>>。 第二张地图是学生偏好示例<Student,<Hospital,Preferences>>。 如何比较两者并根据给定的首选项查找最佳匹配。

查找以下数据 第一张地图

{St. Luke's={ Martin Fowler= 2,  Alan Turing= 1}, Olathe Medical Center={ Martin Fowler= 2,  Alan Turing= 1}}

第二张地图

{Martin Fowler={ Olathe Medical Center= 1,  St. Luke's= 2}, Alan Turing={ Olathe Medical Center= 1,  St. Luke's= 2}, Martin Fowler={ Olathe Medical Center= 1,  St. Luke's= 2}}

用于创建此结构的代码是

    public Map<String,Map<String,String>> readingFile(String filename) {
    Map<String,String> preference = new HashMap<String,String>();
    Map<String,Map<String,String>> DataPreference = new HashMap<String,Map<String,String>>();
    CSVReader reader = null;
    try {
        reader = new CSVReader(new FileReader(filename));
        String[] line;
            while ((line = reader.readNext()) != null) {
                preference.put(line[1], line[2]);
                DataPreference.put(line[0], preference);
            }
            System.out.println(DataPreference);
        }
        catch (IOException |ArrayIndexOutOfBoundsException e) {
         System.out.println("File empty or File not fond in the given Path ");
     }
    return DataPreference;
}

谢谢

1 个答案:

答案 0 :(得分:1)


此问题是stable marriage problem / stable matching problem的变体,即,大小不一,允许一夫多妻制:)

该算法在许多轮次中起作用。医院会根据其偏好匹配学生。然后,学生检查提案,暂时保留最佳提案,然后拒绝其余提案。在下一轮中,被拒绝的医院提出他们的下一个最佳选择,而学生再次保留最佳选择,其余的则拒绝。这个过程一直持续到没有更多的学生可以提出建议为止。

使用的原则是延迟接受

http://www.nrmp.org/matching-algorithm/

相关问题