Stable Matching

Consider the following (idealised) matchmaking problem. We are given


The Gale-Shapley Algorithm O(n2)

begin Gale-Shapley(Boys' ranking, Girls' rankings):
	until all boys are engaged do
		for each boy a with no fiances do
			a proposes to the girl he most prefers among those that 
			a has not yet proposed to
		for each girl b with new proposals do
			let a be b's most preferred new suitor
			if b has no fiance
				b gets engaged to a
			if b prefers a to her exisiting fiance
				b cancels her existing engagement
				b gets engaged to a
	All the engaged couples get married
end