SlideShare a Scribd company logo
Design and Analysis of Algorithms
Lecture 10
A generic way to design Greedy Algorithms
1
Algorithms-II : CS345A
FINDING THE LABELED BINARY TREE FOR
2
the optimal prefix codes
RECAP OF LAST LECTURE
Optimal Prefix Coding
Algorithmic Problem: Given a set of alphabets and their frequencies,
compute coding such that
• is prefix coding
• is minimum.
3
, , … ,
Non-decreasing order of frequency
𝒇(𝒂¿¿𝒊)≤𝒇(𝒂¿¿𝒊+𝟏)¿¿
The binary tree of the optimal prefix code
4
0 1
0 1 1
0
.
.
.
Deepest level 𝒂𝟏 𝒂𝟐
1
1
0
𝐀𝐁𝐋 (𝜸 )= ∑
𝑥∈ 𝑨
𝑓 (𝑥).|𝐝𝐞𝐩𝐭𝐡𝐓 (𝑥)|
Theorem: There exists an optimal prefix coding in which and appear as siblings
A generic way to design and analyse a greedy algorithm
P: a given optimization problem
So, all you need is to just design the Greedy step appropriately.
5
𝑨
𝑨 ′
instance of size of problem P
Greedy
step
instance of size of problem P
Opt()
Opt()
𝑨 ′ ′
Greedy
step
…
Base case
Opt()
?
Then can you see an
efficient algorithm to
compute Opt() ?
Suppose the Greedy step is such that
we can efficiently obtain Opt() from Opt().
Yes, a simple recursive algorithm.
Ponder over this inference for
some time.
Isn’t it amazing ?
EXAMPLE
MINIMUM SPANNING TREE
6
instance
7
170
33
35
31
41 81
52
42
50
102
50 30
60
70
54
57
49
49
80
78
63
54
53
44
29
64
42
43
instance
Theorem 1 : There is an MST() with edge (u,v)
8
170
33
35
31
41 81
52
42
50
102
50 30
60
70
54
57
49
49
80
78
63
54
53
44
29
64
42
43
u
v
instance
Theorem 1 : There is an MST() with edge (u,v)
9
170
33
35
31
41 81
52
42
50
102
50 30
60
70
54
57
49
49
80
78
63
54
53
64
42
44
w
43
How to compute instance ?
Let (u,v) be the least weight edge in =(, ). Transform into as follows.
• Remove vertices u and v and add a new vertex w
• For each edge (u,x)ϵ , add edge (w,x) in .
• For each edge (v,x)ϵ , add edge (w,x) in .
• In case of multiple edges between w and x, keep only the lighter weight edge.
10
11
𝑨
𝑨 ′
MST()
MST()
Weight of MST() Weight of MST() w(u,v)
Homework
Greedy
step Using Theorem 1
How ?
In the rest of the lecture, we shall discuss a technique
which will also help you do this homework.
FINDING THE LABELED BINARY TREE FOR
12
the optimal prefix codes
The binary tree of the optimal prefix code
13
0 1
0 1 1
0
.
.
.
Deepest level 𝒂𝟏 𝒂𝟐
1
1
0
Theorem 2: There exists an optimal prefix coding in which and appear as siblings
From this picture, can you
design the greedy step that
you will perform to compute
the smaller instance ?
The binary tree of the optimal prefix code
14
0 1
0 1 1
0
.
.
.
Deepest level ’
1
+
Note: Establishing this relation will lead to
an efficient algorithm to extract Opt() from Opt()
15
𝑨
𝑨 ′
, , , … …
Greedy
step
, … …
𝒂′
?
Opt()
Opt()
Relation ?
Proof for
+
Spend some time thinking about the proof
before moving ahead.

16
How to prove
+ ?
Question: Can we derive a prefix coding for from ?
Question: Can we derive a prefix coding for from ?
17
A prefix coding for from
: the binary tree corresponding to
18
.
.
.
0 1
0 1 1
0
𝑻 ′
’
1
: the binary tree corresponding to
This gives a prefix coding for with = ??
 +
