Алгоритмы для np-трудных задач, осень 2016: Точные...

126
Coping with NP-completeness: Exact Algorithms Alexander S. Kulikov Steklov Institute of Mathematics at St. Petersburg Russian Academy of Sciences Advanced Algorithms and Complexity Data Structures and Algorithms

Upload: cs-center

Post on 15-Apr-2017

107 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Coping withNP-completeness:Exact Algorithms

Alexander S. KulikovSteklov Institute of Mathematics at St. Petersburg

Russian Academy of Sciences

Advanced Algorithms and ComplexityData Structures and Algorithms

Page 2: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Exact algorithms or intelligent exhaustivesearch: finding an optimal solution withoutgoing through all candidate solutions

Page 3: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Outline

1 3-SatisfiabilityBacktrackingLocal Search

2 Traveling Salesman ProblemDynamic ProgrammingBranch-and-bound

Page 4: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

3-Satisfiability (3-SAT)

Input: A set of clauses, each containing atmost three literals (that is, a3-CNF formula).

Output: Find a satisfying assignment(if exists).

Page 5: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Examples

The formula

(x ∨ y ∨ z)(x ∨ y)(y ∨ z)

is satisfiable: set x = y = z = 1 orx = 1, y = z = 0.The formula

(x∨y ∨z)(x∨y)(y ∨z)(z∨x)(x∨y ∨z)

is unsatisfiable.

Page 6: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

A brute force search algorithm checkingsatisfiability of a 3-CNF formula F with n

variables, goes through all assignments andhas running time O(|F | · 2n).

GoalAvoid going through all 2n assignments

Page 7: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

A brute force search algorithm checkingsatisfiability of a 3-CNF formula F with n

variables, goes through all assignments andhas running time O(|F | · 2n).

GoalAvoid going through all 2n assignments

Page 8: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Outline

1 3-SatisfiabilityBacktrackingLocal Search

2 Traveling Salesman ProblemDynamic ProgrammingBranch-and-bound

Page 9: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Backtracking

Construct a solution piece by piece

Backtrack if the current partial solutioncannot be extended to a valid solution

Page 10: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Backtracking

Construct a solution piece by pieceBacktrack if the current partial solutioncannot be extended to a valid solution

Page 11: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

Page 12: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

Page 13: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

Page 14: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

(x4)(x4)

x3 = 0

Page 15: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

(x4)(x4)

x3 = 0

()

x4 = 0

Page 16: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

(x4)(x4)

x3 = 0

()

x4 = 0

()

x4 = 1

Page 17: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

(x4)(x4)

x3 = 0

()

x4 = 0

()

x4 = 1

()(x4)

x3 = 1

Page 18: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

(x4)(x4)

x3 = 0

()

x4 = 0

()

x4 = 1

()(x4)

x3 = 1

()

x2 = 1

Page 19: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

(x1 ∨ x2 ∨ x3 ∨ x4)(x1)(x1 ∨ x2 ∨ x3)(x1 ∨ x2)(x2 ∨ x4)

(x2 ∨ x3 ∨ x4)(x2 ∨ x3)(x2)(x2 ∨ x4)

x1 = 0

(x3 ∨ x4)(x3)(x4)

x2 = 0

(x4)(x4)

x3 = 0

()

x4 = 0

()

x4 = 1

()(x4)

x3 = 1

()

x2 = 1

()(x2 ∨ x4)

x1 = 1

Page 20: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

SolveSAT(F )

if F has no clauses:return “sat”

if F contains an empty clause:return “unsat”

x ← unassigned variable of F

if SolveSAT(F [x ← 0]) = “sat”:return “sat”

if SolveSAT(F [x ← 1]) = “sat”:return “sat”

return “unsat”

Page 21: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

SolveSAT(F )

if F has no clauses:return “sat”

if F contains an empty clause:return “unsat”

x ← unassigned variable of F

if SolveSAT(F [x ← 0]) = “sat”:return “sat”

if SolveSAT(F [x ← 1]) = “sat”:return “sat”

return “unsat”

Page 22: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

SolveSAT(F )

if F has no clauses:return “sat”

if F contains an empty clause:return “unsat”

x ← unassigned variable of F

if SolveSAT(F [x ← 0]) = “sat”:return “sat”

if SolveSAT(F [x ← 1]) = “sat”:return “sat”

