Design and Analysis of Algorithms
Lecture 7
• Stable Matching Problem: Analysis of the algoirthm
• Job scheduling problem
1
Algorithms-II : CS345A
GaleShapley(, )
 ;
While ()
{  Extract_any_Man_from();
 next();
proposes to ;
If Single()
else
{ let  mate();
If( prefers to )
{ rejects ;
goes back to ;
mate()  ;
remove from ();
}
else
{ remove from ();
goes back to ;
}
}
}
Question: Does the algorithm terminate ?
Answer: Yes
(in O() iterations)
In each iteration :
Either
decreases
OR
some woman is removed from pref. list of some man.
Observations:
• A man never proposes to a woman twice.
• A woman, once engaged, remains always engaged.
• Each new engagement gives a woman a better partn
2
mate()  ;
in list , precedes )
Any measure
of progress ?
GaleShapley(, )
(Proof of stability)
Lemma :
If prefers to
 prefers to .
3
𝑤𝑙
𝑤𝑖
𝑚𝑘
𝑚𝑗
Each new engagement gives a woman a better partner (mate).
There is no unstable pair in the marriage.
Proof was discussed interactively during the class.
The key observation used in the proof was:
How to restate it so that
we can prove it easily ?
Lemma :
If prefers to
 prefers to .
Proof: (a sketch (the details emerged from the interaction in the class))
must have proposed to .
At the moment of the proposal, was either engaged or single.
If was single, would have accepted the offer at that time but rejected later.
But a woman rejects her present mate only when she gets a better mate.
So would have surely got a better mate than .
Since the mate of a woman only improves in future rounds, would indeed be preferred to .
If was married, …
4
𝑤𝑙
𝑤𝑖
𝑚𝑘
𝑚𝑗
GaleShapley(, )
(Proof of stability)
5
𝑤𝑙
𝑤𝑖
𝑚𝑘
𝑚𝑗
𝑚𝑡
𝑚𝑝
GaleShapley(, )
(Proof of stability)
Lemma : If prefers to
 prefers to .
6
𝑤𝑙
𝑤𝑖
𝑚𝑘
𝑚𝑗
GaleShapley(, )
Theorem:
GaleShapley(, ) indeed computes a stable marriage.
Question : Does there exist a unique stable marriage ?
Answer: No.
Homework:
Give an example graph with multiple stable matchings.
Gale Shapley Algorithm
Theorem:
There cannot exist any other stable matching
8
Man Optimal
Woman pessimal
Man
proposing
that matches a man to a better woman.
Homework: Provide a neat, simple, and intuitive proof for the theorem
The proof is not part of the syllabus for Quiz 1. So feel relaxed 
Enthusiastic students may submit the proof to the instructor in the class on 21st
August.
Gale Shapley Algorithm
9
Man Pessimal
Woman Optimal
Woman
proposing
Gale Shapley Algorithm
10
Women Men
𝑤1
𝑤2
𝑤3
𝑤𝑛
𝑤𝑖
𝑚1
𝑚2
𝑚3
𝑚𝑗
𝑚𝑛
Ponder over the connection between Gale Shapley Algorithm and Joint Seat Allocation.
A Job scheduling problem
11
• There are jobs:
• Each job takes certain time for execution.
• Each job also has a deadline.
• There is a single server.
All jobs need to be scheduled.
Aim: Compute an order , ,…, in which the jobs should be scheduled
such that maximum lateness is minimized.
12
, ,…,
Job takes time
Job has deadline
time
𝑗𝑖 1
𝑗𝑖 𝑛
𝑗𝑖 2
𝑗𝑖 3
𝟎
𝑗𝑖 𝑘
𝑑𝑖 𝑘
𝑓 𝑖𝑘 𝑑𝑖 𝑘
Lateness of
Towards designing an algorithm
Each job has two parameters
• Time of execution
• Deadline
Idea 1: Schedule the jobs in the increasing order of Time of execution.
13
Try to prove correctness Try to come up with a counter example
Towards designing a counterexample
14
𝑗1
time
𝑗 2
𝑑2
𝑡 2
𝑡1
𝑗1 𝑗 2
What should be to ensure
that the other permutation
gives the optimal
schedule ?
Let us consider only 2 jobs.
Towards designing a counterexample
 The job with farther deadline should be scheduled later.
 the job with earlier deadline should be scheduled first.
15
𝑗1
time
𝑗 2
𝑑2
𝑡 2
𝑡1
𝑗1 𝑗 2
+
+
𝑑1
?
?
Towards designing an algorithm
This counterexample
hints at a very
important point as well.
How to schedule more than 2 jobs ?
16
𝟎 time
How to schedule more than 2 jobs ?
Idea 2:
Homework:
• Write a neat pseudocode of an efficient algorithm for this problem.
• Write a formal proof of correctness of the algorithm.
17
𝟎 time
What happens if we just
swap these 2 jobs only.
All other jobs remain unaffected.
The maximum lateness for these 2 jobs
Can only reduce.
Schedule the jobs in increasing order of their deadlines.