19
0 1
0 1 1
0
.
.
.
1
’
𝑻 ′
𝒂𝟏 𝒂𝟐
1
0
+
A prefix coding for from
A prefix coding for from
: the binary tree corresponding to
20
.
.
.
Deepest level 𝒂𝟏 𝒂𝟐
1
1
0
0 1
0 1 1
0
𝑻
Using
Theorem 2
A prefix coding for from
: the binary tree corresponding to
This gives a prefix coding for with = ??

21
.
.
.
Deepest level
1
𝒂𝟏 𝒂𝟐
1
0
0 1
0 1 1
0
𝑻
Using
Theorem 2
’
𝐎𝐏 𝐓𝐀𝐁𝐋 (𝑨)− 𝒇 (𝒂𝟏)− 𝒇 (𝒂𝟐)
We proved
Using (1) and (2)
22
(1)
(2)
Can you see how this
relation can help us extract
Opt() from Opt() ?
: the binary tree corresponding to
This gives a prefix coding for with = ??
 +
23
0 1
0 1 1
0
.
.
.
1
’
𝑻 ′
𝒂𝟏 𝒂𝟐
1
0
+
A prefix coding for from
𝐎𝐏 𝐓𝐀𝐁𝐋 (𝑨)=𝐎𝐏𝐓𝐀𝐁𝐋 ( 𝑨′
)+ 𝒇 (𝒂𝟏)+ 𝒇 (𝒂𝟐)
We have established
¿
So
The algorithm based on
+
OPT()
{ If ||=2, return ;
else
{ Let and be the two alphabets with least frequencies.
Remove and from ;
Create a new alphabet ;
 ;
Insert into ;
T  OPT();
Replace node in T by
return T ;
}
}
24
’ 𝒂𝟏 𝒂𝟐
1
0
𝒂𝟏 𝒂𝟐
1
0
time complexity
Homework:
Achieve time
complexity
based on a suitable Theorem about Opt()
A generic way to design and analyse a greedy algorithm
P: a given optimization problem
1. Try to establish a relation between OPT() and OPT();
2. Try to prove the relation formally by
 deriving a solution of from OPT()
 deriving a solution of from OPT() 25
𝑨
𝑨 ′
instance of size of problem P
Greedy
step
instance of size of problem P
Opt()
Opt()
use Theorem
If you succeed,
you have an algorithm
from construction
More Homework
26
The 2 problems we discussed
Synchronizing the delays in a circuit
Scheduling jobs to minimize maximum
lateness
27
5
6
2
2
3 2
6
2 1 2 1
4
2 3
Electric signal
𝟎 time
Design and analyse a greed algorithm based on the generic technique.
SHORTEST PATHS IN A GRAPH
28
Notations and Terminologies
A directed graph
• Represented as Adjacency lists or Adjacency matrix
• ,
Question: what is a path in ?
Answer: A sequence , ,…, such that (,) for all .
Length of a path =
29
𝒗 𝟏 𝒗 𝟐 𝒗 𝟑 𝒗𝒌 −𝟏 𝒗 𝒌
…
12 3 29
No vertex is repeated
Notations and Terminologies
Definition: The path from to of minimum length is called the shortest path
from to
Definition: Distance from to is the length of the shortest path from to .
Notations:
: distance from to .
: The shortest path from to .
30
Problem Definition
Input: A directed graph with and a source vertex
Aim:
• Compute for all
• Compute for all
This problem is simple and beautiful enough to convince anyone about
the importance of designing efficient algorithms.
31
An example to get
an insight into this problem
Inference:
The distance to any vertex depends upon global parameters.
32
𝒔
G
𝒒
12
6
16
11
9
4
7
5
An example to get
an insight into this problem
Question: Is there any vertex in this picture for which you are certain about
the distance from ?
Answer: vertex .
33
𝒔
𝒖
𝒙
𝒚
𝒛
𝒗
3
5
12
10
7
G
Give reasons.
An example to get
an insight into this problem
 The shortest path to vertex is edge (,).
34
𝒔
𝒖
𝒙
𝒚
𝒛
𝒗
3
5
12
10
7
G
¿ 𝟎
Designing a greedy algorithm for shortest paths
Establish a relation between shortest paths in
and
shortest paths in
35
𝑮
𝑮 ′
instance of size
Greedy
step
instance of size
How will look like ?
Do it as Homework

