Time Complexity of an Algorithm

Similar documents
14.1 Encoding for different models of computation

Theory and Frontiers of Computer Science. Fall 2013 Carola Wenk

Material from Recitation 1

From NP to P Musings on a Programming Contest Problem

Theory of Computations Spring 2016 Practice Final Exam Solutions

P and NP (Millenium problem)

Lambda Calculus and Computation

THEORY OF COMPUTATION

Chapter 8. NP-complete problems

Chapter 9 Graph Algorithms

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

Chapter 9 Graph Algorithms

Steven Skiena. skiena

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Lecture 5: The Halting Problem. Michael Beeson

Problems, Languages, Machines, Computability, Complexity

Question 7.11 Show how heapsort processes the input:

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

CSE 120. Computer Science Principles

W[1]-hardness. Dániel Marx. Recent Advances in Parameterized Complexity Tel Aviv, Israel, December 3, 2017

NP-Complete Problems

CSCI 270: Introduction to Algorithms and Theory of Computing Fall 2017 Prof: Leonard Adleman Scribe: Joseph Bebel

Algorithms and Data Structures

Size of a problem instance: Bigger instances take

Lecture 21: Other Reductions Steven Skiena

Lecture 15: Algorithms. AP Computer Science Principles

Computational problems. Lecture 2: Combinatorial search and optimisation problems. Computational problems. Examples. Example

Introduction to Graph Theory

More NP-complete Problems. CS255 Chris Pollett May 3, 2006.

ECE6095: CAD Algorithms. Optimization Techniques

Algorithms and Data Structures

Examples of P vs NP: More Problems

Data Structures and Algorithms

Computational Complexity and Implications for Security DRAFT Notes on Infeasible Computation for MA/CS 109 Leo Reyzin with the help of Nick Benes

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

W[1]-hardness. Dániel Marx 1. Hungarian Academy of Sciences (MTA SZTAKI) Budapest, Hungary

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

Module 6 P, NP, NP-Complete Problems and Approximation Algorithms

Best known solution time is Ω(V!) Check every permutation of vertices to see if there is a graph edge between adjacent vertices

Theory of Computations Spring 2016 Practice Final

CSE 373: Data Structures and Algorithms

4.1 Review - the DPLL procedure

Instant Insanity Instructor s Guide Make-it and Take-it Kit for AMTNYS 2006

Linear Programming Motivation

Introduction to the Theory of Computation, Sipser, PWS, ISBN X, 1996

Module 6 NP-Complete Problems and Heuristics

P and NP CISC5835, Algorithms for Big Data CIS, Fordham Univ. Instructor: X. Zhang

Recursively Enumerable Languages, Turing Machines, and Decidability

(Refer Slide Time: 1:27)

Parameterized Reductions

CSE 373: Data Structures and Algorithms

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

CSE 100: B+ TREE, 2-3 TREE, NP- COMPLETNESS

P vs. NP. Simpsons: Treehouse of Horror VI

Introduction to Algorithms

Theorem 2.9: nearest addition algorithm

Cardinality of Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Cardinality of Sets Fall / 15

Computer Sciences Department

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

CS240 Fall Mike Lam, Professor. Algorithm Analysis

8 NP-complete problem Hard problems: demo

Design and Analysis of Algorithms CS404/504. Razvan Bunescu School of EECS.

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

NP-Completeness. Algorithms

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

Recitation 4: Elimination algorithm, reconstituted graph, triangulation

NP-Hardness. We start by defining types of problem, and then move on to defining the polynomial-time reductions.

MATHEMATICAL STRUCTURES FOR COMPUTER SCIENCE

Greedy algorithms is another useful way for solving optimization problems.

Coping with the Limitations of Algorithm Power Exact Solution Strategies Backtracking Backtracking : A Scenario

1. [5 points each] True or False. If the question is currently open, write O or Open.

UNIT 1 ANALYSIS OF ALGORITHMS

Run Times. Efficiency Issues. Run Times cont d. More on O( ) notation

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

COMP 161 Lecture Notes 16 Analyzing Search and Sort

