Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 55

Debark University

Department of cs

Design and Analysis of Algorithms


(cosc3094)
Target :-3rd Year computer science Students

By: Shimels T.(MSc in Software Engineering )

Jan, 2024
04/22/2024 1
Greedy Algorithms
Chapter 3

04/22/2024 2
Outline

 General Characteristic of Greedy Algorithms


 Graph Minimum Spanning Tree (MST) - Kruskal’s and
Prims’s Algorithms (MST)
 Shortest Paths (SP)
 Scheduling

04/22/2024 3
General Characteristic of Greedy
Algorithms
• Among all the algorithmic approaches, the simplest and
straightforward approach is the Greedy method.
• A greedy algorithm is a simple and intuitive algorithmic
approach that makes locally optimal choices at each step with
the hope of finding a global optimum solution.
• In other words, at each stage of the algorithm, it selects the
best available option without considering the overall impact on
the final solution.
• This characteristic distinguishes greedy algorithms from other
more comprehensive and exhaustive search algorithms.

04/22/2024 4
Cont.…
• In this approach, the decision is taken on the basis of current
available information without worrying about the effect of the
current decision in future.
• This approach never reconsiders the choices taken
previously.
• Greedy algorithms are quite successful in some problems,
such as Huffman encoding which is used to compress data, or
Dijkstra's algorithm, which is used to find the shortest path
through a graph.

04/22/2024 5
Cont.…
 The Activity Selection Problem is an optimization problem
which deals with the selection of non-conflicting activities that
needs to be executed by a single person or machine in each
time frame.
 Each activity is marked by a start and finish time. Greedy
technique is used for finding the solution since this is an
optimization problem.
 Let's consider that you have n activities with their start and
finish times, the objective is to find solution set having
maximum number of non-conflicting activities that can be
executed in a single time frame, assuming that only one person
or machine is available for execution.

04/22/2024 6
Cont.…
• Some points to note here:
 It might not be possible to complete all the activities, since
their timings can collapse.
 Two activities, say i and j, are said to be non-conflicting if si
>= fj or sj >= fi where si and sj denote the starting time of
activities i and j respectively, and fi and fj refer to the finishing
time of the activities i and j respectively.
 Greedy approach can be used to find the solution since we
want to maximize the count of activities that can be executed.
This approach will greedily choose an activity with earliest
finish time at every step, thus yielding an optimal solution.

04/22/2024 7
Steps for Activity Selection Problem
 Following are the steps we will be following to solve the
activity selection problem,
 Step 1: Sort the given activities in ascending order according
to their finishing time.
 Step 2: Select the first activity from sorted array act[] and add
it to sol[] array.
 Step 3: Repeat steps 4 and 5 for the remaining activities in
act[].
 Step 4: If the start time of the currently selected activity is
greater than or equal to the finish time of previously selected
activity, then add it to the sol[] array.
 Step 5: Select the next activity in act[] array.
 Step 6: Print the sol[] array.
04/22/2024 8
The General Method Of Greedy Model
• Suppose that a problem can be solved by a
sequence of decisions. In the greedy method,
each decision is locally optimal. These locally
optimal solutions will finally add up to a
globally optimal solution.
– When we have a choice to make, make the one that
looks best right now.
– Make a locally optimal choice in hope of getting a
globally optimal solution.
• Only a few optimization problems can be solved
by the greedy method.
04/22/2024 9
The General Method Of Greedy Model
• Areas of Application
– Greedy approach is used to solve problems, such as
• Finding the minimal spanning tree in a graph using
Prim’s / Kruskal’s algorithm.
• Finding the shortest path between two vertices using
Dijkstra’s algorithm, etc. (read more…)
• Job sequencing with deadline
• Where Greedy Approach Fails
– In many problems, Greedy algorithm fails to find an
optimal solution, moreover it may produce a worst
solution. Problems like Travelling Salesman and
Knapsack cannot be solved using this approach.

04/22/2024 10
The General Method Of Greedy Model
Disadvantages:
• Lack of Global Optimum
• Lack of Backtracking
• They do not always work.
• Short term choices may be disastrous on the long term.
• Correctness is hard to prove
• Sensitivity to Input
Advantages:
• Simple and easy to implement
• Efficiency: a time complexity proportional to the number of
steps involved,
• Approximation: close to the optimal solution
• Incremental Construction: build solutions incrementally, step
by step
04/22/2024 11
Graph of Minimum Spanning Tree
(MST)
• What is Minimum Spanning Tree?
Given a weighted ,connected and undirected graph,
a spanning tree of that graph is a subgraph that is a tree
and connects all the vertices together.
• A single graph can have many different spanning trees.
• A minimum spanning tree (MST) or minimum weight
spanning tree for a weighted, connected and undirected
graph is a spanning tree with weight less than or equal to
the weight of every other spanning tree, without any
cycles and with the minimum possible total edge weight
• The weight of a spanning tree is the sum of weights given
to each edge of the spanning tree.
04/22/2024 12
Minimum Spanning Tree
• There are quite a few use cases for minimum spanning trees.
• One example would be a telecommunications company trying
to lay cable in a new neighborhood.
• If it is constrained to bury the cable only along certain paths
(e.g. roads), then there would be a graph containing the points
(e.g. houses) connected by those paths.

