0% found this document useful (0 votes)
39 views88 pages

5CS4-AOA-Unit-5 - PPT @zammers

Uploaded by

piyid14807
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)
39 views88 pages

5CS4-AOA-Unit-5 - PPT @zammers

Uploaded by

piyid14807
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/ 88

Arya Institute Of Engineering and Technology,

Jaipur

Submitted by: Pratibha Sharma


Subject: Analysis of Algorithms
Dept.- Computer Science & Engineering
AIET, Jaipur

1
Complexity Classes

 P class
 NP Class
 NP-Hard Class
 NP-Complete Class
Classification of Problems
 In Computer Science, many problems are solved
where the objective is to maximize or minimize
some values, whereas in other problems we try
to find whether there is a solution or not. Hence,
the problems can be categorized as follows −
 Optimization Problem:
 Optimization problems are those for which the
objective is to maximize or minimize some values.
For example,
 Finding the minimum number of colors needed to
color a given graph.
 Finding the shortest path between two vertices in
a graph.
Decision Problem:
 There are many problems for which the
answer is a Yes or a No. These types of
problems are known as decision
problems. For example,

 Whether a given graph can be colored by


only 4-colors.

 FindingHamiltonian cycle in a graph is not a


decision problem, whereas checking a graph
is Hamiltonian or not is a decision problem.
P-Class
 The class P consists of those problems that are solvable in
polynomial time, i.e. these problems can be solved in time O(nk) in
worst-case, where k is constant.

 These problems are called tractable, while others are


called intractable or superpolynomial.

 Formally, an algorithm is polynomial time algorithm, if there exists a


polynomial p(n) such that the algorithm can solve any instance of
size n in a time O(p(n)).

 Problem requiring Ω(n50) time to solve are essentially intractable


for large n. Most known polynomial time algorithm run in
time O(nk) for fairly low value of k.

 The advantages in considering the class of polynomial-time


algorithms is that all reasonable deterministic single processor
model of computation can be simulated on each other with at
most a polynomial slow-d
 For input size n, if worst-case time complexity of
an algorithm is O(nk), where k is a constant, the
algorithm is a polynomial time algorithm.

 Algorithms such as Matrix Chain Multiplication,


Single Source Shortest Path, All Pair Shortest
Path, Minimum Spanning Tree, etc. run in
polynomial time.

 However there are many problems, such as


traveling salesperson, optimal graph coloring,
Hamiltonian cycles, finding the longest path in a
graph, and satisfying a Boolean formula, for which
no polynomial time algorithms is known.

 These problems belong to an interesting class of


problems, called the NP-Complete problems,
whose status is unknown.
NP-Class
 Non-Deterministic polynomial time problem.
 The class NP consists of those problems that are verifiable in
polynomial time. NP is the class of decision problems for which it is
easy to check the correctness of a claimed answer, with the aid of a
little extra information.
 Hence, we aren’t asking for a way to find a solution, but only to
verify that an alleged solution really is correct.
 Every problem in this class can be solved in exponential time using
exhaustive search.
 P versus NP
 Every decision problem that is solvable by a deterministic
polynomial time algorithm is also solvable by a polynomial time
non-deterministic algorithm.
 All problems in P can be solved with polynomial time algorithms,
whereas all problems in NP - P are intractable.
 It is not known whether P = NP. However, many problems are
known in NP with the property that if they belong to P, then it can
be proved that P = NP.
 If P ≠ NP, there are problems in NP that are neither in P nor in
NP-Complete.
Definition of Decision Based Problem
 A problem is called a decision problem if its output is a
simple "yes" or "no" (or you may need this of this as
true/false, 0/1, accept/reject.)
 We will phrase many optimization problems as decision
problems. For example, Greedy method, D.P., given a graph
G= (V, E) if there exists any Hamiltonian cycle.
Definition of NP-hard class
 Here you to satisfy the following points to come into the
