Example: Fib(N) = Fib(N-1) + Fib(N-2), Fib(1) = 0, Fib(2) = 1

Similar documents
Asymptotic Running Time of Algorithms

Computational Complexity

P, NP, NP-Complete, and NPhard

csci 210: Data Structures Program Analysis

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Unit 1A: Computational Complexity

csci 210: Data Structures Program Analysis

Data Structures and Algorithms

Computational Complexity and Intractability: An Introduction to the Theory of NP. Chapter 9

Algorithms. Grad Refresher Course 2011 University of British Columbia. Ron Maharik

Computational Complexity

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Outline. 1 NP-Completeness Theory. 2 Limitation of Computation. 3 Examples. 4 Decision Problems. 5 Verification Algorithm

NP Completeness and Approximation Algorithms

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Essential facts about NP-completeness:

CSCI3390-Lecture 18: Why is the P =?NP Problem Such a Big Deal?

Design and Analysis of Algorithms

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

Easy Problems vs. Hard Problems. CSE 421 Introduction to Algorithms Winter Is P a good definition of efficient? The class P

ICS 252 Introduction to Computer Design

Turing Machines and Time Complexity

NP Complete Problems. COMP 215 Lecture 20

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

CS/COE

Lecture 20: conp and Friends, Oracles in Complexity Theory

3. Algorithms. What matters? How fast do we solve the problem? How much computer resource do we need?

Topic 17. Analysis of Algorithms

Summer School on Introduction to Algorithms and Optimization Techniques July 4-12, 2017 Organized by ACMU, ISI and IEEE CEDA.

CSCI3390-Lecture 17: A sampler of NP-complete problems

Computational complexity theory

NP-Complete Problems. More reductions

CSE 105 THEORY OF COMPUTATION

Introduction to Complexity Theory

Principles of Algorithm Analysis

Admin NP-COMPLETE PROBLEMS. Run-time analysis. Tractable vs. intractable problems 5/2/13. What is a tractable problem?

Algorithms. NP -Complete Problems. Dong Kyue Kim Hanyang University

NP-Completeness. Algorithmique Fall semester 2011/12

NP-Completeness. Andreas Klappenecker. [based on slides by Prof. Welch]

} } } Lecture 23: Computational Complexity. Lecture Overview. Definitions: EXP R. uncomputable/ undecidable P C EXP C R = = Examples

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

SAT, Coloring, Hamiltonian Cycle, TSP

Algorithms and Theory of Computation. Lecture 22: NP-Completeness (2)

There are two types of problems:

Big-O Notation and Complexity Analysis

Computational complexity theory

CSE 3500 Algorithms and Complexity Fall 2016 Lecture 25: November 29, 2016

NP-Completeness Theory

6.045: Automata, Computability, and Complexity (GITCS) Class 15 Nancy Lynch

Principles of Computing, Carnegie Mellon University. The Limits of Computing

Tractable & Intractable Problems

Asymptotic Notation. such that t(n) cf(n) for all n n 0. for some positive real constant c and integer threshold n 0

Copyright 2000, Kevin Wayne 1

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Algorithms: COMP3121/3821/9101/9801

Advanced topic: Space complexity

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

NP-Completeness. Until now we have been designing algorithms for specific problems

CSCE 222 Discrete Structures for Computing

CS173 Running Time and Big-O. Tandy Warnow

CS 5114: Theory of Algorithms

Limitations of Algorithm Power

Algorithms and Theory of Computation. Lecture 19: Class P and NP, Reduction

Some Algebra Problems (Algorithmic) CSE 417 Introduction to Algorithms Winter Some Problems. A Brief History of Ideas

Programming Abstractions

COMPUTATIONAL COMPLEXITY

Solving Recurrences. Lecture 23 CS2110 Fall 2011

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Math 391: Midterm 1.0 Spring 2016

Algorithms and Their Complexity

What is Performance Analysis?

CS 310 Advanced Data Structures and Algorithms

Spring Lecture 21 NP-Complete Problems

CS Fall 2011 P and NP Carola Wenk

R ij = 2. Using all of these facts together, you can solve problem number 9.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

NP-completeness. Chapter 34. Sergey Bereg

Polynomial-time reductions. We have seen several reductions:

NP-Complete and Non-Computable Problems. COMP385 Dr. Ken Williams

CHAPTER 3 FUNDAMENTALS OF COMPUTATIONAL COMPLEXITY. E. Amaldi Foundations of Operations Research Politecnico di Milano 1

