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

Unit-2

Greedy Method
• Makes the choice that looks best at the
moment.
• Works on the property:
– Greedy Choice Property: Choose Local optimal
choice in the hope that this choice lead to global
optimal solution.
– Optimal Sub Structure: Contains Optimal sub
solution.
Components of Greedy Algorithm
• Greedy algorithms have the following
components
o candidate set − A solution is created from this set.
o Feasible solution − Subset that satisfies the
constraints.
o Objective function − Used to find a feasible
solution that either maximizes or minimizes.
o Optimal Solution − final solution
Knapsack Problem
• Given a set of items i1, i2 ..... in
• weights W1, W2 ....Wn
• Values V1, V2 ..... Vn
• Weight limit W
• Objective: Maximize the benefit ( at most W)
• Fractional part of an item is allowed.
Items Weight Value
W = 16
1 6 6

2 10 2

3 3 1

4 5 8

5 1 3

6 3 5
Steps to be followed
• Step 1: Calculate value per weight for each
item.
• Step 2: Sort the value density in descending
order.
• Step 3: take as much as items not already
taken in the knapsack.
Items Weight Value Value
density
1 6 6 1.0

2 10 2 0.20

3 3 1 0.33

4 5 8 1.60

5 1 3 3.00

6 3 5 1.66
Sort the times as per value density
Items Weight Value Value
density
5 1 3 3.00

6 3 5 1.66

4 5 8 1.60

1 6 6 1.00

3 3 1 0.33

2 10 2 0.20
Weight + Total weight<= W Total Weight in Knapsack: 1

1+0 < = 16

Knapsack
Items Weight Value Total Benefit
Items Weight Value Value
density weight

5 1 3 3.00
6 3 5 1.66 5 1 3 1.00 3.00
4 5 8 1.60
1 6 6 1.00
3 3 1 0.33
2 10 2 0.20
Weight + Total weight<= W Total Weight in Knapsack: 5

1+4 < = 16

Knapsack
Items Weight Value Total Benefit
Items Weight Value Value
density weight

5 1 3 3.00
6 3 5 1.66 5 1 3 1.00 3.00
4 5 8 1.60
1 6 6 1.00 6 3 5 4.00 8.00
3 3 1 0.33
2 10 2 0.20
Weight + Total weight<= W Total Weight in Knapsack: 9

5+4 < = 16

Knapsack
Items Weight Value Total Benefit
Items Weight Value Value
density weight

5 1 3 3.00
6 3 5 1.66 5 1 3 1.00 3.00
4 5 8 1.60
1 6 6 1.00 6 3 5 4.00 8.00
3 3 1 0.33
2 10 2 0.20 4 5 8 9.00 16.00
Weight + Total weight<= W Total Weight in Knapsack: 15

9+6 < = 16

Knapsack
Items Weight Value Total Benefit
Items Weight Value Value
density weight

5 1 3 3.00
6 3 5 1.66 5 1 3 1.00 3.00
4 5 8 1.60
1 6 6 1.00 6 3 5 4.00 8.00
3 3 1 0.33
2 10 2 0.20 4 5 8 9.00 16.00
1 6 6 15.00 22.00
Weight + Total weight<= W Total Weight in Knapsack: 16

16 < = 16

Knapsack
Items Weight Value Total Benefit
Items Weight Value Value
density weight

5 1 3 3.00
6 3 5 1.66 5 1 3 1.00 3.00
4 5 8 1.60
1 6 6 1.00 6 3 5 4.00 8.00
3 3 1 0.33
2 10 2 0.20 4 5 8 9.00 16.00
1 6 6 15.00 22.00

3 1 0.33 16.00 22.33


Job Scheduling with Deadlines
• n jobs j1, j2 ..... jn
• Deadline D1, D2 ....Dn
• Profit p1,p2 ..... Pn
• Profit will be awarded if the job is completed
on or before the deadline.
• Assume each job takes unit time.
• Objective: earn maximum profit by assigning
one job at a time.
Job 1 2 3 4 5
Deadline 2 1 3 2 1
Profit 60 100 20 40 20
Job 2 1 4 3 5
Deadline 1 2 2 3 1

Profit 100 60 40 20 20

dmax n=5 dmax=3

Time slot 1 2 3
Status Empty Empty Empty
Check, time slot(1) == empty Yes

Job 2 1 4 3 5
Deadline 1 2 2 3 1

Profit 100 60 40 20 20

Fill slot 1 with job 2

Time slot 1 2 3
Status Empty Empty Empty
Check, time slot (2)== empty Yes

Job 2 1 4 3 5
Deadline 1 2 2 3 1

Profit 100 60 40 20 20

Fill slot 2 with job 1

Time slot 1 2 3
Status Job 2 Job 1 Empty
Check, time slot (2)== empty No

Job 2 1 4 3 5
Deadline 1 2 2 3 1

Profit 100 60 40 20 20

Move to next job

Time slot 1 2 3
Status Job 2 Job 1 Empty
Check, time slot (3)== empty Yes

Job 2 1 4 3 5
Deadline 1 2 2 3 1

Profit 100 60 40 20 20

Fill slot (3) with job 3

Time slot 1 2 3
Status Job 2 Job 1 job 3
Job 2 1 4 3 5
Deadline 1 2 2 3 1

Profit 100 60 40 20 20

Time slot 1 2 3
Status Job 2 Job 1 job 3

Required job sequence is j2 -> j1 -> j3

Max profit = 100 + 60 + 20 = 180


Minimum-Cost Spanning Trees
• Spanning Tree: Connected Graph using all
vertices in which there is no cycle.
• Example:
Undirected graph G = (V,E)
A B

C D
A B

C D

A B A B

C D C D

A B A B

C C D
D
• Minimum Spanning Tree:
• Find a subset of E with minimum total weight
that connect all the vertices into a tree.
Prims Algorithm

4
11
B
D

4 5
5

10 C
A
8
12
Step 1: Remove Loops

4
11
B
D

4 5
5

10 C
A
8
12
Step 2: Remove all parallel edges

11
B
D

4 5
5

10 C
A

12
Step3: create table
Graph has 4 vertices, so table contains 4 rows & 4
columns
11
B
D

4 5
5

10 C
A
A B C D
A
0 5 10 ∞
5 0 4 11
B
10 4 0 5

∞ 11 5 0

11
B
C D

D 4 5
5

10 C
A
A B C D
A
0 5 10 ∞
5 0 4 11
B
10 4 0 5

∞ 11 5 0

11
B D
C

D 4 5
5

10 C
A
Required Minimum Spanning Tree

B D

4 5
5

Cost = 5+4+5= 14
Kruskal’s Algorithm
Step 1: Remove Loops

4
11
B
D

4 5
5

10 C
A
8
12
Step 2: Remove all parallel edges

11
B
D

4 5
5

10 C
A

12
Step3: create edge table
Graph has 4 vertices, so table contains 4 rows & 4
columns
11
B
D

4 5
5

10 C
A
Edge BC AB DC AC BD
Weight 4 5 5 10 11

11
B D

4 5
5

10 C
A
Required Minimum Spanning Tree

B D

4 5
5

Cost = 5+4+5= 14
Optimal Storage on Tapes
• There are n programs that are to be stored on
a computer tape of length l.
• Program I has length Il
• Let programs are stored in the order I = i1,
i2, ....in, Time needed to retrieve program ij
Proportional to
l1
i1 i2 i3 i4 .... in

You might also like