# MA/CSSE 474 Theory of Computation Time Complexity Classes

Post on 18-Jan-2016

214 views

Embed Size (px)

TRANSCRIPT

MA/CSSE 474 Theory of ComputationTime Complexity Classes

Time Complexity Classes

The Language Class PL P iff

there exists some deterministic Turing machine M that decides L, and

timereq(M) O(nk) for some k.

Well say that L is tractable iff it is in P.

Nondeterministic deciding:

L NP iff:

there is some NDTM M that decides L, and

timereq(M) O(nk) for some k. NDTM deciders:s,qabab

q2,#abab q1,qabab q1,qababq3,qbbabThe Language Class NP

Closure under Complement Theorem: The class P is closed under complement.

Proof:

Closure under Complement Theorem: The class P is closed under complement.

Proof: If M accepts L in polynomial time, swap accepting and non accepting states to accept L in polynomial time.

Defining Complement CONNECTED = { : G is an undirected graph and G is connected} is in P.

NOTCONNECTED = { : G is an undirected graph and G is not connected}.

CONNECTED = NOTCONNECTED {strings that are not syntactically legal descriptions of undirected graphs}.

CONNECTED is in P by the closure theorem. What aboutNOTCONNECTED?

If we can check for legal syntax in polynomial time, then we canconsider the universe of strings whose syntax is legal. Then we can conclude that NOTCONNECTED is in P if CONNECTED is.

Languages That Are in P Every regular language.

Every context-free language since there exist context-free parsing algorithms that run in O(n3) time.

Others:

AnBnCn

Nim

To Show That a Language Is In P Describe a one-tape, deterministic Turing machine.

It may use multiple tapes. Price:

State an algorithm that runs on a conventional computer. Price:

How long does it take to compare two strings? q a a a ; a a a q Bottom line: If ignoring polynomial factors, then just describe a deterministic algorithm.

Theorem: Every regular language can be decided in linear time. So every regular language is in P.

Proof: If L is regular, there exists some DFSM M that decides it. Construct a deterministic TM M that simulates M, moving its read/write head one square to the right at each step. When M reads a q, it halts. If it is in an accepting state, it accepts; otherwise it rejects.

On any input of length n, M will execute n + 2 steps.

So timereq(M) O(n).Regular Languages

Context-Free Languages Theorem: Every context-free language can bedecided in O(n18) time. So every context-freelanguage is in P.

Proof: The Cocke-Kasami-Younger (CKY) algorithmcan parse any context-free language in time that isO(n3) if we count operations on a conventionalcomputer. That algorithm can be simulated on astandard, one-tape Turing machine in O(n18) steps.

Graph Languages Represent a graph G = (V, E) as a list of edges:

101/1/11/11/10/10/100/100/101/11/101

1 3

245

Graph Languages

CONNECTED = { : G is an undirected graph and G is connected}.

Is CONNECTED in P?123456789

CONNECTED is in P connected() = 1. Set all vertices to be unmarked. 2. Mark vertex 1. 3. Initialize L to {1}. 4. Initialize marked-vertices-counter to 1. 5. Until L is empty do: 5.1. Remove the first element from L. Call it current-vertex. 5.2. For each edge e that has current-vertex as an endpoint do: Call the other endpoint of e next-vertex. If next-vertex is not already marked then do: Mark next-vertex. Add next-vertex to L. Increment marked-vertices-counter by 1. 6. If marked-vertices-counter = |V| accept. Else reject.

Analyzing connected Step 1 takes time that is O(|V|). Steps 2, 3, and 4 each take constant time. The loop of step 5 can be executed at most |V| times. Step 5.1 takes constant time. Step 5.2 can be executed at most |E| times. Each time, it requires at most O(|V|) time. Step 6 takes constant time.

So timereq(connected) is:

|V|O(|E|)O(|V|) = O(|V|2|E|).

But |E| |V|2. So timereq(connected) is:

O(|V|4).

Seven Bridges of Knigsberg:

Eulerian Paths and Circuits

Seven Bridges of Knigsberg:Eulerian Paths and Circuits As a graph:1342

EULERIAN-CIRCUIT An Eulerian path through a graph G is a path that traverses each edge in G exactly once.

An Eulerian circuit through a graph G is a path that starts at some vertex s, ends back in s, and traverses each edge in G exactly once.

