0% found this document useful (0 votes)
53 views33 pages

Chapter 3

This chapter discusses greedy algorithms, job sequencing with deadlines, minimum spanning trees, and single source shortest paths. It introduces greedy algorithms as making locally optimal choices at each step. Job sequencing with deadlines is solved using a greedy approach by sorting jobs by profit and scheduling them earliest by deadline if possible. Minimum spanning trees are constructed using Kruskal's algorithm by sorting edges by weight and adding edges with no cycles. Single source shortest paths are also discussed.

Uploaded by

mikiberhanu41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views33 pages

Chapter 3

This chapter discusses greedy algorithms, job sequencing with deadlines, minimum spanning trees, and single source shortest paths. It introduces greedy algorithms as making locally optimal choices at each step. Job sequencing with deadlines is solved using a greedy approach by sorting jobs by profit and scheduling them earliest by deadline if possible. Minimum spanning trees are constructed using Kruskal's algorithm by sorting edges by weight and adding edges with no cycles. Single source shortest paths are also discussed.

Uploaded by

mikiberhanu41
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

CHAPTER-3

Greedy Algorithm
• Introduction to Greedy Algorithm
• Job sequencing with deadlines
• Optimal merge pattern
• Minimum spanning trees
• Single source shortest pattern
Introduction to Greedy Algorithm

• Greedy algorithm is an algorithms that makes the best choice locally at


each step without considering future plans.
• Because of the myopic nature of greedy algorithm, it is (as expected) not
correct for many problems.
• However, there are certain problems that can easily be solved using
greedy algorithm, which can be proved to be correct.
Cont’
• A Greedy Algorithm arrives at a solution by making a
sequence of choices, each of which simply looks the best at
the moment.
• That is, each choice is locally optimal.
• The hope is that a globally optimal solution will be
obtained, but this is not always the case.
Cont’
The solutions are called optimal solutions. Since :
• It has strong mathematical foundations and
• Many greedy approach-based solutions are closer to
optimal solutions.
Procedures of a Greedy Algorithm:
• A Selection Procedure: chooses the next item to add to the set
that satisfy some locally optimal consideration at the time.
• A Feasibility Check: determines if the new set is feasible if it
is possible to complete this set.
• A Solution Check: determines whether the new set constitutes
a solution to the instance.
Job sequencing with deadlines
• There are n jobs, J= {1, 2… n}, to be processed on one machine and
one job at a time .
• Each job i has a deadline di>= 0 and
• pi>=0. Pi is earned iff the job is completed by its deadline.
• The job is completed if it is processed for unit time.
• A feasible solution is a subset of jobs J such that each job is completed
by its deadline.
• An optimal solution is a feasible solution with maximum profit value.
Algorithm for Job Sequencing with
Deadlines:
• Step 1: Sort all the given jobs in decreasing order of their profit.
• Step 2: - Check the value of maximum deadline.
- Draw Gantt chart with value of maximum deadline.
• Step 3: - Pick up the jobs one by one.
- Put the job on Gantt chart possible from 0 ensuring that
the job gets completed before its deadline.
Example : Given the jobs, their deadlines and associated profits.

• Question 1: Write the optimal schedule that gives maximum profit.


• Question 2: Are the jobs completed in the optimal schedule?
• Question 3: What is the maximum earned profit?
Solutions
Step 1: Sort all the given jobs in decreasing order of their profit.
Step 2: Value of maximum deadline is 5. Therefore, draw a Gantt
chart with maximum time on Gantt chart=5.

• Take each job one by one in the order they appear in step 1.
• We place the job on Gantt chart as far as possible from 0.
Step 3:
Take job J4. Since the deadline is 2, so we place it in the first empty cell
before deadline 2.

• Step 4:
• Take job J1. Since its deadline is 5, we place it in the first empty
cell before deadline 5.
• Step 5: Take job J3. Since its deadline is 3, we place it in the first empty
cell before deadline 3.

