Sie sind auf Seite 1von 24

h

t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

81
GRAPH TRAVERSALS
As with trees, traversing a graph consists oI visiting each vertex only one time.
The simple traversal algorithms used Ior trees (preorder, inorder, postorder) cannot be
applied here because graphs may include cycles which would cause the tree traversal
algorithms to enter an inIinite loop. To prevent this Irom happening, each visited vertex
is typically marked in some Iashion to avoid revisiting it (a common technique is to
renumber the vertices as they are visited). However, graphs can have isolated vertices
(unconnected vertices), which means that some parts oI the graph are leIt unvisited iI
unmodiIied tree traversal algorithms are applied.
Depth - First Traversal
The depth-Iirst search algorithm Ior graphs was developed by HopcroIt and
Tarjan. In this algorithm, each vertex J is visited and then each unvisited vertex
adjacent to J is visited. II a vertex J has no adjacent vertices or all oI its adjacent
vertices have been visited, the traversal backtracks to the predecessor oI J. The
traversal is complete when this process oI visiting and backtracking leads to the Iirst
vertex where the traversal started. II there are still unvisited vertices in the graph, the
traversal continues by restarting on one oI the unvisited vertices. This algorithm
renumbers each vertex as it is visited.
Algorithm.
DFS(v)
num(v) i;
Ior all vertices u aafacent to v
iI num(u) is 0
attach eage (uv) to edges;
DFS(u);
depthFirstSearch( )
Ior all vertices v
num(v) 0;
edges null,
i 1;
while there is a vertex v such that num(v) is 0
DFS(v);
output edges;
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