division of NP-hard
 L is NP-hard if for all L' ϵ NP, L' ≤p L. Thus if we can solve L
in polynomial time, we can solve all NP problems in
polynomial time.
 If we can solve this problem in polynomial time, then we can
solve all NP problems in polynomial time
 If you convert the issue into one form to another form
within the polynomial time
Definition of NP-complete class
 A problem is in NP-complete, if
 It is in NP
 It is NP-hard
Polynomial Time Verification
 Before talking about the class of NP-complete
problems, it is essential to introduce the notion of
a verification algorithm.
 Many problems are hard to solve, but they have
the property that it easy to authenticate the
solution if one is provided.

 Hamiltonian cycle problem:-


 Consider the Hamiltonian cycle problem. Given
an undirected graph G, does G have a cycle that
visits each vertex exactly once? There is no
known polynomial time algorithm for this dispute.
 Note: - It means you can't build a Hamiltonian cycle in a graph with
a polynomial time even if there is no specific path is given for the
Hamiltonian cycle with the particular vertex, yet you can't verify
the Hamiltonian cycle within the polynomial time

 We could then inspect the graph and check that this is indeed a
legal cycle and that it visits all of the vertices of the graph exactly
once. Thus, even though we know of no efficient way to solve the
Hamiltonian cycle problem, there is a beneficial way to verify that a
given cycle is indeed a Hamiltonian cycle.
 Note:-For the verification in the Polynomial-time of an undirected
Hamiltonian cycle graph G. There must be exact/specific/definite
path must be given of Hamiltonian cycle then you can verify in the
polynomial time.
Reductions
 The class NP-complete (NPC) problems consist of a set of
decision problems (a subset of class NP) that no one knows
how to solve efficiently. But if there were a polynomial
solution for even a single NP-complete problem, then every
problem in NPC will be solvable in polynomial time. For this,
we need the concept of reductions.
 Suppose there are two problems,A and B.
 You know that it is impossible to solve problem A in
polynomial time.
 You want to prove that B cannot be explained in polynomial
time.
 We want to show that (A ∉ P) => (B ∉ P)

 Polynomial Time Reduction


 We say that Decision Problem L1 is Polynomial time
Reducible to decision Problem L2 (L1≤p L2) if there is a
polynomial time computation function f such that of all x,
xϵL1 if and only if xϵL2.
NP-Completeness
A decision problem L is NP-Hard if
L' ≤p L for all L' ϵ NP.

Definition: L is NP-complete if
1.L ϵ NP and
2.L' ≤ p L for some known NP-
complete problem L.'
NP-Complete Problems
 Following are some NP-Complete problems,
for which no polynomial time algorithm is
known.
 Determining whether a graph has a
Hamiltonian cycle
 Determining whether a Boolean formula is
satisfiable, etc.
NP-Hard Problems
 The following problems are NP-Hard
 The circuit-satisfiability problem
 Set Cover
 Vertex Cover
 Travelling Salesman Problem
DECISION PROBLEMS
 n computability theory and computational complexity theory,
a decision problem is a problem that can be posed as
a yes-no question of the input values.

 An example of a decision problem is deciding whether a
given natural number is prime.

 Another is the problem "given two numbers x and y,


does x evenly divide y?“

 The answer is either 'yes' or 'no' depending upon the values


of x and y.

 A method for solving a decision problem, given in the form


of an algorithm, is called a decision procedure for that
problem.
 A decision procedure for the decision problem "given
two numbers x and y, does x evenly divide y?" would
give the steps for determining whether x evenly
divides y. One such algorithm is long division.

 If the remainder is zero the answer is 'yes', otherwise


it is 'no'. A decision problem which can be solved by
an algorithm is called decidable.

 Decision problems typically appear in mathematical


questions of decidability, that is, the question of the
existence of an effective method to determine the
existence of some object or its membership in a set;
some of the most important problems in
mathematics are undecidable.

 The field of computational complexity


