Escolar Documentos
Profissional Documentos
Cultura Documentos
Decision Trees
A convenient model of algorithms
involving comparisons in which:
Adversary Arguments
Adversary argument: a method of
proving a lower bound by playing role
of adversary that makes algorithm
work the hardest by adjusting input
Class P
P: the class of decision problems that are solvable in
O(p(n)) time, where p(n) is a polynomial of problems
input size n
Examples:
searching
element uniqueness
graph connectivity
graph acyclicity
Class NP
NP (nondeterministic polynomial): class of decision
problems whose proposed solutions can be verified in
polynomial time = solvable by a nondeterministic
polynomial algorithm
A nondeterministic polynomial algorithm is an abstract
two-stage procedure that:
generates a solution of the problem (on some input)
by guessing
checks whether this solution is correct in polynomial
time
By definition, it solves the problem if its capable of
generating and verifying a solution on one of its tries
NP-Complete Problems
A decision problem D is NP-complete if it is as hard as
any
problem in NP, i.e.,
D is in NP
every problem in NP is polynomial-time reducible to D
NP problems
NP-complete
problem
Backtracking
Suppose you have to make a series of
decisions, among various choices, where
You dont have enough information to know
what to choose
Each decision leads to a new set of choices
Some sequence of choices (possibly more than
one) may be a solution to your problem
Backtracking
Construct the state-space tree [State space is the set of all
paths from root node to other nodes ]
nodes: partial solutions
edges: choices in extending partial solutions
Explore the state space tree using depth-first search
Prune nonpromising nodes
DFS stops exploring subtrees rooted at nodes that cannot
lead to a solution and backtracks to such a nodes parent to
continue the search
Backtracking Algorithm
Based on depth-first recursive search
Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made
a) Make that choice
b) Recur
c) If recursion returns a solution, return it
queen 1
queen 2
queen 3
queen 4
v5
v6
v4
v5
v6
A state space tree for this problem is as follows. Put the starting vertex at level 0
in the tree, call this the zero'th vertex on the path. At level 1, consider each
vertex other than the starting vertex as the first vertex after the starting one. At
level 2, consider each of these vertices as the second vertex, and so on. You
may now backtrack in this state space tree.
Subset-sum problem
Given positive numbers wi, 1in, and
m, find all subsets of the wi whose sum
is m
eg) (w1,w2,w3,w4) = (11,13,24,7) and
m=31
Solutions are (11,13,7) and (24,7)
Method
Sort the sets elements in increasing order
The root of the tree represents the starting
point, with no decisions about the given elements made as yet.
Its left
and
right
children
represent,
respectively, inclusion and exclusion of (first
element) in a set being sought.
Similarly, going to the left from a node of the first level
corresponds to inclusion of (second element) while going to the
right corresponds to its exclusion, and so on.
Thus, a path from the root to a node on the i th level of the tree
indicates which of the first I numbers have been included in the
subsets represented by that node.
0
w/o 3
with 3
3
with 5
w/o 5
14
X
with 6
8
with 7
w/o 7
14+7>15
9
X
9+7>15
15
solution
with 5
3
w/o 6
with 6
8
X
8<15
5
w/o 6
with 6
w/o 5
0
w/o 6
11
5
X
3+7<15
11+7>14
5+7<15
0+13<15
28
The value of the nodes bound is not better than the value of
the best solution seen so far.
The node reprsents no feasible solutions because the
constraints of the problem are already violated.
The subset of feasible solutions reprsented by the node
consists of a single point(and hence no further choices can be
made)-in this case we compare the value of the objective
function for this feasible solution with that of the best
solution seen so far and update the latter with the former if
the new solution is better.
29
Assignment problem
30
0
Start
Ib=10
a1
Ib=17
Ib=10
Cost=13
Ib=13
Ib=14
Ib=18
7
b
Ib=17
Ib=13
10
Ib=20
b1
8
c
lb=25
Knapsack problem
Example
Item
1
2
3
4
W=10
33
w=0,v=0
Ub=100
With 1
w=4,v=40
Ub=76
w=11
w=0,v=0
Ub=60
w=4,v=40
Ub=70
w=9,v=65
Ub=69
w=12
W/o 1
w=4,v=40
Ub=64
w=9,v=65
Ub=65
34
Assume that the tour starts with vertex a and that b is visited
before c.
36
Traveling salesman
0
a
Ib=14
a,b
3
a,c
Ib=14
a,b,e
a,b,d
Ib=16
Ib=19
Ib=16
a,b,c,d,e,a
a,d
a,e
Ib=16
Ib=19
a,b,c
8
l=24
b is not before c
10
11
a,b,c,d,e,a
a,b,d,c,e,a
a,b,d,e,c,a
l=19
I=24
I=16
37
Approximation Approach
Apply a fast (i.e., a polynomial-time) approximation
algorithm to get a solution that is not necessarily
optimal but hopefully close to it
Accuracy measures:
accuracy ratio of an approximate solution sa
r(sa) = f(sa) / f(s*) for minimization problems
r(sa) = f(s*) / f(sa) for maximization problems
where f(sa) and f(s*) are values of the objective
function f for the approximate solution sa and actual
optimal solution s*
performance ratio RA of the algorithm A
the lowest upper bound of r(sa) on all instances
A polynomial-time approximation
algorithm is said to be a c
approximation algorithm, where c
1, if the accuracy ratio of the
approximation
it produces does not exceed c for any
instance of the problem in question:
r(sa) c
Example:
2
8
c
4
d
Cost
2+3+7+5 = 17
2+4+7+8 = 21
8+3+4+5 = 20
8+7+4+2 = 21
5+4+3+8 = 20
5+7+3+2 = 17
Multifragment-Heuristic
Algorithm
Stage 1: Sort the edges in nondecreasing order of weights.
Initialize the set of tour edges to be an empty set
Stage 2: Add next edge on the sorted list to the tour, skipping
those whose addition would have created a vertex of
degree 3 or a cycle of length less than n. Repeat
this step until a tour of length n is obtained
Step 3
Return the set of tour edges.
Minimum-Spanning-Tree
Based Algorithms
Twice-Around-the-Tree
Algorithm
Stage 1: Construct a minimum spanning tree of the graph
(e.g., by Prims or Kruskals algorithm)
Stage 2: Starting at an arbitrary vertex, create a path that goes
twice around the tree and returns to the same vertex
Stage 3: Create a tour from the circuit constructed in Stage 2 by
making shortcuts to avoid visiting intermediate vertices
more than once
C1
C3
C2
C4
C1
C3
C2
2-opt
Take two edges
(v,w) and (x,y) and
replace them by
(v,x) and (w,y) OR
(v,y) and (w,x) to
get a tour again.
Costly: part of tour
should be turned
around
53
Knapsack problem:
Input: n items: (w1, v1), , (wn, vn) and a
number W
the i-th items is worth vi dollars with weight wi.
05/01/16
Page 55
Example: (1, 5$), (2, 9$), (3, 12$), (3, 11$) and w=4.
vi/wi :
5
4.5
4.0
3.667
Total W: 1,
3,
4.
item
05/01/16
Page 56