82
Example.
The numbers assigned to each vertex are shown in parentheses in the Iigure
below. Once the initializations have been made, depthFirstSearch( ) calles
DFS(a). DFS( ) is invoked Ior vertex a; num(a) is assigned number 1. Vertex a
has Iour adjacent vertices, and vertex e is chosen Ior the next invocation, DFS(e),
which assigns number 2 to this vertex (num(e) 2) and puts the eage(ae) in the
set eages. Vertex e has two unvisited adjacent vertices, and DFS( ) is called Ior
the Iirst oI them, the vertex f. The call DFS(I) will lead to the assignment num(I)
3 and will put eage(ef) in eages.
Vertex f has only one unvisited adjacent vertex, i, thus the Iourth call DFS(i) will
lead to the assignment num(i) 4 and to the attaching oI eage(fi) to eages.
Vertex i has only visited adjacent vertices, thus a return to the call DFS(I) occurs
and then to DFS(e) in which vertex i is accessed only to discover that num(i) is
not 0, thus eage(ei) is not included in the set oI edges. The rest oI the execution
is shown in the Iigure below where the solid lines indicate edges that are included
in the set eages. Part (a) is the original graph and part (b) illustrates the
algorithm`s technique.
The depth-Iirst graph traversal algorithm can also be applied to digraphs. This is
illustrated in the next Iigure using the same symbolism as beIore.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

83
Notice that this algorithm guarantees generating a tree (or a Iorest) which includes or
spans over all vertices oI the original graph. A tree that meets this condition is called a
spanning tree. The Iact that a tree (or a Iorest) is generated is ascertained by the Iact
that the algorithm does not include in the resulting tree any edge which leads Irom the
currently visited vertex to a vertex which has already been visited. An edge is added
only iI the condition in 'iI num(u) is 0 is true, that is, only iI vertex u reachable Irom
vertex v has not been visited. As a result, certain edges in the original graph do not
appear in the resulting tree. The edges included in this tree are called forwara eages (or
tree eages), and the edges not included in the tree are called back eages (the ones shown
by dashed lines in the Iigures above).
Breadth - First Traversal
There are many diIIerent algorithms which are based on a depth-Iirst traversal oI
a graph. Certain algorithms can be made more eIIicient iI the underlying graph traversal
is not depth-Iirst but breadth-Iirst. While depth-Iirst tree traversals rely on a stack
(either explicitly or implicitly with recursion), the breadth Iirst tree traversal relies on a
queue to handle the traversal (recall the level-order breadth Iirst tree-traversal
algorithm). This same technique can be extended to graph traversals.
Algorithm.
breadthFirstSearch( )
Ior all vertices u
num(u) 0;
edges null;
i 1;
while there is a vertex v such that num(v) 0
num(v) i;
enqueue(v);
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

84
while queue is not empty
v aequeue( );
Ior all vertices u aafacent to v
iI num(u) is 0
num(u) i;
enqueue(u);
attach eage (vu) to edges;
output edges;
Example.
The two Iigures below illustrate how breadthFirstSearch operates on both simple
graphs and digraphs. The algorithm Iirst attempts to mark all neighbors oI a vertex v
beIore proceeding to other vertices. This is the opposite oI how the depthFirstSearch
algorithm operated, where it picked one neighbor oI a vertex v and then proceeded to a
neighbor oI this neighbor beIore processing any other neighbors oI vertex v.
Example oI breadthFirstSearch on a simple graph
Example oI breadthFirstSearch on a digraph
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

85
SPANNING TREE
Given a connected unairectea graph ( ) E J G , = , ( )
T T
E J T , = is a spanning tree iI J J
T
= ,
E E
T
_ , T is acyclic, and T is connected.
G can only have a spanning tree iI it is connected.
The number oI edges in a spanning tree is 1 J .
T is a tree because it is acyclic.
T is spanning because it every vertex Irom G .
BRANCH-AND-BOUND
Branch-and-bound is most commonly applied to optimization problems, although it can
be applied to any problem generated by stages in which there are an easy method to
rank the solutions.
As the branch-and-bound method is most commonly applied to optimization problems,
it would be useIul to review some oI the terminology at this point. Basically we are
presented with a problem in which we want to optimize some target Iunction subject to
constraints.
A feasible solution is one that satisIies the constraints oI the problem.
An optimal solution is that Ieasible solution that optimizes the objective Iunction. Note
that optimal solutions need not be unique; indeed, there are many linear programming
problems that admit an inIinite number oI optimal solutions.
Another construct might be called a bounding solution. This is a solution that does not
satisIy the constraints but can be shown to be a bound Ior any optimal solution. For
example, the assignment problem calls Ior making assignments in a way to minimize
cost. We can easily arrive at a bounding solution Ior this problem one that gives a
value that will be a strong lower bound on the objective Iunction Ior any optimal
solution. However, it is very likely that the bounding solution so achieved will satisIy
the constraints, thus it is not a Ieasible solution and hence inadmissible as an optimal
solution.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

86
General Method
The branch-and-bound technique is applied to a problem the solution to which can be
viewed as generating nodes in a state-space tree. The leaI nodes oI a state-space tree
represent a solution to the problem, which may not be a Ieasible solution. The interior
nodes oI the tree represent partial solutions to the problem.
We generate the state-space tree one node at a time, starting with the root node, which is
used to indicate the starting point Ior all solutions. Almost always, the root node
represents the problem beIore any decisions have been made the chess board beIore
any queens have been placed or the job status beIore anybody has been assigned a job.
At any time in generating nodes in the state-space tree, we must be able to assess the
node just generated. In particular we ask the Iollowing questions.
1) How does the partial solution compare to the best existing solution, and
2) Is the solution represented by this node a Ieasible solution?
In order Ior branch-and-bound to work, we need to be able to assess every node just
generated as the root node oI a tree that represents all solutions that can be generated
based on the partial solution we have just generated. We 'prune the search tree iI any
oI the Iollowing holds:
1) It can be shown that no solution in the subtree will satisIy the problem
constraints.
2) It can be shown that no solution in the subtree will be better than the best solution
so Iar generated.
3) The node generated represents a complete Ieasible solution that can be compared
to the best solution so Iar generated and possibly replace it.
At any point in generation oI the tree, we have nodes that are called live and nodes that
are not. A live node is a node that can possibly represent a partial solution leading to an
optimal solution oI the whole problem. II we can assess the live nodes as to the best
solution that can be associated with the node`s subtree, we can apply best-first branch-
and-bound, a variant that continually examines the best node generated so Iar.
There are a number oI ways to assess a node. Suppose we are looking Ior a least-
cost solution. As costs are usually non-negative and are additive, we can assess the cost
oI an interior node (and its subtree) as being the cost oI the solution up to that point.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

