Administrative info
  HW2 due tomorrow!
  PA1 due Friday!

  So far, we've seen how to express statements precisely using
  propositional logic.

  Once we knew how to express a statement, we were then interested in
  how to prove that a statement is true or false. We saw many
  different types of proofs: direct, contraposition, contradiction,
  cases, simple induction, and strong induction.

  Now that we know how to prove facts, the next step is to be able to
  look at actual algorithms and prove facts about them. In this class,
  we will look at one algorithm in particular that has many
  applications in the real world. It is used by some dating services,
  to match medical interns to hospitals, to match jobs to web servers,
  to match organ donors to recipients, and so on. Essentially, the
  algorithm can be applied to match a set of one type of item to a set
  of another type of item.

  Note that while the algorithm we will look at is important in and of
  itself, the main reason we are looking at it is to demonstrate how
  the proof techniques we have learned can be applied to reason about
  computer science algorithms in general.

Problem statement
  Suppose we have a small town of n single men and n single women.
  They all know each other, and they all are looking for love, so
  naturally, each of them has formed a set of preferences ranking the
  members of the opposite sex. Each preference list contains every
  member of the opposite sex, with no ties in the list.

  How should we match the men and women?

  I don't know what the best criteria are, but at a minimum, we have
  to ensure that there are no mutual love affairs; in other words, we
  do not want there to be a man and a woman who aren't paired but like
  each other more than their partners. If this happens, then
  eventually, the two of them will run off with each other, causing a
  great scandal which we don't want in our small town.

  Here is an example. Suppose we have the following matching:
    Brad -------- Jennifer
    BillyBob ---- Angelina
  Now suppose they had the following preference lists:
    Brad: Angelina > Jennifer
    BillyBob: Angelina > Jennifer
    Jennifer: Brad > BillyBob
    Angelina: Brad > BillyBob
  What do you suppose will happen? Brad and Angelina will start spending
  more and more time with each other instead of their partners, until
  one day they decide to run off together.

  We call Brad and Angelina a "rogue couple." Any pairing that has a
  rogue couple is doomed to failure, since the rogue couple will
  eventually become dissatisfied with their own partners and run off
  for a better life together.

  Thus, at the minimum, we want a pairing with no rogue couples. Such a
  pairing is called "stable."

  Our problem is to find a stable pairing given n men and n women and
  their preference lists.

  But wait! We don't even know whether a stable pairing always exists!
  In fact, of we were looking at forming pairs out of a single set of
  people (e.g. roommates), we could demonstrate that no stable pairing

  We will demonstrate an algorithm for the problem and show that it
  always produces a stable pairing. Thus, a stable pairing always
  exists. (Proof by algorithm!)