categorizes decidable decision problems by how
difficult they are to solve. "Difficult", in this sense, is
Cook’s Theorem
 Cook’s Theorem states that
 SAT is NP-Complete Problem
 In order to prove this, we require a
uniform way of representing NP
problems.
 Remember that what makes a problem
NP is the existence of a polynomial-time
algorithm—more specifically, a Turing
machine—for checking candidate
certificates.
 What Cook did was somewhat analogous
to what Turing did when he showed that
the Entscheidungs problem was
equivalent to the Halting Problem.
 He showed how to encode as Propositional
Calculus clauses both the relevant facts
about the problem instance and the Turing
machine which does the certificate-checking,

 In such a way that the resulting set of clauses


is satisfiable if and only if the original
problem instance is positive.

 Thus the problem of determining the latter


is reduced to the problem of determining
the former
P=NP?
 We have seen that a problem is NP-complete if and only if it is NP
and any NP problem can be converted into it in polynomial time.

 (A problem satisfying the second condition is called NP-hard; so


NP-complete means NP and NP-hard.)

 It follows from this that all NP-complete problems are mutually


interconvertible in polynomial time.

 For if D1 and D2 are both NP-complete, then D1 can be converted


into D2 by virtue of the fact that D1 is NP and D2 is NP-hard, and
D2 can be converted into D1 by virtue of the fact that D2 is NP
and D1 is NP-hard.

 Thus as far as computational complexity is concerned, all NP-


complete problems agree to within some polynomial amount of
difference. But what is the computational complexity of these
problems?
 If any one NP-complete problem can be shown to
be of polynomial complexity, then by the above
they all are.

 If on the other hand any one NPcomplete


problem can be shown not to be solvable in
polynomial time, then by the above, none of them
are so solvable.

 All NP-complete problems stand or fall together.


 The current state of our knowledge is this:

 we know how to solve NP-complete problems in


exponential time, but there is no NP-complete
problem for which any algorithm is known that
runs in less than exponential time.
 On the other hand, no-one has ever succeeded in proving that it is
not possible to solve an NP-complete problem faster than that.

 This implies, of course, that no-one has proved that NP-complete


problems can’t be solved in polynomial time.

 If we could solve NP-complete problems in polynomial time, then


the whole class NP would collapse down into the class P of
problems solvable in polynomial time.

 This is because the NP-complete problems are the hardest of all


NP problems, and if they are polynomial then all NP problems are
polynomial.

 Thus the question of whether or not there are polynomial


algorithms for NP-complete problems has become known as the
“P=NP?” problem. Most people who have an opinion on this
believe that the answer is no, that is, NP-complete problems are
strictly harder than polynomial problems.
Circuit -SAT
 According to given decision-based NP
problem, you can design the CIRCUIT
and verify a given mentioned output also
within the P time.
 The CIRCUIT is provided below:-
 Note:-

 You can design a circuit and verified the


mentioned output within Polynomial time
but remember you can never predict the
number of gates which produces the high
output against the set of inputs/high
inputs within a polynomial time.

 So you verified the production and


conversion had been done within
polynomial time. So you are in NPC.
 SAT (Satisfiability):-
 A Boolean function is said to be SAT if the output
for the given value of the input is true/high/1

 F=X+YZ (Created a Boolean function by


CIRCUIT SAT)

 These points you have to be performed for


NPC

 CONCEPTS OF SAT
 CIRCUIT SAT≤ρ SAT
 SAT≤ρ CIRCUIT SAT
 SAT ϵ NPC
 CONCEPT: - A Boolean function is said to be SAT if the
output for the given value of the input is true/high/1.

 CIRCUIT SAT≤ρ SAT: - In this conversion, you have to


convert CIRCUIT SAT into SAT within the polynomial time
as we did it

 SAT≤ρ CIRCUIT SAT: - For the sake of verification of an