87
Alternatively, it may be possible to estimate a remaining cost associated with generating
the rest oI the subtree and add that to the vertex cost.
Here we make an obvious and almost-useless observation. The one way to assess any
solution in a subtree rooted at an interior node is to expand that subtree and assess all oI
its leaI nodes. We are looking Ior a much Iaster way to assess subtrees.
Thus, the key approach to branch-and-bound is to view each newly generated node as
the root node oI a subtree leading to a set oI solutions. We want a reasonably Iast way
to make a useable evaluation oI each oI these subtrees, so that we may chase the best
options.
0/1 - KNAPSACK
We now return to a problem that we have discussed beIore and even use the same
instance oI the problem as an example. This is the discrete knapsack problem. As
beIore, we shall assume a set oI N items, each with positive weight and positive value.
For 1 s K s N, let W
K
be the weight oI item K,
V
K
be the value oI item K, and
deIine
K
W
K
/ V
K
.
As beIore, we shall assume that the items are labeled so that the sequence oI value-to-
weight ratios is arranged in non-increasing order;
K
>
K1
Ior 1 s K s (N 1).
The state-space tree oI this problem is a binary tree, with nodes at level K representing
the choice about item K whether or not to include it in the knapsack. In this approach
we play a slight game with reality in that we will 'put an item into the knapsack and
then check iI the weight has exceeded the limit. We discard the over-weight solutions
as not Ieasible.
The Iull state-space tree will have 2
N
leaI nodes and 1 2 . 2
N1
2
N
1 interior
nodes, Ior a total oI 2
N1
1 nodes. What makes this problem to be hard is that the only
solution that can be proven to work always is to generate all oI these 2
N1
1 nodes.
Thus the problem appears to have exponential time complexity.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

88
In order to apply branch-and-bound to this problem, we need a way to assess the interior
nodes oI the state-space tree. Fortunately, there are two easy ways to do this. Suppose
that we are at level K. Having made the decision on item K, we have the Iollowing.
W the total capacity oI the knapsack.
w the weight added to the knapsack, aIter having decided on item K.
v the value added to the knapsack, aIter having decided on item K.
The feasibility criterion is easily applied. II w ~ W, then this partial solution and all
partial solutions containing this solution (thus every solution contained in the subtree
rooted at this node) are inIeasible and the state-space tree can be pruned at this interior
node.
The upper bound criterion is also easy to apply. II w s W, then the capacity
remaining is
(W w) and the maximum value that can be added is (W w)-
K1
. This maximum
value is due to the Iact that the items have been sorted by non-decreasing value-to-
weight ratio.
Thus, the upper bound on the value oI any solution rooted at this node at level K is
v (W w)-
K1
.
We are now ready to attempt a solution.
The problem that we shall solve is,
w
1
4.0 v
1
40.0
1
10.0 Capacity W 10.0
w
2
7.0 v
2
42.0
2
6.0
w
3
5.0 v
3
25.0
3
5.0
w
4
3.0 v
4
12.0
4
4.0
We now call on the greedy algorithm to give us both the bounding solution and an
initial Ieasible solution. We can then use these bounds in assessing our solution.
To get the bounding solution, we solve the related Iractional knapsack problem. Recall
that this solution will represent an upper bound on the total value oI the knapsack, but is
likely to be inIeasible. Indeed, this solution is inIeasible because it violates the 0/1
constraint.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