CSE 417 Branch & Bound (pt 4) Branch & Bound

The Role of Algorithms in Computing

Some Hardness Proofs

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Mathematics of Optimization Viewpoint of applied mathematics Algorithm

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

6.006 Introduction to Algorithms. Lecture 25: Complexity Prof. Erik Demaine

Lecture 20: Satisfiability Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Selection (deterministic & randomized): finding the median in linear time

P and NP CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Outline and Reading. Analysis of Algorithms 1

English for Computer Science

IN101: Algorithmic techniques Vladimir-Alexandru Paun ENSTA ParisTech

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Unit 8: Coping with NP-Completeness. Complexity classes Reducibility and NP-completeness proofs Coping with NP-complete problems. Y.-W.

Lecture 21: Other Reductions Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

p x i 1 i n x, y, z = 2 x 3 y 5 z

/ Approximation Algorithms Lecturer: Michael Dinitz Topic: Linear Programming Date: 2/24/15 Scribe: Runze Tang

Introduction to the Analysis of Algorithms. Algorithm

Chapter 12. Computability Mechanizing Reasoning

Module 6 NP-Complete Problems and Heuristics

Simple Graph. General Graph

2 The Fractional Chromatic Gap

Does this program exist? Limits of computability. Does this program exist? A helper function. For example, if the string program is