04/22/2024 13
Minimum Spanning Tree
• A spanning tree for that graph would be a subset of those
paths that has no cycles but still connects every house; there
might be several spanning trees possible.
• A minimum spanning tree would be one with the lowest total
cost, representing the least expensive path for laying the
cable.
• How many edges does a minimum spanning tree has?
– A minimum spanning tree has (V – 1) edges where V is the
number of vertices in the given graph.

04/22/2024 14
Minimum Spanning Tree

• An undirected graph and its minimum spanning tree.


• There are two well known algorithms for greedy
model MST
1. Prim’s Algorithm
2. Kruskal’s Algorithm

04/22/2024 15
MST: Prim's Algorithm
• Prim's algorithm for finding an MST is a greedy algorithm.
• it grows the MST one edge at a time.
• Start by selecting an arbitrary vertex, include it into the current
MST.
• Grow the current MST by inserting into it the vertex closest to
one of the vertices already in current MST.
• Qn draw a minimum spanning tree and compute the total
weight for the below undirected graph?

04/22/2024 16
Example 1
Prim's Algorithm

04/22/2024 17
Example 2: Prim's Algorithm

04/22/2024 18
Minimum Spanning Tree: Kruskal's
Algorithm
• Kruskal's algorithm is a minimum-spanning-tree
algorithm which finds an edge of the least possible weight that
connects any two trees in the forest.
• It is a greedy algorithm as it finds a minimum spanning
tree for a connected weighted graph adding increasing cost
arcs at each step.
• This means it finds a subset of the edges that forms a tree that
includes every vertex, where the total weight of all the edges
in the tree is minimized.
• Sorts all edges in increasing order.

04/22/2024 19
Kruskal’s algorithm for…
Step 1: Sort all edges into increasing order.
Step 2: Add the next smallest weight edge to the forest if it will
not cause a cycle.
Step 3: Stop if n-1 edges. Otherwise, go to Step2.

04/22/2024 20
An example of Kruskal’s algorithm

04/22/2024 21
MST

• Time complexity of the MST algorithm:


• O(|E|) to build the heap
• up to |E| calls to delete-min, taking
O(|E|log |E|) time.
• therefore, the total time is O(|E|log |E|).

Note: E is number of edges

04/22/2024 22
Single Source Shortest Pattern (SSSP)
• the shortest path problem is the problem of finding
a path between two vertices (or nodes) in a graph such that the
sum of the weights of its constituent edges is minimized.
• The Single-Source Shortest Path (SSSP) problem consists of
finding the shortest paths between a given vertex v and all
other vertices in the graph.
• Example: The Dijkstra’s algorithm.
– It is a greedy algorithm because it always rely on local
optimum.
– Applications: Maps, Computer Networking …
– Time complexity: O(n2)

04/22/2024 23
Dijkstra’s Rules
1. Make sure there is no negative edges
2. Remove all loops
3. Remove all parallel edges between two vertex
except the one with least weight
4. Create shortest path table (number of
columns equals number of vertices).
5. Calculate the minimum value by using
minimum value formula.
04/22/2024 24
Dijkstra’s algorithm
• Example : Consider the following graph.

04/22/2024 25
Dijkstra…
• Remove all loops

04/22/2024 26
Dijkstra…
• Remove all parallel edges between two vertex
except the one with least weight

04/22/2024 27
Dijkstra…
• Create shortest path table
• As our graph has 4 vertices, so our table will
have 4 columns.

• Note! Column name is same as the name of


the vertex.

04/22/2024 28
Dijkstra…Steps
A B C D
0 ∞ ∞ ∞

Find the smallest unmarked value in the first row

A B C D
A 0 ∞ ∞ ∞

Draw another row and copy all the marked values in the new row

A B C D
A 0 ∞ ∞ ∞
0

Find the edge that directly connects A with B by using


minimum value formula.
04/22/2024 29
Minimum Value Formula

04/22/2024 30
Dijkstra…Steps

04/22/2024 31
Dijkstra…Steps

Find the smallest unmarked value in the Second row

Draw another row and copy all the marked values in the new row

04/22/2024 32
Dijkstra…Steps

Draw another row and copy all the marked values in the new row

04/22/2024 33
Dijkstra…Result

04/22/2024 34
Finding The Actual Solution
• Now to find the shortest path we have to backtrack from
the final vertex.
• Backtracking: we’ll move upwards till we find a change
in value.


04/22/2024 35
Cont…

04/22/2024 36
Exercise

• Solve the following graph by using Dijkstra


Algorithm

04/22/2024 37
Answer

04/22/2024 38
Job Sequencing With Deadlines

• In this problem we have n jobs j1, j2, … jn each has


an associated deadline d1, d2, … dn and profit p1,
p2, ... pn.
• Profit will only be awarded or earned if the job is
completed on or before the deadline.
• We assume that each job takes unit time to complete.
• The objective is to earn maximum profit when only
one job can be scheduled or processed at any given
time.
04/22/2024 39
Example
• Consider the following 5 jobs and their associated deadline
and profit.