89
K 1 take all 4 units oI item 1 v 0.0 40.0 40.0 (W w) 6.0
K 2 take 6 units oI item 2 v 40.0 36.0 76.0 (W w) 0.0
Apply greedy again to get the initial feasible solution.
K 1 take item 1 v 0.0 40.0 40.0 (W w) 6.0
K 2 skip item 2
K 3 take item 3 v 40.0 25.0 65.0
We now generate the Iirst level oI nodes in the state-space tree. The nodes at level 1 oI
the tree correspond to whether or not item 1 is added to the knapsack. Since item 1
weighs less than the capacity, there is no Ieasibility issue here. Now compute the upper
bounds.
Take 1 v 40.0 (W w) 6.0 v (W w)-
2
40.0 6.0 - 6.0 76.0
Skip 1 v 0.0 (W w) 10.0 v (W w)-
2
0.0 10.0 - 6.0 60.0
But note that the upper bound on the right sub-tree is less than the value oI a known
Ieasible solution. For this reason, we prune the right sub-tree and Iocus on the leIt sub-
tree. The next level in this state-space tree corresponds to item 2.
Here is the state-space tree Ior the Iirst two items. Compute the upper bounds
Take 2 v 82.0 (W w) 1.0 Weight constraint is violated
Skip 2 v 40.0 (W w) 6.0 v (W w)-
3
40.0 6.0 - 5.0 70.0
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

90
Now we Iocus on whether or not to take item 3. Again, we evaluate two choices.
Take 3 v 65.0 (W w) 1.0 v (W w)-
4
65.0 1.0 - 4.0 69.0
Skip 3 v 40.0 (W w) 6.0 v (W w)-
4
40.0 6.0 - 4.0 64.0
Again, we prune the right sub-tree because the upper bound is smaller than an existing
Ieasible solution. Note that it is less than the actual value oI the leIt sub-tree.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

91
The next level considers item 4. Here is the complete state-space tree.
Here is the complete solution. Take items 1 and 3 Ior a total value oI 65. Note that we
have generated only 8 nodes oI the 31 nodes that would be required Ior a complete
state-space tree, a savings oI about 75. Four times as Iast is not an order oI
magnitude, but it is good.
In the above example, the greedy algorithm used to obtain the initial Ieasible solution
happened to pick the optimal solution. This was an accident.
To show that greedy can miss, we present a slight variant oI the problem.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