Tranditional Marriage Algorithm
  Our algorithm is based on a model of dating you might see in a movie
  for the 1950s, hence the name "Traditional Marriage Algorithm."
  Here's how it works:
  - Each morning, each boy stands under the balcony of his favorite girl
    whom he hasn't yet crossed off his list and declares his love for
    her (think Romeo and Juliet!). If there are no girls left on his
    list, he stays home and does CS70 homework.
  - Each afternoon, every girl who has at least one suitor outside
    serenading her under her balcony picks her favorite from among
    them. She tells everyone else, "No, I will never marry you!" She
    tells here favorite, "Maybe; ask me again tomorrow."
  - Each evening, every boy who is rejected and dejected crosses the
    girl who rejected him off his list.
  The algorithm stops one every girl has at most one suitor, and each
  girl is paired off with her current suitor.

    Angie: Nate > Max > Oscar
    Betty: Max > Nate > Oscar
    Carol: Max > Nate > Oscar
    (Poor Oscar! Maybe if he wasn't such a grouch?)
    Max: Angie > Betty > Carol
    Nate: Betty > Angie > Carol
    Oscar: Angie > Betty > Carol

    On first day:
      Angie: Max, Oscar propose
      Betty: Nate proposes
      Carol: stays home and washes her hair

      Angie tells Oscar, "I won't marry you, but we can be just
      friends..." She tells Max, "I need some more time to think about
      it. Come back tomorrow."
      Betty also stalls with Nate.

    On second day:
      Angie: Max proposes (again)
      Betty: Nate, Oscar propose
      Carol: orders out for pizza

      Betty tells Oscar, "I'm sorry. It's not you, it's me." She once
      again tells Nate that she needs more time.

    On third day:
      Angie: Max proposes (again)
      Betty: Nate proposes (again)
      Carol: Oscar proposes

    We're done! Time for the party...

  First question: does TMA always terminate, or is poor Max condemned
  to do his best Sisyphus impression and propose to Angie every day
  for the rest of his life?

  Theorem: TMA terminates within n^2 + 1 days.
  Proof: On any day that TMA doesn't terminate, there must be at least
    one girl with at least two suitors. Then she rejects at least one,
    who crosses her off her list. So on each day that TMA doesn't
    terminate, at least one girl gets crossed off one list. Since
    there are only n^2 list entries to cross off (n entries for each
    of n boys), there can be at most n^2 days before which TMA

  Does everyone end up with a partner? In order to answer this, let's
  first show the following:

  Improvement Lemma: If a woman W says "Maybe" to man M on jth day,
    then on every subsequent day she will say "Maybe" to a man she
    likes at least as much as M.
  (Intuitively, she only rejects M if someone better comes along, so
   she only trades upwards on her preference list.)
  Proof: By induction on the day k, k >= j.
  Base case: Man chosen on day j at least as good as man on day j.
    Obviously true!
  Inductive hypothesis: Man chosen on day k at least as good as M.
  Inductive step: On day k+1, the man chosen on day k will come back
    and propose to W again. Since W chooses her best suitor, she will
    choose someone at least as good as the man from day k. Since this
    man is at least as good as M (by inductive hypothesis), she
    chooses someone who is also at least as good as M.

  Corollary: Every woman will marry her favorite of all the men who
    ever propose to her.

  Lemma: No man can be rejected by all the women.
  Proof: Suppose poor Oscar is rejected by every woman. This means
    that he proposed to each woman at some point. Each of these women
    rejected him for someone better, so by the improvement lemma, they
    each end up with someone better than Oscar. But since there are n
    women, that means that there must be n men other than Oscar, or
    n+1 total, which is a contradiction!

  Corollary: TMA terminates with a complete pairing.

  Now we prove that the output of TMA is always stable.

  Theorem: TMA outputs a stable pairing.
  Proof: Consider an arbitrary couple (Jacob, Renesmee) in the pairing
    produced by TMA. Suppose that Jacob prefers some woman Bella to
    his partner Renesmee. Thus, he must have proposed to Bella before
    settling for Renesmee, who rejected him for someone better (say
    Edward). By the improvement lemma, Bella must end up with someone
    she likes at least as much as Edward, so she ends up with someone
    she likes more than Jacob. Thus, Jacob and Bella cannot be a rogue

  We have now seen that not only does TMA produce a pairing, but it
  produces a stable pairing. How many stable pairings are there for a
  particular set of men and women and preferences? It turns out that
  there may be more than (see reader for an example). The obvious
  question then is, which one does TMA produce?

  Let's consider all stable pairings and define the "optimal" woman
  for man M to be his favorite partner from among all those stable
  pairings. Similarly, his "pessimal" woman is his least favorite
  from among those pairing. We can define each woman's optimal and
  pessimal man in the same way.

  Then if a stable pairing exists in which every main is paired to his
  optimal woman, we call this pairing "male-optimal." We can similarly
  define a "male-pessimal" pairing.

  Question: What pairing do you imagine TMA produces?

  It turns out that TMA always produces a male-optimal pairing!
  Proof: Suppose for the sake of contradiction that the output of TMA
    is not male-optimal. This means that at some point, there is a man
    who is rejected by his optimal wife. Let's consider the first
    instance in which this happens. Let's call the rejected man Jacob,
    the woman who rejected him Bella, i.e. Jacob's optimal woman, and
    Edward the man whom she rejected Jacob for. Thus, Bella likes
    Edward > Jacob. Now since Jacob was the first to be rejected by
    his optimal woman, we know that Edward likes Bella at least as
    much as his optimal woman, and in fact, any woman who he is paired
    with in a stable pairing. But if Jacob were matched with Bella in
    a stable pairing, then Bella -- Edward would be a rogue couple,
    since Bella likes Edward > Jacob and Edward likes Bella better
    than the woman whom he is matched with. Contradiction!

  OK, that covers the men. What about the women? Do they get it good
  as well? Sorry, ladies.

  Theorem: Any male-optimal pairing is female-pessimal.
  Proof: Let T by the male-optimal pairing, and consider an arbitrary
    couple (John, Jennifer) in T. Now consider any stable pairing S in
    which John and Jennifer are paired with someone else, say (John,
    Jessica), (Vince, Jennifer). Since T is male-optimal, we know that
    John prefers Jennifer > Jessica. Since S is stable, we know that
    (John, Jennifer) is not a rogue couple, so Jennifer must prefer
    Vince > John. Thus, Jennifer prefers S to T. Since we made no
    assumptions about S, she must prefer any other stable pairing to
    T. And since we made no assumptions about Jennifer, every woman
    must prefer any other stable pairing to T.

  Hospital/intern matching
  - Each hotel has a specified number of slots to fill. Hospitals
    prefer most qualified students, students preferred hospitals in
    favorable locations.
  - Intern salary and working conditions are standardized.
  - In 1940s, hospitals began to compete by sending out offers earlier
    and earlier to students. Eventually it got to the point where some
    students were being asked to commit to a hospital in their junior
    or even sophomore year, and getting worse. Crazy.
  - Led hospitals to adopt TMA for matching students to hospitals.
  - Hospitals used to use TMA in hospital-optimal form, until very
    recently; now they use TMA in student-optimal form. As it turns
    out, hospital- vs intern- optimal doesn't matter much in that
  - Some speculation that "the match" might be contributing to low
    salaries, because students don't have the option to say that
    they'd accept a lower-preference location in exchange for a higher

Why did I show you this?  It's an example of applying proof techniques
to analysis of algorithms and processes.