Chapter 3
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
• 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.
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.