Professional Documents
Culture Documents
Lecture 7
Lecture 7
Algorithms-II : CS345A
Lecture 7
• Job scheduling problem
• Synchronizing a Circuit problem
1
What does correctness of an algorithm mean ?
For every possible valid input, the algorithm must output correct answer.
2
A Job scheduling problem
3
• There are jobs: , ,…,
• Each job takes certain time for execution. Job takes time
• Each job also has a deadline. Job has deadline
• There is a single server.
𝑑𝑖 𝑓𝑖 𝑘 𝑑𝑖
𝑘 𝑘
Lateness of
𝑗𝑖 1
𝑗𝑖 2
𝑗𝑖 3
𝑗𝑖 𝑘
𝑗𝑖 𝑛
time
𝟎 4
Towards designing an algorithm
5
Towards designing a counterexample
What should be to ensure
that the other permutation
𝑡1 gives the optimal
𝑗1 schedule ?
𝑡2
𝑗 2
𝑑2
𝑗1 𝑗 2
time
6
Towards designing aancounterexample
algorithm
The job with farther deadline should be scheduled later.
the job with earlier deadline should be scheduled first.
𝑡1
This counterexample
𝑗1 ?
hints at a very
𝑑1 important point as well.
+
𝑡2
𝑗 2
?
𝑑2
+
𝑗1 𝑗 2
time
7
How to schedule more than 2 jobs ?
time
𝟎
8
How to schedule more than 2 jobs ?
time
𝟎
• Schedule the jobs in increasing order of their deadlines.
Homework:
• Write a neat pseudocode of an efficient algorithm for this problem.
• Write a formal proof of correctness of the algorithm.
9
SYNCHRONIZING A CIRCUIT
10
An Electric Circuit
A complete binary tree
Electric signal
6 5
3 2 4 2
2 1 6 2 1 2 3 2
Delay 11 10 14
14 10 10 11 10 9
Electric signal originates at the root
Signal passing through each edge incurs a delay (a few nano seconds)
Finally signal reaches all the leaves.
11
Problem definition
Given:
• There is a circuit in the form of a complete binary tree.
• Electric signal propagates from root to all leaf nodes.
• Each edge has certain delay
• The delay in reaching signal to a leaf node =
“sum of delays on all edges on the path from root.”
What is the first
Objective: step for designing
Enhance delay along certain edges so that an algorithm ?
• The delay on all paths from root to leaf nodes is the same.
• Total delay enhancement is minimum.
12
Working on an Example
13
Working on an Example
How to achieve 1st +4 is necessary and
What algorithm
Objective: sufficient as well
comes to your mind
Electric signal Spend some time on this
Synchronizing all portion to see if you can
based on this
paths from the root? reduce the total delay ?
inference ? 6 5
How much
enhancement is
3 2
needed for this edge ?4 2
2 1 6 2 1 2 3 2
+3 +4 +4 +4 +3 +4 +5
Delay 11 10 14 10 10 11 10 9
New Delay 14 14 14 14 14 14 14 14
Is this the minimum
Total delay enhancement: 27 delay enhancement ?
14
Working on an Example
What algorithm
comes to your mind
Electric signal
based on this
inference ? 6 5
3 2 4 2 +4
2 1 6 2 1 2 3 2
+3 +4 +4 +4 +3 +1
Delay 11 10 14 10 10 11 10 9
New Delay 14 14 14 14 14 14 14 14
6 5 +3
3 +3 2 4 2 +1
2 1 +1 6 2+4 1+1 2 3 2 +1
Delay 14
11 14
11
10 14 14
10 14
11
10 14
11 14
11
10 11
14
9
10
• Compute the max-delay path from to a leaf node lying in its left subtree.
• Compute the max-delay path from to a leaf node lying in its right subtree.
17
For each non-leaf node do the following:
: max delay along any leftward path
: max delay along any rightward path
𝜸
If ()
u increase delay of right edge by -
𝜶 𝜷 Else
+𝑫 𝑹 (𝒖 ) − 𝑫 𝑳 ( 𝒖 ) +𝑫 𝑳 ( 𝒖 ) − 𝑫 𝑹 ( 𝒖 ) increase delay of left edge by -
𝑫 𝑳(𝒖) 𝑫 𝑹 (𝒖)
18
PROOF OF CORRECTNESS OF THE ALGORITHM
19
What assertion suffices as a proof ?
Usually it is difficult even to find out the claim whose establishment captures the
correctness of the algorithm.
Let us have a re-look at the algorithm from point of view of a single node .
20
For each non-leaf node do the following:
: max delay along any leftward path
: max delay along any rightward path
𝜸
If ()
u increase delay of right edge by -
𝜶 𝜷 Else
+𝑫 𝑳 ( 𝒖 ) − 𝑫 𝑹 ( 𝒖 ) increase delay of left edge by -
Delay enhancement by : | - |
𝑫 𝑳(𝒖) 𝑫 𝑹 (𝒖)
21
Claim :
There is an optimal solution where,
the delay enhancement by = ||
22