CSE 331: Review. Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation”

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Lecture 6 CSE 331 Sep 14. A run of the GS algorithm Mal Wash Simon Inara Zoe Kaylee.
Lecture 4 CSE 331 Sep 9, Blog posts for lectures Starts from today See Sep 8 post on the blog.
Lecture 5 CSE 331 Sep 11, HW 1 out today Will be handed out at the END of the lecture Read the homework policy document carefully START EARLY! ©ehow.com.
Lecture 23 CSE 331 Oct 24, Temp letter grades assigned See the blog post for more details.
Lecture 7 CSE 331 Sep 16, Feedback forms VOLUNTARY Last 5 mins of the lecture.
Lecture 24 CSE 331 Oct 30, Homework stuff Please turn in your HW 6 Graded HW 5 and HW 7 at the END of the lecture.
Lecture 27 CSE 331 Nov 3, Combining groups Groups can unofficially combine in the lectures.
Lecture 24 CSE 331 Oct 27, Online office hours tonight 9:00pm.
Lecture 20 CSE 331 Oct 21, Algorithm for Interval Scheduling R: set of requests Set A to be the empty set While R is not empty Choose i in R with.
Lecture 8 CSE 331 Sep 17, HW 1 due today Place Q1 and Q2 in separate piles I will not accept HWs after 1:15pm.
Lecture 6 CSE 331 Sep 10, Homeworks HW 1 posted online: see blog/piazza Pickup graded HW 0 in TA OHs.
Lecture 4 CSE 331 Sep 6, TA change Swapnoneel will leave us for 531 Jiun-Jie Wang will join us.
Lecture 3 CSE 331. Stable Matching Problem Problem Statement Algorithm Problem Definition Implementation Analysis.
Lecture 2 CSE 331. Day 1 Survey On UBlearns Day 1 Survey (talking points) Security MS PhD for research Building PC’s for 442 It’s ok to play games –
Lecture 5 CSE 331 Sep 11, Submit the form I’ll need confirmation in writing. No graded material will be handed back till I get this signed form.
Lecture 23 CSE 331 Oct 24, Reminder 2 points for Piazza participation 3 points for mini-project.
Lecture 8 CSE 331. Main Steps in Algorithm Design Problem Statement Algorithm Problem Definition “Implementation” Analysis n! Correctness+Runtime Analysis.
CSEP 521 Applied Algorithms Richard Anderson Winter 2013 Lecture 3.
CSE 331: Review.
Lecture 18 CSE 331 Oct 6, Group/Algo registration deadline BOTH DUE WED by 11:59pm!
Lecture 9 CSE 331 June 18, The “real” end of Semester blues MondayTuesdayWednesdayThursdayFriday Project 331 HW Exam study Party! Write up a term.
CSE 331: Review August 1, Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical.
Lecture 4 CSE 331 Sep 7, 2016.
Lecture 8 CSE 331 Sep 14, 2011.
Graph Algorithms BFS, DFS, Dijkstra’s.
Lecture 5 CSE 331 Sep 8, 2017.
Lecture 6 CSE 331 Sep 9, 2013.
Lecture 23 CSE 331 Oct 26, 2016.
Lecture 5 CSE 331 Sep 10, 2010.
Lecture 4 CSE 331 Sep 6, 2017.
Lecture 7 CSE 331 Sep 14, 2016.
Lecture 6 CSE 331 Sep 11, 2017.
Lecture 21 CSE 331 Oct 21, 2016.
Lecture 24 CSE 331 Oct 27, 2017.
Lecture 21 CSE 331 Oct 20, 2017.
Lecture 17 CSE 331 Oct 7, 2016.
Lecture 24 CSE 331 Oct 25, 2013.
Lecture 22 CSE 331 Oct 23, 2017.
Lecture 23 CSE 331 Oct 25, 2017.
Lecture 24 CSE 331 Oct 29, 2012.
Lecture 23 CSE 331 Oct 24, 2011.
Lecture 19 CSE 331 Oct 12, 2016.
Lecture 26 CSE 331 Nov 2, 2012.
Lecture 7 CSE 331 Sep 13, 2017.
Lecture 22 CSE 331 Oct 24, 2016.
Lecture 5 CSE 331 Sep 6, 2013.
Lecture 5 CSE 331 Sep 7, 2012.
Lecture 19 CSE 331 Oct 8, 2014.
Lecture 20 CSE 331 Oct 17, 2011.
Lecture 16 CSE 331 Oct 8, 2012.
Lecture 7 CSE 331 Sep 13, 2011.
Lecture 22 CSE 331 Oct 15, 2014.
Lecture 18 CSE 331 Oct 9, 2017.
Lecture 20 CSE 331 Oct 13, 2017.
Lecture 21 CSE 331 Oct 22, 2012.
Lecture 24 CSE 331 Oct 24, 2014.
Lecture 6 CSE 331 Sep 12, 2011.
Lecture 9 CSE 331 Sep 20, 2010.
Lecture 5 CSE 331 Sep 5, 2014.
Lecture 6 CSE 331 Sep 12, 2016.
Lecture 7 CSE 331 Sep 10, 2014.
Lecture 23 CSE 331 Oct 24, 2011.
Lecture 26 CSE 331 Nov 1, 2010.
Lecture 5 CSE 331 Sep 9, 2011.
Lecture 7 CSE 331 Sep 11, 2013.
Lecture 19 CSE 331 Oct 10, 2016.
Lecture 5 CSE 331 Sep 9, 2016.
Richard Anderson Autumn 2015 Lecture 7
Lecture 24 CSE 331 Oct 29, 2018.
Presentation transcript:

CSE 331: Review

Main Steps in Algorithm Design Problem Statement Algorithm Real world problem Problem Definition Precise mathematical def “Implementation” Data Structures Analysis Correctness/Run time

Stable Matching Problem Gale-Shaply Algorithm

Stable Marriage problem Set of men M and women W Matching (no polygamy in M X W) Perfect Matching (everyone gets married) Instablity m m w w m’m’w’w’ Preferences (ranking of potential spouses) Stable matching = perfect matching+ no instablity

Gale-Shapley Algorithm Intially all men and women are free While there exists a free woman who can propose Let w be such a woman and m be the best man she has not proposed to w proposes to m If m is free (m,w) get engaged Else (m,w’) are engaged If m prefers w’ to w w remains free Else (m,w) get engaged and w’ is free Output the engaged pairs as the final output At most n 2 iterations O(1) time implementation

GS algorithm: Firefly Edition Mal Wash Simon Inara Zoe Kaylee

GS algo outputs a stable matching Lemma 1: GS outputs a perfect matching S Lemma 2: S has no instability

Proof technique de jour Source: 4simpsons.wordpress.com Proof by contradiction Assume the negation of what you want to prove After some reasoning

Two obervations Obs 1: Once m is engaged he keeps getting engaged to “better” women Obs 2: If w proposes to m’ first and then to m (or never proposes to m) then she prefers m’ to m

Proof of Lemma 2 By contradiction m m w w m’m’w’w’ Assume there is an instability (m,w’) m prefers w’ to w w’ prefers m to m’ w’ last proposed to m’

Contradiction by Case Analysis Depending on whether w’ had proposed to m or not Case 1: w’ never proposed to m w’ prefers m’ to m Assumed w’ prefers m to m’ Source: 4simpsons.wordpress.com By Obs 2

By Obs 1 Case 2: w’ had proposed to m Case 2.1: m had accepted w’ proposal m is finally engaged to w Thus, m prefers w to w’ 4simpsons.wordpress.com Case 2.2: m had rejected w’ proposal m was engaged to w’’ (prefers w’’ to w’) m is finally engaged to w (prefers w to w’’) m prefers w to w’ 4simpsons.wordpress.com By Obs 1