output you have to convert SAT into CIRCUIT SAT within
the polynomial time, and through the CIRCUIT SAT you can
get the verification of an output successfully

 SAT ϵ NPC: - As you know very well, you can get the SAT
through CIRCUIT SAT that comes from NP.

 Proof of NPC: - Reduction has been successfully made


within the polynomial time from CIRCUIT SAT TO SAT.
Output has also been verified within the polynomial time as
you did in the above conversation.
Clique Decision problem is NP-
Complete
 A clique is a subgraph of a graph such that all the
vertices in this subgraph are connected with each
other that is the subgraph is a complete graph.

 The Maximal Clique Problem is to find the


maximum sized clique of a given graph G, that is a
complete graph which is a subgraph of G and
contains the maximum number of vertices.

 This is an optimization problem. Correspondingly,


the Clique Decision Problem is to find if a clique
of size k exists in the given graph or not.
 To prove that a problem is NP-Complete, we
have to show that it belongs to both NP and NP-
Hard Classes.

 (Since NP-Complete problems are NP-Hard


problems which also belong to NP)

 The Clique Decision Problem belongs to


NP – If a problem belongs to the NP class, then it
should have polynomial-time verifiability, that is
given a certificate, we should be able to verify in
polynomial time if it is a solution to the problem.

 Proof:
 Certificate – Let the certificate be a set S
consisting of nodes in the clique and S is a
subgraph of G.
 Verification – We have to check if there exists a
clique of size k in the graph. Hence, verifying if
number of nodes in S equals k, takes O(1) time.
 Verifying whether each vertex has an out-degree
of (k-1) takes O(k2) time.
 (Since in a complete graph, each vertex is
connected to every other vertex through an
edge. Hence the total number of edges in a
complete graph = kC2 = k*(k-1)/2 ).
 Therefore, to check if the graph formed by the k
nodes in S is complete or not, it takes O(k2) =
O(n2) time (since k<=n, where n is number of
vertices in G).
 Therefore, the Clique Decision Problem has
polynomial time verifiability and hence belongs to
the NP Class.
 The Clique Decision Problem belongs to NP-
Hard – A problem L belongs to NP-Hard if every NP
problem is reducible to L in polynomial time. Now, let
the Clique Decision Problem by C.

 To prove that C is NP-Hard, we take an already


known NP-Hard problem, say S, and reduce it to C
for a particular instance.

 If this reduction can be done in polynomial time, then


C is also an NP-Hard problem. The Boolean
Satisfiability Problem (S) is an NP-Complete problem
as proved by the Cook’s theorem.

 Therefore, every problem in NP can be reduced to S


in polynomial time. Thus, if S is reducible to C in
polynomial time, every NP problem can be reduced
to C in polynomial time, thereby proving C to be NP-
Hard.
 Proof that the Boolean Satisfiability problem reduces
to the Clique Decision Problem
Let the boolean expression be – F = (x1 v x2) ^ (x1‘ v
x2‘) ^ (x1 v x3) where x1, x2, x3 are the variables, ‘^’
denotes logical ‘and’, ‘v’ denotes logical ‘or’ and x’
denotes the complement of x.

 Let the expression within each parentheses be a


clause. Hence we have three clauses – C1, C2 and C3.

 Consider the vertices as – <x1, 1>; <x2, 1>; <x1’, 2>;


<x2’, 2>; <x1, 3>; <x3, 3> where the second term in
each vertex denotes the clause number they belong
to.We connect these vertices such that –

 No two vertices belonging to the same clause are


connected.
 No variable is connected to its complement.
 Thus, the graph G (V, E) is constructed such that – V = { <a, i> | a
belongs to Ci } and E = { ( <a, i>, <b, j> ) | i is not equal to j ; b is
not equal to a’ }

 Consider the subgraph of G with the vertices <x2, 1>; <x1’, 2>; <x3,
3>. It forms a clique of size 3 (Depicted by dotted line in above
figure) .

 Corresponding to this, for the assignment – <x1, x2, x3> = <0, 1,