92
w
1
4.0 v
1
40.0
1
10.0 Capacity W 10.0
w
2
7.0 v
2
42.0
2
6.0
w
3
5.0 v
3
25.0
3
5.0
w
4
3.0 v
4
14.0
4
4.67
w
5
3.0 v
5
14.0
5
4.67
Here greedy picks items 1 and 3, with a value oI 65, as above. The optimal answer is to
pick items 1, 4, and 5 with a value oI 68. Greedy loses because it does not Iill the
knapsack.
NP-HARD AND NP-COMPLETENESS
'All basic operations take unit time, we have infinite memory at the register level,
ana every step is aeterminea aeterministically as aescribea in an algorithm.`
Different types of problems: decision problems, optimization problems, .
Decision problems: Output is True/False.
Decision Problem -~ corresponding optimization problem.
Example oI 0-1 Knapsack Decision (KSD) problem:
KS problem a proIit value P a question "does there exist a knapsack with proIit >
P?"
Algorithms are also inter-operable between a problem and its corresponding decision
problem.
Solve a KSD problem T, using an optimizing algorithm:
p Algorithm-Optimization-KS (Iirst part oI T without given proIit P);
iI p>P then return TRUE else return FALSE.
Solve a KS problem N, using a decision algorithm:
For (p Sum-oI-all-objects-proIit; p~0; p p - aelta) do // Ior a small aelta
II (Algorithm-Decision-KS (N, p) ) then continue
Else return (the last value oI p beIore Iailure in above step);
EndIor. // or, deploy a binary search algorithm, that is even Iaster!!
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

93
The NP-completeness theory is developed Ior Decision problems, but valid Ior other
problems as well
Computationally Tractable - Intractable Problems
A problem is oIten said to be computationally tractable iI there exists an algorithm oI
polynomial complexity Ior all instances and solves the problem. Conversely, a problem
will be said to be computationally intractable (also computationally complex or
computationally hard) iI the (provably) optimal algorithm Ior solving the problem
cannot solve all oI its instances in polynomial time.
Some problems are even unsolvable/ undecidable algorithmically, so you cannot write a
computer program Ior them.
Example: Halting problem: Given an algorithm as input, determine iI it has an inIinite
loop.
There does not exist any general-purpose algorithm Ior this problem. Any such
algorithm would not be able to determine yes/no on the Iollowing algorithm
P(X):
while (not X) }; // X is any 'input algorithm to P, and 'not X is true only when X
terminates
Then, provide P itselI as the input X to the algorithm |i.e., P(P()) | and the algorithm
neither terminates nor has an inIinite loop!
It is equivalent to the truth value oI the sentence 'this sentence is a lie.
Note, we are considering Problems (i.e., Ior all instances oI the problem) as opposed to
some instances of the problem. For some sub-cases you may be able to solve the
halting problem, but you cannot have an algorithm, which would solve the halting
problem Ior all input.
NP-class of solvable problems
Deterministic algorithms are where no step is random.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

94
II the program/algorithm chooses a step non-deterministically (by some extraneous
inIluence to the algorithm) such that it is always the right choice, then such an algorithm
is called non-deterministic algorithm.
Example, suppose a 0-1 KS backtrack-algorithm always knows which object to pick up
next in order to Iind an optimal proIit-making knapsack!
II one has a polynomial aeterministic algorithm Ior a problem (e.g., the sorting
problem), then the problem is called a P-class problem. Or, the set oI all such problems
constitute the P-class.
II one has a polynomial non-aeterministic algorithm Ior a problem, then the problem is
called a NP-class problem. Or, the set oI all such problems constitute the NP-class.
It is impossible to check Ior a problem's being in NP-class this way, because non-
deterministic algorithms are impossible to develop, as deIined above. So, how can one
check Ior polynomial complexity oI such non-existent algorithms?
However, an equivalent way oI developing non-deterministic polynomial algorithm is:
when a solution to the problem is provided (as iI someone knows what the solution
could be!), then that proposed solution is checked by the algorithm in polynomial time.
Such a proposea solution is called a certiIicate to the input problem-instance.
For example: in a KSD problem, given a knapsack content check iI the total proIit is > p
or not.
Complexity: calculation oI total proIit oI the given knapsack is worst case O(n), Ior n
objects.
Two important points to note:
(1) NP-class problems are sub-set oI the Solvable problems,
(2) P-class problems are sub-set oI NP-class problems (because iI you have a
deterministic algorithm to solve any problem instance, then that algorithm can be
used to check any certiIicate in polynomial time also).
Problems belonging to the NP-class have at least exponential algorithms.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

95
A related question is: does there exist any solvable problem that is not NP?
Answer: yes.
Example: non-HC problem (iI there is no HC in a given input graph) may not be solved
in polynomial time even with non-deterministic algorithms.
II a problem is in NP-class its complement (negative problem as the non-HC problem
is) is in Co-NP. All Co-NP problems constitute the Co-NP class oI problems. P-class is
a subset oI the intersection oI NP and Co-NP sets.
An IMPORTANT question is: can we write a polynomial algorithm Ior every NP-class
problem?
The answer is: we ao not know.
From a reverse angle, we would like to Iind an example problem that is in the NP-class and whose
inIormation-theoretic lower bound is exponential. Then, we would at least know that P is a proper
subset oI NP. We do not yet have any such example either.
All we know now is that P NP.
Question remains: (1) P c NP? or, P = NP? |Find counter example(s)|
Or, (2) NP P, so that P NP? |Prove a theorem|
Polynomial Transformation
The basic idea Ior problem classiIication lies on whether there exists a polynomial-time
(computer) algorithm that can provide the solution to a problem. We say that algorithms
run in polynomial time iI i.e., Ior a problem oI size n, the time or number oI steps
needed to Iind the solution is a polynomial Iunction oI n, so their time-complexity
Iunction is a polynomial. These algorithms solve the so-called easy, or tractable,
problems. Algorithms Ior solving hard, or intractable, problems, on the other hand,
require times that are exponential Iunctions oI the problem size n. These algorithms are
said to take exponential time to resolve. Polynomial-time algorithms are considered to
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

96
be eIIicient, while exponential-time algorithms are considered ineIIicient, because the
execution times oI the latter grow much more rapidly as the problem size increases.
Problem Transformation: some algorithms which take a decision problem X (or rather
ANY instance oI the problem oI type X), and output a corresponding instance oI the
decision problem oI type Y, in such a way that iI the input has answer True, then the
output (oI type Y) is also True and vice versa.
For example, you can write a problem transIormation algorithm Irom 3-SAT problem to
3D-Matching problem (will see later).
Note that the problem transIormations are directed.
When a problem transIormation algorithm is polynomial we call it a polynomial
transformation.
Existence oI a polynomial transIormation algorithm has a great significance Ior the
complexity issues.
Suppose you have (1) a poly-transIormation Irom a (source) problem X to another
(target) problem Y,
and (2) Y has a poly algorithm, then
you can solve any instance oI the source problem X polynomially, by the Iollowing
method.
Just transIorm any instance oI X into another instance oI Y Iirst, and then use Y`s poly-
algorithm. Both oI these steps are polynomial, and the output (T/F) Irom Y`s algorithm
is valid Ior the source instance (oI X) as well. Hence, the True/False answer Ior the
original instance oI X would be obtained in poly-time. This constitutes an indirect poly-
algorithm Ior X, thus making X also belonging to the P-class.
Once again, note the direction. You will be amazed with how many practicing
computer scientists get conIused with this direction!
Cook`s theorem.
Cook modeled all NP-problems (an inIinite set) to an abstract Turing machine. Then he
developed a poly-transIormation Irom this machine (i.e., all NP-class problems) to a
particular decision problem, namely, the Boolean SatisIiability (SAT) problem.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

