27
Master’s Programme in Computer Science Juha Kärkkäinen Based on slides by Veli Mäkinen DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) 09/10/2019 1 Design and Analysis of Algorithms 2019 week 6

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

Juha Kärkkäinen

Based on slides by Veli Mäkinen

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019)

09/10/2019 1 Design and Analysis of Algorithms 2019 week 6

Page 2: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

SAT is NP-complete & encodings

Week VI

09/10/2019 Design and Analysis of Algorithms 2019 week 6 2

Page 3: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

SAT is NP-complete

Cook-Levin theorem, proof using Turing machine

09/10/2019 Design and Analysis of Algorithms 2019 week 6 3

Page 4: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

REDUCTION TREE COVERED IN THIS COURSE

SAT (now)

3CNF-SAT (last week)

CLIQUE (last week) SUBSET-SUM (today)

VERTEX-COVER (last week)

HAM-CYCLE (see the book, this shows how complex reduction gadgets can be)

Independent set (study group)

Multi-LCS (study group)

Inapproximability of TSP (last week) Hamiltonian path (study group)

09/10/2019 Design and Analysis of Algorithms 2019 week 5 4

Unbounded Knapsack (exercises)

Page 5: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

RECALL: COMPLEXITY CLASSES

P = decision problems that can be solved in O(nk) time

NP = decision problems that can be verified in O(nk) time

k constant

n input length (with appropriate encoding)

NP-complete = decision problem L s.t.

1. L is in NP

2. L’ ≤p L for every L’ in NP

Design and Analysis of Algorithms 2019 week 5 5

Next

week

09/10/2019

Page 6: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

RECALL: BOOLEAN SATISFIABILITY SAT

09/10/2019 Design and Analysis of Algorithms 2019 week 6 6

Example: Φ=((x1˄x2)˅¬x3) (x4→x5)

Page 7: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

BACK TO TURING

Proposition: Computation in RAM model can be simulated in

polynomial time with Turing machine, and vice versa. (proof

omitted here)

Corollary. Problem L is in NP iff it can be solved in O(nc) time on

a non-deterministic Turing machine (NDTM), where c is a

constant.

09/10/2019 Design and Analysis of Algorithms 2019 week 6 7

Page 8: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

NON-DETERMINISTIC TURING MACHINE (NDTM)

q

p

start state accepting state

a

Head in an input / working tape

Transition: Read ”a” on tape, replace it with ”b”, and

move head to the right (+1), move state p->q

09/10/2019 Design and Analysis of Algorithms 2019 week 6 8

Nondeterministic:

accept input if

there exists a path

from the start state

to an accepting state.

Page 9: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

MORE FORMALLY…

09/10/2019 Design and Analysis of Algorithms 2019 week 6 9

Page 10: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

09/10/2019 Design and Analysis of Algorithms 2019 week 6 10

Page 11: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

a

b

… p(n)

2p(n)

09/10/2019 Design and Analysis of Algorithms 2019 week 6 11

Page 12: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

SAT variables: Hi,k = 1 if head is at position i at step k

Qq,k = 1 if state q is active at step k

Ti,c,k = 1 if tape contains symbol c at index i at step k

09/10/2019 Design and Analysis of Algorithms 2019 week 6

a

b

… p(n)

2p(n)

i

k c

Page 13: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

Initialization (step 0): H0,0=1

Qs,0=1

Ti,c,0=1 if c is initial content of cell i

09/10/2019 Design and Analysis of Algorithms 2019 week 6

a

b

… p(n)

2p(n)

i c

Page 14: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

09/10/2019 Design and Analysis of Algorithms 2019 week 6 14

a

b

… p(n)

2p(n)

Page 15: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

09/10/2019 Design and Analysis of Algorithms 2019 week 6 15

a

b

… p(n)

2p(n)

Page 16: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

a

b

… p(n)

2p(n)

c

i

k

blackboard/exercise

09/10/2019 Design and Analysis of Algorithms 2019 week 6 16

˅

Page 17: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

a

b

p(n)

2p(n)

c

c’

k

k+1 p

q

i

09/10/2019 Design and Analysis of Algorithms 2019 week 6 17

˄ ˄

Page 18: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

THEOREM: SAT IS NP-COMPLETE

Enforcing ”one head position at a time” creates a O(p(n)3) long

expression, and other parts create shorter expressions.

Thus, an instance of any problem in NP can be casted in

polynomial time to a boolean expression, such that a ”yes”

instance maps into a satisfying variable assignment where the

variables set to ”true” reveal a state-path transition in the NDTM

to an accepting state.

09/10/2019 Design and Analysis of Algorithms 2019 week 6

Page 19: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

REDUCTION TREE COVERED IN THIS COURSE

SAT (done)

3CNF-SAT (last week)

CLIQUE (last week) SUBSET-SUM (today)

VERTEX-COVER (last week)