1> F evaluates to true. Therefore, if we have k clauses in our
satisfiability expression,

 we get a max clique of size k and for the corresponding assignment
of values, the satisfiability expression evaluates to true. Hence, for a
particular instance,

 the satisfiability problem is reduced to the clique decision


problem.

 Therefore, the Clique Decision Problem is NP-Hard.


 Conclusion
The Clique Decision Problem is NP and NP-Hard. Therefore, the
Clique decision problem is NP-Complete.
Vertex Cover is NP complete
 Problem – Given a graph G(V, E) and a
positive integer k, the problem is to find
whether there is a subset V’ of vertices of
size at most k, such that every edge in the
graph is connected to some vertex in V’.

 Since an NP Complete problem, by


definition, is a problem which is both in
NP and NP hard, the proof for the
statement that a problem is NP Complete
consists of two parts:
 Proof that vertex cover is in NP –
If any problem is in NP, then, given a
‘certificate’ (a solution) to the problem and
an instance of the problem (a graph G and a
positive integer k, in this case), we will be
able to verify (check whether the solution
given is correct or not) the certificate in
polynomial time.

 The certificate for the vertex cover problem


is a subset V’ of V, which contains the
vertices in the vertex cover.

 We can check whether the set V’ is a vertex


cover of size k using the following strategy
(for a graph G(V, E)):
 let count be an integer
 set count to 0
 for each vertex v in V’
 remove all edges adjacent to v from set E
 increment count by 1
 if count = k and E is empty
 then
 the given solution is correct
 else
 the given solution is wrong
 Proof that vertex cover is NP Hard –
To prove that Vertex Cover is NP Hard, we
take some problem which has already been
proven to be NP Hard, and show that this
problem can be reduced to the Vertex
Cover problem. For this, we consider the
Clique problem, which is NP Complete (and
hence NP Hard).

 In computer science, the Clique problem is


the computational problem of finding cliques
(subsets of vertices, all adjacent to each other,
also called complete subgraphs) in a graph.”
Here, we consider the problem of finding out
whether there is a clique of size k in the given
graph.

Therefore, an instance of the clique problem is a


graph G (V, E) and a non-negative integer k, and we
need to check for the existence of a clique of
size k in G.

Now, we need to show that any instance (G, k) of


the Clique problem can be reduced to an instance
of the vertex cover problem.

Consider the graph G’ which consists of all edges


not in G, but in the complete graph using all
vertices in G. Let us call this the complement of G.
 Now, the problem of finding whether a
clique of size k exists in the graph G is the
same as the problem of finding whether
there is a vertex cover of size |V| – k in G’.

 We need to show that this is indeed the


case.

 Assume that there is a clique of size k in G.


Let the set of vertices in the clique be V’.

 This means |V’| = k. In the complement


graph G’, let us pick any edge (u, v).
 Then at least one of u or v must be in the
set V – V’. This is because, if both u and v
were from the set V’, then the edge (u, v)
would belong to V’, which, in turn would
mean that the edge (u, v) is in G.

 This is not possible since (u, v) is not in G.


Thus, all edges in G’ are covered by
vertices in the set V – V’.
 Now assume that there is a vertex cover V’’ of
size |V| – k in G’. This means that all edges in G’
are connected to some vertex in V’’.
 As a result, if we pick any edge (u, v) from G’,
both of them cannot be outside the set V’’. This
means, all
edges (u, v) such that both u and v are outside
the set V’’ are in G, i.e., these edges constitute a
clique of size k.
 Thus, we can say that there is a clique of size k in
graph G if and only if there is a vertex cover of
size |V| – k in G’, and hence, any instance of the
clique problem can be reduced to an instance of
the vertex cover problem. Thus, vertex cover is
NP Hard.
 Since vertex cover is in both NP and NP Hard
classes, it is NP Complete.
 To understand the proof, consider the following