• Step 6:
• Take job J2. Since its deadline is 3, we place it in the first empty
cell before deadline 3.
• Since the second and third cells are already filled, we place job J2
in the first cell.
• Step 7:
• Take job J5.
• Since its deadline is 4, we place it in the first empty cell before deadline 4.

• The only job left is J6 whose deadline is 2.


• All the slots before deadline 2 are already occupied.
• Thus, job J6 can’t be completed.
Solution for question 1:

• The optimal schedule is: J2, J4, J3, J5, J1


• This is the required order in which the jobs must be
completed in order to obtain the maximum profit
Solution for question 2:

• All jobs are not completed in optimal schedule.


• This is because job J6 could not be completed within its
deadline.
Solution for question 3:

• The maximum earned profit is the sum of profit of


all the jobs in optimal schedule.
Profit of J2 + Profit of J4 + Profit of J3 + Profit of J5 +
profit of J1
• 180+300+190+120+200
• 990 Units
Optimal merge pattern

• Is a pattern that is related to the merging of 2 or more sorted files in a single


sorted file.
• It can be done by 2 way merging method.
• It uses greedy method/algorithm while merging.
• If we have 2 sorted files containing N and M records respectively then they
could be merged together, to obtain one sorted file in time O (N+M).
• There are many ways in which pairwise merge can be done to get a single
sorted file.
Example 1: We have 4 lists and their
respective size.

• Solution: We can have different patterns, but we will choose a pair of


smaller size list to get the best result.
• We will see each pattern and finally choose the best one.
• There is no any other way which can lead us to lower total cost which is
less than 31. So that pattern 3 is the best one.
Example 2: We have 5 files with file name and size.

• We will follow the greedy approach. We have to find the pattern at


minimized cost. Therefore, in greedy method, we should arrange the given
size of the list in increasing order.
• Solution: The list will be sorted in increasing order.
Therefore, we will follow optimal merge pattern, we will select a
smaller pair of files and merge them first.

Total cost of merging = 15+35+60+95


= 205

or by multiplying each
external node with their
distance and add the result of
each node.
5*3+ 10*3+ 20*2+ 30*2+30*2= 205
Minimum Spanning Trees
• Is a special kind of spanning tree that minimizes the lengths
(weights) of the edges of the tree.
• whose sum of edge weights is as small as possible.
• There is an algorithm known as Kruskal’s algorithm, which
finds the minimum spanning tree for a connected weighted
graph
The steps of Kruskal‟s algorithm
1. Sort all the edges in increasing order of their weight.
2. Pick the smallest edge.
• Check if it forms a cycle with the spanning tree formed so far.
• If cycle is not formed, include this edge, else discard it.
3. Repeat step 2 until there are (V-1) edges in the spanning tree.
Example: Find a minimum spanning
tree.
• Solution: The graph contains 9 vertices and 14 edges. So the minimum
spanning tree will have (V-1) edges, which is (9-1) =8 edges.

• Step 1: Sort all the edges in non-decreasing order of their weight and put
them with their respective source and destination.
Weight Source Destination
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
Step 2: Pick all the edges one by one from sorted list of edges.

A. Pick edge 7-6: No cycle is formed, therefore include it.


B. Pick edge 8-2: No cycle is formed, therefore include it.
C. Pick edge 6-5: No cycle is formed, therefore include it.
D. Pick edge 0-1: No cycle is formed, therefore include it.
E. Pick edge 2-5: No cycle is formed, therefore include it.
Cont’
F. Pick edge 8-6: This edge results a cycle, therefore discard it.
G. Pick edge 2-3: No cycle is formed, therefore include it.
H. Pick edge 7-8: This edge results a cycle, therefore discard it.
I. Pick edge 0-7: No cycle is formed, therefore include it.
J. Pick edge 1-2: This edge results a cycle, therefore discard it.
K. Pick edge 3-4: No cycle is formed, therefore include it.
Since the number of edges in the minimum spanning tree (V-1) is 8, the
algorithm will stop here. And the minimum spanning tree will be as follows:
Single source shortest pattern
Chapter 4
Dynamic Programming and traversal techniques:

You might also like