HAM-CYCLE (see the book, this shows how complex reduction gadgets can be)

Independent set (study group)

Multi-LCS (study group)

Inapproximability of TSP (last week) Hamiltonian path (study group)

09/10/2019 Design and Analysis of Algorithms 2019 week 5 19

(Un)bounded Knapsack (exercises)

Page 20: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Encodings

Pseudopolynomiality of dynamic programming solutions to

subset sum and knapsack

09/10/2019 Design and Analysis of Algorithms 2019 week 6 20

Page 21: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

ENCODING INPUT P = problems that can be solved in O(nk) time

NP = problems that can be verified in O(nk) time

k constant

n input length

The size of input depends on its encoding:

• Any encoding of polynomial size in n is ok

• Encoding integers can be tricky

• Binary encoding: 6=110=00000110. How many bits to use?

• Unary encoding: 6=0000001=061. Exponentially larger than binary.

• Gamma encoding: 6=0001 110. (0001 is number of bits in unary)

09/10/2019 Design and Analysis of Algorithms 2019 week 5 21

Encoding?

Page 22: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

EXAMPLE: HAMILTONIAN CYCLE PROBLEM

Given an undirected graph G=(V,E), is there a cycle visiting each vertex exactly once?

Assume |V|≤|E|.

Encoding:

vertices numbered 1..|V|.

Input = <|E|,(v1,w1),(v2,w2),…, (v|E|,w|E|)>

000000..0 1 bin(|E|) 00..0bin(v1) 00..0bin(w1) …

Input size ~ 2(|E|+1)log |E| bits

Unary encoding for all values, ≤ (2|E|+1)(|V|+1) bits, would be polynomial too.

number of bits in unary 0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

x bin(x)

09/10/2019 Design and Analysis of Algorithms 2019 week 6 22

Page 23: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

EXAMPLE: UNBOUNDED KNAPSACK

Unbounded Knapsack: Given integers W and V and m pairs of (weight,value) item types, is there a collection of items with total weight ≤W and total value ≥V.

In study groups we saw an O(mW) dynamic programming algorithm which makes Knapsack pseudopolynomially solvable: there is a fast algorithm if input parameters are suitably bounded.

However, Unbounded Knapsack is NP-hard (exercise). This does not contradict pseudopolynomiality because of encodings.

• Input can be encoded in O(m log N) bits, where N is the largest integer in input.

• Assume N=W=2m. Then input size is O(m2) bits but the time complexity is O(m2m), which is exponential in input size.

09/10/2019 Design and Analysis of Algorithms 2019 week 6 23

Page 24: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

EXAMPLE: SUBSET-SUM

SUBSET-SUM: Given an integer t and a set S of integers, is there

a subset S’ of S with a total sum of exactly t?

SUBSET-SUM is another pseudopolynomially solvable but NP-

complete problem.

• There is an O(|S|t) time dynamic programming algorithm.

• NP-hardness proof by reduction from 3CNF-SAT (blackboard,

book Sect. 34.5.5).

09/10/2019 Design and Analysis of Algorithms 2019 week 6 24

Page 25: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

REDUCTION TREE COVERED IN THIS COURSE

SAT (today)

3CNF-SAT (last week)

CLIQUE (last week) SUBSET-SUM (today)

VERTEX-COVER (last week)

HAM-CYCLE (see the book, this shows how complex reduction gadgets can be)

Independent set (study group)

Multi-LCS (study group)

Inapproximability of TSP (last week) Hamiltonian path (study group)

09/10/2019 Design and Analysis of Algorithms 2019 week 5 25

(Un)bounded Knapsack (exercises)

Page 26: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

RECAP: PROVING NP-COMPLETENESS

09/10/2019 Design and Analysis of Algorithms 2019 week 6 26

Prove NP-completeness of L by reduction from L’

1. Prove L is in NP

• Certificate that can be verified in polynomial time

2. Describe conversion from L’ to L

• L’ is known NP-complete problem

• Conversion of input to input, not solution to solution

3. Prove that “yes”-instance maps to “yes”-instance

• Conversion of solution to solution

4. Prove that “no”-instance maps to “no”-instance

• Often proof by contradiction: “yes”-instance maps to “yes”-instance in

opposite direction (which contradicts “no”-instance mapping to “yes”-

instance).

Page 27: DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2019) · NP = decision problems that can be verified in O(nk) time k constant n input length (with appropriate encoding) NP-complete = decision

Master’s Programme in Computer Science

SOLVING NP-HARD PROBLEMS

09/10/2019 Design and Analysis of Algorithms 2019 week 6 27

1. Exact algorithms

• Always optimal solution

• May take exponential time

• Can be practical for small instances and important special cases

2. Approximation algorithms

• Guarantee of being “close” to optimal solution

• Polynomial time

3. Heuristic algorithms

• No guarantee of even close-to-optimality

• Sometimes best one can do in practice