Algorithm Design and Analysis

Similar documents
Algorithm Design and Analysis

Analysis of Algorithms Fall Some Representative Problems Stable Matching

Algorithms. [Knuth, TAOCP] An algorithm is a finite, definite, effective procedure, with some input and some output.

1. REPRESENTATIVE PROBLEMS

1. REPRESENTATIVE PROBLEMS

Approximation and Randomized Algorithms (ARA) Lecture 1, September 3, 2012

Matching Residents to Hospitals

CS320 Algorithms: Theory and Practice. Course Introduction

1.1 First Problem: Stable Matching. Six medical students and four hospitals. Student Preferences. s6 h3 h1 h4 h2. Stable Matching Problem

CS 6901 (Applied Algorithms) Lecture 2

Creating new worlds inside the computer. COS 116: 2/10/2011 Sanjeev Arora

Omega notation. Transitivity etc.

1. REPRESENTATIVE PROBLEMS

Game Theory: Lecture #5

Data Structures and Algorithms CSE 465

CSE 417, Winter Introduc6on, Examples, and Analysis. Ben Birnbaum Widad Machmouchi

Matchings in Graphs. Definition 3 A matching N in G is said to be stable if it does not contain a blocking pair.

Review Asympto&c Bounds

Consider a complete bipartite graph with sets A and B, each with n vertices.

CSC2556. Lecture 5. Matching - Stable Matching - Kidney Exchange [Slides : Ariel D. Procaccia]

Copyright 2000, Kevin Wayne 1

cs.washington.edu/417

COMP251: Bipartite graphs

COMP251: Bipartite graphs

1. STABLE MATCHING. stable matching problem Gale Shapley algorithm hospital optimality context

Ma/CS 6b Class 3: Stable Matchings

Bipartite Matchings and Stable Marriage

Complexity. CS 320, Fall Dr. Geri Georg, Instructor Some materials adapted from Prof. Wim Bohm.

Asymptotic Analysis 1

Matching Problems. Roberto Lucchetti. Politecnico di Milano

PROBLEMS OF MARRIAGE Eugene Mukhin

CS711008Z Algorithm Design and Analysis

Ma/CS 6b Class 3: Stable Matchings

Matching Theory and the Allocation of Kidney Transplantations

Matching Problems. Roberto Lucchetti. Politecnico di Milano

Growth of Functions (CLRS 2.3,3)

Great Theoretical Ideas in Computer Science. Lecture 7: Introduction to Computational Complexity

Computational Complexity

What do you do when you can t use money to solve your problems?

Stable Matching Existence, Computation, Convergence Correlated Preferences. Stable Matching. Algorithmic Game Theory.

Algorithms: COMP3121/3821/9101/9801

COMP 9024, Class notes, 11s2, Class 1

Subramanian s stable matching algorithm

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

Intro to Theory of Computation

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

Great Theoretical Ideas in Computer Science. Lecture 9: Introduction to Computational Complexity

Matching. Terence Johnson. April 17, University of Notre Dame. Terence Johnson (ND) Matching April 17, / 41

CS Non-recursive and Recursive Algorithm Analysis

3.1 Asymptotic notation

The key is that there are two disjoint populations, and everyone in the market is on either one side or the other

Algorithms Design & Analysis. Analysis of Algorithm

with the size of the input in the limit, as the size of the misused.

COMP 382: Reasoning about algorithms

The Time Complexity of an Algorithm

Data Structures and Algorithms Running time and growth functions January 18, 2018

CPSC 320 Sample Solution, Reductions and Resident Matching: A Residentectomy

The Time Complexity of an Algorithm

CS 4407 Algorithms Lecture 3: Iterative and Divide and Conquer Algorithms

Lecture 5: The Principle of Deferred Decisions. Chernoff Bounds

Two-Sided Matching. Terence Johnson. December 1, University of Notre Dame. Terence Johnson (ND) Two-Sided Matching December 1, / 47

We set up the basic model of two-sided, one-to-one matching

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 1: Introduction

CS/SE 2C03. Sample solutions to the assignment 1.

Topic 17. Analysis of Algorithms

