Locally Optimal Choice at Each Stage in Hope of Getting A Globally Optimal Solution

You might also like

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

A Greedy Approach

• Idea
– When we have a choice to make, make the one that looks best right now. Make a
locally optimal choice at each stage in hope of getting a globally optimal solution.

This method is used for solving optimization problem( A problem which requires
either minimum result or maximum result).

For a problem there are many solutions, but these solutions which are satisfying the
conditions / constraints given in the problem , these type of solution becomes feasible
solution.
If the problem demands results should be minimum(or maximum), then such problem
is a minimization ( or maximization problem)

A feasible solution which is giving minimum(or maximum) cost is Optimal Solution.


Such problems are called optimization problems.
Algorithm Greedy(a , n)
{
for i = 1 to n do
{
x = Select(a);
if Feasible(x) then
Solution = Solution + x;
}
}

Applications:

1. Knapsack Problem
2. Job Sequencing
3. Minimum Spanning Tree
4. Optimal Merge Pattern
5. Huffman Coding
6. Dijkstra algorithm
Job Sequencing using Deadline

• An interesting problem that can be solved using


matroids is the problem of optimally scheduling
unit-time tasks on a single processor, where each
task has a deadline, along with a penalty that
must be paid if the deadline is missed.

• The problem looks complicated, but it can be


solved in a surprisingly simple manner using a
greedy algorithm.
Job Sequencing using Deadline

Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2

Profits 200 180 190 300 120 10

Step-1: Sort all the given jobs in decreasing order of their profit- 


Jobs J4 J1 J3 J2 J5 J6
Deadlines 2 5 3 3 4 2
Profits 300 200 190 180 120 100

Step-2: Value of maximum deadline = 5.


So, draw a Gantt chart with maximum time on Gantt chart = 5 units as shown-
Applications to MST
Minimum Spanning Trees
A minimum spanning tree is a spanning tree such that
the total length of its arcs (edges) is as small as possible.

A spanning tree is a subgraph, which includes all the


vertices and is a tree A minimum
spanning tree is
A subgraph of G is a graph , each of whose vertices
belongs to G and each of whose edges belongs to G. sometimes called
a minimum
A tree is a connected graph with no cycles.
connector.
A graph is connected if all its vertices are connected.

Two vertices are connected if there is a path between


them.
A path is a walk in which no vertex is visited more than
once.
A walk is a route through a graph along edges from one vertex to the
next. next.
Kruskal’s Algorithm
Kruskal’s Algorithm can be used to find a minimum spanning tree.
1. Sort all the arcs into ascending order of weight.
2. Select the arc of least weight to start the tree.
3. Consider the next arc of least weight.
• If it would form a cycle with the arcs already selected reject it.
• If it does not form a cycle, add it to the tree.
• If there is a choice of equal arcs then consider each in turn.
4. Repeat step 3 until all vertices are connected.
Uses of minimum spanning trees
• Cluster Analysis.
• Real-time face tracking and verification (i.e. locating Kruskal’s Algorithm is
human faces in a video stream). sometimes called the “greedy
• Protocols in computer science to avoid network cycles. algorithm” because it gobbles
• Entropy based image registration. up the best (least weight) arcs
• Max bottleneck paths. first. It considers the arcs, not
• Dithering (adding white noise to a digital recording in the vertices.
order to reduce distortion).
https://www.statisticshowto.com/minimum-spanning-tree/
Example of using Kruskal’s Algorithm

Use Kruskal’s algorithm to find the MST for the below network.
D

4 8 Kruskal’s Algorithm can be used to


find a minimum spanning tree.
E 6 C
6 1. Sort all the arcs into
5 ascending order of weight.
5
7 B 2. Select the arc of least weight
A Next to start the tree.
3. Consider the next arc of
least weight.
• If it would form a cycle with
Order of the arcs is. the arcs already selected
reject it.
DE(4), AE(5), BC(5), AD(6), BD(6), AB(7), CD(8), • If it does not form a cycle,
add it to the tree.
Start with DE • If there is a choice of equal
D
D arcs then consider each in
All vertices are connected turn.
4 so this is a minimum 4. Repeat step 3 until all
E C
E 6 C spanning tree. vertices are connected.
6
5
5 Its weight is 20.
B
A B
A
Test your understanding
Use Kruskal’s algorithm to find the MST for the below network.
A
6 6 Order of the arcs is.
B D
1 6 BC(1), IJ(1), GI(2),
2 2 18 CG(2), BE(2), CD(2), Next
6 C KL(3), EF(4), AB(6),
E 2 J
AD(6), AC(6), EC(6),
4 11 G 1
8 JL(8), FH(10),FG(11),
F 2
IH(12), IK(16), DJ(18),
22 L
10 I GH (22), HK(25) AA
12 6 6
16 3 BB 6 DD
H
25
Start with BC 1
2 2
K 6
CC
EE 2 JJ
All vertices are connected so this is a minimum 4
GG
1
8
spanning tree. 2
FF
Its weight is 41. LL
10 II

Note – there are several different MST’s for this 3


HH
question, depending which order you take the equal
KK
weight edges in.
Prim’s Algorithm
1) Choose any vertex to start the tree.
2) Select an arc of least weight that joins a vertex that is already in the tree to a vertex
that is not yet in the tree.
If there is a choice of arcs of equal weight, choose randomly.
3) Repeat step 2 until all the vertices are connected.

What is the main difference between Prim’s and Kruskal’s algorithm?

Prim’s considers vertices, whereas Kruskal’s considers edges.

Why might you use one rather than the other?

To use Kruskal you have to sort all the edges into order first, this could
be time consuming so Prim’s may be faster unless the edges are
already sorted. Prim’s is usually faster if you have a graph with high
ratio of edges to vertices.
Prim’s Algorithm - example
D 1) Choose any vertex to start
the tree.
4 8 2) Select an arc of least weight
that joins a vertex that is
E 6 C already in the tree to a vertex
6
5 that is not yet in the tree.
5 If there is a choice of arcs of
7 B equal weight, choose
A randomly.
3) Repeat step 2 until all the
Start anywhere – we will start vertices are connected.
at vertex A.
Add AE (5)
Next
Add ED (4) D
D
Add DB (6) 4
Add BC (5) EE 6 CC

All vertices are now connected 5


BB
5

so the minimum spanning tree AA

is weight 20.
Test your understanding
Use Prim’s algorithm to find the MST for the below network.
A
6 6 Starting at vertex A
B D
1 6 Choose AB (6)
2 2 18 Add BC (1) Next
6 C Choose BE(2)
E 2 J
Choose CD (2)
4 11 G 1
8 Add CG (2)
F 2
Add GI (2)
22 L
10 I Add IJ (1) A A
12 Add EF(4) 6
16 B B
3 D D
H
25
Add JL (8) 1
2 2
K Add LK (3)
C C
Add FH (10) E E 2 J J
4 1
G G 8
All vertices are connected so this is a minimum F F 2
spanning tree. I I L L
10
Its weight is 41.
3
H H
K K

You might also like