return “unsat”

Page 23: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

SolveSAT(F )

if F has no clauses:return “sat”

if F contains an empty clause:return “unsat”

x ← unassigned variable of F

if SolveSAT(F [x ← 0]) = “sat”:return “sat”

if SolveSAT(F [x ← 1]) = “sat”:return “sat”

return “unsat”

Page 24: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

SolveSAT(F )

if F has no clauses:return “sat”

if F contains an empty clause:return “unsat”

x ← unassigned variable of F

if SolveSAT(F [x ← 0]) = “sat”:return “sat”

if SolveSAT(F [x ← 1]) = “sat”:return “sat”

return “unsat”

Page 25: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

SolveSAT(F )

if F has no clauses:return “sat”

if F contains an empty clause:return “unsat”

x ← unassigned variable of F

if SolveSAT(F [x ← 0]) = “sat”:return “sat”

if SolveSAT(F [x ← 1]) = “sat”:return “sat”

return “unsat”

Page 26: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Thus, instead of considering all 2n

branches of the recursion tree, we trackcarefully each branch

When we realize that a branch is dead(cannot be extended to a solution),we immediately cut it

Page 27: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Thus, instead of considering all 2n

branches of the recursion tree, we trackcarefully each branchWhen we realize that a branch is dead(cannot be extended to a solution),we immediately cut it

Page 28: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Backtracking is used in manystate-of-the-art SAT-solvers

SAT-solvers use tricky heuristics tochoose a variable to branch on and tosimplify a formula before branchingAnother commonly used technique islocal search — will consider it in thenext part

Page 29: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Backtracking is used in manystate-of-the-art SAT-solversSAT-solvers use tricky heuristics tochoose a variable to branch on and tosimplify a formula before branching

Another commonly used technique islocal search — will consider it in thenext part

Page 30: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Backtracking is used in manystate-of-the-art SAT-solversSAT-solvers use tricky heuristics tochoose a variable to branch on and tosimplify a formula before branchingAnother commonly used technique islocal search — will consider it in thenext part

Page 31: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Outline

1 3-SatisfiabilityBacktrackingLocal Search

2 Traveling Salesman ProblemDynamic ProgrammingBranch-and-bound

Page 32: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Local Search

Start with a candidatesolution

Iteratively move fromthe current candidateto its neighbor tryingto improve thecandidate

Page 33: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Local Search

Start with a candidatesolutionIteratively move fromthe current candidateto its neighbor tryingto improve thecandidate

Page 34: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Local Search

Start with a candidatesolutionIteratively move fromthe current candidateto its neighbor tryingto improve thecandidate

Page 35: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Local Search

Start with a candidatesolutionIteratively move fromthe current candidateto its neighbor tryingto improve thecandidate

Page 36: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Main Idea of Local Search

Start with a candidatesolutionIteratively move fromthe current candidateto its neighbor tryingto improve thecandidate

Page 37: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Let F be a 3-CNF formula over variablesx1, x2, . . . , xn

A candidate solution is a truthassignment to these variables, that is,a vector from {0, 1}n

Page 38: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Let F be a 3-CNF formula over variablesx1, x2, . . . , xn

A candidate solution is a truthassignment to these variables, that is,a vector from {0, 1}n

Page 39: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

DefinitionHamming distance (or just distance) betweentwo assignments 𝛼, 𝛽 ∈ {0, 1}n is thenumber of bits where they differ:dist(𝛼, 𝛽) = |{i : 𝛼i ̸= 𝛽i}| .

DefinitionHamming ball with center 𝛼 ∈ {0, 1}n andradius r , denoted by ℋ(𝛼, r), is the set of alltruth assignments from {0, 1}n at distanceat most r from 𝛼.

Page 40: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

DefinitionHamming distance (or just distance) betweentwo assignments 𝛼, 𝛽 ∈ {0, 1}n is thenumber of bits where they differ:dist(𝛼, 𝛽) = |{i : 𝛼i ̸= 𝛽i}| .

DefinitionHamming ball with center 𝛼 ∈ {0, 1}n andradius r , denoted by ℋ(𝛼, r), is the set of alltruth assignments from {0, 1}n at distanceat most r from 𝛼.

Page 41: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

ℋ(1011, 0) = {1011}

ℋ(1011, 1) ={1011, 0011, 1111, 1001, 1010}ℋ(1011, 2) ={1011, 0011, 1111, 1001, 1010,0111, 0001, 0010, 1101, 1110, 1000}

Page 42: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

ℋ(1011, 0) = {1011}ℋ(1011, 1) ={1011, 0011, 1111, 1001, 1010}

ℋ(1011, 2) ={1011, 0011, 1111, 1001, 1010,0111, 0001, 0010, 1101, 1110, 1000}

Page 43: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

ℋ(1011, 0) = {1011}ℋ(1011, 1) ={1011, 0011, 1111, 1001, 1010}ℋ(1011, 2) ={1011, 0011, 1111, 1001, 1010,0111, 0001, 0010, 1101, 1110, 1000}

Page 44: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Searching a Ball for a Solution

LemmaAssume that ℋ(𝛼, r) contains a satisfyingassignment 𝛽 for F . We can then find a(possibly different) satisfying assignment intime O(|F | · 3r).

Page 45: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

ProofIf 𝛼 satisfies F , return 𝛼

Otherwise, take an unsatisfied clause — say,(xi ∨ x j ∨ xk)

𝛼 assigns xi = 0, xj = 1, xk = 0

Let 𝛼i , 𝛼j , 𝛼k be assignments resulting from 𝛼by flipping the i -th, j-th, k-th bit, respectively

Crucial observation: at least one of them iscloser to 𝛽 than 𝛼

Hence there are at most 3r recursive calls

Page 46: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

ProofIf 𝛼 satisfies F , return 𝛼

Otherwise, take an unsatisfied clause — say,(xi ∨ x j ∨ xk)

𝛼 assigns xi = 0, xj = 1, xk = 0

Let 𝛼i , 𝛼j , 𝛼k be assignments resulting from 𝛼by flipping the i -th, j-th, k-th bit, respectively

Crucial observation: at least one of them iscloser to 𝛽 than 𝛼

Hence there are at most 3r recursive calls

Page 47: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

ProofIf 𝛼 satisfies F , return 𝛼

Otherwise, take an unsatisfied clause — say,(xi ∨ x j ∨ xk)

𝛼 assigns xi = 0, xj = 1, xk = 0

Let 𝛼i , 𝛼j , 𝛼k be assignments resulting from 𝛼by flipping the i -th, j-th, k-th bit, respectively

Crucial observation: at least one of them iscloser to 𝛽 than 𝛼

Hence there are at most 3r recursive calls

Page 48: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

ProofIf 𝛼 satisfies F , return 𝛼

Otherwise, take an unsatisfied clause — say,(xi ∨ x j ∨ xk)

𝛼 assigns xi = 0, xj = 1, xk = 0

Let 𝛼i , 𝛼j , 𝛼k be assignments resulting from 𝛼by flipping the i -th, j-th, k-th bit, respectively

Crucial observation: at least one of them iscloser to 𝛽 than 𝛼

Hence there are at most 3r recursive calls

Page 49: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

ProofIf 𝛼 satisfies F , return 𝛼

Otherwise, take an unsatisfied clause — say,(xi ∨ x j ∨ xk)

𝛼 assigns xi = 0, xj = 1, xk = 0

Let 𝛼i , 𝛼j , 𝛼k be assignments resulting from 𝛼by flipping the i -th, j-th, k-th bit, respectively

Crucial observation: at least one of them iscloser to 𝛽 than 𝛼

Hence there are at most 3r recursive calls

Page 50: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

ProofIf 𝛼 satisfies F , return 𝛼

Otherwise, take an unsatisfied clause — say,(xi ∨ x j ∨ xk)

𝛼 assigns xi = 0, xj = 1, xk = 0

Let 𝛼i , 𝛼j , 𝛼k be assignments resulting from 𝛼by flipping the i -th, j-th, k-th bit, respectively

Crucial observation: at least one of them iscloser to 𝛽 than 𝛼

Hence there are at most 3r recursive calls

Page 51: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

𝛼

𝛽

Page 52: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

𝛼

𝛽

Page 53: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

𝛼

𝛽

Page 54: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

𝛼

𝛽

Page 55: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

CheckBall(F , 𝛼, r)if 𝛼 satisfies F:

return 𝛼

if r = 0:return “not found”