More Related Content

PPTX
01 - DAA - PPT.pptx
PPTX
Applied Algorithms and Structures week999
PPTX
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
PDF
Algorithm chapter 1
PPTX
Lecture 8 about data mining and how to use it.pptx
PDF
chapter1.pdf ......................................
01 - DAA - PPT.pptx
Applied Algorithms and Structures week999
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Algorithm chapter 1
Lecture 8 about data mining and how to use it.pptx
chapter1.pdf ......................................

Similar to Lecture-10-CS345A-2023 of Design and Analysis (20)

PPT
Design and Analysis of Algorithm Brute Force 1.ppt
PDF
Anlysis and design of algorithms part 1
PPT
Data_Structure_and_Algorithms_Lecture_1.ppt
PPTX
Undecidable Problems and Approximation Algorithms
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
PDF
CS345-Algorithms-II-Lecture-1-CS345-2016.pdf
PPT
lecture 1
PDF
Algorithm review
PPT
Introducción al Análisis y diseño de algoritmos
PPTX
Signals and Systems Homework Help.pptx
PPT
PPTX
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
PPTX
UNIT DAA PPT cover all topics 2021 regulation
PPTX
Algorithms DM
PPT
lecture01.ppt
PDF
Mit6 094 iap10_lec03
PDF
Combinatorial Problems2
PPT
Randomized algorithms ver 1.0
PPTX
Winter 10 Undecidability.pptx
PPT
Greedy Algorithms Chapter for new students 4.ppt
Design and Analysis of Algorithm Brute Force 1.ppt
Anlysis and design of algorithms part 1
Data_Structure_and_Algorithms_Lecture_1.ppt
Undecidable Problems and Approximation Algorithms
Cs6402 design and analysis of algorithms may june 2016 answer key
CS345-Algorithms-II-Lecture-1-CS345-2016.pdf
lecture 1
Algorithm review
Introducción al Análisis y diseño de algoritmos
Signals and Systems Homework Help.pptx
DYNAMIC PROGRAMMING AND GREEDY TECHNIQUE
UNIT DAA PPT cover all topics 2021 regulation
Algorithms DM
lecture01.ppt
Mit6 094 iap10_lec03
Combinatorial Problems2
Randomized algorithms ver 1.0
Winter 10 Undecidability.pptx
Greedy Algorithms Chapter for new students 4.ppt
Ad

More from ssuser9183b6 (8)

PPTX
DEsign and Analysis of ALgorithms slides of lecture 1, 2 and 3
PPTX
Lecture-13-CS345A-2023 of Design and Analysis
PPTX
Lecture-11-CS345A-2023 of Design and Analysis
PPTX
Lecture-12-CS345A-2023 of Design and Analysis
PPTX
Lecture-7-CS345A-2023 of Design and Analysis
PPTX
Lecture-8-CS345A-2023 of Design and Analysis
PPTX
Lecture-7-CS345A-2023 of Design and Analysis
PPTX
DiscreteMathematicsfor btechAsWeProgress.pptx
DEsign and Analysis of ALgorithms slides of lecture 1, 2 and 3
Lecture-13-CS345A-2023 of Design and Analysis
Lecture-11-CS345A-2023 of Design and Analysis
Lecture-12-CS345A-2023 of Design and Analysis
Lecture-7-CS345A-2023 of Design and Analysis
Lecture-8-CS345A-2023 of Design and Analysis
Lecture-7-CS345A-2023 of Design and Analysis
DiscreteMathematicsfor btechAsWeProgress.pptx
Ad

Recently uploaded (20)

