Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
37 views6 pages

Daa Unit III

1. The document discusses the greedy approach and its application to problems like job sequencing with deadlines and the knapsack problem. 2. It provides pseudocode for the greedy method algorithm and explains how it works by making locally optimal choices at each step. 3. Examples are given to show how the greedy approach can be applied to solve the job sequencing and knapsack problems to optimize objectives like profit maximization.

Uploaded by

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

Daa Unit III

1. The document discusses the greedy approach and its application to problems like job sequencing with deadlines and the knapsack problem. 2. It provides pseudocode for the greedy method algorithm and explains how it works by making locally optimal choices at each step. 3. Examples are given to show how the greedy approach can be applied to solve the job sequencing and knapsack problems to optimize objectives like profit maximization.

Uploaded by

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

02-03-2017

What is Greedy Approach?

 Suppose that a problem can be solved by a sequence


of decisions. The greedy method has that each
decision is locally optimal. These locally optimal
solutions will finally add up to a globally optimal
solution.
 Only a few optimization problems can be solved by
the greedy method.

Control abstraction for Greedy Method Three important activities


Algorithm GreedyMethod (a, n)
{ 1. A selection of solution from the given input
// a is an array of n inputs
Solution: =Ø;
domain is performed, i.e. s:= select(a).
for i: =0 to n do 2. The feasibility of the solution is performed, by
{ using feasible ‘(solution, s)’ and then all feasible
s: = select (a);
if (feasible (Solution, s)) then
solutions are obtained.
{ 3. From the set of feasible solutions, the particular
Solution: = union (Solution, s); solution that minimizes or maximizes the given
}
else objection function is obtained. Such a solution is
reject (); // if solution is not feasible reject it. called optimal solution.
}
return solution;
}

1
02-03-2017

Differentiate Greedy and Divide-and-Conquer Application - JOB SEQUENCING WITH DEADLINES


GREEDY APPROACH DIVIDE AND CONQUER Procedure
1.Many decisions and sequences areguar 1.Divide the given problem into many su
anteed and all the overlapping subinstan bproblems.Find the individual solutions • In this problem we have n jobs j1, j2, … jn, each has an
cesare considered. andcombine them to get the solution for t associated their deadlines d1, d2, … dn and their profits
hemain problem p1, p2, ... pn.
2. Follows Bottom-up technique 2. Follows top down technique
3.Split the input at every possible points 3.Split the input only at specific points (
rather midpoint), each problem is independent.
• Profit will only be awarded or earned if the job is
than at a particular point completed on or before the deadline.
4. Sub problems are dependent 4. Sub problems are independent
on the main on the main • We assume that each job takes unit time to complete.
Problem Problem
5. Time taken by this approach is not 5. Time taken by this approach efficient
that much efficient when compared with when compared with GA.
• The objective is to earn maximum profit when only
DAC. one job can be scheduled or processed at any given
6.Space requirement is less when 6.Space requirement is very much high time.
compared DAC approach. when compared GA approach.

Contd … Initially
Contd ...
time slot 1 2 3
Example:
index 1 2 3 4 5
status EMPTY EMPTY EMPTY
JOB j1 j2 j3 j4 j5
DEADLINE 2 1 3 2 1
PROFIT 60 100 20 40 20 Optimal Solution
time slot 1 2 3
index 1 2 3 4 5
status J2 J1 J3
JOB j2 j1 j4 j3 j5
DEADLINE 1 2 2 3 1
PROFIT 100 60 40 20 20
Maximum Profit: 100+60+20 = 180

2
02-03-2017

Algorithm
Application - KNAPSACK PROBLEM
• In this problem we have a Knapsack that has a weight
limit M
• There are items i1, i2, ..., in each having weight w1, w2,
… wn and some benefit (value or profit) associated with
it p1, p2, ..., pn
• Our objective is to maximise the benefit such that the
total weight inside the knapsack is at most M, and we
are also allowed to take an item in fractional part.

Example Algorithm

ITEM WEIGHT VALUE


i1 6 6
M=16
i2 10 2
i3 3 1
i4 5 8
i5 1 3
i6 3 5
• Maximum Profit (20)
• Minimum Weight (17)
• Maximum Profit – Weight ratio (22.333336)

3
02-03-2017

i. Kruskal’s Algorithm
Application – Minimum Spanning Tree
• A spanning tree is a subset of Graph G, which has all Step 1 - Remove all loops and Parallel Edges.
the vertices covered with minimum possible number Step 2 - Arrange all edges in their increasing order of
of edges. Hence, a spanning tree does not have cycles weight.
and it cannot be disconnected. Step 3 - Add the edge which has the least weightage iff it
Note 1: Every connected and undirected Graph G has does not form cycle.
at least one spanning tree.
Note 2: A disconnected graph does not have any Ex:
spanning tree.
• A complete undirected graph can have
maximum nn-2 number of spanning trees, where n is
the number of nodes.

Algorithm

4
02-03-2017

ii. Prim’s Algorithm


• Prim's algorithm, in contrast with Kruskal's
algorithm, treats the nodes as a single tree and keeps
on adding new nodes to the spanning tree from the
given graph.
• Step 1 - Remove all loops and parallel edges.
• Step 2 - Choose any arbitrary node as root node.
• Step 3 - Check outgoing edges and select the one
with less cost.

Algorithm Application – Single source shortest path


• For a given source node in the graph, the algorithm
finds the shortest path between that node and every
other. It also used for finding the shortest paths from
a single node to a single destination node by
stopping the algorithm once the shortest path to the
destination node has been determined.

5
02-03-2017

Algorithm

Kruskal’s vs Prim’s
 Prim’s algorithm initializes with a node, whereas
Kruskal’s algorithm initiates with an edge.
 Prim’s algorithms span from one node to another
while Kruskal’s algorithm select the edges in a way
that the position of the edge is not based on the
last step.
 In prim’s algorithm, graph must be a connected
graph while the Kruskal’s can function on
disconnected graphs too.
 Prim’s algorithm has a time complexity of O(V2),
and Kruskal’s time complexity is O(ElogV).

You might also like