xi , xj , xk ← variables of unsatisfied clause𝛼i , 𝛼j , 𝛼k ← 𝛼 with bits i , j , k flippedCheckBall(F , 𝛼i , r − 1)CheckBall(F , 𝛼j , r − 1)CheckBall(F , 𝛼k , r − 1)if a satisfying assignment is found:

return itelse:

return “not found”

Page 56: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

CheckBall(F , 𝛼, r)if 𝛼 satisfies F:

return 𝛼if r = 0:

return “not found”

xi , xj , xk ← variables of unsatisfied clause𝛼i , 𝛼j , 𝛼k ← 𝛼 with bits i , j , k flippedCheckBall(F , 𝛼i , r − 1)CheckBall(F , 𝛼j , r − 1)CheckBall(F , 𝛼k , r − 1)if a satisfying assignment is found:

return itelse:

return “not found”

Page 57: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

CheckBall(F , 𝛼, r)if 𝛼 satisfies F:

return 𝛼if r = 0:

return “not found”xi , xj , xk ← variables of unsatisfied clause𝛼i , 𝛼j , 𝛼k ← 𝛼 with bits i , j , k flipped

CheckBall(F , 𝛼i , r − 1)CheckBall(F , 𝛼j , r − 1)CheckBall(F , 𝛼k , r − 1)if a satisfying assignment is found:

return itelse:

return “not found”

Page 58: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

CheckBall(F , 𝛼, r)if 𝛼 satisfies F:

return 𝛼if r = 0:

return “not found”xi , xj , xk ← variables of unsatisfied clause𝛼i , 𝛼j , 𝛼k ← 𝛼 with bits i , j , k flippedCheckBall(F , 𝛼i , r − 1)CheckBall(F , 𝛼j , r − 1)CheckBall(F , 𝛼k , r − 1)

if a satisfying assignment is found:return it

else:return “not found”

Page 59: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

CheckBall(F , 𝛼, r)if 𝛼 satisfies F:

return 𝛼if r = 0:

return “not found”xi , xj , xk ← variables of unsatisfied clause𝛼i , 𝛼j , 𝛼k ← 𝛼 with bits i , j , k flippedCheckBall(F , 𝛼i , r − 1)CheckBall(F , 𝛼j , r − 1)CheckBall(F , 𝛼k , r − 1)if a satisfying assignment is found:

return itelse:

return “not found”

Page 60: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Assume that F has a satisfyingassignment 𝛽

If it has more 1’s than 0’s then it hasdistance at most n/2 from all-1’sassignmentOtherwise it has distance at most n/2from all-0’s assignmentThus, it suffices to make two calls:CheckBall(F , 11 . . . 1, n/2) andCheckBall(F , 00 . . . 0, n/2)

Page 61: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Assume that F has a satisfyingassignment 𝛽If it has more 1’s than 0’s then it hasdistance at most n/2 from all-1’sassignment

Otherwise it has distance at most n/2from all-0’s assignmentThus, it suffices to make two calls:CheckBall(F , 11 . . . 1, n/2) andCheckBall(F , 00 . . . 0, n/2)

Page 62: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Assume that F has a satisfyingassignment 𝛽If it has more 1’s than 0’s then it hasdistance at most n/2 from all-1’sassignmentOtherwise it has distance at most n/2from all-0’s assignment

Thus, it suffices to make two calls:CheckBall(F , 11 . . . 1, n/2) andCheckBall(F , 00 . . . 0, n/2)

Page 63: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Assume that F has a satisfyingassignment 𝛽If it has more 1’s than 0’s then it hasdistance at most n/2 from all-1’sassignmentOtherwise it has distance at most n/2from all-0’s assignmentThus, it suffices to make two calls:CheckBall(F , 11 . . . 1, n/2) andCheckBall(F , 00 . . . 0, n/2)

Page 64: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Running Time

The running time of the resultingalgorithm isO(|F | · 3n/2) ≈ O(|F | · 1.733n)

On one hand, this is still exponentialOn the other hand, it is exponentiallyfaster than a brute force searchalgorithm that goes through all 2n truthassignments!

Page 65: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Running Time

The running time of the resultingalgorithm isO(|F | · 3n/2) ≈ O(|F | · 1.733n)On one hand, this is still exponential

On the other hand, it is exponentiallyfaster than a brute force searchalgorithm that goes through all 2n truthassignments!

