CSCE 222 Discrete Structures for Computing

Similar documents
Algorithms and Their Complexity

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

Discrete Mathematics CS October 17, 2006

COMP 182 Algorithmic Thinking. Algorithm Efficiency. Luay Nakhleh Computer Science Rice University

Analysis of Algorithms

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

3 The Fundamentals:Algorithms, the Integers, and Matrices

Algorithms 2/6/2018. Algorithms. Enough Mathematical Appetizers! Algorithm Examples. Algorithms. Algorithm Examples. Algorithm Examples

csci 210: Data Structures Program Analysis

Limitations of Algorithm Power

Mat Week 6. Fall Mat Week 6. Algorithms. Properties. Examples. Searching. Sorting. Time Complexity. Example. Properties.

Student Responsibilities Week 6. Mat Properties of Algorithms. 3.1 Algorithms. Finding the Maximum Value in a Finite Sequence Pseudocode

csci 210: Data Structures Program Analysis

Copyright 2000, Kevin Wayne 1

CISC 235: Topic 1. Complexity of Iterative Algorithms

With Question/Answer Animations

Decision Procedures. Jochen Hoenicke. Software Engineering Albert-Ludwigs-University Freiburg. Winter Term 2016/17

CS 350 Algorithms and Complexity

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

Analysis of Algorithm Efficiency. Dr. Yingwu Zhu

ECOM Discrete Mathematics

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

Analysis of Algorithms

Announcements. CompSci 102 Discrete Math for Computer Science. Chap. 3.1 Algorithms. Specifying Algorithms

Analysis of Algorithms [Reading: CLRS 2.2, 3] Laura Toma, csci2200, Bowdoin College

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

CS 350 Algorithms and Complexity

O Notation (Big Oh) We want to give an upper bound on the amount of time it takes to solve a problem.

Computational Complexity

Running Time Evaluation

Computer Science. Questions for discussion Part II. Computer Science COMPUTER SCIENCE. Section 4.2.

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

Principles of Algorithm Analysis

CSED233: Data Structures (2017F) Lecture4: Analysis of Algorithms

Growth of Functions (CLRS 2.3,3)

Analysis of Algorithms

Lecture 2. Fundamentals of the Analysis of Algorithm Efficiency

Introduction to Computer Science Lecture 5: Algorithms

Data Structures and Algorithms Chapter 2

P, NP, NP-Complete, and NPhard

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Divide and Conquer CPE 349. Theresa Migler-VonDollen

CS156: The Calculus of Computation

5. Program Correctness: Mechanics

Chapter 2. Reductions and NP. 2.1 Reductions Continued The Satisfiability Problem (SAT) SAT 3SAT. CS 573: Algorithms, Fall 2013 August 29, 2013

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

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

Turing Machines and Time Complexity

Theory of Computation

Topic 17. Analysis of Algorithms

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

The Complexity of Optimization Problems

Big O 2/14/13. Administrative. Does it terminate? David Kauchak cs302 Spring 2013

CSCI 3110 Assignment 6 Solutions