NP-Completeness. NP-Completeness 1

Growth of Functions. As an example for an estimate of computation time, let us consider the sequential search algorithm.

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

P,NP, NP-Hard and NP-Complete

Theory of Computation Chapter 1: Introduction

Lecture 18: More NP-Complete Problems

Problem-Solving via Search Lecture 3

NP-complete Problems

TIME COMPLEXITY AND POLYNOMIAL TIME; NON DETERMINISTIC TURING MACHINES AND NP. THURSDAY Mar 20

Algorithm. Executing the Max algorithm. Algorithm and Growth of Functions Benchaporn Jantarakongkul. (algorithm) ก ก. : ก {a i }=a 1,,a n a i N,

CMSC 441: Algorithms. NP Completeness

CISC 4090 Theory of Computation

P is the class of problems for which there are algorithms that solve the problem in time O(n k ) for some constant k.

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

SAT, NP, NP-Completeness

Analysis of Algorithms. Unit 5 - Intractable Problems

Lecture 1 - Preliminaries

P P P NP-Hard: L is NP-hard if for all L NP, L L. Thus, if we could solve L in polynomial. Cook's Theorem and Reductions

Cpt S 223. School of EECS, WSU

Transcription:

Algorithm Analysis Readings: Chapter 1.6-1.7. How can we determine if we have an efficient algorithm? Criteria: Does it meet specification/work correctly? Is it understandable/maintainable/simple? How much storage (memory & disk) does it use? How much time does it take to execute? Example: Fib(N) = Fib(N-1) + Fib(N-2), Fib(1) = 0, Fib(2) = 1 FibSolve(N) { If (N==1) return 0; if (N==2) return 1; return FibSolve(N-1)+FibSolve(N-2); 15 Performance Measurement The amount of time a program takes to execute depends on: Algorithm Computer Compiler Input size Input organization Approach For representative inputs { Start timer Run algorithm (multiple times?) Stop timer What about different computers, compilers, etc.? 16

Complexity Analysis Time complexity of an algorithm: amount of work done by an algorithm as a function of the size of the input Count all operations ("operation" = semantically meaningful program segment whose runtime independent of input or output size/complexity) Operations: Integer addition for (i=1; i < 100; i++) { A[i]=0; Not Operations: for (i=1; i < INPUT_VAL; i++) { A[i]=0; count(list); Result: Compiler & Computer-independent metrics 17 Example - Sequential Search for (i=1; i <= N; i++) { if (A[i]==search_element) return TRUE; return FALSE; Assume Q=Probability element isn t in the list Worst-case: Average-case: Note: we typically focus primarily on worst-case. 18

Constants in Complexity Question: How meaningful is the exact number of steps when individual steps can take different amounts of time? Since duration of operations is variable, we ignore constant factors, and just consider orders of magnitude. For large enough problems, constant factors irrelevant for comparisons Example: f(n) = 3n 2 g(n) = 25n -> g(n) < f(n) n>8 f(n) = 3n 2 g(n) = 300n -> g(n) < f(n) n>100 19 Asymptotic Complexity "Big-Oh": C 1 *f(n) = O(f(n)) f(n) + C 1 = O(f(n)) ith degree polynomials C i N i +C i-1 N i-1 +..C 1 N+C 0 = O(N i ) Lg(N) is greater than a constant, but less than N N + lg(n) -> O(N) N + Nlg(N) -> O(Nlg(N)) N 2 + Nlg(N) -> O(N 2 ) Note: "Big-Oh" is normally worst-case. 20

Example Asymptotic Complexity: Bubble Sort BubbleSort(array A, length N) { int j, k, temp; for (j = 1; j <= N; j++) { for (k = 1; k < N; k++) { if (A[k] > A[k+1]) { temp = A[k]; A[k] = A[k+1]; A[k+1] = temp; 21 Example Asymptotic Complexity: Binary Search Search for an element present in a sorted list A[0]..A[N], A[i] < A[i+1] int Search(A) { if (A[0] == search_element) return 0; if (A[N] == search_element) return N; low = 0; high = N; while (low < high -1) { middle = (low+high)/2; if (A[middle] < search_element) low = middle; else if (A[middle] > search_element) high = middle; else return middle; /* Found the location */ return ERROR; /* Element should be in the list */ In each step, half of list is eliminated from consideration. N steps can handle a list of ~2 N length. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 22

Example Asymptotic Complexity: Searching lists Algorithm 1: for (i=1; i <= N; i++) { if (A[i]==search_element) return TRUE; return FALSE; Algorithm 2: BubbleSort(array, N); BinarySearch(array); Algorithm 3: UltraSort(array, N) //assume UltraSort is O(lgN) BinarySearch(array); 23 Practical Complexity Assume 1GHz machine (1 billion instructions per second) n log 2 n n nlog 2 n n 2 n 3 n 4 n 10 2 n n! 10 0.003 us 0.01 us 0.033 us 0.1 us 1. us 10. us 10. sec 1.02 us 3.63 ms 20 0.004 us 0.02 us 0.086 us 0.4 us 8. us 160. us 2.84 hr 1.05 ms 77.1 yr 30 0.005 us 0.03 us 0.15 us 0.9 us 27. us 810. us 6.83 day 1.07 sec 8.41E+15 yr 40 0.005 us 0.04 us 0.213 us 1.6 us 64. us 2.56 ms 121 day 18.3 min 2.59E+31 yr 50 0.006 us 0.05 us 0.282 us 2.5 us 125. us 6.25 ms 3.10 yr 13.0 day 9.64E+47 yr 100 0.007 us 0.1 us 0.664 us 10. us 1. ms 100. ms 3.17E+03 yr 4.02E+13 yr 2.96E+141 yr 1k 0.01 us 1. us 9.97 us 1. ms 1. sec 16.7 min 3.17E+13 yr 3.40E+284 yr 10k 0.013 us 10. us 133 us 100. ms 16.7 min 116 day 3.17E+23 yr 100k 0.017 us 100. us 1.66 ms 10. sec 11.6 day 3170 yr 3.17E+33 yr us = microsecond (10-6 ), ms = millisecond (10-3 ) Given twice as much time, how much can you do: O(logn): n 2, O(n): 2n, O(n 2 ): n*2 1/2, O(n 3 ): n*2 1/3, O(2 n ): n+1 24

Practical Complexity (cont.) 60 n! 2^n 50 n^2 Time 40 30 nlogn 20 10 0 n logn 0 2 4 6 8 10 N 25 Problem Complexity Do all problems have efficient solutions? When should I apply heuristics (rules of thumb) instead of looking for an exact algorithm? What the heck are P, NP, and NP-complete? P: algorithms that can be solved efficiently NP: algorithms that probably can be solved heuristically NP-complete/NP-hard: Problems that almost definitely cannot be solved both exactly & efficiently. 26

P = Polynomial Time Algorithms Never met a problem I couldn t solve P is the class of problems for which there is a polynomial time solution O(n C ) for some constant C Sort is O(nlog 2 n) Search is O(log 2 n) for sorted lists, O(n) for unsorted lists Fibonacci is O(n) Note: O(n 57 ) may not be efficient, but most problems in P are relatively efficiently solved 27 NP = Nondeterministic Polynomial Algorithms I ll know it when I see it Assume you are given a devious Oracle or Genie Knows the answer to all problems Will lie to you unless you thoroughly check its work Some similarity to Quantum Computing Any problem whose answer can be checked in polynomial time is in NP Why NP? Many interesting problems are easy to check, but hard to compute Some heuristic techniques (Simulated Annealing, Simulated Evolution, etc.) can help guess the solution given a check on correctness. 28

NP Example: Travelling Salesman Problem A salesman must visit a set of N cities in any order. Given current airline rates, find a set of flights that visits all of these cities for less than X dollars. Seattle San Francisco Denver Chicago New York D.C. Boston Los Angeles Dallas New Orleans Polynomial Algorithm? NP Algorithm? Heuristic? Miami 29 P vs. NP Are all algorithms in P also in NP? NP algorithm for a P problem: Don t use oracle, just create a solution. Are any algorithms in NP not in P? If you can prove it, there s a Full Professorship at M.I.T. waiting for you. What do we know? It is likely that P NP There are NP-Complete problems which are the least likely to be in P NP-Complete NP NP-hard: a problem which, if it were in P would mean all problems in NP are in P P NP-complete: an NP-hard problem which is in NP 30

NP-Complete A problem is NP-Complete if: It is in NP. It is NP-hard - The creation of a polynomial-time algorithm for it provides a polynomial-time algorithm for all problems in NP. The first NP-Complete algorithm: Boolean Satisfiability (SAT) Is there an assignment of values to variables that makes a given Boolean Equation true? NP NP P In NP: Take the solution from the Oracle and check that the Boolean Equation becomes true (Can be done in O(n)). Is NP-hard: We can show that any problem in NP can be converted into a Boolean equation in polynomial time. If there was a polynomial time solution to SAT, we would thus have a polynomial time solution to all NP problems. Details in CSE 431 or CSE 531. 31 NP-Hard Given that we have at least one NP-Complete problem, showing others are NPhard becomes easier: For a problem under consideration X 1.) Take any NP-complete problem Y (pick wisely!) 2.) Show that there is a polynomial time method for changing any instance of problem Y into an instance of problem X Now, a polynomial solution to X means there is a polynomial solution to Y. A polynomial solution to Y implies a polynomial solution to all of NP. X Y Complete NP P 32

Example NP-Completeness Proof Vertex Cover: Given an undirected graph G(V, E), where V are the vertices and E are the edges, and a size S. Find a set of vertices C, C V, such that for every edge at least one of its endpoints is in C, and C has at most S elements. A E B D C This graph has a cover of 3 vertices ({A,C,E and others), but not of 2. 33 Example NP-Completeness Proof (cont.) Vertex cover is in NP: Vertex cover is NP-hard: Pick the NP-complete problem 3-SAT to reduce to vertex cover 3-SAT: Given a Boolean equation in Product of Sums form, where each Sum term has 3 variables, find an assignment of values to variables that makes the equation true. F ( A B C) * ( A C C) * ( A B B) * ( A B C ) Note: 3-SAT was shown to be NP-Complete because any SAT equation can be converted efficiently to 3-SAT form. 34

Example NP-Completeness Proof (cont.) Mapping 3-SAT to Vertex cover: Each literal becomes a vertex. If the vertex ISN T in the cover, it is true. Must ensure: 1.) At least one literal per term is true/uncircled 2.) Both X and not(x) can t both be true B C B B A C A C A B A C F ( A B C) * ( A C C) *( A B B) * ( A B C ) 35 Why this Works (Intuitive) We can map Y to X in poly time: Y X (Y is no harder than X, or X is no easier than Y) Y is NP-hard: Z Y Forall Z in NP Thus, X is NP-hard: Z Y X Forall Z in NP Z X Forall Z in NP 36

Why This Works (Mathematical) X is NP-Hard because we show that there is a polynomial time method for changing any instance of problem Y into an instance of problem X, and Y is NP- Hard NP-Hard means that if the problem is in P, then all NP problems are in P. Why is X NP-Hard? What happens if X is in P? Algorithm for Y: Convert problem to an instance of problem X Poly Time Solve problem X Poly Time Thus, if X is in P, Y is in P. Y is NP-hard, which means if Y is in P, then all NP problems are in P Thus, if X is in P, all NP problems are in P. Therefore, X is NP-hard. 37 How Not to Show NP-Hardness Common mistake: show that the problem can be transformed to an NPcomplete problem. Proves only that the problem is in NP, not that it is NPcomplete. X is NP-hard + Y can be mapped to X: Z X Forall Z in NP, and Y X, does not mean Z Y Example - transform 1-SAT to 3-SAT: Complete X NP Y P Problem: Just because 3-SAT is capable of solving 1-SAT, doesn t mean 1-SAT is as hard as 3-SAT. In fact 1-SAT is in P, 3-SAT is NP-Complete 38

Some NP-Complete Problems Travelling Salesman Problem: Find the cheapest path that visits all vertices in a graph. 2-level Logic Minimization: Find the simplest Sum-of-Products implementation for a Boolean Equation. Integer Linear Programming: Given an mxn matrix of integers A and a column vector B of n integers, does there exist a column vector of integers X such that A*X B? Graph Coloring: Given a graph and an integer K, can the graph be colored with K colors so that no two adjacent vertices have the same color? Clique: Does the specified graph have a clique of K vertices, where each of these vertices is adjacent to all other members of the clique? Numerous other NP-hard problems exist, including most of Physical Design! 39 Who Cares About NP-Hardness? NP-Hardness means that an efficient, exact algorithm is unlikely Heuristic algorithms are justified Some problems believed hard aren t, and you re scooped with exact algorithm Routing two-terminal nets in a crossbar system 40

Heuristic Algorithms When a problem proves to be too complex to solve exactly, we instead come up with a heuristic algorithm. Heuristics = rules of thumb, ways of getting reasonable solutions, but which may: 1.) Not get the exact answer 2.) Not get an answer for some problems Examples: Think of heuristics for the following problems: Find the minimum coloring for a graph. The travelling salesman problem. CAD example Espresso for 2-level logic minimization 41