Page 66: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Running Time

The running time of the resultingalgorithm isO(|F | · 3n/2) ≈ O(|F | · 1.733n)On one hand, this is still exponentialOn the other hand, it is exponentiallyfaster than a brute force searchalgorithm that goes through all 2n truthassignments!

Page 67: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Outline

1 3-SatisfiabilityBacktrackingLocal Search

2 Traveling Salesman ProblemDynamic ProgrammingBranch-and-bound

Page 68: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Traveling salesman problem (TSP)

Input: A complete graph with weights onedges and a budget b.

Output: A cycle that visits each vertexexactly once and has total weightat most b.

It will be convenient to assume that verticesare integers from 1 to n and that thesalesman starts his trip in (and also returnsback to) vertex 1.

Page 69: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Traveling salesman problem (TSP)

Input: A complete graph with weights onedges and a budget b.

Output: A cycle that visits each vertexexactly once and has total weightat most b.

It will be convenient to assume that verticesare integers from 1 to n and that thesalesman starts his trip in (and also returnsback to) vertex 1.

Page 70: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

1

2

5

3

4

5

2

4

2

2

1

13

33

Page 71: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

1

2

5

3

4

5

2

4

2

2

1

13

33

length: 15

Page 72: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

1

2

5

3

4

5

2

4

2

2

1

13

33

length: 11

Page 73: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

1

2

5

3

4

5

2

4

2

2

1

13

33

length: 9

Page 74: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Brute Force SolutionA naive algorithm just checks all possible(n − 1)! cycles.

This part

Use dynamic programming to solve TSPin O(n2 · 2n)The running time is exponential, but ismuch better than (n − 1)!.

Page 75: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Brute Force SolutionA naive algorithm just checks all possible(n − 1)! cycles.

This part

Use dynamic programming to solve TSPin O(n2 · 2n)

The running time is exponential, but ismuch better than (n − 1)!.

Page 76: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Brute Force SolutionA naive algorithm just checks all possible(n − 1)! cycles.

This part

Use dynamic programming to solve TSPin O(n2 · 2n)The running time is exponential, but ismuch better than (n − 1)!.

Page 77: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Outline

1 3-SatisfiabilityBacktrackingLocal Search

2 Traveling Salesman ProblemDynamic ProgrammingBranch-and-bound

Page 78: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Dynamic ProgrammingWe are going to use dynamicprogramming: instead of solving oneproblem we will solve a collection of(overlapping) subproblems

A subproblem refers to a partial solutionA reasonable partial solution in case ofTSP is the initial part of a cycleTo continue building a cycle, we need toknow the last vertex as well as the set ofalready visited vertices

Page 79: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Dynamic ProgrammingWe are going to use dynamicprogramming: instead of solving oneproblem we will solve a collection of(overlapping) subproblemsA subproblem refers to a partial solution

A reasonable partial solution in case ofTSP is the initial part of a cycleTo continue building a cycle, we need toknow the last vertex as well as the set ofalready visited vertices

Page 80: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Dynamic ProgrammingWe are going to use dynamicprogramming: instead of solving oneproblem we will solve a collection of(overlapping) subproblemsA subproblem refers to a partial solutionA reasonable partial solution in case ofTSP is the initial part of a cycle

To continue building a cycle, we need toknow the last vertex as well as the set ofalready visited vertices

Page 81: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Dynamic ProgrammingWe are going to use dynamicprogramming: instead of solving oneproblem we will solve a collection of(overlapping) subproblemsA subproblem refers to a partial solutionA reasonable partial solution in case ofTSP is the initial part of a cycleTo continue building a cycle, we need toknow the last vertex as well as the set ofalready visited vertices

Page 82: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Subproblems

For a subset of vertices S ⊆ {1, . . . , n}containing the vertex 1 and a vertexi ∈ S , let C (S , i) be the length of theshortest path that starts at 1, ends at iand visits all vertices from S exactlyonce

C ({1}, 1) = 0 and C (S , 1) = +∞ when|S | > 1

Page 83: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Subproblems

For a subset of vertices S ⊆ {1, . . . , n}containing the vertex 1 and a vertexi ∈ S , let C (S , i) be the length of theshortest path that starts at 1, ends at iand visits all vertices from S exactlyonceC ({1}, 1) = 0 and C (S , 1) = +∞ when|S | > 1