Bridge inspectors, road cleaners, and network analysts can minimize their effort if they traverse their systems by following an Eulerian path.

EULERIAN-CIRCUIT Difference between: Eulerian circuit, and Hamiltonian circuit

EULERIAN-CIRCUIT = { : G is an undirected graph, and G contains an Eulerian circuit}

Is EULERIAN-CIRCUIT in P?

Define the degree of a vertex to be the number of edges with it as an endpoint.

Eulers Observation

Define the degree of a vertex to be the number of edges with it as an endpoint.

Euler observed that:

A connected graph possesses an Eulerian path that is not a circuit iff it contains exactly two vertices of odd degree. Those two vertices will serve as the first and last vertices of the path. A connected graph possess an Eulerian circuit iff all its vertices have even degree. Because each vertex has even degree, any path that enters it can also leave it without reusing an edge.Eulers Observation

Eulerian() = 1. If connected(G) rejects, reject. Else: 2. For each vertex v in G do: 2.1. Count the number of edges that have v as one endpoint but not both. 2.2. If the count is odd, exit the loop and reject. 3. If all counts are even, accept.

We have shown that connected runs in time that is polynomial in ||. The loop in step 2 is executed at most |V| times. Each time through, it requires time that is O(|E|). Step 3 takes constant time.

So the total time for steps 2 - 3 of Eulerian is |V|O(|E|). But |E| |V|2. So the for steps 2-3 of Eulerian is O(|V|3).EULERIAN-CIRCUIT

A spanning tree T of a graph G is a subset of the edges of G such that:

T contains no cycles and

Every vertex in G is connected to every other vertex using just the edges in T.

An unconnected graph has no spanning trees.

A connected graph G will have at least one spanning tree; it may have many.

Spanning Trees

A weighted graph is a graph that has a weight associated with each edge.

An unweighted graph is a graph that does not associate weights with its edges.

If G is a weighted graph, the cost of a tree is the sum of the costs (weights) of its edges.

A tree T is a minimum spanning tree of G iff:it is a spanning tree and there is no other spanning tree whose cost is lower than that of T. Minimum Spanning Trees

Minimum Spanning Trees Can we find an MST without searching all the possible trees?The cheapest way to lay cable that connects a set of points is along a minimum spanning tree that connects those points.

Kruskal(G : = (V, E) and connected) = 1. Sort the edges in E in ascending order by their cost. Break ties arbitrarily. 2. Initialize T to a forest with an empty set of edges. 3. Until all edges in E have been considered do: 3.1. Select e, the next edge in E. If the endpoints of e are not connected in T then add e to T. 4. Return T.

Why does this work?Kruskals Algorithm

MST = { : G is an undirected graph with a positive cost attached to each of its edges and thereexists a minimum spanning tree of G with total cost less than cost}.

Is MST in P?MST

Kruskal(G : = (V, E) and connected) = 1. Sort the edges in E in ascending order by their cost. Break ties arbitrarily. 2. Initialize T to a forest with an empty set of edges. 3. Until all edges in E have been considered do: 3.1. Select e, the next edge in E. If the endpoints of e are not connected in T then add e to T. 4. Return T.

Step 1, takes |E|log |E| comparisons and each comparison takes constant time. Step 2 takes constant time. The loop in step 3 takes O(|E||V|) time.

So timereq(Kruskal) O(|E||V|) or O(||2).

With a more efficient implementation of step 3, it is possible to show that it is also O(|E|log |V|).MST is in P

RELATIVELY-PRIME ={ : n and m are integers that are relatively prime}.

PRIMES = {w : w is the binary encoding of a prime number}

COMPOSITES = {w : w is the binary encoding of a nonprime number}Primality Testing

But Finding Factors Remains Hardhttp://xkcd.com/247/

TSP-DECIDE = { : encodes an undirected graph with a positive distance attached to each of its edges and G contains a Hamiltonian circuit whose total cost is less than }.

An NDTM to decide TSP-DECIDE:Returning to TSP 152025892340104732830

An NDTM to decide TSP-DECIDE:Returning to TSP 1520258923401047328301. For i = 1 to |V| do:Choose a vertex that hasnt yet been chosen.2. Check that the path defined by the chosen sequence of vertices is a Hamiltonian circuit through G with distance less than cost.

TSP-DECIDE, and other problems like it, share three properties:

1. The problem can be solved by searching through a space of partial solu