CS20a: NP problems Graph theory Strongly-connected components T - - PowerPoint PPT Presentation

cs20a np problems
SMART_READER_LITE
LIVE PREVIEW

CS20a: NP problems Graph theory Strongly-connected components T - - PowerPoint PPT Presentation

CS20a: NP problems Graph theory Strongly-connected components T U T E I O T S F N T I E A C I H N N Computation, Computers, and Programs Recursive functions R O O 1 I F L I L O G


slide-1
SLIDE 1

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

1

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

CS20a: NP problems

  • Graph theory

– Strongly-connected components

slide-2
SLIDE 2

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

2

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Graph theory

  • A graph is a set of points (vertices) that are

interconnected by a set of lines (edges)

v2 v1 v4 v3 v5 v6 v7 v8

This is not a vertex

e1 e3 e2 e5 e4 e6 e8 e7 e9 e10

slide-3
SLIDE 3

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

3

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Formal definition of graphs

  • A graph G is defined as a pair G = (V, E) where

– V is a set of vertices – E is a set of edges (vi, vj), . . .

  • n = |V| is the size of the graph
  • |E| is the number of edges
slide-4
SLIDE 4

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

4

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Directed graphs

  • A directed graph assigns a direction to the edges
slide-5
SLIDE 5

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

5

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Paths, cycles

A directed cycle An undirected path An acyclic graph An articulation point

slide-6
SLIDE 6

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

6

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Connected components

  • A directed graph G is defined as a pair G = (V, E)

where – V is a set of vertices – E is a set of edges (vi → vj), . . .

  • Two vertices vi, vj are strongly connected iff there

is a directed path from vi to vj, and a directed path from vj to vi.

  • Two vertices vi, vj are weakly connected iff there

is an undirected path from vi to vj

  • Use these to define strongly-connected and weakly-

connected components.

slide-7
SLIDE 7

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

7

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Using DFS to get strongly-connected comp

  • Variables:

– count: the current DFS index – dfs[v]: the DFS number of vertex v – low[v]: the lowest numbered vertex x such that there is a back-edge from some descen- dent of v to x – stack a stack of the collected vertices

  • if low[v] = dfs[v], then v is the root of a strongly-

connected component

slide-8
SLIDE 8

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

8

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Strongly-connected components

slide-9
SLIDE 9

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

9

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Compute DFS spanning forest

3 2 1 4 5 7 6 8

DFS Forest Backedges

slide-10
SLIDE 10

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

10

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Compute low values

3 2 1 4 5 7 6 8 low[3] = 1 low[2] = 1 low[1] = 1 low[4] = 3 low[5] = 4 low[6] = 6 low[7] = 7 low[8] = 6 x = min{low(w) | w is an immediate descendent of v} y = min{z | z is reachable by a back-edge from v} low(v) = min(x, y)

slide-11
SLIDE 11

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

11

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Using DFS to get strongly-connected comp

  • Variables:

– count: the current DFS index – dfs[v]: the DFS number of vertex v – low[v]: the lowest numbered vertex x such that there is a back-edge from some descen- dent of v to x – stack a stack of the collected vertices

  • if low[v] = dfs[v], then v is the root of a strongly-

connected component

slide-12
SLIDE 12

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

12

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

DFS algorithm

let rec search v = mark v as old; dfs[v] ← count; count ← count + 1; low[v] ← dfs[v]; push v onto stack; for w ∈ outedges[v] do if w is new then search w; low[v] ← min(low[v], low[w]) else if dfs[w] < dfs[v] and w on stack then low[v] ← min(dfs[w], low[v]);

slide-13
SLIDE 13

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

13

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

2SAT graph

A

¬A

slide-14
SLIDE 14

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

14

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

2SAT algorithm

Theorem A 2SAT formula B is satisfiable iff no pair

  • f complementary literals appear in the same strongly-

connected component of G. Proof

  • If they do, then A ⇔ ¬A, so B is not satisfiable.
  • Conversely

– Collapse the strong components of G into sin- gle vertices; this new graph is acyclic – If A occurs in a strong component before a strong component for ¬A, assign A = ⊥ – Otherwise assign A = ⊤

slide-15
SLIDE 15

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

15

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

2SAT algorithm

  • Given formula B

– Form the directed graph – Find the strongly-connected components – If any strongly-connected component contains a literal and its negation, report “not satisfiable” – Otherwise, build a satisfying assignment

  • Collapse strongly-connected components to get DAG
  • If A occurs before not A, then let A = false
  • If not A occurs before A, then let A = true
slide-16
SLIDE 16

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

16

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

3SAT formulas

  • 2SAT is easy
  • Let’s try to find an algorithm for 3SAT
  • Consider a 3SAT formula (l1 ∨ l2 ∨ l3) ∧ · · ·
  • Build a graph G with a vertex for each occurrence
  • f a literal
  • Add an edge between two literals if they are in

different clauses, and they are not complementary

slide-17
SLIDE 17

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

17

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Conflict graph for 3SAT x ¬x ¬y x ¬y y C1 C1 C2 C2 C3 C3 (x ∨ y) ∧ (¬x ∨ ¬y) ∧ (x ∨ ¬y)

slide-18
SLIDE 18

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

18

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Cliques and satisfiablility

Definition A k-clique of graph G is a subgraph with k vertices that is full-connected. Claim For any 3SAT formula B with k clauses, the con- flict graph G has a k-clique iff B is satisfiable.

slide-19
SLIDE 19

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

19

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

SAT/clique example x ¬x ¬y x ¬y y C1 C1 C2 C2 C3 C3 (x ∨ y) ∧ (¬x ∨ ¬y) ∧ (x ∨ ¬y) Satisfying assignment: (Both cases): (x ∧ ¬y)