Page 84: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Recurrence RelationConsider the second-to-last vertex j onthe required shortest path from 1 to i

visiting all vertices from S

The subpath from 1 to j is the shortestone visiting all vertices from S − {i}exactly onceHenceC (S , i) = min{C (S − {i}, j) + dji},where the minimum is over all j ∈ S

such that j ̸= i

Page 85: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Recurrence RelationConsider the second-to-last vertex j onthe required shortest path from 1 to i

visiting all vertices from S

The subpath from 1 to j is the shortestone visiting all vertices from S − {i}exactly once

HenceC (S , i) = min{C (S − {i}, j) + dji},where the minimum is over all j ∈ S

such that j ̸= i

Page 86: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Recurrence RelationConsider the second-to-last vertex j onthe required shortest path from 1 to i

visiting all vertices from S

The subpath from 1 to j is the shortestone visiting all vertices from S − {i}exactly onceHenceC (S , i) = min{C (S − {i}, j) + dji},where the minimum is over all j ∈ S

such that j ̸= i

Page 87: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Order of Subproblems

Need to process all subsetsS ⊆ {1, . . . , n} in an order thatguarantees that when computing thevalue of C (S , i), the values ofC (S − {i}, j) have already beencomputed

For example, we can process subsets inorder of increasing size

Page 88: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Order of Subproblems

Need to process all subsetsS ⊆ {1, . . . , n} in an order thatguarantees that when computing thevalue of C (S , i), the values ofC (S − {i}, j) have already beencomputedFor example, we can process subsets inorder of increasing size

Page 89: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

TSP(G )C ({1}, 1)← 0

for s from 2 to n:for all 1 ∈ S ⊆ {1, . . . , n} of size s:

C (S , 1)← +∞for all i ∈ S, i ̸= 1:

for all j ∈ S, j ̸= i:C (S , i)← min{C (S , i),C (S−{i}, j)+dji}

return mini{C ({1, . . . , n}, i) + di ,1}

Page 90: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

TSP(G )C ({1}, 1)← 0for s from 2 to n:

for all 1 ∈ S ⊆ {1, . . . , n} of size s:C (S , 1)← +∞

for all i ∈ S, i ̸= 1:for all j ∈ S, j ̸= i:

C (S , i)← min{C (S , i),C (S−{i}, j)+dji}return mini{C ({1, . . . , n}, i) + di ,1}

Page 91: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

TSP(G )C ({1}, 1)← 0for s from 2 to n:

for all 1 ∈ S ⊆ {1, . . . , n} of size s:C (S , 1)← +∞for all i ∈ S, i ̸= 1:

for all j ∈ S, j ̸= i:C (S , i)← min{C (S , i),C (S−{i}, j)+dji}

return mini{C ({1, . . . , n}, i) + di ,1}

Page 92: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

TSP(G )C ({1}, 1)← 0for s from 2 to n:

for all 1 ∈ S ⊆ {1, . . . , n} of size s:C (S , 1)← +∞for all i ∈ S, i ̸= 1:

for all j ∈ S, j ̸= i:C (S , i)← min{C (S , i),C (S−{i}, j)+dji}

return mini{C ({1, . . . , n}, i) + di ,1}

Page 93: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Implementation RemarkHow to iterate through all subsets of{1, . . . , n}?

There is a natural one-to-onecorrespondence between integers in therange from 0 and 2n − 1 and subsets of{0, . . . , n − 1}:

k ↔ {i : i -th bit of k is 1}

Page 94: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Implementation RemarkHow to iterate through all subsets of{1, . . . , n}?There is a natural one-to-onecorrespondence between integers in therange from 0 and 2n − 1 and subsets of{0, . . . , n − 1}:

k ↔ {i : i -th bit of k is 1}

Page 95: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example

k bin(k) {i : i -th bit of k is 1}

0 000 ∅1 001 {0}2 010 {1}3 011 {0,1}4 100 {2}5 101 {0,2}6 110 {1,2}7 111 {0,1,2}

Page 96: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

If k corresponds to S , how to find outthe integer corresponding to S − {j}(for j ∈ S)?

For this, we need to flip the j-th bit of k(from 1 to 0)For this, in turn, we compute a bitwiseXOR of k and 2j (that has 1 only in j-thposition)In C/C++, Java, Python:k ^ (1 < < j)