Overall structure of case analysis Did w’ propose to m? Did m accept w’ proposal? 4simpsons.wordpress.com

Graph Searching BFS/DFS

O(m+n) BFS Implementation BFS(s) CC[s] = T and CC[w] = F for every w≠ s Set i = 0 Set L 0 = {s} While L i is not empty L i+1 = Ø For every u in L i For every edge (u,w) If CC[w] = F then CC[w] = T Add w to L i+1 i++ Array Linked List Input graph as Adjacency list Version in KT also computes a BFS tree

An illustration

O(m+n) DFS implementation BFS(s) CC[s] = T and CC[w] = F for every w≠ s Intitialize Q= {s} While Q is not empty Delete the front element u in Q For every edge (u,w) If CC[w] = F then CC[w] = T Add w to the back of Q O(n) O(1) Repeated n u times O(n u ) Repeated at most once for each vertex u Σ u O(n u ) = O(Σ u n u ) = O(m) Σ u O(n u ) = O(Σ u n u ) = O(m) O(1)

A DFS run using an explicit stack

Topological Ordering

Run of TopOrd algorithm

Greedy Algorithms

Interval Scheduling: Maximum Number of Intervals Schedule by Finish Time

End of Semester blues MondayTuesdayWednesdayThursdayFriday Project 331 HW Exam study Party! Write up a term paper Can only do one thing at any day: what is the maximum number of tasks that you can do?

Schedule by Finish Time Set A to be the empty set While R is not empty Choose i in R with the earliest finish time Add i to A Remove all requests that conflict with i from R Return A*=A O(n log n) time sort intervals such that f(i) ≤ f(i+1) O(n) time build array s[1..n] s.t. s[i] = start time for i Do the removal on the fly

The final algorithm MondayTuesdayWednesdayThursdayFriday Project 331 HW Exam study Party! Write up a term paper Order tasks by their END time

Proof of correctness uses “greedy stays ahead”

Interval Scheduling: Maximum Intervals Schedule by Finish Time

Scheduling to minimize lateness MondayTuesdayWednesdayThursdayFriday Project 331 HW Exam study Party! Write up a term paper All the tasks have to be scheduled GOAL: minimize maximum lateness All the tasks have to be scheduled GOAL: minimize maximum lateness

The Greedy Algorithm (Assume jobs sorted by deadline: d 1 ≤ d 2 ≤ ….. ≤ d n ) f=s For every i in 1..n do Schedule job i from s(i)=f to f(i)=f+t i f=f+t i

Proof of Correctness uses “Exchange argument”

Proved the following Any two schedules with 0 idle time and 0 inversions have the same max lateness Greedy schedule has 0 idle time and 0 inversions There is an optimal schedule with 0 idle time and 0 inversions

Shortest Path in a Graph: non- negative edge weights Dijkstra’s Algorithm

Shortest Path problem Input: Directed graph G=(V,E) Edge lengths, l e for e in E “start” vertex s in V Output: All shortest paths from s to all nodes in V s u w 5 s u 5 s u w

Dijkstra’s shortest path algorithm Input: Directed G=(V,E), l e ≥ 0, s in V R = {s}, d(s) =0 While there is a x not in R with (u,x) in E, u in R d’(w) = min e=(u,w) in E, u in R d(u)+l e Pick w that minimizes d’(w) Add w to R d(w) = d’(w) s s w w u u z z x x y y d(s) = s s u u d(u) = w w d(w) = 2 5 x x d(x) = y y d(y) = 3 z z d(z) = 4 Shortest paths

Dijkstra’s shortest path algorithm (formal) Input: Directed G=(V,E), l e ≥ 0, s in V S = {s}, d(s) =0 While there is a v not in S with (u,v) in E, u in S Pick w that minimizes d’(w) Add w to S d(w) = d’(w) At most n iterations O(m) time O(mn) time bound is trivial O(m log n) time implementation is possible

Proved that d’(v) is best when v is added