Algorithms Test 1. Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) {

Data Structures and Algorithms Running time and growth functions January 18, 2018

Big-O Notation and Complexity Analysis

CSCI3390-Lecture 14: The class NP

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

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Space Complexity. The space complexity of a program is how much memory it uses.

The Time Complexity of an Algorithm

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

Fundamentals of Programming. Efficiency of algorithms November 5, 2017

Problem-Solving via Search Lecture 3

CSC236 Week 3. Larry Zhang

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

COMP Analysis of Algorithms & Data Structures

Divide and Conquer Problem Solving Method

Ch 01. Analysis of Algorithms

Fundamental Algorithms

Fundamental Algorithms

Theory of Computation. Theory of Computation

Lecture 12: Algorithm Analysis

CS 4407 Algorithms Lecture 2: Iterative and Divide and Conquer Algorithms

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

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

COMP Analysis of Algorithms & Data Structures

2. ALGORITHM ANALYSIS

Central Algorithmic Techniques. Iterative Algorithms

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

The Time Complexity of an Algorithm

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

Solutions. Problem 1: Suppose a polynomial in n of degree d has the form

The Beauty and Joy of Computing

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Algorithm runtime analysis and computational tractability

Running Time. Overview. Case Study: Sorting. Sorting problem: Analysis of algorithms: framework for comparing algorithms and predicting performance.

Ch01. Analysis of Algorithms

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

Chapter 2: The Basics. slides 2017, David Doty ECS 220: Theory of Computation based on The Nature of Computation by Moore and Mertens

20.1 2SAT. CS125 Lecture 20 Fall 2016

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Outline. Part 5. Computa0onal Complexity (3) Compound Interest 2/15/12. Recurrence Rela0ons: An Overview. Recurrence Rela0ons: Formal Defini0on

The P-vs-NP problem. Andrés E. Caicedo. September 10, 2011

Asymptotic Analysis Cont'd

SAT, NP, NP-Completeness

Reminder of Asymptotic Notation. Inf 2B: Asymptotic notation and Algorithms. Asymptotic notation for Running-time

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

Transcription:

CSCE 222 Discrete Structures for Computing Algorithms Dr. Philip C. Ritchey

Introduction An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem. Searching Sorting Optimizing Etc.

Example Describe an algorithm for finding the maximum value in a list (finite sequence) of integers. Solution Set the temporary maximum to the first element of the list. For each remaining element in the list, compare it to the temporary maximum. If it is larger, set the temporary maximum to this integer. Return the temporary maximum as the answer.

Psuedocode Psuedocode is an intermediate between an English description and an implementation in a particular language of a an algorithm. English is very high-level, not always well-suited to precise descriptions of algorithms Programming languages are very precise, but can make algorithms hard to understand.

Psuedocode for Finding the Max procedure max(a_1, a_2,, a_n) temp_max = a_1 for i=2 to n do if temp_max < a_i then temp_max = a_i return temp_max

Properties of Algorithms Input. Input values from a specified set. Output Output values from a specified set. The solution to the problem. Definiteness Steps are defined precisely. Correctness Produces correct answer for every input. Finiteness Terminate after a finite number of steps. Effectiveness Each step performed in finite time. Generality Works for all problems of the desired form.

Does the max-finding algorithm have all of these properties? Input. A list of integers Output The largest integer in the list. Definiteness Assignments, finite loops, and comparisons all have precise definitions. Correctness Yes. Informal proof: temp_max is updated every time a large value is seen; all values seen; therefore temp_max is the largest value in the list after the loop ends. Finiteness Stops after seeing all elements of the list. Effectiveness Assignments, finite loops, and comparisons all take finite time. Generality Finds the maximum of any list of integers.

Search Search Find a given element in a list. Return the location of the element in the list (index), or -1 if not found. Linear Search Compare key (element being searched for) with each element in the list until a match is found, or the end of the list is reached. Binary Search Compare key only with elements in certain locations. Split list in half at each comparison. Requires list to be sorted.

Linear Search procedure linear_search (key, {a_1,,a_n}) for index = 1 to n if a_i equals key return index return -1

Binary Search procedure binary_search (key, {a_1,,a_n}) left = 1 right = n while left < right middle = left + right /2 if key == a_middle, then return middle elseif key > a_middle, then left = middle + 1 else right = middle if key == a_left, then return left return -1

Linear Search Exercise Write the numbers 1 to 20 on post-it notes. 1 number per note. Randomly order the notes on the table. How many comparisons to find: 7? 13? 1? 20?

Binary Search Exercise Sort the notes in ascending order How many comparisons to find: 7? 13? 1? 20?

Sort Sort: put the elements of a list in ascending order Example: List: 7,2,1,4,5,9 Sorted List: 1,2,4,5,7,9 Bubble Sort Compare every element to its neighbor and swap them if they are out of order. Repeat until list is sorted. Insertion Sort For each element of the unsorted portion of the list, insert it in sorted order in the sorted portion of the list.

Bubble Sort procedure bubble_sort( a 1,, a n ) for i = 1 to n-1 for j = 1 to n-i if a j > a j+1 then, swap a j and a j+1 a 1,, a n is in sorted order.

procedure insertion_sort( a 1,, a n ) for j = 2 to n i = 1 while a j > a i i = i + 1 m = a j for k = 0 to j-i-1 a j k =a j k 1 a i =m a 1,, a n is in sorted order. Insertion Sort

Bubble Sort Exercise Order the notes on the table as follows: 10, 2, 1, 5, 3, 9, 6, 4, 7, 8 Sort them using Bubble Sort. How many comparisons and swaps did you use? Don t count condition checks in for loops.

Insertion Sort Exercise Order the notes on the table as follows: 10, 2, 1, 5, 3, 9, 6, 4, 7, 8 Sort them using Insertion Sort. How many comparisons and swaps did you use? Don t count condition checks in for loops.

Binary Insertion Sort Exercise Order the notes on the table as follows: 10, 2, 1, 5, 3, 9, 6, 4, 7, 8 Sort them using Binary Insertion Sort. Use binary search, instead of linear search, when searching for the correct place to insert each number. How many comparisons and swaps did you use? Don t count condition checks in for loops.

The Growth of Functions The time required to solve a problem using a procedure depends on: Number of operations used Depends on the size of the input Speed of the hardware and software Does not depend on the size of the input Can be accounted for using a constant multiplier The growth of functions refers to the number of operations used by the function to solve the problem.

Big-O Notation Estimate the growth of a function without worrying about constant multipliers or smaller order terms. Do not need to worry about hardware or software used Assume that different operations take the same time. Addition is actually much faster than division, but for the purposes of analysis we assume they take the same time.

Big-O Let f and g be functions from Z or R, to R. We say that f x is O g x if there are constants C and k such that f x C g x whenever x > k. f x is bounded above by g x f x grows slower than Cg x, as x grows without bound Constants C and k are called witnesses.

Example: Max Let f n be the number of operations to find the maximum value in a list of n elements. procedure max(a_1, a_2,, a_n) - assign = depending on implementation, 1 or n op. temp_max = a_1 - assign = 1 op. for i=2 to n do - assign + compare = 1+1 = 2 ops. if temp_max < a_i - access + comparison = 1+1 = 2 ops. (n-1) times then temp_max = a_i - access + assign = 1+1 = 2 ops. (n-1) times - increment + compare = 1+1 = 2 ops. (n-1) times return temp_max - return = 1 op. f n = 1 + 1 + 2 + n 1 (2 + 2 + 2) + 1 f n = 6n 1

Example: Max Let f n be the number of operations to find the maximum value in a list of n elements. f n = 6n 1 f n Cg n, n > k 6n 1 6n, n > 0 Let g n = n f n is O n. Witnesses: C = 6, k = 0

Example: Sort Let f n be the number of operations to sort a list of n elements. procedure bubble_sort( a 1,, a n ) - 1: assign for i = 1 to n-1-2: assign and compare in loop1 for j = 1 to n-i - 2(n 1): assign and compare in loop2 n 1 i=1 n 1 i=1 n 1 if a j > a j+1-3(n i) : accesses and compare then, swap a j and a j+1-3(n i) : assigns n 1 - i=1 2(n i) : increment and compare in loop 2-2(n 1): increment and compare in loop 1 f n = 1 + 2 + 2 n 1 + 3(n i) + 3(n i) + 2 n i + 2 n 1 i=1 n 1 i=1 f n = 4n 2 1 n 1 i=1

Example: Sort Let f n be the number of operations to sort a list of n elements. f n = 4n 2 1 f n Cg n, n > k 4n 2 1 4n 2, n > 0 Let g n = n 2 f n is O n 2. Witnesses: C = 4, k = 0

Big-O for Polynomials Let f x = a n x n + a n 1 x n 1 + + a 1 x + a 0. Then, f x is O x n. Example: f x = 5x 2 18x + 20 5x 2 18x + 20 5x 2 + 20 for x > 0 5x 2 + 20 5x 2 + 20x 2 for x > 1 5x 2 + 20x 2 = 25x 2 Cg x for x > 1 Let g x = x 2 f x is O x 2. Witnesses: C = 25, k = 1

Exercise Give a big-o estimate for the sum of the first n positive integers. Solution: 1 + 2 + + n n + n + + n = n 2 1 + 2 + + n is O n 2, C = 1, k = 1

Exercise Give a big-o estimate for the factorial function, f n = n!, and the logarithm of the factorial. Solution: n! = 1 2 3 n n n n n = n n n! is O n n log n! log n n = n log n log n! is O n log n

Basic Growth Functions Constant: O 1 Logarithmic: O log n Linear: O n Linearithmic: O n log n Polynomial: O n c Exponential: O 2 n Factorial: O n!

Useful Big-O Estimates n c is O n d, but n d is not O n c, d > c > 1 log b n c is O n d, but n d is not O log b n c, b > 1, c, d > 0 n d is O b n, but b n is not O n d, d > 0, b > 1 b n is O c n, but c n is not O b n, c > b > 1

The Growth of Combinations of Functions Suppose f 1 n is O g 1 n and f 2 n is O g 2 n f 1 + f 2 n is O max g 1 n, g 2 n If g 1 n = g 2 n = g n, then f 1 + f 2 n is O g n f 1 f 2 n is O g 1 n g 2 n

Exercise Which of these functions is O x? f x = 10 C = 1, k = 10 f x = 3x + 7 C = 4, k = 7 f x = x 2 + x + 1 Not O x f x = 5 log x C = 5, k = 2 f x = x C = 1, k = 0 f x = x 2 C = 1, k = 0

Exercise Find the least integer c such that f n is O n c : f n = 2n 3 + n 2 log n c = 3 C = 3, k = 1 f n = n4 +n 2 +1 n 3 +1 c = 1 C = 1.5, k = 1

Big-Ω Big-O C, k n > k f n Cg n Big- Ω (big omega) C, k n > k f n Cg n C must be positive. f n is Ω g n g n is O f n f x is bounded below by g x

Big-Θ Big- Θ (big theta) f n is O g n and Ω g n f n is O g n and g n is O f n f n is Θ g n g n is Θ f n C 1, C 2, k n > k C 1 g n f n C 2 g n f n is of order g n f n and g n are of the same order

Big-Θ for Polynomials Let f x = a n x n + a n 1 x n 1 + + a 1 x + a 0. Then, f x is of order x n. f x is bounded [above and below] by g x Example: 3x 8 + 10x 7 + 221x 2 + 1444 is of order x 8 Witnesses: C = 6, k = 10

Complexity of Algorithms Computational complexity is the amount of time and space an algorithm uses to solve a problem. Space complexity Depends on data structures used to implement the algorithm Time complexity Depends on the number of operations used by the algorithm. Use big-o (or big-θ, if possible) to specify

Time Complexity Elementary operations have constant time (Θ 1 ) complexity: Assignment Arithmetic operations Boolean operations Comparisons Array access

Time Complexity Blocks of statements Block 1 ; // takes T 1 time Block 2 ; // takes T 2 time Block k ; // takes T k time To execute the sequence of Blocks 1 through k takes O T 1 + T 2 + + T k time.

Time Complexity Control Structures if(boolexpr) // takes T B time Block 1 ; // takes T 1 time else Block 2 ; // takes T 2 time To execute the control structures takes O T B + max T 1, T 2 time.

Time Complexity For Loops for i=a to b Block 1 ; // takes T 1 k time when i=k To execute the loop takes T 1 a + T 1 a + 1 + +T 1 b time If T 1 k is Θ 1, then the loop takes O b a + 1 T 1 time

Time Complexity Function Calls def f(params) // takes T p time to assign params Block 1 ; // takes T 1 time To execute the function takes O T p + T 1 time

Bubble Sort Revisited procedure bubble_sort( a 1,, a n ) // O T p = O 1 for i = 1 to n-1 // Block 1 for j = 1 to n-i // Block 2 if a j > a j+1 // Block 3 then, swap a j and a j+1 Block 3 is a control structure which takes O 3 + 3 = O 1 time Block 2 is a for loop, which takes O n i 1 + 1 O 1 = O n i time Block 1 is a for loop, which takes O O n 1 + O n 2 + + O 1 = O Therefore, the procedure takes O O 1 + O n 2 = O n 2 time n n 1 2 = O n 2 time

Tractability A problem which can be solved by an algorithm with worst-case polynomial time complexity (Θ n c ) is called tractable. Does not guarantee that it can be solved in any reasonable amount of time. Reasonable input sizes can be solved in relatively short time. A problem which cannot be solved by any algorithm with worse-case polynomial polynomial time complexity is called intractable. Average case complexity may be better. Many important problems are intractable, but still get solved everyday. Approximate solutions. A problem for which there does not exists any algorithm is called unsolvable. The first unsolvable, proved by Turing: The halting problem.

P vs NP All the tractable problems belong to a set called P. Can be solved in worst-case polynomial time. All the problems whose solutions can be verified in polynomial time belong to a set called NP. Example: Boolean Satisfiability (SAT) find an assignment of truth values that satisfies some Boolean expression. Solution can be verified very easily. Finding a solution for n variables requires Ω 2 n operations

NP-Complete It turns out that a bunch of problems in NP are actually the same problem. These are called NP-complete problems. Every problem in NP can be reduced in polynomial time to an NPcomplete problem. SAT was the first to be proved to be NP-complete. If any NP-complete problem can be solved in polynomial time, then every NP problem can, too. P = NP. $1,000,000 prize for proof of whether P = NP. General consensus is that P NP.