welcome to cse 589 -- applied algorithms anna r. karlin (karlin@cs) office hours: wed 9:20 - 10:00...

133
Welcome to Welcome to CSE 589 -- Applied Algorithms CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Upload: anna-jennifer-powers

Post on 12-Jan-2016

221 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Welcome toWelcome toCSE 589 -- Applied AlgorithmsCSE 589 -- Applied Algorithms

Anna R. Karlin (karlin@cs)

Office Hours: Wed 9:20 - 10:00 pm

and by appointment

TA: Ashish Sabharwal (ashish@cs)

Office Hours: W 5:30-6:20pm

Page 2: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

AdministriviaAdministrivia

Books, etc.• The Algorithm Design Manual

• Steven Skiena

Grading:• Weekly problem sets 50%

• Final 50%

Page 3: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Course GoalsCourse Goals

An appreciation for applications of algorithmic techniques in the real world.

A larger toolbox. A better sense of how to model problems

you encounter as well-known algorithmic problems.

A deeper understanding of the issues and tradeoffs involved in algorithm design.

FUN!!!!!!!!!!!!!! BEAUTY!!!!!!!!!!

Page 4: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

OtherOther

Interested in suggestions, feedback, constructive criticism. (E.g., topics/depth vs. breadth)

Ask lots of questions! Participate! Offer opinions!

Page 5: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Plan For TodayPlan For Today

Intro -- Skiena, chapter 1

Cool lectures from CMU• intro to “great theoretical ideas in computer science”

• the stable marriage problem

Warning: very different from the typical lecture in this course

Page 6: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Algorithm Design GoalsAlgorithm Design Goals

correctness efficiency

According to the Oxford English Dictionary algorist “ one skillful in reckonings or

figuring”

Page 7: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Keeping Score:Keeping Score:The RAM Model of ComputationThe RAM Model of Computation

Each simple operation takes 1 time step. Loops and subroutines are not simple operations. Each memory access takes one time step, and

there is no shortage of memory.For a given problem instance: Running time of an algorithm = # RAM steps (ops) Space used by an algorithm = # RAM memory cells

useful abstraction => allows us to analyze algorithms in a machine independent fashion.

Page 8: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Types of complexityTypes of complexity

Function from size of instance to real numbers

Worst-case complexity Best-case complexity Average-case complexity

Page 9: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Big Oh NotationBig Oh Notation

Goals :• ignore details that do not impact comparisons of algorithms• ignore constant factors

f(n) = O(g(n)) <=> cg(n) is upper bound on f(n) <=> There exist c, N s.t. for n>=N, f(n) <= cg(n)

f(n) = g(n)) <=> cg(n) is lower bound on f(n) <=> There exist c, N s.t. for n>=N, f(n) >= cg(n)

f(n) = (g(n)) <=> f(n) = O(g(n)) and f(n) = g(n))

<=> There exist c1, c2, N s.t. for n>=N,

c1 g(n) <= f(n) <= c2g(n)

Page 10: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Growth Rates, etc.Growth Rates, etc.

Even by ignoring constant factors, can get an excellent idea of whether a given algorithm will be able to run in a reasonable amount of time on a problem of a given size.

The “big Oh” notation and worst-case analysis are tools that greatly simplify our ability to compare the efficiency of algorithms.

Page 11: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

CS 15-251CS 15-251

Professors Steven Rudich and Bruce Maggs

Carnegie Mellon University

CS 15-251LectureLecture 11

Page 12: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

• Content: An up to date grasp of fundamental problems and solutions

• Method: Principles and techniques to solve the vast array of unfamiliar problems that arise in a rapidly changing field

The future belongs to the The future belongs to the computer scientist who hascomputer scientist who has

Page 13: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Course ContentCourse Content

• A survey of fundamental theoretical ideas in Computer Science as they occur in a variety of important applications

• An introduction to discrete mathematics

• Effective problem solving, learning, and communication techniques

Page 14: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

A survey of fundamental A survey of fundamental theoretical ideas in Computer theoretical ideas in Computer

Science as they occur in a Science as they occur in a variety of important applicationsvariety of important applications

For example . . .