PPT
Project quality management in manufacturing
PDF
PPT on Performance Review to get promotions
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Welding lecture in detail for understanding
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
web development for engineering and engineering
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
composite construction of structures.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPT
Mechanical Engineering MATERIALS Selection
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Project quality management in manufacturing
PPT on Performance Review to get promotions
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
CH1 Production IntroductoryConcepts.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Welding lecture in detail for understanding
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
OOP with Java - Java Introduction (Basics)
web development for engineering and engineering
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
composite construction of structures.pdf
Foundation to blockchain - A guide to Blockchain Tech
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
R24 SURVEYING LAB MANUAL for civil enggi
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Mechanical Engineering MATERIALS Selection
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Lecture Notes Electrical Wiring System Components
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Lecture-10-CS345A-2023 of Design and Analysis

  • 1. Design and Analysis of Algorithms Lecture 10 A generic way to design Greedy Algorithms 1 Algorithms-II : CS345A
  • 2. FINDING THE LABELED BINARY TREE FOR 2 the optimal prefix codes RECAP OF LAST LECTURE
  • 3. Optimal Prefix Coding Algorithmic Problem: Given a set of alphabets and their frequencies, compute coding such that • is prefix coding • is minimum. 3 , , … , Non-decreasing order of frequency 𝒇(𝒂¿¿𝒊)≤𝒇(𝒂¿¿𝒊+𝟏)¿¿
  • 4. The binary tree of the optimal prefix code 4 0 1 0 1 1 0 . . . Deepest level 𝒂𝟏 𝒂𝟐 1 1 0 𝐀𝐁𝐋 (𝜸 )= ∑ 𝑥∈ 𝑨 𝑓 (𝑥).|𝐝𝐞𝐩𝐭𝐡𝐓 (𝑥)| Theorem: There exists an optimal prefix coding in which and appear as siblings
  • 5. A generic way to design and analyse a greedy algorithm P: a given optimization problem So, all you need is to just design the Greedy step appropriately. 5 𝑨 𝑨 ′ instance of size of problem P Greedy step instance of size of problem P Opt() Opt() 𝑨 ′ ′ Greedy step … Base case Opt() ? Then can you see an efficient algorithm to compute Opt() ? Suppose the Greedy step is such that we can efficiently obtain Opt() from Opt(). Yes, a simple recursive algorithm. Ponder over this inference for some time. Isn’t it amazing ?
  • 8. instance Theorem 1 : There is an MST() with edge (u,v) 8 170 33 35 31 41 81 52 42 50 102 50 30 60 70 54 57 49 49 80 78 63 54 53 44 29 64 42 43 u v
  • 9. instance Theorem 1 : There is an MST() with edge (u,v) 9 170 33 35 31 41 81 52 42 50 102 50 30 60 70 54 57 49 49 80 78 63 54 53 64 42 44 w 43
  • 10. How to compute instance ? Let (u,v) be the least weight edge in =(, ). Transform into as follows. • Remove vertices u and v and add a new vertex w • For each edge (u,x)ϵ , add edge (w,x) in . • For each edge (v,x)ϵ , add edge (w,x) in . • In case of multiple edges between w and x, keep only the lighter weight edge. 10
  • 11. 11 𝑨 𝑨 ′ MST() MST() Weight of MST() Weight of MST() w(u,v) Homework Greedy step Using Theorem 1 How ? In the rest of the lecture, we shall discuss a technique which will also help you do this homework.
  • 12. FINDING THE LABELED BINARY TREE FOR 12 the optimal prefix codes
  • 13. The binary tree of the optimal prefix code 13 0 1 0 1 1 0 . . . Deepest level 𝒂𝟏 𝒂𝟐 1 1 0 Theorem 2: There exists an optimal prefix coding in which and appear as siblings From this picture, can you design the greedy step that you will perform to compute the smaller instance ?
  • 14. The binary tree of the optimal prefix code 14 0 1 0 1 1 0 . . . Deepest level ’ 1
  • 15. + Note: Establishing this relation will lead to an efficient algorithm to extract Opt() from Opt() 15 𝑨 𝑨 ′ , , , … … Greedy step , … … 𝒂′ ? Opt() Opt() Relation ?
  • 16. Proof for + Spend some time thinking about the proof before moving ahead.  16
  • 17. How to prove + ? Question: Can we derive a prefix coding for from ? Question: Can we derive a prefix coding for from ? 17
  • 18. A prefix coding for from : the binary tree corresponding to 18 . . . 0 1 0 1 1 0 𝑻 ′ ’ 1
  • 19. : the binary tree corresponding to This gives a prefix coding for with = ??  + 19 0 1 0 1 1 0 . . . 1 ’ 𝑻 ′ 𝒂𝟏 𝒂𝟐 1 0 + A prefix coding for from
  • 20. A prefix coding for from : the binary tree corresponding to 20 . . . Deepest level 𝒂𝟏 𝒂𝟐 1 1 0 0 1 0 1 1 0 𝑻 Using Theorem 2
  • 21. A prefix coding for from : the binary tree corresponding to This gives a prefix coding for with = ??  21 . . . Deepest level 1 𝒂𝟏 𝒂𝟐 1 0 0 1 0 1 1 0 𝑻 Using Theorem 2 ’ 𝐎𝐏 𝐓𝐀𝐁𝐋 (𝑨)− 𝒇 (𝒂𝟏)− 𝒇 (𝒂𝟐)
  • 22. We proved Using (1) and (2) 22 (1) (2) Can you see how this relation can help us extract Opt() from Opt() ?
  • 23. : the binary tree corresponding to This gives a prefix coding for with = ??  + 23 0 1 0 1 1 0 . . . 1 ’ 𝑻 ′ 𝒂𝟏 𝒂𝟐 1 0 + A prefix coding for from 𝐎𝐏 𝐓𝐀𝐁𝐋 (𝑨)=𝐎𝐏𝐓𝐀𝐁𝐋 ( 𝑨′ )+ 𝒇 (𝒂𝟏)+ 𝒇 (𝒂𝟐) We have established ¿ So
  • 24. The algorithm based on + OPT() { If ||=2, return ; else { Let and be the two alphabets with least frequencies. Remove and from ; Create a new alphabet ;  ; Insert into ; T  OPT(); Replace node in T by return T ; } } 24 ’ 𝒂𝟏 𝒂𝟐 1 0 𝒂𝟏 𝒂𝟐 1 0 time complexity Homework: Achieve time complexity
  • 25. based on a suitable Theorem about Opt() A generic way to design and analyse a greedy algorithm P: a given optimization problem 1. Try to establish a relation between OPT() and OPT(); 2. Try to prove the relation formally by  deriving a solution of from OPT()  deriving a solution of from OPT() 25 𝑨 𝑨 ′ instance of size of problem P Greedy step instance of size of problem P Opt() Opt() use Theorem If you succeed, you have an algorithm from construction
  • 27. The 2 problems we discussed Synchronizing the delays in a circuit Scheduling jobs to minimize maximum lateness 27 5 6 2 2 3 2 6 2 1 2 1 4 2 3 Electric signal 𝟎 time Design and analyse a greed algorithm based on the generic technique.
  • 28. SHORTEST PATHS IN A GRAPH 28
  • 29. Notations and Terminologies A directed graph • Represented as Adjacency lists or Adjacency matrix • , Question: what is a path in ? Answer: A sequence , ,…, such that (,) for all . Length of a path = 29 𝒗 𝟏 𝒗 𝟐 𝒗 𝟑 𝒗𝒌 −𝟏 𝒗 𝒌 … 12 3 29 No vertex is repeated
  • 30. Notations and Terminologies Definition: The path from to of minimum length is called the shortest path from to Definition: Distance from to is the length of the shortest path from to . Notations: : distance from to . : The shortest path from to . 30
  • 31. Problem Definition Input: A directed graph with and a source vertex Aim: • Compute for all • Compute for all This problem is simple and beautiful enough to convince anyone about the importance of designing efficient algorithms. 31
  • 32. An example to get an insight into this problem Inference: The distance to any vertex depends upon global parameters. 32 𝒔 G 𝒒 12 6 16 11 9 4 7 5
  • 33. An example to get an insight into this problem Question: Is there any vertex in this picture for which you are certain about the distance from ? Answer: vertex . 33 𝒔 𝒖 𝒙 𝒚 𝒛 𝒗 3 5 12 10 7 G Give reasons.
  • 34. An example to get an insight into this problem  The shortest path to vertex is edge (,). 34 𝒔 𝒖 𝒙 𝒚 𝒛 𝒗 3 5 12 10 7 G ¿ 𝟎
  • 35. Designing a greedy algorithm for shortest paths Establish a relation between shortest paths in and shortest paths in 35 𝑮 𝑮 ′ instance of size Greedy step instance of size How will look like ? Do it as Homework

Editor's Notes

  • #5: Let us illustrate its usefulness with the help of an example.