index 1 2 3 4 5

JOB j1 j2 j3 j4 j5

DEADLINE 2 1 3 2 1

PROFIT 60 100 20 40 20

04/22/2024 40
Step1: Sort The Jobs According To Their
Profit In Descending Order

• Note! If two or more jobs are having the same profit then sort
them as per their entry in the job list.

index 1 2 3 4 5

JOB j2 j1 j4 j3 j5

DEADLINE 1 2 2 3 1

PROFIT 100 60 40 20 20

04/22/2024 41
Step 2: Find The Maximum Deadline
Value
• Looking at the jobs we can say the max deadline value is 3.
So, dmax = 3
• As dmax = 3 so we will have THREE slots to keep track of
free time slots. Set the time slot status to EMPTY

time slot 1 2 3

status EMPTY EMPTY EMPTY

04/22/2024 42
Cont..
• Total number of jobs is 5.
So we can write n = 5
• If we look at job j2, it has a deadline 1. This means we have to
complete job j2 in time slot 1 if we want to earn its profit.
• Similarly, if we look at job j1 it has a deadline 2. This means
we have to complete job j1 on or before time slot 2 in order to
earn its profit.
• Our objective is to select jobs that will give us higher profit.

04/22/2024 43
Algorithm
for i = 1 to n do
Set k = min(dmax, DEADLINE(i))
while k >= 1 do
if timeslot[k] is EMPTY then
timeslot[k] = job(i)
break
endif
Set k = k - 1
endwhile
endfor

04/22/2024 44
N=5
i dmax=3

• i=1
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,1)
JOB j2 j1 j4 j3 j5
• Or k=1
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (1)== EMPTY?
YES

time slot 1 2 3 • Fill the time slot(1) with


j2
status EMPTY EMPTY EMPTY
04/22/2024 45
N=5
i dmax=3

• i=1
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,1)
JOB j2 j1 j4 j3 j5
• Or k=1
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (1)== EMPTY?
YES

time slot 1 2 3 • Fill the time slot(1) with


j2
status j2 EMPTY EMPTY
04/22/2024 46
N=5
i dmax=3

• i=2
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,2)
JOB j2 j1 j4 j3 j5
• Or k=2
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (2)== EMPTY?
YES

time slot 1 2 3 • Fill the time slot(2) with


j1
status j2 EMPTY EMPTY
04/22/2024 47
N=5
i dmax=3

• i=2
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,2)
JOB j2 j1 j4 j3 j5
• Or k=2
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (2)== EMPTY?
YES

time slot 1 2 3 • Fill the time slot(2) with


j1
status j2 j1 EMPTY
04/22/2024 48
N=5
i dmax=3

• i=3
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,2)
JOB j2 j1 j4 j3 j5
• Or k=2
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (2)== EMPTY?
NO

time slot 1 2 3 • Reduce k by 1


• K=1
status j2 j1 EMPTY
04/22/2024 49
N=5
i dmax=3

index 1 2 3 4 5 • i=3
• K=1
JOB j2 j1 j4 j3 j5 • Is k>=1? Yes

DEADLI 1 2 2 3 1
• Check time
NE
slot(k)==EMPTY?
PROFIT 100 60 40 20 20 • Time slot (1)==
EMPTY? NO

time slot 1 2 3 • Reduce k by 1


• K=0
status j2 j1 EMPTY
04/22/2024 50
N=5
i dmax=3

index 1 2 3 4 5 • i=3
• K=0
JOB j2 j1 j4 j3 j5 • Is k>=1? NO

DEADLI 1 2 2 3 1
• Increment i by 1
NE
• i=4
PROFIT 100 60 40 20 20

time slot 1 2 3
status j2 j1 EMPTY
04/22/2024 51
N=5
i dmax=3

• i=4
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,3)
JOB j2 j1 j4 j3 j5
• Or k=3
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (3)== EMPTY?
YES

time slot 1 2 3 • Fill the time slot(3) with


j3
status j2 j1 EMPTY
04/22/2024 52
N=5
i dmax=3

• i=4
index 1 2 3 4 5
• k=min(dmax , deadline(i))
• Or k=min(3,3)
JOB j2 j1 j4 j3 j5
• Or k=3
• Is k>=1? YES
DEADLI 1 2 2 3 1
NE
• Check time
PROFIT 100 60 40 20 20 slot(k)==EMPTY?
• Time slot (3)== EMPTY?
YES

time slot 1 2 3 • Fill the time slot(3) with


j3
status j2 j1 j3
04/22/2024 53
N=5
dmax=3

index 1 2 3 4 5 • Time slot is full


• So we stop here!
JOB j2 j1 j4 j3 j5
• The required job
DEADLI 1 2 2 3 1 sequence is :
NE
J2j1j3
PROFIT 100 60 40 20 20 • The maximum profit
is:
100+60+30=180
time slot 1 2 3
The time complexity
status j2 j1 j3 of this problem
04/22/2024 54
is O(n ).
2
Thank
Thank You
You ...
... Question?
Question?

04/22/2024 55

You might also like