Page 15: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Growth RatesGrowth Rates

Input Size

Tim

e

Page 16: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

RecursionRecursion

?

?

Page 17: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

RandomizationRandomization

Page 18: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

HashingHashing

Page 19: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

ReductionReduction

=

Page 20: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Parallel Versus Sequential WorkParallel Versus Sequential Work

Page 21: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

DatingDating

Page 22: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Private CommunicationPrivate Communication

Page 23: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

FactoringFactoring / Multiplication / Multiplication

p, q

pq

Page 24: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

An introduction to discrete An introduction to discrete mathematicsmathematics

Page 25: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Count without counting:Count without counting:Estimate, Calculate, AnalyzeEstimate, Calculate, Analyze

Page 26: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

InductionInduction has many guises. has many guises.Master their Master their interrelationship.interrelationship.

• Formal Arguments

• Loop Invariants

• Recursion

• Algorithm Design

• Recurrences

Page 27: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Map ColoringMap Coloring

Page 28: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Effective problem solving, Effective problem solving, learning, and communication learning, and communication

techniquestechniques

Page 29: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Exemplification:Exemplification:Try out a problem orTry out a problem or

solution on small examples. solution on small examples.

Page 30: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Representation:Representation:Understand the relationship between Understand the relationship between different representations of the same different representations of the same

information or ideainformation or idea

1

2

3

Page 31: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Modularity:Modularity: Decompose a complex problem Decompose a complex problem

into simpler subproblemsinto simpler subproblems

Page 32: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

AbstractionAbstraction: : Abstract away the inessential Abstract away the inessential

features of a problemfeatures of a problem

=

Page 33: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Refinement:Refinement:The best solution comes from a The best solution comes from a

process of repeatedly refining and process of repeatedly refining and inventing alternative solutionsinventing alternative solutions

Page 34: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Build your toolbox of abstract structures and concepts.

Know the capacities and limits of each tool.

Page 35: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Similarity:Similarity:A significant form of intellectual A significant form of intellectual

progress isprogress is to be able to classify and to be able to classify and manipulate distinct objects with manipulate distinct objects with

regard to a sense in which they are regard to a sense in which they are similar.similar.

=

13 = 21 (modulo 2)

Page 36: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Please feel free to ask questions!

Page 37: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

How many yellow dots on this page?

Page 38: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Gauss

1+2+3+...+(n-1)+n = ?

Page 39: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Page 40: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Page 41: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Page 42: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

2