97
Significance of Cook`s theorem: iI one can Iind a poly algorithm Ior SAT, then by
using Cook`s poly-transIormation one can solve all NP-class problems in poly time
(consequently, P-class NP-class would be proved).
SAT is the Iirst identiIied NP-hara problem!
Further significance of Cook`s theorem: iI you Iind a poly-transIormation Irom SAT
to another problem Z, then Z becomes another NP-hard problem. That is, iI anyone
Iinds a poly algorithm Ior Z, then by using your poly-trans Irom SAT-to-Z anyone
would be able to solve SAT in poly time, and hence would be able to solve all NP-class
problems in poly-time (by Cook`s theorem).
These problems, which have a chain oI poly-trans Irom SAT, are called NP-hard
problems.
II an NP-hard problem also belong to the NP-class it is called an NP-complete problem,
and the group oI such problems are called NP-complete problems.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

98
Significance of NP-hard problems
As stated beIore, iI one Iinds any poly-algorithm Ior any NP-hard problem, then
We would be able to write polynomial algorithm Ior each oI NP-class problems, or
NP-class P-class will be proved (in other words, poly-algorithms would be Iound Ior
all NP-class problems).
UnIortunately, neither anyone could Iind any poly algorithm Ior any NP-hard problem
(which would signiIy that P-class NP-class),
nor anyone could prove an exponential inIormation-theoretic bound Ior any NP-
complete problem, say L (which would signiIy that L is in NP but not in P, or in other
words that would prove that P-class c NP-class). The NP-hard problems are the best
candidate Ior Iinding such counter-examples. |There is a claim in Summer 2010 of a
lower bouna-proof of some NP-hara problem, from IBM research'|
As a result, when we say a problem X (say, the KSD problem) is NP-complete all we mean is that
IF one Iinds a poly-alg Ior X, THEN all the NP-class problems would have poly algorithm.
We also mean, that it is UNLIKELY that there would be any poly algorithm Iound Ior
X.
P = NP is a mathematical conjecture currently (YET to be proved as a theorem, see
above though). Based on this conjecture many new results about the complexity-
structure oI computational problems have been obtained.
Note this very careIully: NP (as yet in history) does NOT stand Ior 'non-polynomial.
|Also, note that NP-complete problems do have solutions, but they are all exponential
algorithms, so Iar! A common student-mistake is to conIuse NP-complete problems
with unsolvable problems.|
There exist other hierarchies. For example, all problems, which need polynomial
memory-space (rather than time) Iorm PSPACE problems. Poly-time algorithm will not
need more than poly-space, but the reverse may not be necessarily true. Answer to this
question is not known. There exists a chain oI poly-transIormations Irom all PSPACE
problems to the elements oI a subset oI it. That subset is called PSPACE-complete.
PSPACE-complete problems may lie outside NP-class, and may be harder than the NP-
complete problems. Example: Quantifiea Boolean Formula (Iirst-order logic) is
PSPACE-complete.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