Introduction to Algorithms: Asymptotic Notation

Math 391: Midterm 1.0 Spring 2016

CIS 121. Analysis of Algorithms & Computational Complexity. Slides based on materials provided by Mary Wootters (Stanford University)

CS173 Running Time and Big-O. Tandy Warnow

Lecture 2: Just married

CS 310 Advanced Data Structures and Algorithms

CSC Design and Analysis of Algorithms. Lecture 1

2.1 Computational Tractability. Chapter 2. Basics of Algorithm Analysis. Computational Tractability. Polynomial-Time

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

CIS 121 Data Structures and Algorithms with Java Spring Big-Oh Notation Monday, January 22/Tuesday, January 23

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Grade 11/12 Math Circles Fall Nov. 5 Recurrences, Part 2

data structures and algorithms lecture 2

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 3. Θ Notation. Comparing Algorithms

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

P, NP, NP-Complete, and NPhard

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

Problem Set 1 Solutions

2.2 Asymptotic Order of Growth. definitions and notation (2.2) examples (2.4) properties (2.2)

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Matching and Stable Matching

CS473 - Algorithms I

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

CPSC 320 Sample Final Examination December 2013

Asymptotic Analysis. Slides by Carl Kingsford. Jan. 27, AD Chapter 2

Module 1: Analyzing the Efficiency of Algorithms

Strategic Behavior and Manipulation in Gender- Neutral Matching Algorithms

Define Efficiency. 2: Analysis. Efficiency. Measuring efficiency. CSE 417: Algorithms and Computational Complexity. Winter 2007 Larry Ruzzo

More Asymptotic Analysis Spring 2018 Discussion 8: March 6, 2018

2. ALGORITHM ANALYSIS

COMP 355 Advanced Algorithms

1 Definitions and Things You Know

Transcription:

Algorithm Design and Analysis LECTURE 2 Analysis of Algorithms Stable matching problem Asymptotic growth Adam Smith

Stable Matching Problem Unstable pair: man m and woman w are unstable if m prefers w to his assigned match, and w prefers m to her assigned match Unstable pairs have an incentive to elope Stable matching: no unstable pairs. favorite least favorite favorite least favorite 1 st 2 nd 3 rd Xavier Amy Bertha Clare Yancey Bertha Amy Clare Zeus Amy Bertha Clare 1 st 2 nd 3 rd Amy Yancey Xavier Zeus Bertha Xavier Yancey Zeus Clare Xavier Yancey Zeus Men s Preference Profile Women s Preference Profile

Stable Matching Problem Input: preference lists of n men and n women Goal: find a stable matching if one exists favorite least favorite favorite least favorite 1 st 2 nd 3 rd Xavier Amy Bertha Clare Yancey Bertha Amy Clare Zeus Amy Bertha Clare 1 st 2 nd 3 rd Amy Yancey Xavier Zeus Bertha Xavier Yancey Zeus Clare Xavier Yancey Zeus Men s Preference Profile Women s Preference Profile

Review Questions In terms of n, what is the length of the input to the Stable Matching problem, i.e., the number of entries in the tables? How many bits do they take to store? (Answer: 2n 2 list entries, or 2n 2 log n bits)

Review Questions Brute force algorithm: an algorithm that checks every possible solution. In terms of n, what is the running time for the brute force algorithm for checking whether a give matching is stable? In terms of n, what is the running time for the brute force algorithm for Stable Matching Problem? (Assume your algorithm goes over all possible perfect matchings.) (Answer: n! (time to check if a matching is stable) = (n! n 2 ) )

Propose-and-Reject Algorithm Propose-and-reject algorithm. [Gale-Shapley 1962] Initialize each person to be free. while (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1 st woman on m's list to whom m has not yet proposed if (w is free) assign m and w to be engaged else if (w prefers m to her fiancé m') assign m and w to be engaged, and m' to be free else w rejects m }

Proof of correctness Three claims: The algorithm always 1. terminates, 2. matches everyone (matching is perfect ), and 3. outputs a stable matching

