Notes:


Problem : Complete the Canvas quiz on loop invariants and the Gale-Shapley algorithm.

Problem : Consider the following implementation of insertion sort.

insertionsort(A):
  i ← 1
  while i < len(A)
    insert(A, i)
    i ← i + 1

insert(A, i):
  j ← i
  while j > 0 and A[j-1] > A[j]
    temp ← A[j-1]
    A[j-1] ← A[j]
    A[j] ← temp
    j ← j - 1

Problem : Consider the special case of the stable matching problem with $n$ machinists and $n$ welders in which all the preferences are the same: each machinist's preference from highest to lowest are $w_1, w_2, \ldots, w_n$, and each welder's preferences are $m_1, m_2, \ldots, m_n$. Prove that in this special case there is a unique stable matching.

Problem : Describe a $\Theta(n)$ algorithm to find a stable matching in the following special case: every welder has the same preference list, and every machinist has one of two preference lists (so perhaps welders are ranked by either speed or accuracy and some machinists prefer fast welders while others prefer accurate welders). The inputs will be

For example, if the preference lists are

Machinist$pm$Welder$pw$
$m_1$ $w_1, w_2, w_3, w_4$ $w_1$ $m_1, m_3, m_2, m_4$
$m_2$ $w_2, w_1, w_4, w_3$ $w_1$ $m_1, m_3, m_2, m_4$
$m_3$ $w_2, w_1, w_4, w_3$ $w_1$ $m_1, m_3, m_2, m_4$
$m_4$ $w_1, w_2, w_3, w_4$ $w_1$ $m_1, m_3, m_2, m_4$
Then n = 4, prefW = [1, 3, 2, 4], prefM = [ [1, 2, 3, 4], [2, 1, 4, 3] ], and type = [1, 2, 2, 1] (note that the arrays use 1-based indexing).

You needn't prove that your algorithm is correct, but you should explain why your algorithm runs in $\Theta(n)$ time.