99
Complexity classes of problems - P and NP problems
P-problem: A problem is assigned to the P-problem (polynomial time) class iI the
number oI steps needed to solve it, is bounded by some power oI the problem's size. A
problem is assigned to this class iI the number oI steps is bounded by a polynomial.
"If the solution to a problem can be found in ( )
k
n O (polynomial) time, it is said to be in the
set P."
e.g. Sorting
NP-problem: A problem is called NP (nondeterministic polynomial) iI its solution (iI
one exists) can be guessed and veriIied in polynomial time; nondeterministic means that
no particular rule is Iollowed to make the guess. A problem is assigned to the NP-
problem (nondeterministic polynomial time) class iI it permits a nondeterministic
solution and the number oI steps oI the solution is bounded by some power oI the
problem's size.
"If a solution to a problem can be verified in polynomial time, the problem is in the set
NP."
All problems in P are in NP
Whether or not all NP problems can be solved in polynomial time ( NP P = ) is
an open question in computer science and mathematics.
The class oI P-problems is a subset oI the class oI NP-problems.
Intractable classes oI problems NP-Hard and NP-Complete problems
Proving that a problem is intractable is diIIicult researchers have been unable to
determine whether most problems are tractable. There are, however, a number oI large
classes oI problems that computer scientists believe to be intractable. The oldest ones
considered are the class oI NP-hard problems and the one oI NP-complete problems.
NP-Hard: A problem is NP-hard iI an algorithm Ior solving it can be translated into one
Ior solving any other NP-problem (nondeterministic polynomial time) problem. NP-
hard thereIore means "at least as hard as any NP-problem," although it might, in Iact, be
harder. The NP-hard class is a superset containing the NP-complete class (see below);
this class is potentially harder to solve than NP-complete problems because although iI
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

100
any NP-complete problem is intractable then all NP-hard problems are intractable, the
reverse is not true.
"If a problem in NP-complete can be reduced to a problem x in polynomial time, then x is in
the set NP-hard."
All NP-complete problems are in NP-hard.
An NP-hard problem is not necessarily in NP.
An NP-hard problem can is ~at least as hard as NP-complete
NP-complete: The most important property oI NP-complete problems is the so-called
polynomial-time reducibility. Any NP-complete problem can be transIormed into any
other NP-complete problem in polynomial time. Thus, iI it could be proved that any NP-
complete problem is Iormally intractable, all such problems would have proved
intractable, and vice-versa. While no prooI oI intractability has been Iound, no
polynomial algorithms have ever been Iound that solve any oI these problems, and
because oI the breadth oI the class oI problems it is widely believed that no such
algorithms exist.
II a problem is NP and all other NP problems are polynomial-time reducible to it, the
problem is NP-complete. Thus, Iinding an eIIicient algorithm Ior any NP-complete
problem implies that an eIIicient algorithm can be Iound Ior all such problems, since
any problem belonging to this class can be recast into any other member oI the class. It
is not known whether any polynomial-time algorithms will ever be Iound Ior NP-
complete problems, and determining whether these problems are tractable or intractable
remains one oI the most important questions in theoretical computer science.
When an NP-complete problem must be solved, one approach is to use a polynomial
algorithm to approximate the solution; the answer thus obtained will not necessarily be
optimal but will be reasonably close.
" If a problem x is NP, and another problem in NP-complete can be reduced to x in
polynomial time, then x is in the set NP-complete. "
Examples oI NP-complete problems include the Hamiltonian cycle and Travelling
salesman problems.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