Men s Preference Profile 0 th 1 st 2 nd 3 rd 4 th Victor Bertha Amy Diane Erika Clare Wyatt Diane Bertha Amy Clare Erika Xavier Bertha Erika Clare Diane Amy Yancey Amy Diane Clare Bertha Erika Zeus Bertha Diane Amy Erika Clare Women s Preference Profile 0 th 1 st 2 nd 3 rd 4 th Amy Zeus Victor Wyatt Yancey Xavier Bertha Xavier Wyatt Yancey Victor Zeus Clare Wyatt Xavier Yancey Zeus Victor Diane Victor Zeus Yancey Xavier Wyatt Erika Yancey Wyatt Zeus Xavier Victor STOP - Everyone matched. - Stable matching!

Proof of Correctness: Termination Claim. Algorithm terminates after at most n 2 iterations of while loop. Pf. Each time through the loop a man proposes to a new woman. There are only n 2 possible proposals. 1 st 2 nd 3 rd 4 th 5 th 1 st 2 nd 3 rd 4 th 5 th Victor A B C D E Amy W X Y Z V Wyatt B C D A E Bertha X Y Z V W Xavier C D A B E Clare Y Z V W X Yancey D A B C E Diane Z V W X Y Zeus A B C D E Erika V W X Y Z An instance where n(n-1) + 1 proposals required

Propose-and-Reject Algorithm Observation 1. Men propose to women in decreasing order of preference. Observation 2. Once a woman is matched, she never becomes unmatched; she only "trades up." Men s Preference Profile 0 th 1 st 2 nd 3 rd 4 th Victor Bertha Amy Diane Erika Clare Wyatt Diane Bertha Amy Clare Erika Xavier Bertha Erika Clare Diane Amy Yancey Amy Diane Clare Bertha Erika Zeus Bertha Diane Amy Erika Clare Women s Preference Profile 0 th 1 st 2 nd 3 rd 4 th Amy Zeus Victor Wyatt Yancey Xavier Bertha Xavier Wyatt Yancey Victor Zeus Clare Wyatt Xavier Yancey Zeus Victor Diane Victor Zeus Yancey Xavier Wyatt Erika Yancey Wyatt Zeus Xavier Victor

Proof of Correctness: Perfection Claim. All men and women get matched. Proof: (by contradiction) Suppose, for sake of contradiction, some guy, say Zeus, is not matched upon termination of algorithm. Then some woman, say Amy, is not matched upon termination. By Observation 2, Amy was never proposed to. But Zeus proposes to everyone, since he ends up unmatched.

Proof of Correctness: Stability Claim. No unstable pairs. Proof: (by contradiction) Suppose A-Z is an unstable pair: they prefer each other to their partners in Gale-Shapley matching S*. Case 1: Z never proposed to A. Z prefers his GS partner to A. A-Z is stable. Case 2: Z proposed to A. A rejected Z (right away or later) A prefers her GS partner to Z. A-Z is stable. men propose in decreasing order of preference women only trade up In either case A-Z is stable, a contradiction.

Stable Roommate Problem Stable roommate problem 2n people; each person ranks others from 1 to 2n-1. Assign roommate pairs so that no unstable pairs. 1 st 2 nd 3 rd Adam Bob Chris B C A C A B D D D A-B, C-D B-C unstable A-C, B-D A-B unstable A-D, B-C A-C unstable Doofus A B C Exercise. Where does the correctness proof break down for the roommates version?

Efficient Implementation We describe O(n 2 ) time implementation. Assume men have IDs 1,, n, and so do women. Engagements data structures: a list of free men, e.g., a queue. two arrays wife[m], and husband[w]. set entry to 0 if unmatched if m matched to w then wife[m]=w and husband[w]=m Men proposing data structures: an array men-pref[m,i]= i th women on m th list an array count[m] = how many proposals m made. In Python: http://euler.slu.edu/~goldwasser/courses/slu/csci314/2006_spring/lectures/marriagealgorithm/

