Posted by: bwhitten518 | September 28, 2010

Lecture 12 – GS implementation

(Guest post by Brady Whitten)

Lecture 12
We began with finishing the analysis and implementation of the G-S algorithm.

Specifically finishing steps:

(IV) How do we know who   m   is engaged to?

(V)   How do we decide if    w’ > w   in   Lm (m’s preference list).

We have previously predicted the run-time of the G-S algorithm to be at most n² iterations of the while loop and O(1) time for the rest.

From last lecture: we created a two way linked-list of free women an array Next, where Next[w] = rank of next man w should propose to, and two 2D arrays ManPref [m,j] & WomanPref [m,j].

Most -> Least preferred

1    2   3   4 …..

m1    |   1   2   3       |

m2   |   2   1   3       |

m3   |   3   1   2       |


We found that for G-S steps (i) & (ii), initialization was O(n) and updating was O(1).

So overall run-time was:   O(n) + O(n) + Initialization(iv) + Initialization(v)


n² x [ O(1) + O(1) + Update (iv) + Update(v) ]

G-S section (iv): Array Current : [][][][][m][][][]….[]    where Current[m] = current fiance of m


make current[m] = -1 for 1 ≤ m ≤ n   where -1 means a man is free

O(n) time


current[m] := w

O(1) time

G-S section (v): We first tried a brute force approach to find if w’ > w in   Lm:

Go over ManPref [m,j]

find j & j’ such that ManPref [m,j] = w ; ManPref [m,j’] = w’

w > w’ in Lm if and only if j’ > j

j       j’

|      ↑     ↑   |          —>   go through list one by one: O(n) time and is therefore WASTEFUL!

m  | w    w’ |

|                    |


Now for a more efficient solution: Representing the preferences in another way

– 2D array Ranking

w     w’

|      ↑    ↑    |          —>   Ranking [m,w] = rank of   w   in  Lm.

m  | j    j’ |

|                    |                    therefore w’ > w  in  Lm   if and only if Ranking [m,w’] < Ranking [m,w]



Ranking [m,w] = j  if and only if  ManPref [m,j] = w

↓                              ↓

Ranking [m, ManPref [m,j]] = j    for   1 ≤ m ≤ n   and  1 ≤ j ≤ n

Nested loop to fill 2D array therefore O(n²)


time is O(1)

Therefore we have completed the implementation and proven…

Theorem: GS algo can be implemented in O(n²)

And then we moved on to Chapter 3 and graphs

Graphs – representation of relationships between pairs of entities/elements

Eg) given the entities of news hosts and the relationship that they are mentioned in each other’s      news programs

And we ended on the ominous quote from Atri that graphs are omnipresent.

Safe algorithming,




  1. I’m glad that came out formatted completely different than how I had it.. sorry about the sloppiness

    • Hi Brady,

      No worries. Looks fine.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: