Showing Page:
1/7
Time Complexity
Complexity of a
n
b
n
na’s nb’s
n moves to right to match a b and replace it with y
a
a
a
a
a
b
b
b
b
b
Showing Page:
2/7
1. Total moves match an a with a b are n right moves + n left moves = 2n moves
2. Total moves to match na’s with nb’s = n (2n) = 2n
2
Complexity of a
n
b
n
Note that:
The step (i) consists of going through the
input string (a
n
b
n
) forward and backward
a
a
a
a
y
b
b
b
b
n
moves to left to match a
a
and replace it with
a
a
a
a
y
b
b
b
b
Showing Page:
3/7
and replacing the leftmost a by x and the
leftmost b by y. So we require at most 2n
moves to match a a with a b.
Step (ii) is repetition of step (i) n times.
Hence the number of moves for accepting
(a
n
b
n
) is at most (2n)(n) = 2n
2
.
For strings not of the form (a
n
b
n
), TM halts
with less than 2n
2
steps. Hence T(M) =
O(n
2
).
Euclidean algorithm for evaluating gcd(a, b)
The Euclidean algorithm has the following steps:
Showing Page:
4/7
1. The input is (a, b)
2. Repeat until b = 0
3. Assign a a mod b 4. Exchange a and b
5. Output a.
OTHER NP-COMPLETE PROBLEMS
1. CSAT: Given a Boolean expression in CNF
(Conjunctive Normal Form), is it satisfiable?
It can be proved that CSAT is NP-complete by
proving that CSAT is in NP and getting a
polynomial reduction from SAT to CSAT,
2. Hamiltonian circuit problem: Does graph G have a
Hamiltonian circuit (i.e. a circuit passing through
each edge of graph G exactly once)?
Showing Page:
5/7
3. Travelling salesman problem (TSP): Given n cities,
the distance between them and a number D, does
there exist a tour program for a salesman to visit
all the cities exactly once so that the distance
travelled is at most D?
OTHER NP-COMPLETE PROBLEMS
4. Vertex cover problem: Given a graph G and a natural
number k, does there exist a vertex cover for G
with k vertices?
(A subsets C of vertices of G is a vertex cover for G
if each edge of G has an odd vertex in C.)
4. Knapsack problem: Given a set A = {a
1
, a
2
, ..., a
n
} of
nonnegative integers. and an integer K, does there
Showing Page:
6/7
exist a subset B of A such that ∑b
j
= K where b
j
B
1-D knapsack problem Example
Problem: Which boxes should be chosen to maximize the
amount of money while still keeping the overall weight
under or equal to 15 kg?
Solution:
1. if any number of each box is available, then three
yellow boxes and three grey boxes;
2. if only the shown boxes are available, then all but not
the green box.)
Showing Page:
7/7
Solution 1:
1. Weight = 4Kg x 3 + 1Kg x 3 = 12Kg+3Kg =
15Kg
2. Money = \$10 x 3 + \$2 x 3 = \$30 + \$6 =
\$36 Solution 2:
1. Weight = 4Kg + 2Kg + 1 Kg + 1Kg = 8Kg
2. (Money = \$10 + \$2 + \$2 + \$1 = \$15

### Unformatted Attachment Preview

Time Complexity Complexity of anbn na’s a a a … … nb’s … a a b b n moves to right to match a b and replace it with y b … … … b b x a a … … … a a y b b … … … b b b … … … b b n moves to left to match a a and replace it with x x a a … … … a a y b 1. Total moves match an a with a b are n right moves + n left moves = 2n moves 2. Total moves to match na’s with nb’s = n (2n) = 2n2 Complexity of n n ab Note that: • The step (i) consists of going through the input string (anbn) forward and backward and replacing the leftmost a by x and the leftmost b by y. So we require at most 2n moves to match a a with a b. • Step (ii) is repetition of step (i) n times. • Hence the number of moves for accepting (anbn) is at most (2n)(n) = 2n2. • For strings not of the form (anbn), TM halts with less than 2n2 steps. Hence T(M) = O(n2). Euclidean algorithm for evaluating gcd(a, b) The Euclidean algorithm has the following steps: 1. The input is (a, b) 2. Repeat until b = 0 3. Assign a ← a mod b 4. Exchange a and b 5. Output a. OTHER NP-COMPLETE PROBLEMS 1. CSAT: Given a Boolean expression in CNF (Conjunctive Normal Form), is it satisfiable? – It can be proved that CSAT is NP-complete by proving that CSAT is in NP and getting a polynomial reduction from SAT to CSAT, 2. Hamiltonian circuit problem: Does graph G have a Hamiltonian circuit (i.e. a circuit passing through each edge of graph G exactly once)? 3. Travelling salesman problem (TSP): Given n cities, the distance between them and a number D, does there exist a tour program for a salesman to visit all the cities exactly once so that the distance travelled is at most D? OTHER NP-COMPLETE PROBLEMS 4. Vertex cover problem: Given a graph G and a natural number k, does there exist a vertex cover for G with k vertices? (A subsets C of vertices of G is a vertex cover for G if each edge of G has an odd vertex in C.) 4. Knapsack problem: Given a set A = {a1, a2, ..., an} of nonnegative integers. and an integer K, does there exist a subset B of A such that B ∑bj = K where bj 1-D knapsack problem Example • Problem: Which boxes should be chosen to maximize the amount of money while still keeping the overall weight under or equal to 15 kg? • Solution: 1. if any number of each box is available, then three yellow boxes and three grey boxes; 2. if only the shown boxes are available, then all but not the green box.) Solution 1: 1. Weight = 4Kg x 3 + 1Kg x 3 = 12Kg+3Kg = 15Kg 2. Money = \$10 x 3 + \$2 x 3 = \$30 + \$6 = \$36 Solution 2: 1. Weight = 4Kg + 2Kg + 1 Kg + 1Kg = 8Kg 2. (Money = \$10 + \$2 + \$2 + \$1 = \$15 Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4