Efficient Implementation Women rejecting/accepting data structures Does woman w prefer man m to man m'? For each woman, create inverse of preference list of men. Constant time queries after O(n) preprocessing per woman. Amy Pref 1 st 8 2 nd 3 rd 3 7 4 th 5 th 6 th 7 th 8 th 1 4 5 6 2 Amy 1 2 3 4 5 6 7 8 Inverse 4 th 8 th 2 nd 5 th 6 th 7 th 3 rd 1 st for i = 1 to n inverse[pref[i]] = i Amy prefers man 3 to 6 since inverse[3] < inverse[6] 2 7

Summary Stable matching problem. Given n men and n women, and their preferences, find a stable matching if one exists. Gale-Shapley algorithm. Guarantees to find a stable matching for every problem instance. (Also proves that stable matching always exists) Time and space complexity: O(n 2 ), linear in the input size.

Brief Syllabus Reminders Worst-case analysis Asymptotic notation Basic Data Structures Design Paradigms Greedy algorithms, Divide and conquer, Dynamic programming, Network flow and linear programming, randomization Analyzing algorithms in other models Parallel algorithms, Memory hierarchies (?) P, NP and NP-completeness

Measuring Running Time Focus on scalability: parameterize the running time by some measure of size (e.g. n = number of men and women) Kinds of analysis Worst-case Average-case (requires knowing the distribution) Best-case (how meaningful?) Exact times depend on computer; instead measure asymptotic growth

Asymptotic notation O-notation (upper bounds): We write f(n) = O(g(n)) if there exist constants c > 0, n 0 > 0 such that 0 f(n) cg(n) for all n n 0. EXAMPLE: 2n 2 = O(n 3 ) functions, not values (c = 1, n 0 = 2) funny, one-way equality

Set Definition O(g(n)) = { f(n) : there exist constants c > 0, n 0 > 0 such that 0 f(n) cg(n) for all n n 0 } EXAMPLE: 2n 2 O(n 3 ) (Logicians: n.2n 2 O( n.n 3 ), but it s convenient to be sloppy, as long as we understand what s really going on.)

Examples 10 6 n 3 + 2n 2 -n +10 = O(n 3 ) n + log(n) = O(n ) n (log(n) + n ) = O(n 3/2 ) n = O(n 2 )

-notation (lower bounds) O-notation is an upper-bound notation. It makes no sense to say f(n) is at least O(n 2 ). (g(n)) = { f(n) : there exist constants c > 0, n 0 > 0 such that 0 cg(n) f(n) for all n n 0 } EXAMPLE: (c = 1, n 0 = 16)

-notation (lower bounds) Be careful: Any comparison-based sorting algorithm requires at least O(n log n) comparisons. Meaningless! Use for lower bounds.

-notation (tight bounds) (g(n)) = O(g(n)) (g(n)) EXAMPLE: Polynomials are simple: a d n d + a d 1 n d 1 + + a 1 n + a 0 = (n d )

o-notation and -notation O-notation and -notation are like and. o-notation and -notation are like < and >. o(g(n)) = { f(n) : for any constant c > 0, there is a constant n 0 > 0 such that 0 f(n) < cg(n) for all n n 0 } EXAMPLE: 2n 2 = o(n 3 ) (n 0 = 2/c)

o-notation and -notation O-notation and -notation are like and. o-notation and -notation are like < and >. (g(n)) = { f(n) : for any constant c > 0, there is a constant n 0 > 0 such that 0 cg(n) < f(n) for all n n 0 } EXAMPLE: (n 0 = 1+1/c)

Common Functions: Asymptotic Bounds Polynomials. a 0 + a 1 n + + a d n d is (n d ) if a d > 0. Polynomial time. Running time is O(n d ) for some constant d independent of the input size n. Logarithms. log a n = (log b n) for all constants a, b > 0. can avoid specifying the base log grows slower than every polynomial For every x > 0, log n = O(n x ). Exponentials. For all r >1 and all d > 0, n d = O(r n ). Factorial. Every exponential grows faster than every polynomial n! =( ( n ) n 2πn) (1 + o(1)) = 2 Θ(n log n) e grows faster than every exponential

Sort by asymptotic order of growth a) n log(n) b) c) log(n) d) n 2 e) 2 n f) n g) n! h) n 1,000,000 i) n 1/log(n) j) log(n!) ) k) l) ( n 2 ( n n/2 )