Transcription:

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 335] Time Complexity of an Algorithm Time complexity of an algorithm: the function T (n) that describes the (worst-case) running time as input size, n, increases. Given a particular algorithm, discover this function by attacking the problem from two directions: find an upper bound U (n) on the function T (n), i.e., convince ourselves that the algorithm will never take more than U (n) time on any input of size n. find a lower bound L(n) on the function T (n), i.e., convince ourselves that, for each n, there is at least one input of size n on which the algorithm takes at least time L(n). Try to find smallest U and largest L, so that T is squeezed in between and has no room to hide.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 336] Time Complexity of an Algorithm (cont d) U(n) (a) T(n) (b) (c) L(u) n (a) No execution on an input of size n 0 takes more time than this. (b) The slowest execution on all inputs of size n 0 takes exactly this much time. (c) At least one execution on an input of size n 0 takes at least this much time. n

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 337] Time Complexity of Heapsort Let T (n) be the time complexity of heapsort. First cut at upper bound: each heap operation never takes more than O(n) time. Thus T (n) is at most O(n 2 ). First cut at lower bound: each heap operation always takes at least O(1) time. Thus T (n) is at least O(n). Refined argument for upper bound: each heap operation never takes more than O(log n) time. Thus T (n) is at most O(n log n). Refined argument for lower bound: Describe a particular input that causes running time of at least O(n log n). On input n; n, 1;n,2;:::;3;2;1, running time is at least O(log 1 + log 2 + :::+ log n) =O(nlog n). Thus T (n) now precisely identified as O(n log n) (to within constant factors).

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 338] Time Complexity of a Problem Time complexity of a problem: the time complexity for the fastest possible algorithm for the problem. To show that a problem has time complexity T (n): Identify a specific algorithm for the problem with time complexity T (n). Then prove that any algorithm for the problem has time complexity at least T (n). Example: Sorting problem has time complexity O(n log n). Heapsort has time complexity O(n log n). It can be proved that no (comparison-based) sorting algorithm can have better time complexity. Problems can be classified by their time complexity. Harder problems are considered to be those with larger time complexity.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 339] The Class P All problems (not algorithms) whose time complexity is at most some polynomial are said to be in the class P (P for polynomial). Example: Sorting is in P, since O(n log n) is less than O(n 2 ). Not all problems are in P. Example: Consider the problem of listing all permutations of the integers 1 through n. Output size is n!. Thus running time is at least O(n!). n! is larger than 2 n, thus larger than any polynomial.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 340] NP-Complete Problems There is an important class of problems that might or might not be in P nobody knows! These problems are called NP-complete. These problems have the following characteristic: A candidate solution can be verified in polynomial time as being a real solution or not. However, there are an exponential number of candidate solutions. Many real-world problems in science, math, engineering, operations research, etc. are NP-complete.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 341] Traveling Salesman Problem An example NP-complete problem is the traveling salesman problem: Given a set of cities and the distances between them, determine an order in which to visit all the cities that does not exceed the salesman s allowed mileage. A candidate solution for TSP is a particular listing of the cities. To check whether the allowed mileage is exceeded, add up the distances between adjacent cities in the listing, which will take time linear in the number of cities. But the total number of different candidate solutions is n!, so it s not feasible to check them all.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 342] Pvs.NP Imagine an (unrealistically) powerful model of computation in which the computer first makes a lucky guess (a nondeterministic choice) as to a candidate solution in constant time, and then behaves as an ordinary computer and verifies the solution. Problems solvable on this computer in polynomial time are in the class NP (nondeterministic polynomial). NP includes all the NP-complete problems. Having polynomial running time on this funny computer would not seem to ensure polynomial running time on a real computer. That is, it seems likely that NP is a strictly larger class of problems than P, and that the NP-complete problems cannot be solved in polynomial time. But no one has yet been able to prove P 6= NP. Outstanding open question in CS since the 1970 s.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 343] Computability Theory Complexity theory focuses on how expensive it is to solve various problems. Computability theory focuses on which problems are solvable at all by a computer (i.e., with an algorithm), regardless of how expensive a solution might be. We will focus on computing (mathematical) functions, with inputs and outputs. We would like to know if there exist functions that are so complicated that no algorithm can compute them.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 344] Church-Turing Thesis First, we have to decide what constitutes an algorithm. Assembly languages have restricted sets of primitives. High-level languages have a wider choice of primitives. What s to say you couldn t have some language with very powerful primitives? Church-Turing thesis: ( thesis means conjecture ) Anything that can reasonably be considered an algorithm can be represented as a Turing machine. A Turing machine is a very abstract, yet low-level, model of computation. Every actual programming language is equivalent, in computational power, to the Turing machine model. Thus, for theoretical purposes, the choice of programming language is irrelevant.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 345] Computing Functions Some sample functions: f (n) = 3: very easy to compute, always return 3, no matter what the input is f (n) = 2n: easy to compute, since multiplication can be done with an algorithm f (n) = sin n: getting more complicated, especially with issues of precision There exist non-computable functions, functionswhose input/output relationships are so complicated that there is no well-defined, step-by-step process for determining the function s output based on its input value. We will assume your favorite programming language L, with a very powerful implementation, in which integers can be any length, and only consider programs in L that take a single integer input and produce a single integer output.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 346] Goedel Number of a Program Here is a way to convert a program into an integer. Convert all the characters in the program to their ASCII codes. Interpret the result as a (big) integer. Call this integer the Goedel number of the program. Conversely, any integer can be converted into a series of characters: Most of the time, the result is garbage. Sometimes it isn t garbage, but it isn t a legal program in language L. Rarely, it is a legal program in L. More rarely, the resulting program has a single integer input and single integer output. Use this numbering scheme to list all the programs in language L. The list is infinite.

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 347] An Uncomputable Function Define a function h called the halting problem: If the program with Goedel number n halts when its input is n, then h(n) =1. If the program with Goedel number n does not halt when its input is n, then h(n) =0. Theorem: h is uncomputable (has no program in the Goedel listing). Proof: Assume in contradiction that h is computable. Then some program H (in the Goedel listing) computes the function h. Define another program I (which will be in the listing): 1. n is the input 2. run program H as a subroutine on input n 3. let x be the output returned by H 4. if x =0then return 0 5. else go into an infinite loop

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 348] An Uncomputable Function (cont d) Let n I be the Goedel number of I. What does I do on input n I? Case 1: I halts on input n I. Then in Line 4, x = 0, i.e., the subroutine H returned 0, meaning that I does not halt on n I. Contradiction. Case 2: I does not halt on input n I. Then in Line 4, x = 1, i.e., the subroutine H returned 1, meaning that I does halt on n I. Contradiction. Thus the hypothetical program H cannot exist. 2 Another way to view this result is that there is only a countably infinite number of programs (algorithms), but there are uncountably many functions.