Can we solve it efficiently? • We have seen that there is a distinction between problems that can be solved by a computer CS 3813: Introduction to Formal algorithm and those that cannot Languages and Automata • Among the class of problems that can be solved by a computer algorithm, there is also a distinction between problems that can be solved The Classes P and NP efficiently and those that cannot • To understand this distinction, we need a mathematical definition of what it means for an algorithm to be efficient. Worst-case time complexity Time and space complexity • The running time of a standard one-tape and one- • Theoretical computer scientists have considered head DTM is the number of steps it carries out on both time and space complexity, in considering input w from the initial configuration to a halting whether a problem can be solved efficiently. configuration • For a Turing machine, time complexity is the • Running time typically increases with the size of number of computational steps required to solve the input. We can characterize the relationship a problem. Space complexity is the amount of between running time and input size by a function tape required to solve the problem. • Both time and space complexity are important, • The worst-case running time (or time complexity) of a TM is a function f:N → N where f(n) is the but we will focus on the question of time complexity. maximum number of steps it uses on any input of length n. Problem complexity Asymptotic analysis • In addition to analyzing the time complexity of • Because exact running time is often a complex Turing machines (or algorithms), we want to expression, we usually estimate it. analyze the time complexity of problems . • Asymptotic analysis is a way of estimating the • We establish in upper bound on the complexity running time of an algorithm on large inputs by of a problem by describing a Turing machine that considering only the highest-order term of the solves it and analyzing its worst-case complexity. expression and disregarding its coefficient. • For example, the function f(n) = 6n 3 + 2n 2 + 20n • But this is only an upper bound. Someone may come up with a faster algorithm. + 45 is aymptotically at most n 3 . • In the next class, we will show how to establish a • Using big-Oh notation, we say that f(n) = O(n 3 ). lower bound on the complexity of a problem. • We will use this notation to describe the • In the rest of this class, we consider how to complexity of algorithms as a function of the establish upper bounds. size of their input. 1
P is the same for every model of computation The class P • An important fact is that all deterministic • A Turing machine is said to be polynomially models of computation are polynomially bounded if its running time is bounded by a equivalent. That is, any one of them can polynomial function p(x), where x is the size of simulate another with only a polynomial the input. increase in running time. • A language is called polynomially decidable if • The class P does not change if the Turing there exists a polynomially bounded Turing machine has multiple tapes, multiple heads, etc., machine that decides it. or if we use any other deterministic model of computation. • The class P is the set of all languages that are • A different model of computation may increase polynomially decidable by a deterministic efficiency, but only by a polynomial factor. Turing machine. The class NP Examples of problems in P • The class NP is the set of all languages that are polynomially decidable by a nondeterministic • Recognizing any regular or context-free Turing machine. language. • We can think of a nondeterministic algorithm as acting in two phases: • Testing whether there is a path between – guess a solution (called a certificate ) from a points two points a and b in a graph. finite number of possibilities • Sorting and most other problems considered – test whether it indeed solves the problem in a course on algorithms • The algorithm for the second phase is called a verification algorithm and must take polynomial time. Optimization problems and languages Problems in NP that may not be in P • These examples are optimization problems. • Traveling salesman problem Aren’t P and NP classes of languages? • Hamiltonian cycle problem • We can convert an optimization problem into a • Clique problem language by considering a related decision • Subset sum problem problem, such as: Is there a solution of length less than k? • Boolean satisfiability problem • The decision problem can be reduced to the • Many, many others optimization problem, in this sense: if we can • Exercise: For these five problems, show that solve the optimization problem, we can also each is in the class NP by describing a solve the decision problem. nondeterministic algorithm that solves it in • The optimization problem is at least as hard as polynomial time the decision problem. 2
Hamiltonian cycle problem Traveling salesman problem • A somewhat simplified version of the traveling • Given a weighted, fully-connects undirected salesman problem graph and a starting vertex v 0 , find a • Given an undirected graph (that has no weights), minimal-cost path that begins and ends at v 0 a Hamiltonian cycle is a path that begins and and visits every vertex of the graph. ends at vertex v 0 and visits every other vertex in the graph. • Think of the vertices as cities, arcs between • The Hamiltonian cycle problem is the problem vertices as roads, and the weights on each of determining whether a graph contains a arc as the distance of the road. Hamiltonian cycle Clique problem Subset sum problem • In an undirected graph, a clique is a subset • Given a set of integers and a number t of vertices that are all connected to each (called the target), determine whether a other. The size of a clique is the number of subset of these integers adds up to t. vertices in it. • The clique problem is the problem of finding the maximum-size clique in an undirected graph. Boolean satisfiability problem Boolean satisfiability (continued) • A clause is composed of Boolean variables • A set of values for the variables x1, x2, x3, x1, x2, x3, … and operators or and not . … is called a satisfying assigment if it causes the formula to evaluate to true. • Example: x1 or x2 or not x3 • The satisfiability problem is to determine • A Boolean formula in conjunctive normal whether a Boolean formula is satisfiable. form is a sequence of clauses in parentheses connected by the operator and . • Example: ( not x1 or x2) and (x3 or not x2) 3
Recommend
More recommend