101
Hamiltonian cycle problem (HCP)
A Hamiltonian circuit (HC) is a cyclic ordering oI a set oI nodes such that there is an
edge connecting every pair oI nodes in the graph in order. The cyclic condition ensures
that the circuit is closed, and the requirement that all the nodes are included (with no
repeats) ensures that the circuit does not cross over itselI, and passes through every
node. The problem is to Iind iI a HC exists Ior a given graph, thus determining iI a
given graph has a cycle visiting each vertex exactly once. This is considered to be the
Hamiltonian cycle problem.
Travelling salesman problem (TSP):
Given a graph in which the nodes (cities) are connected by directed edges (routes),
where the weight oI an edge is the distance between two cities, the problem is to Iind a
path that visits each city once, returns to the starting city, and minimizes the distance
travelled. The only known solution that guarantees the shortest path, requires a solution
time that grows exponentially with the problem size (i.e., number oI cities). This is an
example oI an NP-complete problem, Ior which no known eIIicient (i.e., polynomial
time) algorithm exists.
Other well-known NP-problems are: the Graph coloring problem, the Independent set
problem, the Bin packing problem, the SatisIiability problem (SAT), the 3-SatisIiability
problem (3SAT), the Maximum clique problem, and others.
Why care about NP-completeness?
The polynomial-time reducibility property oI NP-complete problems makes, in a sense,
all NP-complete problems equivalent, so solving any NP-complete problem gives the
key to all others and insight into solving any one gives insight into solving a vast array
oI problems oI extraordinary practical and economic signiIicance
So when the objective is to prove that a problem is NP-complete, all that has to be done
is to show that the problem belongs to the class oI NP problems and Iind a known NP-
complete problem reducible to it.
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

102
A Iinal comment regarding the subject presented, would be that understanding the
notion oI NP-completeness and NP-complete problems is important Ior a good
algorithm designer. II a problem can be establish as NP-complete, a good evidence Ior
its intractability is thus provided, so one would do better spending their time developing
an approximation algorithm (returning near-optimal good-enough solutions) rather than
searching Ior a Iast one that solves the problem exactly.
Summary: Apparently some problems are 'harder in a relative sense than the other
problems within the NP-class oI problems. II you can write polynomial algorithm Ior
any one oI this group oI apparently hard problems, then it can be shown that every NP-
class problem will have a polynomial algorithm Ior each. This group oI problems is
called NP-complete problems.
The secret lies in the Iact that they are all connected to all NP-class problems (!!!) by
directed chains oI polynomial transformations (explained below).
http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

103
SUMMARY
Bounds: O,
O(g(n)) ] f(n) : there exist positive constants c and n

such that 0 f(n) cg(n) Ior all n


n

}
(g(n)) ] f(n) : there exist positive constants c and n

such that 0 cg(n) f(n) Ior all n


n

}
g(n)) ] f(n) : There exist positive constants c
1
, c
2
, and n

such that 0 c
1
g(n) f(n)
c
2
(g(n)
Ior all n n
o
}
Divide and conquer: MergeSort, QuickSort
Base value n < n

Recurrence relations for divide-and-conquer algorithms: 1(n)


a1(n/b) + f(n) n n

The substitution method


Recursion tree
Master method
Dynamic programming: Optimal substructure, matrix chain multiplication, longest common
subsequence.
Optimal substructur: A problem exhibits optimal substructure iI an optimal solution to the
problem contains
within it optimal solutions to subproblems.

http://csetube.co.nr/
h
t
t
p
:
/
/
c
s
e
t
u
b
e
.
c
o
.
n
r
/
LECTURE PLAN
Code & Name of the subject: 101401 / Design and Analysis oI Algorithm
Unit No.: V Prepared by: Purushothaman.R

104
Problems are solved bottom-up.
Greedy algorithms: Activity-selection problem, HuIIman codes.
Problems are solved top-down.
Backtracking: n-queens, graph coloring, sum oI subsets.
Backtrack when bounding Iunction returns Ialse.
Approximation algorithms: vertex-cover problem, traveling-salesman problem.
NP-completeness:
The complexity class P Consists oI those problems that are solvable in polynomial time.
The complexity class NP Consists oI those problems that are veriIiable in polynomial time.
The complexity class NP-Complete Consists oI those problems that are as 'hard as any
problem in NP.
II any NP-Complete problem can be solved in
polynomial time,
Then every problem in NP can be solved in
polynomial time.
That is, P NP.
http://csetube.co.nr/

Das könnte Ihnen auch gefallen