Analysis of Algorithms
CHAPTER FOUR
Greedy Algorithm
Lecturer Name:
Adem M
4/15/2021 1
Introduction
In this chapter:
▪ Introduction – Greedy
▪ Greedy - Activity Selection Problem
▪ Greedy Algorithm - Activity or Task Scheduling Problem
▪ Greedy - Knapsack Problem
▪ Greedy - Huffman Coding
▪ Greedy - Minimum Spanning Tree (Kruskals and Primes Algorithm)
4/15/2021 2
Greedy Algorithm - Introduction
"Greedy Method finds out of many options, but you have to choose the best option."
In this method, we have to find out the best method/option out of many present ways.
4/15/2021 3
Greedy Algorithm - Activity Selection Problem
4/15/2021 4
Greedy Algorithm - Activity Selection Problem
4/15/2021 5
Greedy Algorithm - Activity Selection Problem
4/15/2021 6
Greedy Algorithm - Activity Selection Problem
4/15/2021 7
Greedy Algorithm - Activity Selection Problem
4/15/2021 8
Greedy Algorithm - Activity Selection Problem
4/15/2021 9
Greedy Algorithm - Activity Selection Problem
4/15/2021 10
Greedy Algorithm - Activity Selection Problem
4/15/2021 11
Greedy Algorithm - Activity Selection Problem
4/15/2021 12
Greedy Algorithm - Activity Selection Problem
4/15/2021 13
Greedy Algorithm - Activity Selection Problem
4/15/2021 14
Greedy Algorithm - Activity Selection Problem
4/15/2021 15
Greedy Algorithm - Activity or Task Scheduling Problem
This is the dispute of optimally scheduling unit-time tasks on a single processor,
where each job has a deadline and a penalty that necessary be paid if the
deadline is missed.
4/15/2021 16
Greedy Algorithm - Activity or Task Scheduling Problem
4/15/2021 17
Greedy Algorithm - Activity or Task Scheduling Problem
• Now, schedule A1
• Next schedule A3 as A1 and A3 are non-interfering.
• Next skip A2 as it is interfering.
• Next, schedule A4 as A1 A3 and A4 are non-interfering, then next, schedule A6 as
A1 A3 A4 and A6 are non-interfering.
• Skip A5 as it is interfering.
• Next, schedule A7 as A1 A3 A4 A6 and A7 are non-interfering.
• Next, schedule A9 as A1 A3 A4 A6 A7 and A9 are non-interfering.
• Skip A8 as it is interfering.
• Next, schedule A10 as A1 A3 A4 A6 A7 A9 and A10 are non-interfering.
• Thus the final Activity schedule is:
4/15/2021 18
Greedy Algorithm - Activity or Task Scheduling Problem
• Tasks –N
• Machines – N at most
• Tasks have duration
• Machines can only do one task at a time
• Overlapping duration – use different machines
• Allocate tasks machines ,minimize machines
4/15/2021 19
Greedy Algorithm - Activity or Task Scheduling Problem
• Order tasks by starting time
• Ascending order
• Each Job
• Allocate it to smallest numbered machines that is idle
• Process tasks in order of starting time
4/15/2021 20
Greedy Algorithm - Activity or Task Scheduling Problem
Task A
Task F
Task B
Task C
4/15/2021 21
Greedy Algorithm - Activity or Task Scheduling Problem
Task G
Task E
Task D
4/15/2021 22
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 23
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 24
Greedy Algorithm - Fractional Knapsack Problem
Steps
4/15/2021 25
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 26
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 27
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 28
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 29
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 30
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 31
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 32
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 33
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 34
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 35
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 36
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 37
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 38
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 39
Greedy Algorithm - Fractional Knapsack Problem
4/15/2021 40
Thank You !!!
4/15/2021 41