Professional Documents
Culture Documents
Locally Optimal Choice at Each Stage in Hope of Getting A Globally Optimal Solution
Locally Optimal Choice at Each Stage in Hope of Getting A Globally Optimal Solution
Locally Optimal Choice at Each Stage in Hope of Getting A Globally Optimal Solution
• 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)
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
Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2
Use Kruskal’s algorithm to find the MST for the below network.
D
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
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