slide-20
SLIDE 20

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

20

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Satisfiable iff k-clique

Claim For any 3SAT formula B with k clauses, the con- flict graph G has a k-clique iff B is satisfiable. ⇐ Suppose B is satisfiable.

  • At least one literal in each clause must be as-

signed to ⊤.

  • Choose one lieral from each clause.
  • The literals are all connected, so the graph

hasa k-clique.

slide-21
SLIDE 21

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

21

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Converse

⇒ Suppose G has a k-clique.

  • It must have one vertex in each clause, since

there are no edges within a clause.

  • Assign each vertex to ⊤
  • This sets one literal to ⊤ in each clause, so

this is a satisfying assignment.

slide-22
SLIDE 22

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

22

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Reductions

  • We just reduced the SAT problem to the clique-

finding problem

  • If we have an efficient algorithm to find k-cliques,

then we have an efficient algorithm for SAT

– Well, we don’t know of efficient algorithms for either…

slide-23
SLIDE 23

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

23

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Reductions

Definition Let A ⊆ Σ∗ and B ⊆ Γ ∗ be decision problems. We write A ≤p

m B, and say A reduces to B in polynomial

time if there is a function σ : Σ∗ → Γ ∗ such that

  • σ is computable by a deterministic TM in poly

time,

  • For all x ∈ Σ∗,

x ∈ A iff σ(x) ∈ B

  • We say A ≡p

m B if A ≤p m B and B ≤p m A

slide-24
SLIDE 24

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

24

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Hardness

Theorem If A ≤p

m B, and B has a poly-time algorithm,

then so does A Proof Given an instance x of problem A, compute σ(x) and determine if σ(x) ∈ B.

  • Note that the size of σ(x) is at most polynomial

in the size of x (since σ(x) runs in poly-time)

  • The composition of two polynomials is still poly-

nomial.

slide-25
SLIDE 25

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

25

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

More NP problems: Independent set

Definition An independent set in an undirected graph G = (V, E) is a subset U ⊆ V such that U2 ∩ E = {} (no two vertices of U are connected by an edge in E). The problem is, given a graph G = (V, E) and k ≥ 0, determine if G has an independent set with k or more vertices. Reduction k-Independent-set ≡p

m k-Clique

  • Build the complement G = (V, E)

E = {(u, v) | u ≠ v ∧ (u, v) ∉ E}

  • The G has a k-clique iff G has an independent set
  • f size k.
slide-26
SLIDE 26

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

26

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

More NP problems: graph coloring

Definition Consider an undirected graph G = (V, E), and a finite set C. A coloring is a map ϕ : V → C such that ϕ(u) ≠ ϕ(v) for (u, v) ∈ E. Given G and a con- stant k, the k-coloring problem is to determine whether there exists a coloring using no more than k colors. Reduction We show that coloring is hard by showing SAT ≤p

m 3-colorability.

  • Use the colors R, G, B.
  • Build a triangle for each literal.
slide-27
SLIDE 27

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

27

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Graph coloring triangle

  • Add a fully-connected

triangle

– Call the vertices R, G, B

G B R

slide-28
SLIDE 28

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

28

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Literal triangle

  • Add a triangle for each

literal

– Upper node is the B node from the 3-color triangle

x ¬x

B

slide-29
SLIDE 29

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

29

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Clause graph

  • For each clause, add a graph of the following

form

– The G nodes are all the same vertex from the RGB triangle

¬x y ¬z w

G G G G G G

slide-30
SLIDE 30

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

30

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Coloring claim

  • The constructed graph is colorable iff one literal

can be colored green in each clause

– Intuitively, green represents true – Red represents false

slide-31
SLIDE 31

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

31

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Unsatisfiable clause

¬x y ¬z w Conflict Suppose top row is entirely red

G G G G G G

slide-32
SLIDE 32

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

32

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Satisfiable clause

¬x y ¬z w Suppose at least one is green

G G G G G G

slide-33
SLIDE 33

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

33

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Some additional NP problems

Knapsack problem Given a finite set S, integer weight functions w : S → N, benefit function b : S → N, weight limit W ∈ N, and desired benefit B ∈ N, determine whe- hter there is a subset S′ ⊆ S such that

  • a∈S′

w(a) ≤ W

  • a∈S′

b(a) ≥ B

slide-34
SLIDE 34

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

34

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Subset Sum

Subset sum Given a finite set S, integer weight function w : S → N, and target B : N, does there exist a subset S′ ⊆ S such that

  • a∈S′

w(a) = B Partition Given a finite set S and integer weight function w : S → N, does there exist a subset S′ ⊆ S such that

  • a∈S′

w(a) =

  • a∈S−S′

w(a)

slide-35
SLIDE 35

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

35

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

Reductions

  • Partition → SubsetSum by taking B = 1

2

  • a∈S w(a)
  • SubsetSum → Partition

– Introduce two new elements of weight N − B and weight N − (Σ − B), where N is large Σ =

  • a∈S

w(a) – Ask whether the new set can be partitioned into two sets of equal weight.

  • Partition → Knapsack take b = w and W = B =

1 2Σ

slide-36
SLIDE 36

Computation, Computers, and Programs Recursive functions http://www.cs.caltech.edu/courses/cs20/a/ November 25, 2002

36

C A L I F O R N I A I N S T I T U T E O F T E C H N O I L O G Y

If this page displays slowly, try turning off the “smooth line art” option in Acrobat, under Edit->Preferences

NP reductions

  • Reductions are a fundamental tool

– If A reduces to B, then if B can be solved efficiently, so can A

  • Next up

– Graph theory – NP problems in graph theory – Ptime reductions – NP-completeness