Table of Contents

2.3 Implementing The Stable Matching Algorithm using Lists and Arrays

To asymptotically analyze the running time of an algorithm, we don't need to implement and run it, but we definitely have to think about how the data will be represented and manipulated in an implementation of the algorithm so we could bound the number of computational steps it takes. This section looks at how to implement the Gale-Shapley algorithm using lists and arrays. In the Stable Matching Problems that the algorithm solves, we need to think about how the rankings will be represented, as each man and each woman has a ranking of all members of the opposite gender. In addition, we will need to know which men and women are free, and who is matched with whom. Finally, we need to decide which data structures to use for all of the mentioned data. The goal of for the designer is to choose data structures that will make the algorithm efficient and easy to implement.

Algorithm Analysis

Let's focus on a single list, such as a list of women in order of preference by a single man.

For dynamically maintaining a list of elements that change a lot over time such as a list of free men, an array is less preferable. Indeed, we our collection needs to grow and shrink very frequently throughout the execution of the algorithm. Thus we choose an alternative to an array: the linked list.

In a linked list, each element points to the next in the list. Thus:

Disadvantage of linked lists: To find the ith element we have to follow the “next” pointers starting from “First” until we get the element, which takes O(i), whereas it takes us O(1) time with arrays.

Using arrays and linked lists, it takes us O(n) to convert back and forth.

Implementation of the algorithm

The algorithm takes O(n2) to terminate, so we need to implement each iteration in O(1) time.

To implement our algorithm in constant time, we need to implement each of the following in constant time:

  1. Selecting a free man: We need to maintain a list of free men as a linked list. To select a man, we take the first man m on the list. If m becomes engaged, we delete m from the list. If some other man m' becomes free, he's inserted at the front of the list. All of these operations are thus done in constant time using a linked list
  2. Identifying the highest-ranked woman to whom a man m has not yet proposed: To achieve this:
    • We maintain an extra array,call it Next that indicates for each man mthe position of the next woman he will propose to on his list.
    • We initialize Next = 1 for all men m.
    • If a man m needs to propose to a woman, he will propose to w = ManPref[m,Next[m]]
    • Once m proposes to w, we increment the value of Next[m] by one, whether or not w accepts m's proposal.
  3. Identifying the man m'(if such man exists) w is currently engaged to in case m proposes to w: To do this:
    • We can maintain an array Current of length n
    • Current = null if w is not currently engaged
    • Thus Current = null for all women w at the start of the algorithm.
  4. Deciding which of m or m' is preferred by w:To achieve constant time for this step:
    • At the start of the algorithm, we create an n X n array Ranking
    • Ranking[w,m] contains the rank of man m in the sorted order of w's preferences
    • Creating the array Ranking is O(n2) in time, since it requires us to create such an array for each woman w.
    • To decide which of m or m' is preferred by w, we just compare the values Ranking[w,m] and Ranking[w,m'],effectively doing it in constant time.

All of the data structures mentioned above help us implement the Gale-Shapley algorithm in O(n2) time.

To be honest, I hadn't understood quite well how we can specify the woman w a man m will propose to, and do it in constant time,before I reread the section and get how we can work it out. The preference lists representation was a little bit trickier because I was thinking of a different way of implementing them, but I'm now convinced the method in the book is really efficient.

This section was really interesting, it does seem like materials in the book get more and more interesting as we advance through the book.I give this section a 9/10.