Homework 1
Due by 11:30pm, Wednesday, September 16, 2026.
Make sure you follow all the homework policies.
Submit non-code work through UBLearns. Submit the programming assignment through Autolab.
-
Just in case there is any confusion: this is a totally made up scenario: I'm not claiming the ability to predict the future.
Some Questions on Stable Matching
Sample Problem
The Problem
Decide whether the following statement is true or false:
If you state true then you will have to formally argue why the statement is correct. If you state false, then you have to give a counter-example.
Proof Idea
We'll construct a simple counterexample to illustrate that the statement is false. Although you can certainly devise plenty of possible instances of the Stable Matching Problem in which there is a stable matching not containing any pair $(m,w)$ who both rank each other last, the statement says it must be true in every case, so even one counterexample makes it false. Since we want to make life easy for ourselves, we will construct a counterexample with two men and two women, since we can't find a smaller counterexample than for $n=2$. If we chose the preference lists such that each of the two men has the same preference list, and likewise, each of the two women also has preference list, there exists a stable matching having a pair $(m, w)$ who both rank each other last.
Proof Details
False Consider the following instance of the Stable Marriage Problem (SMP) that we saw in class. Let $n=2$ such that $M=\{m_{1},m_{2}\}$, $W =\{w_{1},w_{2}\}$ and: \[m_1: (w_1>w_2) ~~ w_1: (m_1>m_2)\] \[m_2: (w_1>w_2) ~~ w_2: (m_1>m_2)\] $m_2$ and $w_2$ have each other as their least preferred partner.
For the above preference lists: $m_1$ and $w_1$ have each other as their first preferred partner. So, $(m_1, w_1)$ must be a stable pair. Since $(m_1, w_1)$ is a stable pair, $m_2$ must choose $w_2$ as another stable pair. So, these preference lists only have the following stable matching: $(m_1,w_1), (m_2,w_2)$. Note that $m_2$ and $w_2$ are matched to their least preferred partner.
More Practice Questions
The Problem
Exercises $1$ and $2$ in Chapter $1$.
Submission
You will NOT submit this question. This is for you to get into thinking more about the stable matching problem.
Question 1 (Stable TV Show Schedules) [50 points]
The Problem
It is the year 2035 and only two online video content platforms have survived the frenzied competition to get people to watch their ``TV" online. 1 In the future, Amazing and Bombastic are the two surviving platforms. As is the case now, both these platforms release all the episodes of a new show on the same day and the future consumer has been trained to watch all of the episodes in one sitting. However, Amazing (henceforth just called $A$) and Bombastic (henceforth just called $B$) are still competing with each other and they have to figure out how to schedule the release dates of their new shows for fall of 2035.
In particular, $A$ and $B$ have $n$ shows in their roster that they want to schedule on the same $n$ release dates.
Since this is 2035 both $A$ and $B$ have the entire TV show preferences of every human being on the planet, given a show $m$ it can accurately predict a rating for $m$. You can assume that the rating is an integer and a larger rating the larger number of viewers will watch that show (and hence make more money). We say for a given release date a platform wins the release date if its released show on that date has a higher rating than any other show released on the same day.
What about ties?
If on a given date both platforms release shows with the same rating, then you should assume that $A$ wins.
At the beginning of each fall, $A$ reveals a schedule $S$ and $B$ reveals a schedule $T$. A schedule for a platform assigns each of its $n$ shows to the $n$ release dates. Thus, given the schedules $S$ and $T$, each release date has exactly two shows being released on that date: one from $A$ and another from $B$. The goal of the studios in designing the schedules $S$ and $T$ is to win on as many release dates as possible.
In this problem, you will explore the notion of stability in this context. In particular, a given pair of schedules $(S,T)$ is said to be stable if no platform can unilaterally change its own schedule and end up winning on more release dates. More precisely, the pair of schedule $(S,T)$ is stable if for any alternate set of schedules $S'$ for $A$, $A$ wins on no more release dates with the pair of schedules $(S',T)$ than with the pair $(S,T)$. Further, for any other schedule $T'$ for $B$, $B$ wins on no more release dates with the pair of schedules $(S,T')$ than with the pair $(S,T)$.
Here is an example to illustrate the above. Consider the case of $n=3$ and $A$ has show/rating pairs of $(m_1,100), (m_2,101), (m_3,102)$ while $B$ has pairs $(m_4,1), (m_5,2), (m_6,3)$, where each $m_i$ is a show and the corresponding number is its rating. In this case every pair of schedule $(S,T)$ is stable (since $A$ wins on all the release dates irrespective of the schedule).
The corresponding question to one we asked in class is the following: For every set of $n$ shows and their corresponding ratings, does there always exist a stable pair of schedules?
Here are the two parts:
- Part (a): Show that the answer is yes for the following special case: the lowest rating of any show in $A$ is strictly larger than the largest rating of any show in $B$.
- Part (b): Resolve the question for the general case in one of the following two ways:
- Give an algorithm that for any set of shows for $A$ and $B$ and their corresponding ratings, computes a stable pair of schedules (aka the answer is yes); OR
- Give an example of shows and their corresponding ratings, for which there does not exist any stable pair of schedules (aka the answer is no).
Note
It is helpful for this problem, to formally write down in first order logic, what the problem is asking for. To help y'all out, here is an overview of what you need to do:
- If you are providing an algorithm then for every input, your algorithm should output a pair of schedules $(S,T)$ that is stable. You will also have to prove that your algorithm outputs a pair of stable schedules on every input.
- if you are providing a counter-example, then you have to present only one input and then prove that every possible pair of schedules $(S,T)$ is NOT stable.
Submission
Submit part (a) and (b) separately
You need to submit two (2) PDF files through UBLearns: one for part (a) and one for part (b). While you can assume part (a) as a given for part (b), to get credit for part (a) you have to submit your solution for part (a) separately from part (b).
Make sure you submit the correct PDF to the correct submission link on UBLearns. If you do not (e.g. if you submit Q1(a) PDF to Q1(b) or even Q2(a) or Q2(b)), then you will lose ALL points.
We recommend that you typeset your solution but we will accept scans of handwritten solution-- you have to make sure that the scan is legible.
PDF only please
If UBLearns cannot display your file, (irrespective of the reason) then you will get a zero (0) on the entire question.
UBLearns might not be able to display files in formats other than PDF (e.g. Word cannot be displayed). Note that UBLearns will "accept" your submission even if you submit non-PDF file, so it is YOUR responsibility to make sure you submit in the correct format.
Also the file size has to be at most 3MB.
Grading Guidelines
We will follow the usual grading guidelines for non-programming questions. Here is a high level grading rubric specific to part (a) of this problem:
- Proof idea:
10points.
- Proof/Algorithm idea:
20points for If your answer is yes: the idea behind the algorithm that for any input, computes a pair of stable schedules.
- If your answer is no: a counterexample idea explaining the insight behind why you think the property does not hold.
- Proof/Algorithm details:
20points for If your answer is yes: details of the algorithm that for any input, computes a pair of stable schedules and an argument as to why your algorithm will always output a pair of stable schedules for every input.
- If your answer is no: a complete description of a counterexample and a complete proof for why the given counter example does not have any stable schedule.
Note
If you do not have separated out and labeled proof/algorithm idea and proof/algorithm details for part (b), you will get a zero (0) irrespective of the technical correctness of your solution.
Note
You must explicitly list your sources and collaborators in your submission file before uploading it to UBLearns. Note that you can only use one of the five allowed sources. If you have used a source that is not allowed, please do not submit your homework. If you did not consult any source and/or did not collaborate with anyone just say None.
Question 2 (Many Stable Matchings) [25 points]
The Problem
In class we will shortly see that every stable matching instance has at least one stable matching. We have also seen a stable matching instance that has two stable matchings. In this problem, you will be asked to come up with stable matching instance that have (many) more stable matchings. Here are the two parts:
- Part (a): Show that for every large enough $n$, there is a stable matching instance on $n$ men and $n$ women such that the instance has at least $\Omega(n)$ distinct stable matchings.
- Part (b):
For every $n \ge 2$ that is a multiple of $2$, show that there is a stable matching instance on $n$ men and $n$ women such that the instance has at least $2^{n/2}$ distinct stable matchings.
To get full credit, you should be able to present an instance for every $n \ge 2$ that is a multiple of $2$.
Hint
First try and construct such an instance for $n=2$. Try and extend the instance to every $n$ that is a multiple of $2$.
Note
If you show a family of instances with (many) more stable matchings than $2^{n/2}$ that is of course fine. In fact, if you are looking for a puzzle, try and prove as large a lower bound on the number of stable matching as you can. If you get something better than say $3^n$ come talk to me. Warning: Be warned that doing so might not be easy (it took many decades for such a result to be proven).
Submission
Submit part (a) and (b) separately
You need to submit two (2) PDF files through UBLearns: one for part (a) and one for part (b). While you can assume part (a) as a given for part (b), to get credit for part (a) you have to submit your solution for part (a) separately from part (b).
Make sure you submit the correct PDF to the correct submission link on UBLearns. If you do not (e.g. if you submit Q2(a) PDF to Q2(b) or even to Q1(a) or Q1(b)), then you will lose ALL points.
We recommend that you typeset your solution but we will accept scans of handwritten solution-- you have to make sure that the scan is legible.
PDF only please
If UBLearns cannot display your file, (irrespective of the reason) then you will get a zero (0) on the entire question.
UBLearns might not be able to display files in formats other than PDF (e.g. Word cannot be displayed). Note that UBLearns will "accept" your submission even if you submit non-PDF file, so it is YOUR responsibility to make sure you submit in the correct format.
Also the file size has to be at most 3MB.
Grading Guidelines
We will follow the usual grading guidelines for non-programming questions. Here is a high level grading rubric specific to part (a) of this problem:
- Proof idea:
10points.
- Proof idea:
7points for outlining the proof idea. - Proof details:
8points for the proof of the general case (i.e. for every $n \ge 2$ that is a multiple of $2$). -
Note: If your solution only presents example(s) for some specific $n \ge 2$, then you will get no points.
Note
If you do not have separated out and labeled proof idea and proof details for part (b), you will get a zero (0) irrespective of the technical correctness of your solution..
Note
You must explicitly list your sources and collaborators in your submission file before uploading it to UBLearns. Note that you can only use one of the five allowed sources. If you have used a source that is not allowed, please do not submit your homework. If you did not consult any source and/or did not collaborate with anyone just say None.
Question 3 (Programming Assignment) [25 points]
Note
This assignment can be solved in either Java, Python or C++ (you should pick the language you are most comfortable with). Please make sure to look at the supporting documentation and files for the language of your choosing.
The Problem
Given an input for the stable matching problem, output all stable matchings for that instance.
Input
The input is an instance of the stable marriage problem in a text file of the following format:
n <- Number of men and women (each)
m11 m21 m31 ... mn1 <- Preference of the 1st woman (most preferred first)
m12 m22 m32 ... mn2 <- Preference of the 2nd woman (most preferred first)
m13 m23 m33 ... mn3 <- Preference of the 3rd woman (most preferred first)
.
.
.
m1n m2n m3n ... mnn <- Preference of the nth woman (most preferred first)
w11 w21 w31 ... wn1 <- Preference of the 1st man (most preferred first)
w12 w22 w32 ... wn2 <- Preference of the 2nd man (most preferred first)
w13 w23 w33 ... wn3 <- Preference of the 3rd man (most preferred first)
.
.
.
w1n w2n w3n ... wnn <- Preference of the nth man (most preferred first)
For example
4 <- Number of men and women (each)
3 4 2 1 <- Preference of the 1st woman (most preferred first)
2 4 3 1 <- Preference of the 2nd woman (most preferred first)
2 3 4 1 <- Preference of the 3rd woman (most preferred first)
4 1 2 3 <- Preference of the 4th woman (most preferred first)
1 2 3 4 <- Preference of the 1st man (most preferred first)
3 1 4 2 <- Preference of the 2nd man (most preferred first)
2 1 4 3 <- Preference of the 3rd man (most preferred first)
4 1 2 3 <- Preference of the 4th man (most preferred first)
Output
The output is the set of all stable matchings for the input in a text file of the following format:
x <- Number of stable matchings found
[(m11,1),(m21,2),(m31,3),(m41,4),...,(mn1,n)] <- Pairing of the form (m,w)
[(m12,1),(m22,2),(m32,3),(m42,4),...,(mn2,n)] <- Each stable matching is sorted by women (1...n)
[(m13,1),(m23,2),(m33,3),(m43,4),...,(mn3,n)]
.
.
.
[(m1x,1),(m2x,2),(m3x,3),(m4x,4),...,(mnx,n)] <- xth stable matching
Please note that each stable matching outputted is sorted by women first. For example:
3 <- Number of stable matchings found
[(3,1),(1,2),(2,3),(4,4)] <- Pairing of the form (m,w)
[(4,1),(2,2),(1,3),(3,4)]
[(4,1),(3,2),(1,3),(2,4)]
Please note that the above output is an example just for illustration purposes and is not the correct solution to the input example given above. For correct pairs of input and output, please look at any of the provided input/output pairs.
Hint
The best possible algorithm for this problem that we are aware of runs in time $O(n^2\cdot n!)$. This is not terribly efficient but an exponential runtime for this problem is unavoidable -- see Question 2. For this reason we will be testing your implementations with testcases on $n \le 10$.
It might be useful to implement Heap's algorithm as one of the functions that your algorithm implementation uses. Alternatively, you can use the language specific alternatives specified below.
Note
Both the input and output parsers in each of the three languages are already written for you. Note that you have to work with the input data structures provided (which will come pre-loaded with the data from an input file). Also note that you do not have to sort your output: we'll take care of that.
Addition is the only change you should make
Irrespective of what language you use, you will have to submit just one file. That file will come pre-populated with some stuff in it. You should not change any of those things because if you do you might break what the grader expects and end up with a zero on the question. You should of course add stuff to it (including helper functions and data structures as you see fit).
Directory Structure
├── src
│ ├── ub
│ ├── cse
│ ├── algo
│ ├── Driver.java
│ ├── HW1Utility.java
│ ├── Marriage.java
│ ├── Matching.java
│ ├── PreferenceLists.java
│ ├── Solution.java
├── testcases/
│ ├── input1.txt
│ ├── input2.txt
│ ├── input5.txt
├── outputs/
│ ├── output1.txt
│ ├── output2.txt
│ └── output5.txt
You are given six coding files: Driver.java, HW1Utility.java, Marriage.java, Matching.java, PreferenceLists.java and Solution.java. Driver.java takes the input file, parses it with a new instance of HW1Utility and creates an instance of the class Solution and calls the outputStableMatchings() method on it. It then prints all the computed perfect matchings (which, if your code is correct, will all be stable matchings). You only need to update the Solution.java file.
Hint
You're also given a helper method allPermutations in Solution.java that generates all the permutations (see below for one way to call this helper function). This helper function could be useful in your solution.
The testcases folder has 3 input files and their corresponding output files for your reference. We will use these three input files (and seven others) in our autograding.
Method you need to write:
/**
* This method must be filled in by you. You may add other methods and subclasses as you see fit,
* but they must remain within the Solution class.
* @return Your set of stable matches. Order does not matter.
*/
public ArrayList outputStableMatchings() {
/* The code below just calls the allPermutations function, and then just prints all permutattions*/
/* To compare your code's output with the sample output you need to comment out the part about printing the permutations*/
ArrayList<ArrayList<Integer>> listOfAllPermutations = new ArrayList<>();
listOfAllPermutations = allPermutations(numberOfMenAndWomen);
System.out.println("----------------------------");
System.out.println("Printing all possible permutations of [1,2,...n] for n ="+numberOfMenAndWomen);
System.out.println("Total number of permutation generated ="+listOfAllPermutations.size());
System.out.println("----------------------------");
for(ArrayList set : listOfAllPermutations){
System.out.println(set);
}
System.out.println("----------------------------");
/*allPermutations call done*/
return stableMatchings;
}
The Solution class has 5 instance variables.
numberOfMenAndWomenwhich is of type int and stores n.menwhich is of typeHashMap<Integer, ArrayList<Integer>>and stores the preference lists of men. Please note that the front of theArrayList<Integer>(index 0) denotes the most preferred partner.womenwhich is of typeHashMap<Integer, ArrayList<Integer>>and stores the preference lists of women. Please note that the front of theArrayList<Integer>(index 0) denotes the most preferred partner.stableMatchingswhich is of typeArrayList<Matching>and stores the set of stable matchings you find for the given instance.countwhich is of type int and used for the helper methodpermutate.
The Other files
Marriage.java defines the Marriage class. This should be fairly intuitive. Below is the entire content of the class:
public class Marriage implements Comparable{
public Integer man;
public Integer woman;
Marriage(Integer man, Integer woman){
this.man = man;
this.woman = woman;
}
@Override
public boolean equals(Object obj){
return (man.equals(compare.man)) && (woman.equals(compare.woman));
}
@Override
public String toString(){
return "(" + man + ", " + woman + ")";
}
@Override
public int compareTo(Marriage other){
return this.woman.compareTo(other.woman)
}
}
The file HW1Utility.java handles some of the background stuff: e.g. readFile reads in the file passed as a command line argument to Driver.java and populates the preference lists within the HW1Utility class.
Compiling and executing from command line:
Assuming you're in the same directory level as src. Run javac src/ub/cse/algo/*.java to compile.
To execute your code on input1.txt, run java -cp "src" ub.cse.algo.Driver testcases/input1.txt.
Submission
You only need to submit Solution.java to Autolab.
Directory Structure
├── Driver.py
├── Utility.py
├── Marriage.py
├── PreferenceLists.py
├── Solution.py
├── testcases/
│ ├── input1.txt
│ ├── input2.txt
│ ├── input5.txt
├── outputs/
│ ├── output1.txt
│ ├── output2.txt
│ └── output5.txt
You are given five coding files: Driver.py, Utility.py, Marriage.py, PreferenceLists.py and Solution.py. Driver.py takes the input file, parses it and creates an instance of the class Utility.py and calls the output_stable_matchings() method on it. It then prints all the computed perfect matchings (which, if your code is correct, will all be stable matchings). You only need to update the Solution.py file. You may write your own helper methods and data structures in it.
The testcases folder has 3 input files and their corresponding output files for your reference. We will use these three input files (and seven others) in our autograding.
Hint
Python has a standard library tool: itertools.permutations which you might find useful for your code.
Method you need to write:
def output_stable_matchings(self):
"""
We are expecting the output to be a list containing stable matchings.
- each matching is a list of marriages
This does not have to be ordered in any specific way.
:return: a list of stable matching instances
"""
return self.stable_matchings
The Solution class has 4 instance variables.
numwhich stores n.menwhich is adictionaryof list of IDs of women in the preference order. Please note that the front of the listmen[i]for everyi(index 0) denotes the most preferred partner.womenwhich is adictionaryof list of men in the preference order. Please note that the front of the listwomen[i]for anyi(index 0) denotes the most preferred partner.stable_matchingswhich is a list of stable matchings. Each stable matching is a list ofMarriageclass.
The Other files
Marriage.py defines the Marriage class. This should be fairly intuitive. Below is the entire content of the class:
class Marriage:
def __init__(self, male_val, female_val):
self.m = male_val
self.w = female_val
def set_man(self, male_val):
self.m = male_val
def set_woman(self, female_val):
self.w = female_val
def man(self):
return self.m
def woman(self):
return self.w
def equals(self, marriage):
return (marriage.man() == self.m) and (marriage.woman() == self.w)
def __str__(self):
return "(" + str(self.m) + ", " + str(self.w) + ")"
__repr__ = __str__
def __lt__(self, other):
return self.w < other.woman()
def __eq__(self, other):
return self.w == other.woman() and self.m == other.man()
The file Utility.py handles some of the background stuff: e.g. read_file reads in the file passed as a command line argument to Driver.py and populates the preference lists within the Utility class.
Executing from command line:
Assuming you're in the same directory level as Driver.py and you want to run your code on the input1.txt. Run python Driver.py testcases/input1.txt.
Submission
You only need to submit Solution.py to Autolab.
Directory Structure
├── Driver.cpp
├── Solution.cpp
├── PreferenceLists.h
├── HW1Utility.h
├── Utility.h
├── testcases/
│ ├── input1.txt
│ ├── input2.txt
│ ├── input5.txt
├── outputs
│ ├── output1.txt
│ ├── output2.txt
│ └── output5.txt
You are given three coding files: Driver.cpp, HW1Utility.h and Solution.cpp. Driver.cpp takes the input file, parses it and creates an instance of the class Solution and calls the outputStableMatchings() method on it. It then prints all the computed perfect matchings (which, if your code is correct, will all be stable matchings). You only need to update the Solution.cpp file. You may write your own helper methods and data structures in it.
The testcases folder has 3 input files and their corresponding output files for your reference. We will use these three input files (and seven others) in our autograding.
Hint
C++ has a standard function: next_permutation which you might find useful for your code.
Method you need to write:
vector<vector<pair<int,int>>> Solution::outputStableMatchings() {
/*This is the function you will need to implement. */
// Takes the preferences in the map<int, vector<int>> men and women
// and outputs all the stable matchings as vector<vector<pair<int,int>>>
printf("Doing nothing now!\n");
return {};
}
The Solution class has 4 instance variables.
nwhich stores $n$.menwhich is amap<int, vector<int>>. I.e.men[i]is the preference list of manigiven asvectorof IDs of women in the preference order. Please note that the front of the vectormen[i]for anyi(index 0) denotes the most preferred partner.womenwhich is amap<int, vector<int>>. I.e.women[i]is the preference list of manigiven asvectorof IDs of men in the preference order. Please note that the front of the vectorwomen[i]for anyi(index 0) denotes the most preferred partner.stableMatchingsis a list of stable matchings, represented asvector<vector<pair<int,int>>>.
Compiling and executing from command line:
Assuming you're in the same directory level as Driver.cpp. Run g++ -std=c++11 Driver.cpp to compile.
To execute your code on input1.txt, run ./a.out testcases/input1.txt.
Submission
You only need to submit Solution.cpp to Autolab.
Grading Guidelines
We will follow the usual grading guidelines for programming questions.