example graph and its complement:
Hamiltonian Path is NP-Complete
 The class of languages for which membership can
be decided quickly fall in the class of P and The class of
languages for which membership can be verified quickly fall in
the class of NP(stands for problem solved in Non-
deterministic Turing Machine in polynomial time).
In straight words, every NP problem has its own polynomial-
time verifier.

 We are interested in NP-Complete problems. NP-Complete


problem is defined as We are interested in NP-Complete
problems.

 NP-Complete problem is defined as follows:


 (1)The problem itself is in NP class.
 (2)All other problems in NP class can be polynomial time
reducible to that.
 If the 2nd condition is only satisfied then the problem is
called NP-Hard.
 But it is not possible to reduce every NP problem into
another NP problem to show its NP-Completeness all
the time.
 That is why if we want to show a problem is NP-
Complete we just show that the problem is in NP and
any NP-Complete problem is reducible to that then we
are done.
 Hamiltonian Path is NP-Complete.
 Hamiltonian Path or HAMPATH in a directed graph G is
a directed path that goes through each node exactly
once. We Consider the problem of testing whether a
directed graph contain a Hamiltonian path connecting
two specified nodes, i.e.
 HAMPATH = {(G, s, t) | G is directed
graph with a Hamiltonian path from s to
t}
 To prove HAMPATH is NP-Complete we
have to prove that HAMPATH is in NP.
 To prove HAMPATH is in NP we must have
a polynomial-time verifier.
 Even though we don’t have a fast polynomial
time algorithm to determine whether a
graph contains a HAMPATH or not, if such a
path is discovered somehow (maybe with
exponential time brute force searching) we
could easily-work it out whether the path is
HAMPATH or not, in polynomial time.

 Here the certificate will be a Hamiltonian


path from s to t itself in G if exists. So
HAMPATH is in NP proved.
 So, now we have to show that every problem to
NP class is polynomial time reducible to
HAMPATH to show its NP-Completeness.

 Rather we shall show 3SAT (A NP-Complete


problem proved previously from SAT(Circuit
Satisfiability Problem)) is polynomial time
reducible to HAMPATH.

 We will convert a given cnf (Conjunctive Normal


Form) form to a graph where gadgets (structure
to simulate variables and clauses) will mimic the
variables and clauses (several literals or variables
connected with \vee)
We start With a 3-cnf formula containing k clauses,
Approximation Algorithms
 An Approximate Algorithm is a way of approach NP-
COMPLETENESS for the optimization problem.

 This technique does not guarantee the best solution.

 The goal of an approximation algorithm is to come as close as


possible to the optimum value in a reasonable amount of time
which is at the most polynomial time.

 Such algorithms are called approximation algorithm or heuristic


algorithm.

 For the traveling salesperson problem, the optimization problem is


to find the shortest cycle, and the approximation problem is to find
a short cycle.

 For the vertex cover problem, the optimization problem is to find


the vertex cover with fewest vertices, and the approximation
problem is to find the vertex cover with few vertices.
Performance Ratios
 Suppose we work on an optimization
problem where every solution carries a cost.

 An Approximate Algorithm returns a legal


solution, but the cost of that legal solution
may not be optimal.

 For Example, suppose we are considering


for a minimum size vertex-cover (VC).
An approximate algorithm returns a VC for
us, but the size (cost) may not be minimized.
 Another Example is we are considering for
a maximum size Independent set (IS). An
approximate Algorithm returns an IS for us, but the size
(cost) may not be maximum.

 Let C be the cost of the solution returned by an


approximate algorithm, and C* is the cost of the
optimal solution.

 We say the approximate algorithm has an approximate


ratio P (n) for an input size n, where
 Intuitively, the approximation ratio measures how
bad the approximate solution is distinguished with
the optimal solution.

 A large (small) approximation ratio measures the


solution is much worse than (more or less the
same as) an optimal solution.

 Observe that P (n) is always ≥ 1, if the ratio does