1)(n n S

Page 43: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Let’s restate this argument using a

geometric representation

Algebraic argument

Page 44: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 2 . . . . . . . . n

= number of white dots.1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

Page 45: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

1 2 . . . . . . . . n

= number of white dots

= number of yellow dots

n . . . . . . . 2 1

Page 46: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

n+1 n+1 n+1 n+1 n+1

= number of white dots

= number of yellow dots

n

n

n

n

n

n

There are n(n+1) dots in the grid

Page 47: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

1 + 2 + 3 + . . . + n-1 + n = S

n + n-1 + n-2 + . . . + 2 + 1 = S

(n+1) + (n+1) + (n+1) + . . . + (n+1) + (n+1) = 2S

n (n+1) = 2S

n+1 n+1 n+1 n+1 n+1

= number of white dots

= number of yellow dots

n

n

n

n

n

n

2

1)(n n S

Page 48: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Gauss

A first glance, these problems appeared unrelated, but A first glance, these problems appeared unrelated, but a a representationalrepresentational change revealed a correspondence. change revealed a correspondence. An extension of the An extension of the similaritysimilarity principle is that principle is that when we when we come to understand that seemingly unrelated things come to understand that seemingly unrelated things areare related, we are making intellectual progress. related, we are making intellectual progress.

1+2+3+...+ n = ?

Page 49: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Appreciate Alternative Solutions To The Same Problem

When presented with multiple solutions many students remember only the one that they find easiest to understand. The The expert student takes the opportunity to expert student takes the opportunity to think through the similarities and think through the similarities and differences between the approachesdifferences between the approaches. Such meditations lay the foundations for effective learning and problem solving.

Page 50: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Martial Arts 101Martial Arts 101

• The The novicenovice makes a makes a hugehuge motionmotion

• The The black beltblack belt makes a makes a smallsmall motion motion

• The The mastermaster makes a makes a tiny tiny motionmotion

Page 51: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Scanning the brains of master Scanning the brains of master problem solversproblem solvers

The better the The better the problem solver, problem solver, the less brain the less brain

activity is evident. activity is evident. The real masters The real masters show almost no show almost no brain activity!brain activity!

Simple and to the point

Page 52: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Aside to university studentsAside to university students

It is natural to think that you are really getting your money’s worth when the professor says very complicated things, but the real value is when the professor makes complex things simple.

Value Simplicity

Page 53: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Aside to university studentsAside to university students

A good martial arts student will attentively repeat each fundamental technique many times. In contrast, many college students tune out when a concept (e.g., depth first search) is taught more than once. The better students listen carefully in order to refine and develop their understanding.

Repetition Is An Opportunity

Page 54: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

If you value simplicity, listen actively to repeated points, and work through

the correspondences between different solutions, you are on your way to a

lifetime of effective learning and thinking.

With diligence, you may someday reduce your brain activity to zero!

Page 55: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Master ProgrammerThe Master Programmer

The master seeks an algorithm that will use as small an amount of computer resources (e.g., time, space, communication) as possible. Most “expert” programmers (black belts) will miss the best program because they did not have the patience to refine their solutions further.

Page 56: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

A case study.A case study.

Anagram Programming Task.

You are given a 70,000 word dictionary. Write an anagram utility that given a word as input returns all anagrams of that word appearing in the dictionary.

Page 57: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

ExamplesExamples

Input: CAT Output: ACT, CAT, TAC

Input: SUBESSENTIAL Output: SUITABLENESS

Page 58: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Impatient Hacker Impatient Hacker (Novice Level Solution)(Novice Level Solution)

Loop through all possible ways of Loop through all possible ways of rearranging the input wordrearranging the input word

–Use binary search to look up Use binary search to look up resulting word in dictionary.resulting word in dictionary.

–If found, output itIf found, output it

Page 59: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Performance Analysis:Performance Analysis:Counting Without ExecutingCounting Without Executing

On input "microphotographic" the loop

will run though iterations.

Even at one microsecond (10 per

iteration this will take seconds.

This is about a decade!

(About seconds in a nanocentury (10

-6

-9

17 3 10

3 10

14

8

!

)

))

Page 60: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

““Expert” HackerExpert” Hacker(Black Belt Level)(Black Belt Level)

Subroutine ANAGRAM(X,Y) returns TRUE exactly when X and Y are anagrams. It works by sorting the letters in X and Y

Input X Loop through all dictionary words Y

–If ANAGRAM(X,Y) output Y

Page 61: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Comparing an input word with Comparing an input word with each of 70,000 dictionary entries each of 70,000 dictionary entries

takes about 15 seconds.takes about 15 seconds.

The hacker is satisfied and reflects no further

Page 62: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The master keeps trying The master keeps trying to refine the solution.to refine the solution.

The master’s program runs in less than 1/1000 seconds.

Page 63: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Master SolutionMaster Solution

Don’t keep the dictionary in sorted order!

Rearranging the dictionary into anagram classes will make the

original problem simple.

Page 64: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Suppose the dictionary was the Suppose the dictionary was the list below.list below.

ASP DOG LURE GOD NICE RULE SPA

Page 65: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

After each word, write its After each word, write its “signature” (sort its letters)“signature” (sort its letters)

ASP APS DOG DGO LURE ELRU GOD DGO NICE CEIN RULE ELRU SPA APS

Page 66: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Sort by the signaturesSort by the signatures

ASP APS SPA APS NICE CEIN DOG DGO GOD DGO LURE ELRU RULE ELRU

Page 67: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Master ProgramMaster Program

Input word W X := signature of W Use binary search to find the

anagram class of W and output it.

About microseconds

.0004 seconds

log ( , )2 70 000 25

Page 68: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Of course, it takes about 30 seconds to create the dictionary, but it is perfectly fair to think of this as programming time. The building of the dictionary is a one-time cost that is part of writing the program.

Page 69: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Neat! I wish I had thought of that.

Page 70: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Learning AdviceLearning Advice

Whenever you see something you wish you had thought of, try and formulate the minimal and most general lesson that will insure that you will not miss the same thing the next time. Name the lesson to make it easy to remember.

Page 71: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

NAME: NAME: PreprocessingPreprocessing

It is sometimes possible to pay a reasonable, one-time preprocessing cost to reorganize your data in such a way as to use it more efficiently later. The extra time required to preprocess can be thought of as additional programming effort.

Page 72: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Mathematics Of 1950’s Dating:

Who wins the battle of the sexes?

CS 15-251Lecture Lecture 22

Page 73: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

WARNING: This lecture contains mathematical content that may be shocking to some students.

Page 74: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

3,5,2,1,4

1

5,2,1,4,3

4,3,5,1,2

3

1,2,3,4,5

4

2,3,4,1,5

5

1

3,2,5,1,4

2

1,2,5,3,4

3

4,3,2,1,5

4

1,3,4,2,5

5

1,2,4,5,3

2

Page 75: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Dating ScenarioDating Scenario

• There are n boys and n girls• Each girl has her own ranked

preference list of all the boys• Each boy has his own ranked

preference list of the girls• The lists have no ties

Question: How do we pair them off? Which criteria come to mind?

Page 76: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

3,5,2,1,4

1

5,2,1,4,3

2

4,3,5,1,2

3

1,2,3,4,5

4

2,3,4,1,5

5

1

3,2,5,1,4

2

1,2,5,3,4

3

4,3,2,1,5

4

1,3,4,2,5

5

1,2,4,5,3

Page 77: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

There is more than one notion of There is more than one notion of what constitutes a “good” what constitutes a “good”

pairing. pairing. • Maximizing total satisfaction

• Hong Kong and to an extent the United States

• Maximizing the minimum satisfaction• Western Europe

• Minimizing the maximum difference in mate ranks• Sweden

• Maximizing the number of people who get their first choice• Barbie and Ken Land

Page 78: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

We will ignore the issue of what is “equitable”!

Page 79: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Rogue CouplesRogue Couples

Suppose we pair off all the boys and girls. Now suppose that some boy and some girl prefer each other to the people to whom they are paired. They will be called a rogue couple.

Page 80: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Why be with them when Why be with them when we can be with each we can be with each

other?other?

Page 81: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Stable PairingsStable Pairings

A pairing of boys and girls is called stable if it contains no rogue couples.

Page 82: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Stability is primary.Stability is primary.

Any list of criteria for a good pairing

must include stability. (A pairing is doomed if it contains a rogue couple.)

Any reasonable list of criteria must contain the stability criterion.

Page 83: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The study of stability will be the The study of stability will be the subject of the entire lecture.subject of the entire lecture.

We will:• Analyze various mathematical

properties of an algorithm that looks a lot like 1950’s dating

• Discover the naked mathematical naked mathematical truthtruth about which sex has the romantic edge

• Learn how the world’s largest, most successful dating service operates

Page 84: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Given a set of preference lists, Given a set of preference lists, how do we find a stable pairing?how do we find a stable pairing?

Page 85: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Given a set of preference lists, Given a set of preference lists, how do we find a stable pairing?how do we find a stable pairing?

Wait! There is a more primary

question!

Page 86: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Existence QuestionExistence Question

Does every set of preferences lists have at least one

stable pairing?

Page 87: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

3,5,2,1,4

1

5,2,1,4,3

2

4,3,5,1,2

3

1,2,3,4,5

4

2,3,4,1,5

5

1

3,2,5,1,4

2

1,2,5,3,4

3

4,3,2,1,5

4

1,3,4,2,5

5

1,2,4,5,3

Page 88: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Can you argue that the couples will not continue

breaking up and reforming forever?

Page 89: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

An Instructive Variant:An Instructive Variant:Roommate ProblemRoommate Problem

1

3

2,3,4

1,2,4

2

4

3,1,4

*,*,*

Page 90: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

An Instructive Variant:An Instructive Variant:Roommate ProblemRoommate Problem

1

3

2,3,4

1,2,4

2

4

3,1,4

*,*,*

Page 91: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

An Instructive Variant:An Instructive Variant:Roommate ProblemRoommate Problem

1

3

2,3,4

1,2,4

2

4

3,1,4

*,*,*

Page 92: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

An Instructive Variant:An Instructive Variant:Roommate ProblemRoommate Problem

3

2,3,4

1,2,4

2

4

3,1,4

*,*,*

1

Page 93: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Unstable roommatesUnstable roommatesin perpetual motion.in perpetual motion.

3

2,3,4

1,2,4

2

4

3,1,4

*,*,*

1

Page 94: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

InsightInsight

Any proof that couples do not break up and reform forever must contain a step that fails in the case of the roommate problem

Page 95: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

InsightInsight

If you have a proof idea that works equally well in the marriage problem and the roommate problem, then your idea is not adequate to show the couples eventually stop.

Page 96: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Traditional Marriage The Traditional Marriage AlgorithmAlgorithm

Page 97: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Traditional Marriage The Traditional Marriage AlgorithmAlgorithm

String

Worshipping males

Female

Page 98: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Traditional Marriage AlgorithmTraditional Marriage Algorithm

For each day that some boy gets a “No” do:• MorningMorning

• Each girl stands on her balcony

• Each boy proposes under the balcony of the best girl whom he has not yet crossed off

• Afternoon (for those girls with at least one suitor)Afternoon (for those girls with at least one suitor)

• To today’s best suitor: “Maybe, come back tomorrow”“Maybe, come back tomorrow”

• To any others: “No, I will never marry you” “No, I will never marry you”• EveningEvening

• Any rejected boy crosses the girl off his list

Each girl marries the boy to whom she just said

“maybe”

Page 99: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Does the Traditional Marriage Does the Traditional Marriage Algorithm always produce a Algorithm always produce a

stablestable pairing? pairing?

Page 100: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Does the Traditional Marriage Does the Traditional Marriage Algorithm always produce a Algorithm always produce a

stablestable pairing? pairing?

Wait! There is a more primary

question!

Page 101: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Does TMA always terminate?Does TMA always terminate?

• It might encounter a situation where algorithm does not specify what to do next (core dump error)

• It might keep on going for an infinite number of days

Page 102: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Traditional Marriage AlgorithmTraditional Marriage Algorithm

For each day that some boy gets a “No” do:• MorningMorning

• Each girl stands on her balcony

• Each boy proposes under the balcony of the best girl whom he has not yet crossed off

• Afternoon (for those girls with at least one suitor)Afternoon (for those girls with at least one suitor)

• To today’s best suitor: “Maybe, come back tomorrow”“Maybe, come back tomorrow”

• To any others: “No, I will never marry you” “No, I will never marry you”• EveningEvening

• Any rejected boy crosses the girl off his list

Each girl marries the boy to whom she just said

“maybe”

Page 103: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

LemmaLemma: No boy can be rejected : No boy can be rejected by all the girlsby all the girls

Proof by contradiction. Suppose boy b is rejected by all the

girls. At that point:• Each girl must have a suitor other than b

(Once a girl has a suitor she will always have at least one)

• But there are n girls and only n-1 boys besides b

Contradiction

Page 104: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

TheoremTheorem: The TMA always : The TMA always terminates in at most nterminates in at most n22 days days

• A “master list” of all n of the boys lists starts with a total of n X n = n2 girls on it.

• Each day that at least one boy gets a “No”, at least one girl gets crossed off the master list

• Therefore, the number of days is bounded by the original size of the master list

Page 105: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Great! We know that TMA will Great! We know that TMA will terminate and produce a pairing.terminate and produce a pairing.

But is it stable?But is it stable?

Page 106: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

MAYBE LemmaMAYBE Lemma:: In TMA if on day i In TMA if on day i a girl says “maybe” to boy b, she is a girl says “maybe” to boy b, she is guaranteed to marry a husband that guaranteed to marry a husband that she likes at least as much as bshe likes at least as much as b

• She would only let go of him in order to “maybe” someone better

• She would only let go of that guy for someone even better

• She would only let go of that guy for someone even better

• AND SO ON . . . . . . . . . . . . .

Informal Induction

Page 107: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

(*) For all k 0, on day i+k the girl will say “maybe” to a boy she likes as much as b.

BASE: k=0 (true by assumption) Assume (*) is true for k-1. Thus she has

a boy as good as b on day i+k-1. The next day she will either keep him or reject him for some better. Thus (*) is true for k.

MAYBE LemmaMAYBE Lemma:: In TMA if on day i In TMA if on day i a girl says “maybe” to boy b, she is a girl says “maybe” to boy b, she is guaranteed to marry a husband that guaranteed to marry a husband that she likes at least as much as bshe likes at least as much as b

Formal Induction

Page 108: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

CorollaryCorollary: Each girl will marry her : Each girl will marry her absolute favorite of the boys who absolute favorite of the boys who

visit her during the TMAvisit her during the TMA

Page 109: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

TheoremTheorem: Let T be the paring : Let T be the paring produced by TMA. T is stable.produced by TMA. T is stable.

• Let b and g be any couple in T.

• Suppose b prefers g* to g. We will argue that g* prefers her husband to b.

• During TMA, b proposed to g* before he proposed to g. Hence, at some point g* rejected b for someone she preferred. By the MAYBE lemma, the person she married was also preferable to b.

• Thus, every boy will be rejected by any girl he prefers to his wife. T is stable.

Page 110: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Opinion PollOpinion Poll

Who is better off

in traditio

nal

dating, the boys

or the girls

?

Page 111: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Forget TMA for a momentForget TMA for a moment

How should we define what we mean when we say “the optimal girl for boy b”?

Flawed Attempt:Flawed Attempt: “The girl at the top of b’s “The girl at the top of b’s

list”list”

Page 112: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Optimal GirlThe Optimal Girl

A boy’s optimal girl is the highest ranked girl for whom there is some stable pairing in which the boy gets

her.

She is the best girl he can conceivably get in a stable world. Presumably, she might be better than the girl he gets in the stable pairing output by TMA.

Page 113: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Pessimal GirlThe Pessimal Girl

A boy’s pessimal girl is the lowest ranked girl for whom there is some stable pairing in which the boy gets

her.

She is the worst girl he can conceivably get in a stable world.

Page 114: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Dating Heaven and HellDating Heaven and Hell

A pairing is male-optimal if every boy gets his optimal mate. This is the best of all possible stable worlds for every boy simultaneously.

A pairing is male-pessimal if every boy gets his pessimal mate. This is the worst of all possible stable worlds for every boy simultaneously.

Page 115: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Dating Heaven and HellDating Heaven and Hell

A pairing is male-optimal if every boy gets his optimal mate. Thus, the pairing is simultaneously giving each boy his optimal.

Is a male-optimal pairing always stable?

Page 116: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Dating Heaven and HellDating Heaven and Hell

A pairing is female-optimal if every girl gets her optimal mate. This is the best of all possible stable worlds for every girl simultaneously.

A pairing is female-pessimal if every girl gets her pessimal mate. This is the worst of all possible stable worlds for every girl simultaneously.

Page 117: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The Naked The Naked Mathematical Mathematical

Truth!Truth! The Traditional Marriage Algorithm always

produces a male-optimal, female-pessimal pairing.

Page 118: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

TheoremTheorem: TMA produces a : TMA produces a male-optimal pairingmale-optimal pairing

• Suppose, for a contradiction, that some boy gets rejected by his optimal girl during TMA. Let t be the earliest time at which this happened.

• In particular, at time t, some boy b got rejected by his optimal girl g because she said “maybe” to a preferred b*. By the definition of t, b* had not yet been rejected by his optimal girl.

• Therefore, b* likes g at least as much as his optimal.

Page 119: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Some boy b got rejected by his optimal girl gSome boy b got rejected by his optimal girl g because she said “maybe” to a preferred bbecause she said “maybe” to a preferred b**. b* . b* likes g at least as much as his optimal girl.likes g at least as much as his optimal girl.

There must exist a stable paring S in which b and g are married. • b* wants g more than his wife in S

• g is as at least as good as his best and he does not have her in stable pairing S

• g wants b* more than her husband in S• b is her husband in S and she rejects him for b*

in TMA

Page 120: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

There must exist a stable paring S in which b and g are married. • b* wants g more than his wife in S

• g is as at least as good as his best and he does not have her in stable pairing S

• g wants b* more than her husband in S• b is her husband in S and she rejects him for b*

in TMA

Some boy b got rejected by his optimal girl gSome boy b got rejected by his optimal girl g because she said “maybe” to a preferred bbecause she said “maybe” to a preferred b**. b* . b* likes g at least as much as his optimal girl.likes g at least as much as his optimal girl.

Contradiction of the stability of S.

Page 121: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

What proof technique did we just What proof technique did we just use?use?

Page 122: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

What proof technique did we just What proof technique did we just use?use?

Page 123: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

TheoremTheorem: : The TMA pairing, T, The TMA pairing, T, is female-pessimal.is female-pessimal.

We know it is male-optimal. Suppose there is a stable pairing S where some girl g does worse than in T.

Let b be her mate in T. Let b* be her mate in S.• By assumption, g likes b better than her mate in S • b likes g better than his mate in S

• We already know that g is his optimal girl

• Therefore, S is not stable.

Contradiction

Page 124: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

Advice to femalesAdvice to females

Learn to make the first move.

Page 125: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

The largest, most The largest, most successful dating service successful dating service

in the world uses a in the world uses a computer to run TMA !computer to run TMA !

Page 126: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

““The Match”:The Match”:Doctors and Medical ResidenciesDoctors and Medical Residencies

• Each medical school graduate submits a ranked list of hospitals where he/she wants to do a residency

• Each hospital submits a ranked list of newly minted doctors

• A computer runs TMA (extended to handle Mormon marriages)

• Until recently, it was hospital-optimal

Page 127: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

History History

1900• Idea of hospitals having residents (then called

“interns”)

Over the next few decades• Intense competition among hospitals for an

inadequate supply of residents• Each hospital makes offers independently• Process degenerates into a race. Hospitals steadily

advancing date at which they finalize binding contracts

Page 128: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

HistoryHistory

1944 Absurd Situation. Appointments being made 2 years ahead of time!• All parties were unhappy

• Medical schools stop releasing any information about students before some reasonable date

• Did this fix the situation?

Page 129: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

HistoryHistory

1944 Absurd Situation. Appointments being made 2 years ahead of time!• All parties were unhappy• Medical schools stop releasing any

information about students before some reasonable date

• Offers were made at a more reasonable Offers were made at a more reasonable date, but new problems developeddate, but new problems developed

Page 130: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

HistoryHistory

1945-1949 Just As Competitive• Hospitals started putting time limits on offers

• Time limit gets down to 12 hours

• Lots of unhappy people

• Many instabilities resulting from lack of cooperation

Page 131: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

HistoryHistory

1950 Centralized System• Each hospital ranks residents

• Each resident ranks hospitals

• National Resident Matching Program produces a pairing

Whoops! The pairings were not always stable. By 1952 the algorithm was the TMA (hospital-optimal) and therefore

stable.

Page 132: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

History Repeats Itself!History Repeats Itself!NY TIMES, March 17, 1989NY TIMES, March 17, 1989

• The once decorous process by which federal judges select their law clerks has degenerated into a free-for-all in which some of the judges scramble for the top law school students . . .

• The judge have steadily pushed up the hiring process . . .

• Offered some jobs as early as February of the second year of law school . . .

• On the basis of fewer grades and flimsier evidence . . .

Page 133: Welcome to CSE 589 -- Applied Algorithms Anna R. Karlin (karlin@cs) Office Hours: Wed 9:20 - 10:00 pm and by appointment TA: Ashish Sabharwal (ashish@cs)

NY TIMESNY TIMES• “Law of the jungle reigns . . “• The association of American Law Schools

agreed not to hire before September of the third year . . .

• Some extend offers from only a few hours, a practice known in the clerkship vernacular as a “short fuse” or a “hold up”.

• Judge Winter offered a Yale student a clerkship at 11:35 and gave her until noon to accept . . . At 11:55 . . he withdrew his offer