Create a program that reads in a set of 2n ranked preferences from n students an
ID: 650631 • Letter: C
Question
Create a program that reads in a set of 2n ranked preferences from n students and n instructors and outputs a stable set of pairings using the deferred acceptance algorithm. Deferred Acceptance Algorithm This algorithm produces a stable matching between two sets of objects. It is also referred to as the stable marriage problem (SMP) which is being modified as follows: A set of n students and another set of n instructors have each ranked all members in the opposite group with a unique number between 1 and n in order of preference of who they would like as their advisor and student. Find a set of n pairings such that there are no two people in opposite sets who would both rather be paired with each other than their current partner. Here is a pseudoc ode version of the algorithm: round = 1 Initialize all students and instructors to unpaired while there exists an unpaired student, s, who still has an instructor, i, to be paired with: = the highest ranked instructor on s?s list to whom he or she has not yet been paired with iii is unattached: s and i become paired else s2 = the student to whom i is currently paired with if i prefers s to s2: s and i become paired s2 becomes unpaired else s2 and i remain paired end if end if add 1 to round end while It can be shown that this algorithm always terminates and always produces a stable set of pairings, in the sense that there is no pair (s,i) who would both rather be paired with each other than their assigned pairing. Slightly different versions of the pseudocode can be found in various sources, but at the core they are all essentially the same algorithm.Explanation / Answer
package deffered;
import java.util.Scanner;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
public class DefferedAlgorithm {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter the count of students and instructors");
int studentdscount=scan.nextInt();
//======> Single Entry
SimpleEntry<String, String> myEntry = new SimpleEntry<String, String>("ID", "Text");
System.out.println("student: " + myEntry.getKey() + " Instructor:" + myEntry.getValue());
//======> List of Entries
List<Entry<String, String>> pairList = new ArrayList<>();
System.out.println("pairing started..........");
for (int i = 1; i <=studentdscount; i++) {
//-- one liner:
if(i!=studentdscount)
pairList.add(new SimpleEntry<String,String>("student"+i, "Instructor"+i)); //Ananomous add.
}
//-- Iterate over Entry array:
for (Entry<String, String> entr : pairList) {
System.out.println("student Instructor Pairing: " + entr.getKey() +":"+ entr.getValue());
}
System.out.println("Last instruction " +studentdscount+ " is free.........");
System.out.println("pairing complete..........");
System.out.println("enter to change index of student...");
int changestu=scan.nextInt();
System.out.println("enter to change index of student...");
int changeins=scan.nextInt();
pairList.remove(2);
pairList.add(new SimpleEntry<String,String>("student"+changestu, "Instructor"+changeins)); //Ananomous add.
for (Entry<String, String> entr : pairList) {
System.out.println("student Instructor Pairing: " + entr.getKey() +":"+ entr.getValue());
}
System.out.println("new one added to list");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.