not depend on n, we may write P.

 Therefore, a 1-approximation algorithm gives an


optimal solution. Some problems have
polynomial-time approximation algorithm with
small constant approximate ratios, while others
have best-known polynomial time approximation
algorithms whose approximate ratios grow with
n.
Vertex Cover
 A Vertex Cover of a graph G is a set of vertices such that each
edge in G is incident to at least one of these vertices.

 The decision vertex-cover problem was proven NPC. Now, we


want to solve the optimal version of the vertex cover problem, i.e.,
we want to find a minimum size vertex cover of a given graph.

 We call such vertex cover an optimal vertex cover C*.


 An approximate algorithm for vertex cover:

 Approx-Vertex-Cover (G = (V, E))


 {
 C = empty-set;
 E'= E;
 While E' is not empty do
 {
 Let (u, v) be any edge in E': (*)
 Add u and v to C;
 Remove from E' all edges incident to
 u or v;
 }
 Return C;
 }
 The idea is to take an edge (u, v) one by one, put both
vertices to C, and remove all the edges incident to u or
v.
 We carry on until all edges have been removed. C is a
VC. But how good is C?
 VC = {b, c, d, e, f, g}
Set Cover Problem (Approximate
Algorithm)
 Given a universe U of n elements, a collection of subsets of U say S =
{S1, S2…,Sm} where every subset Si has an associated cost. Find a
minimum cost subcollection of S that covers all elements of U.
 Example:

 U = {1,2,3,4,5}
 S = {S1,S2,S3}

 S1 = {4,1,3}, Cost(S1) = 5
 S2 = {2,5}, Cost(S2) = 10
 S3 = {1,4,3,2}, Cost(S3) = 3

 Output: Minimum cost of set cover is 13 and


 set cover is {S2, S3}

 There are two possible set covers {S1, S2} with cost 15
 and {S2, S3} with cost 13.
 2-Approximate Greedy Algorithm:
 Let U be the universe of elements, {S1, S2,
… Sm} be collection of subsets of U and
Cost(S1), C(S2), … Cost(Sm) be costs of
subsets.

 (1) Let I represents set of elements


included so far. Initialize I = {}

 (2) Do following while I is not same as U.



 (1) Let I represents set of elements included
so far. Initialize I = {}

 (2) Do following while I is not same as U.

 a) Find the set Si in {S1, S2, ... Sm} whose


cost effectiveness is smallest, i.e., the ratio of
cost C(Si) and number of newly added
elements is minimum.
 Basically we pick the set for which following
value is minimum
Cost(Si) / |Si - I|
 b) Add elements of above picked Si to I, i.e.,
I = I U Si
Example:

Let us consider the above example to understand Greedy


Algorithm.

 First Iteration:
I = {}
 The per new element cost for S1 = Cost(S1)/|S1 – I| = 5/3
 The per new element cost for S2 = Cost(S2)/|S2 – I| = 10/2
 The per new element cost for S3 = Cost(S3)/|S3 – I| = 3/4

 Since S3 has minimum value S3 is added, I becomes {1,4,3,2}.

 Second Iteration:
I = {1,4,3,2}

 The per new element cost for S1 = Cost(S1)/|S1 – I| = 5/0


Note that S1 doesn’t add any new element to I.
The per new element cost for
 S2 = Cost(S2)/|S2 – I| = 10/1

 Note that S2 adds only 5 to I.

 The greedy algorithm provides the


optimal solution for above example, but it
may not provide optimal solution all the
time.
Consider the following example.

 S1 = {1, 2}
 S2 = {2, 3, 4, 5}
 S3 = {6, 7, 8, 9, 10, 11, 12, 13}
 S4 = {1, 3, 5, 7, 9, 11, 13}
 S5 = {2, 4, 6, 8, 10, 12, 13}

 Let the cost of every set be same.

 The greedy algorithm produces result as {S3, S2, S1}

 The optimal solution is {S4, S5}

You might also like