Lecture-7-CS345A-2023 of Design and Analysis

  • 1.
    Design and Analysisof Algorithms Lecture 7 • Stable Matching Problem: Analysis of the algoirthm • Job scheduling problem 1 Algorithms-II : CS345A
  • 2.
    GaleShapley(, )  ; While() {  Extract_any_Man_from();  next(); proposes to ; If Single() else { let  mate(); If( prefers to ) { rejects ; goes back to ; mate()  ; remove from (); } else { remove from (); goes back to ; } } } Question: Does the algorithm terminate ? Answer: Yes (in O() iterations) In each iteration : Either decreases OR some woman is removed from pref. list of some man. Observations: • A man never proposes to a woman twice. • A woman, once engaged, remains always engaged. • Each new engagement gives a woman a better partn 2 mate()  ; in list , precedes ) Any measure of progress ?
  • 3.
    GaleShapley(, ) (Proof ofstability) Lemma : If prefers to  prefers to . 3 𝑤𝑙 𝑤𝑖 𝑚𝑘 𝑚𝑗 Each new engagement gives a woman a better partner (mate). There is no unstable pair in the marriage. Proof was discussed interactively during the class. The key observation used in the proof was: How to restate it so that we can prove it easily ?
  • 4.
    Lemma : If prefersto  prefers to . Proof: (a sketch (the details emerged from the interaction in the class)) must have proposed to . At the moment of the proposal, was either engaged or single. If was single, would have accepted the offer at that time but rejected later. But a woman rejects her present mate only when she gets a better mate. So would have surely got a better mate than . Since the mate of a woman only improves in future rounds, would indeed be preferred to . If was married, … 4 𝑤𝑙 𝑤𝑖 𝑚𝑘 𝑚𝑗 GaleShapley(, ) (Proof of stability)
  • 5.
  • 6.
    GaleShapley(, ) (Proof ofstability) Lemma : If prefers to  prefers to . 6 𝑤𝑙 𝑤𝑖 𝑚𝑘 𝑚𝑗
  • 7.
    GaleShapley(, ) Theorem: GaleShapley(, )indeed computes a stable marriage. Question : Does there exist a unique stable marriage ? Answer: No. Homework: Give an example graph with multiple stable matchings.
  • 8.
    Gale Shapley Algorithm Theorem: Therecannot exist any other stable matching 8 Man Optimal Woman pessimal Man proposing that matches a man to a better woman. Homework: Provide a neat, simple, and intuitive proof for the theorem The proof is not part of the syllabus for Quiz 1. So feel relaxed  Enthusiastic students may submit the proof to the instructor in the class on 21st August.
  • 9.
    Gale Shapley Algorithm 9 ManPessimal Woman Optimal Woman proposing
  • 10.
    Gale Shapley Algorithm 10 WomenMen 𝑤1 𝑤2 𝑤3 𝑤𝑛 𝑤𝑖 𝑚1 𝑚2 𝑚3 𝑚𝑗 𝑚𝑛 Ponder over the connection between Gale Shapley Algorithm and Joint Seat Allocation.
  • 11.
    A Job schedulingproblem 11
  • 12.
    • There arejobs: • Each job takes certain time for execution. • Each job also has a deadline. • There is a single server. All jobs need to be scheduled. Aim: Compute an order , ,…, in which the jobs should be scheduled such that maximum lateness is minimized. 12 , ,…, Job takes time Job has deadline time 𝑗𝑖 1 𝑗𝑖 𝑛 𝑗𝑖 2 𝑗𝑖 3 𝟎 𝑗𝑖 𝑘 𝑑𝑖 𝑘 𝑓 𝑖𝑘 𝑑𝑖 𝑘 Lateness of
  • 13.
    Towards designing analgorithm Each job has two parameters • Time of execution • Deadline Idea 1: Schedule the jobs in the increasing order of Time of execution. 13 Try to prove correctness Try to come up with a counter example
  • 14.
    Towards designing acounterexample 14 𝑗1 time 𝑗 2 𝑑2 𝑡 2 𝑡1 𝑗1 𝑗 2 What should be to ensure that the other permutation gives the optimal schedule ? Let us consider only 2 jobs.
  • 15.
    Towards designing acounterexample  The job with farther deadline should be scheduled later.  the job with earlier deadline should be scheduled first. 15 𝑗1 time 𝑗 2 𝑑2 𝑡 2 𝑡1 𝑗1 𝑗 2 + + 𝑑1 ? ? Towards designing an algorithm This counterexample hints at a very important point as well.
  • 16.
    How to schedulemore than 2 jobs ? 16 𝟎 time
  • 17.
    How to schedulemore than 2 jobs ? Idea 2: Homework: • Write a neat pseudocode of an efficient algorithm for this problem. • Write a formal proof of correctness of the algorithm. 17 𝟎 time What happens if we just swap these 2 jobs only. All other jobs remain unaffected. The maximum lateness for these 2 jobs Can only reduce. Schedule the jobs in increasing order of their deadlines.