cse 331: review. main steps in algorithm design problem statement algorithm real world problem...
TRANSCRIPT
Main Steps in Algorithm DesignProblem StatementProblem Statement
AlgorithmAlgorithm
Real world problem
Problem DefinitionProblem Definition Precise mathematical def
“Implementation”“Implementation” Data Structures
AnalysisAnalysis Correctness/Run time
Stable Marriage problem
Set of men M and women W
Matching (no polygamy in M X W)
Perfect Matching (everyone gets married)
Instablity
mm ww
m’ w’
Preferences (ranking of potential spouses)
Stable matching = perfect matching+ no instablity
Gale-Shapley AlgorithmIntially 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 freeElse
(m,w) get engaged and w’ is free
Output the engaged pairs as the final output
At most n2 iterationsAt most n2 iterations
O(1) time implementation
O(1) time implementation
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 proveAssume the negation of what you want to prove
After some reasoning
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
mm ww
m’ w’
Assume there is an instability (m,w’)
m prefers w’ to w
w’ prefers m to m’
w’ last proposed 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 2By Obs 2
By Obs 1By Obs 1
Case 2: w’ had proposed to m
Case 2.1: m had accepted w’ proposalm 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 1By Obs 1
By Obs 1By Obs 1
Overall structure of case analysis
Did w’ propose to m?Did w’ propose to m?
Did m accept w’ proposal?
Did m accept w’ proposal?
4simpsons.wordpress.com
4simpsons.wordpress.com4simpsons.wordpress.com
O(m+n) BFS Implementation
BFS(s)
CC[s] = T and CC[w] = F for every w≠ s
Set i = 0Set L0= {s}
While Li is not empty
Li+1 = Ø
For every u in Li
For every edge (u,w)
If CC[w] = F then
CC[w] = TAdd w to Li+1
i++
ArrayArray
Linked ListLinked List
Input graph as Adjacency listInput graph as Adjacency list
Version in KT also
computes a BFS tree
Version in KT also
computes a BFS tree
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] = TAdd w to the back of Q
O(n)O(n)
O(1)O(1)
O(1)O(1)
Repeated nu times
Repeated nu times
O(nu)O(nu)
Repeated at most once for each
vertex u
Repeated at most once for each
vertex u
Σu O(nu) = O(Σu nu) =
O(m)
Σu O(nu) = O(Σu nu) =
O(m) O(1)O(1)
End of Semester blues
Monday Tuesday Wednesday Thursday Friday
ProjectProject
331 HW331 HWExam studyExam study
Party!Party!
Write up a term paperWrite up a term paper
Can only do one thing at any day: what is the maximum number of tasks that you can do?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 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 iO(n) time build array s[1..n] s.t. s[i] = start time for i
Do the removal on
the fly
Do the removal on
the fly
The final algorithm
Monday Tuesday Wednesday Thursday Friday
ProjectProject
331 HW331 HWExam studyExam study
Party!Party!
Write up a term paperWrite up a term paper
Order tasks by their END timeOrder tasks by their END time
Scheduling to minimize lateness
Monday Tuesday Wednesday Thursday Friday
ProjectProject
331 HW331 HW
Exam studyExam study
Party!Party!
Write up a term paperWrite up a term paper
All the tasks have to be scheduledGOAL: minimize maximum latenessAll the tasks have to be scheduled
GOAL: minimize maximum lateness
The Greedy Algorithm
(Assume jobs sorted by deadline: d1≤ d2≤ ….. ≤ dn)
f=s
For every i in 1..n do
Schedule job i from s(i)=f to f(i)=f+ti
f=f+ti
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 problem
Input: Directed graph G=(V,E)
Edge lengths, le for e in E
“start” vertex s in V
Output: All shortest paths from s to all nodes in V
100
155
s
u
w
5
s
u
155
s
u
w
Dijkstra’s shortest path algorithm
Input: Directed G=(V,E), le ≥ 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)+le
Pick w that minimizes d’(w) Add w to Rd(w) = d’(w)
ss
ww
uu
zz
xx
yy
1
2
4
3
3
1
2
1
2
d(s) = 0
1
4
2 ss
uu
d(u) = 1
4
2
ww
d(w) = 2
5
xx
d(x) = 2
3
4
yy
d(y) = 3
zz
d(z) = 4
Shortest paths
Shortest paths
Dijkstra’s shortest path algorithm (formal)
Input: Directed G=(V,E), le ≥ 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 Sd(w) = d’(w)
At most n iterationsAt most n iterations
O(m) timeO(m) time
O(mn) time bound is trivial
O(m log n) time implementation is possible