24
CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Embed Size (px)

DESCRIPTION

Algorithm –[webster.com] A procedure for solving a mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation –[Knuth, TAOCP] An algorithm is a finite, definite, effective procedure, with some input and some output Algorithms Great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing. - Francis Sullivan

Citation preview

Page 1: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

CSCI 256 Data Structures and Algorithm Analysis lecture 1

Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some by Iker Gondra

Page 2: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Course Overview

• MSCS Dept webpage: CS 256 http://www.sites.stfx.ca/mscs/CS256

Page 3: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

• Algorithm– [webster.com] A procedure for solving a

mathematical problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation

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

Algorithms

Great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even

mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing. - Francis Sullivan

Page 4: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Theory of Algorithms

"As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise - By what course of calculation can these results be arrived at by the machine in the shortest time? - Charles Babbage

Page 5: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Theory of Algorithms

• Claim: All* of Computer Science is the study of algorithms• What constitutes expertise in this area?• How do experts differ from novices?

– Partial answer – there is a difference between doing something and doing it well – we strive to do it well (eg: guarantee correctness every time termination, scalability (at least for well described subclass of problems)

(* well maybe much!!!)

Page 6: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

The Algorithmic Enterprise

• The algorithmic enterprise consists of two fundamental components: – the task of getting to the mathematically clean core of

a problem, and then – the task of identifying the appropriate algorithm

design techniques, based on the structure of the problem.

Page 7: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Course Goals

• Goal: To convey the algorithmic approach as a design process that:– begins with problems arising across the full range of computing

applications, – builds on an understanding of algorithm design techniques, and – results in the development of efficient solutions to these

problems.• The material and our discussions in this course will be structured

around a sequence of three steps: – Describing a problem and working out its precise (clean mathematical)

formulation;– Employing an appropriate design technique to develop an algorithm; – Proving properties of the algorithm (including its correctness) and analyzing its

efficiency.

Page 8: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Algorithmic Paradigms

• Design and analysis of computer algorithms– Greed– Divide-and-conquer– Dynamic programming– Network flow– Intractability– Coping with intractability

Page 9: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

A First Problem: Stable Matching

• Setting:– Assign TAs to Instructors– Avoid having TAs and Instructors wanting to change

• E.g., Prof A. would rather have student X than her current TA, and student X would rather work for Prof A. than his current instructor

Page 10: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stable Matching Problem

• Goal: Given n men and n women, find a "suitable" matching– Each man lists women in order of preference from best to worst– Each woman lists men in order of preference from best to worst

Zeus Amy ClareBerthaYancey Bertha ClareAmyXavier Amy ClareBertha

1st 2nd 3rd

Men’s Preference Profile

favorite least favorite

Clare Xavier ZeusYanceyBertha Xavier ZeusYanceyAmy Yancey ZeusXavier

1st 2nd 3rd

Women’s Preference Profile

favorite least favorite

Page 11: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stable Matching Problem

• Perfect matching: everyone is matched monogamously– Each man gets exactly one woman– Each woman gets exactly one man

• Instability: incentive for some pair of participants to undermine assignment by joint action– In matching M, a pair m-w is unstable if man m and

woman w prefer each other over their current partners

Page 12: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stable Matching Problem

• Stable matching: perfect matching with no unstable pairs

• Stable matching problem: Given the preference lists of n men and n women, find a stable matching if one exists

Page 13: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stable Matching Problem

• Q: Is assignment X-C, Y-B, Z-A stable?

Zeus Amy ClareBerthaYancey Bertha ClareAmyXavier Amy ClareBertha

1st 2nd 3rd

Men’s Preference Profile

Clare Xavier ZeusYanceyBertha Xavier ZeusYanceyAmy Yancey ZeusXavier

1st 2nd 3rd

Women’s Preference Profile

favorite least favorite favorite least favorite

Page 14: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stable Matching Problem

• Q: Is assignment X-C, Y-B, Z-A stable?• A: No. Bertha and Xavier will hook up

Zeus Amy ClareBerthaYancey Bertha ClareAmyXavier Amy ClareBertha

1st 2nd 3rd

Men’s Preference Profile

Clare Xavier ZeusYanceyBertha Xavier ZeusYanceyAmy Yancey ZeusXavier

1st 2nd 3rd

Women’s Preference Profile

favorite least favorite favorite least favorite

Page 15: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Stable Matching Problem

• Q: Is assignment X-A, Y-B, Z-C stable?• A: Yes

Zeus Amy ClareBerthaYancey Bertha ClareAmyXavier Amy ClareBertha

Clare Xavier ZeusYanceyBertha Xavier ZeusYanceyAmy Yancey ZeusXavier

1st 2nd 3rd 1st 2nd 3rd

favorite least favorite favorite least favorite

Men’s Preference Profile Women’s Preference Profile

Page 16: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Example – stable matching everyone gets first choice• m1: w1 w2

• m2: w2 w1

• w1: m1 m2

• w2: m2 m1

m1

m2 w2

w1

Page 17: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Example: unique stable matching

• m1: w1 w2

• m2: w1 w2

• w1: m1 m2

• w2: m1 m2

m1

m2 w2

w1• m1: w1 w2

• m2: w1 w2

• w1: m1 m2

• w2: m1 m2

Page 18: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

2 stable matchings

• m1: w1 w2

• m2: w2 w1

• w1: m2 m1

• w2: m1 m2

both men are as happy as possible; or both women are as happy as possible

Page 19: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Example: can you find a stable matching? (Exercise for the student!)

m1: w1 w2 w3

m2: w1 w3 w2

m3: w1 w2 w3

w1: m2 m3 m1

w2: m3 m1 m2

w3: m3 m1 m2

m1

m2 w2

w1

m3 w3

Page 20: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Indicators of Stable Matching Problem

• Want Perfect matching• Have Preference lists (ranking is strictly increasing)• Want Stability

– Instable (dotted line: m1 and w2 prefer each other)

m1 w1

m2 w2

Page 21: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Intuitive Idea for a Proposal Algorithm

• m proposes to w– If w is unmatched, w accepts– If w is matched to m'

• If w prefers m to m', w accepts• If w prefers m' to m, w rejects

• Unmatched m proposes to w highest on its preference list that m has not already proposed to

Page 22: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Propose-And-Reject Algorithm

• Propose-and-reject algorithm [Gale-Shapley 1962]: Intuitive method that guarantees output which is a stable matching

Initialize each person to be freewhile (some man is free and hasn't proposed to every woman) { Choose such a man m w = 1st 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}

Page 23: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Example

m1: w1 w2 w3

m2: w1 w3 w2

m3: w1 w2 w3

w1: m2 m3 m1

w2: m3 m1 m2

w3: m3 m1 m2

m1

m2 w2

w1

m3 w3

Page 24: CSCI 256 Data Structures and Algorithm Analysis lecture 1 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some

Does this Work?

• Does it terminate?• Is the result a stable matching?• Is the solution unique?• What is the complexity?• To analyse, we begin by identifying measures of

progress and invariants:– 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.”