Page 97: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

If k corresponds to S , how to find outthe integer corresponding to S − {j}(for j ∈ S)?For this, we need to flip the j-th bit of k(from 1 to 0)

For this, in turn, we compute a bitwiseXOR of k and 2j (that has 1 only in j-thposition)In C/C++, Java, Python:k ^ (1 < < j)

Page 98: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

If k corresponds to S , how to find outthe integer corresponding to S − {j}(for j ∈ S)?For this, we need to flip the j-th bit of k(from 1 to 0)For this, in turn, we compute a bitwiseXOR of k and 2j (that has 1 only in j-thposition)

In C/C++, Java, Python:k ^ (1 < < j)

Page 99: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

If k corresponds to S , how to find outthe integer corresponding to S − {j}(for j ∈ S)?For this, we need to flip the j-th bit of k(from 1 to 0)For this, in turn, we compute a bitwiseXOR of k and 2j (that has 1 only in j-thposition)In C/C++, Java, Python:k ^ (1 < < j)

Page 100: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Outline

1 3-SatisfiabilityBacktrackingLocal Search

2 Traveling Salesman ProblemDynamic ProgrammingBranch-and-bound

Page 101: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

The branch-and-bound technique can beviewed as a generalization ofbacktracking for optimization problems

We grow a tree of partial solutionsAt each node of the recursion tree wecheck whether the current partialsolution can be extended to a solutionwhich is better than the best solutionfound so farIf not, we don’t continue this branch

Page 102: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

The branch-and-bound technique can beviewed as a generalization ofbacktracking for optimization problemsWe grow a tree of partial solutions

At each node of the recursion tree wecheck whether the current partialsolution can be extended to a solutionwhich is better than the best solutionfound so farIf not, we don’t continue this branch

Page 103: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

The branch-and-bound technique can beviewed as a generalization ofbacktracking for optimization problemsWe grow a tree of partial solutionsAt each node of the recursion tree wecheck whether the current partialsolution can be extended to a solutionwhich is better than the best solutionfound so far

If not, we don’t continue this branch

Page 104: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

The branch-and-bound technique can beviewed as a generalization ofbacktracking for optimization problemsWe grow a tree of partial solutionsAt each node of the recursion tree wecheck whether the current partialsolution can be extended to a solutionwhich is better than the best solutionfound so farIf not, we don’t continue this branch

Page 105: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: brute force search

1

4

2

3

1

5

3

10 1 2

Page 106: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: brute force search

1

4

2

3

1

5

3

10 1 2

1

2 3 4

3 4 2 4 2 3

4 3 4 2 3 2

119 1 7 118 1 7 118 1 19

Page 107: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

Page 108: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

10

21

Page 109: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

10

21

36

Page 110: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

10

21

36

49

Page 111: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 19

10

21

36

49

119

Page 112: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 19

10

21

36

49

119

4 3

Page 113: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 19

10

21

36

49

119

4 3

3 6

Page 114: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

Page 115: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

Page 116: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

26

Page 117: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

26

48

Page 118: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

26

48

4 4

Page 119: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

26

48

4 4

2 6

Page 120: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

26

48

4 4

2 6

1 7

Page 121: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: pruned search

1

4

2

3

1

5

3

10 1 2

best so far: 7

10

21

36

49

119

4 3

3 6

1 7

31

26

48

4 4

2 6

1 7

410

Page 122: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

We used the simplest possible lowerbound: any extension of a path haslength at least the length of the path

Modern TSP-solvers use smarter lowerbounds to solve instances withthousands of vertices

Page 123: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

We used the simplest possible lowerbound: any extension of a path haslength at least the length of the pathModern TSP-solvers use smarter lowerbounds to solve instances withthousands of vertices

Page 124: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: lower bounds (still simple)The length of an optimal TSP cycle is at least

12

∑︀v∈V (two min length edges adjacent to v)

the length of a minimum spanning tree

Page 125: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Example: lower bounds (still simple)The length of an optimal TSP cycle is at least

12

∑︀v∈V (two min length edges adjacent to v)

the length of a minimum spanning tree

Page 126: Алгоритмы для NP-трудных задач, осень 2016: Точные алгоритмы

Next timeApproximation algorithms: polynomialalgorithms that find a solution that is notmuch worse than an optimal solution