cpsc 320: intermediate algorithm design and analysis
DESCRIPTION
CPSC 320: Intermediate Algorithm Design and Analysis. July 2, 2014. Instructor and Course Structure. Instructor. Jonatan Schroeder [email protected] Office: ICICS/CS 247 Office hours: Mon/Wed/Fri: 12-1pm Or by appointment. Teaching Assistants. Alireza Shafaei Anupam Srivastava - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/1.jpg)
1
CPSC 320: Intermediate AlgorithmDesign and Analysis
July 2, 2014
![Page 2: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/2.jpg)
2
Instructor and Course Structure
![Page 3: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/3.jpg)
3
InstructorJonatan [email protected]: ICICS/CS 247
Office hours:• Mon/Wed/Fri: 12-1pm• Or by appointment
![Page 4: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/4.jpg)
4
Teaching Assistants• Alireza Shafaei• Anupam Srivastava• Jianing Yu• Juyoung Moon• Reza Babanezhad
![Page 5: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/5.jpg)
5
Pre-requisites• Listed student numbers should come to me after class• If you fail to act, you may be dropped from the course• Walk-in advising hours:
• Today 4-5pm: ICCS 391 (Paul Carter)• Thu 10:30-11:30am: ICCS 391 (Paul Carter)• Thu 3-4pm: ICCS 307 (George Tsiknis)• Additional hours may be listed in front of CS main office
![Page 6: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/6.jpg)
6
Learning Goals• Understand techniques used to design efficient algorithms• Prove (or disprove) that designed algorithms are correct• Prove that designed algorithms are efficient
![Page 7: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/7.jpg)
7
Course Outline• Introduction and basic concepts• Asymptotic notation• Greedy algorithms• Graph theory• Amortized analysis• Recursion• Divide-and-conquer algorithms• Randomized algorithms• Dynamic programming algorithms• NP-completeness
![Page 8: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/8.jpg)
8
Schedule• Lectures: Mon/Wed/Fri 9:30-12:00• Tutorials:
• T2A: Wed/Fri 1-2pm (Anupam)• T2B: Wed/Fri 2-3pm (Anupam)• T2C: Wed/Fri 3-4pm (Alireza)
![Page 9: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/9.jpg)
9
Piazza• All course announcements will be posted on Piazza• All course-related questions should be asked on Piazza
• Instructors and TAs will direct your questions to Piazza when relevant
• Questions including part of a possible solution should be made private
• Instructors may change a private question to public if deemed useful for other students
• Students are encouraged to answer questions as well
![Page 10: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/10.jpg)
10
Piazza (continued)• Please take a minute to understand how Piazza works
• Students’ response is for answering questions, not follow-ups
• Remember to mark follow-ups as resolved• If you found the answer on your own, please add an
answer (don’t delete the question)
![Page 11: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/11.jpg)
11
Marking scheme• 6 or 7 assignments: 20%• 5 quizzes: 30%• Final exam: 50%
• You must pass the exam to pass the course• You must pass in the average of the quizzes• You must submit at least five of the assignments
![Page 12: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/12.jpg)
12
Assignments• 6 or 7 assignments• Electronic submission or physical copy submission?
• Please answer a poll on Piazza today before 6PM!!!• Assignment 1 will be posted some time today
![Page 13: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/13.jpg)
13
Quizzes• Quiz 1: Wed, July 9• Quiz 2: Wed, July 16• Quiz 3: Wed, July 23• Quiz 4: Wed, July 30• Quiz 5: Wed, August 6
• Closed book, closed notes, no calculator• Individual component: about 30 minutes• Group component: about 15 minutes
• The group component is to be done in groups of 3-4• Group will solve one of the questions in the individual
component
![Page 14: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/14.jpg)
14
Textbook• John Kleinberg and Éva Tardos, Algorithm Design, Addison-
Wesley Publishing company, 2005, ISBN 0-321-29535-8.• Optional: Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest and Clifford Stein, Introduction to Algorithms, 3rd edition, MIT Press, 2009, ISBN 0-262-03384-4.
![Page 15: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/15.jpg)
15
Representative Problem
The Stable Matching Problem
![Page 16: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/16.jpg)
16
Algorithm• Sequence of steps to solve a specific task or problem• Takes an input, provides a result• Each step depends solely on the input and on previous steps• Stops in finite time
![Page 17: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/17.jpg)
17
Algorithm Analysis• Does the algorithm terminate?• How long does it take (as a function of the input size)?• Does it produce a valid result?• Is the produced result correct?• Is the produced result optimal?
![Page 18: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/18.jpg)
18
Stable Matching Problem• Given men and women• Each man ranks all women in preferred order and vice-versa• Assign every man to exactly one woman (and vice-versa)• Assignment needs to be stable
• Matching • If there is a pair such that prefers over and prefers
over then matching is unstable
![Page 19: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/19.jpg)
19
Example• Assume the input:
• M = {Alvin, Bob, Chuck, Donald}• W = {Janice, Kelly, Linda, Madison}• Preference ranking:
Alvin L J K MBob J M L KChuck K M L JDonald K J M L
Janice A D C BKelly A B C DLinda B D C AMadison C A B D
![Page 20: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/20.jpg)
20
Example• Example matching: (A, L), (B, J), (C, K), (D, M)
• Unstable (D prefers J, J prefers D)
Alvin L J K MBob J M L KChuck K M L JDonald K J M L
Janice A D C BKelly A B C DLinda B D C AMadison C A B D
![Page 21: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/21.jpg)
21
Example• Example matching: (A, L), (B, M), (C, K), (D, J)
• Stable (no changes give better option)
Alvin L J K MBob J M L KChuck K M L JDonald K J M L
Janice A D C BKelly A B C DLinda B D C AMadison C A B D
![Page 22: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/22.jpg)
22
Example• Example matching: (A, L), (B, M), (C, K), (D, J)
• Stable (no changes give better option)
Alvin L J K MBob J M L KChuck K M L JDonald K J M L
Janice A D C BKelly A B C DLinda B D C AMadison C A B D
![Page 23: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/23.jpg)
23
Discussion• What strategies can be used to find a matching?• Is there an optimal solution?
• What defines optimal in this case?• Design an algorithm
![Page 24: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/24.jpg)
24
Gale-Shapley Algorithmset all and to freewhile some free woman hasn’t proposed to every man do
← the highest-ranking man hasn’t proposed toif is free then
engage else
← s current fiancéeif prefers to then
set to freeengage
return the set of engaged pairs
![Page 25: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/25.jpg)
25
What should we do now?• Does the algorithm produce a matching?
• Result includes every woman and every man?• Each woman is assigned to exactly one man?• Each man is assigned to exactly one woman?
• Is the matching stable?• How long (in the worst case) does the algorithm take to
terminate?
![Page 26: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/26.jpg)
26
Complexity• Does the algorithm terminate?• What happens to a man m as the algorithm progresses?
• How many times can a man get engaged?• What happens to a woman w as the algorithm progresses?
• How many times can a woman get engaged?• What happens overall as the algorithm progresses?
![Page 27: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/27.jpg)
27
Complexity• Theorem: the algorithm terminates in no more than
iterations• Every iteration contains one proposal• Every woman proposes to a man she hasn’t proposed to
before• At most, each woman proposes to men• There are women• Number of iterations:
![Page 28: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/28.jpg)
28
Complexity (discussion)• So, does the algorithm run in ?
• How long to find a free woman?• How long to find the preferred man to propose?• How long to decide if a man should switch?• How long to add/replace a pair from the matching set?
![Page 29: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/29.jpg)
29
Correctness: Is the result a matching?• Does the resulting matching include every woman?
• Every man, once engaged, is always engaged• If there is a free woman, there is a free man• Free woman will find a free man
• Is each man engaged to one woman only?• If a man is free during proposal, he will be engaged to
one woman• If a man is not free, he may replace one woman for
another• Is each woman engaged to one man only?
• Only free women propose
![Page 30: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/30.jpg)
30
Correctness: Is matching stable?• By contradiction, suppose matching is unstable
• and are matched• prefers , prefers
• Three possible cases:• never proposed to • proposed to , but got rejected• proposed to , but got replaced
![Page 31: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/31.jpg)
31
Correctness: Is matching stable?• Case 1: never proposed to • proposed to , since they got a matching• Since proposes in order of preference, proposed to first• Contradiction
![Page 32: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/32.jpg)
32
Correctness: Is matching stable?• Case 2: proposed to , but got rejected• Men only reject if they are engaged to someone preferred• If rejects when engaged to :
• prefers to , contradiction• If rejects when engaged to someone else ()
• rejects because he prefers over • later engages to because he prefers over • So, prefers over and over , so prefers over ,
contradiction
![Page 33: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/33.jpg)
33
Correctness: Is matching stable?• Case 3: proposed to , but got replaced• If replaces with , prefers over , contradiction
• Finally, since all cases generate a contradiction, an unstable matching is never generated by this algorithm
• QED
![Page 34: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/34.jpg)
34
Discussion• Is the result optimal?
• What is considered optimal?• Who gets it better, women or men?• How could this be changed?
![Page 35: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/35.jpg)
35
Equivalent problems• Matching students and co-op jobs• Matching medical students and hospitals• Matching potential students and college openings
![Page 36: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/36.jpg)
36
Variations and Similar Problems• Hospitals can take several students• Number of men and women is different• Restrictions and limited preference lists• Stable roommate problem (matched pairs from same set)• Hospital/student matching with couples• Taxis and customers (reduced cost of pick up)
![Page 37: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/37.jpg)
37
Asymptotic Notation
![Page 38: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/38.jpg)
38
Machine Model• In all algorithms we will assume:
• Sequential execution• One processor• Memory position can hold arbitrarily large integer
• Unless otherwise specified, we will discuss the worst case• Best case: usually not very useful• Average case: depends on input distribution assumptions
![Page 39: CPSC 320: Intermediate Algorithm Design and Analysis](https://reader035.vdocuments.net/reader035/viewer/2022062501/56816156550346895dd0dee4/html5/thumbnails/39.jpg)
39
Upper Bound
• Describes the upper bound of the growth rate of the function
Assume